CSE/Command/Update.pm untested

filter: Statementstmt 0.0% 0 / 66 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 89 CC 7 SCAR 34.4
CC
7
Cov
0%
execute
56.0
BEGIN
2.0
BEGIN
2.0
CRAP 31.1
1 package App::CSE::Command::Update;
2 $App::CSE::Command::Update::VERSION = '0.016';
3 use Moose;
4 extends qw/App::CSE::Command/;
5  
6 use App::CSE::Command::Check;
7 use App::CSE::Command::Index;
8 use App::CSE::File;
9  
10 use File::MimeInfo::Magic;
11  
12  
13 use Log::Log4perl;
14 my $LOGGER = Log::Log4perl->get_logger();
15  
16 sub execute{
17 my ($self) = @_;
18  
19 my $colorizer = $self->cse()->colorizer();
20  
21 my $colored = sub{ $colorizer->colored(@_);};
22  
23 # Check the index.
24 # Re-index if nothing is there.
25 my $check = App::CSE::Command::Check->new({ cse => $self->cse() });
26 if( $check->execute() ){
27 $LOGGER->info(&$colored("Rebuilding the index..", 'green bold'));
28 my $index_cmd = App::CSE::Command::Index->new( { cse => $self->cse() });
29 if( $index_cmd->execute() ){
30 $LOGGER->error(&$colored("Building index failed", 'red'));
31 return 1;
32 }
33 # Nothing else to do.
34 return 0;
35 }
36  
37 # Right time to reindex dirty files.
38 my @dirty_files = sort keys %{$self->cse->dirty_files()};
39 unless( @dirty_files ){
40 $LOGGER->info(&$colored("No dirty files", 'green bold'));
41 return 0;
42 }
43  
44 # Build an indexer.
45 my $searcher = Lucy::Search::IndexSearcher->new( index => $self->cse()->index_dir().'' );
46 my $indexer = Lucy::Index::Indexer->new( schema => $searcher->get_schema(),
47 index => $self->cse()->index_dir().'' );
48  
49 my $NFILES = 0;
50 foreach my $dirty_file ( @dirty_files ){
51 $indexer->delete_by_term( field => 'path.raw',
52 term => $dirty_file );
53 unless( -e $dirty_file ){
54 # File is gone.
55 delete $self->cse()->dirty_files()->{$dirty_file};
56 next;
57 }
58
59 my $mime_type = File::MimeInfo::Magic::mimetype($dirty_file.'') || 'application/octet-stream';
60 my $file_class = App::CSE::File->class_for_mime($mime_type, $dirty_file.'');
61 unless( $file_class ){
62 $LOGGER->warn("Cannot reindex Mimetype $mime_type for $dirty_file is not supported anymore?");
63 next;
64 }
65  
66 ## Build a file instance.
67 my $file = $file_class->new({cse => $self->cse(),
68 mime_type => $mime_type,
69 file_path => $dirty_file.'' })->effective_object();
70  
71 $LOGGER->info("Reindexing file $dirty_file as ".$file->mime_type());
72 # And index it
73 my $content = $file->content();
74 $indexer->add_doc({
75 path => $file->file_path(),
76 'path.raw' => $file->file_path(),
77 dir => $file->dir(),
78 mime => $file->mime_type(),
79 mtime => $file->mtime->iso8601(),
80 $content ? ( content => $content ) : ()
81 });
82 # Remove from the dirty files hash
83 delete $self->cse()->dirty_files()->{$dirty_file};
84 $NFILES++;
85 }
86  
87 # Commit and save that.
88 $indexer->commit();
89 $self->cse()->save_dirty_files();
90 $LOGGER->info(&$colored('Re-indexed '.$NFILES.' files' ,'green bold'));
91 return 0;
92 }
93  
94 __PACKAGE__->meta->make_immutable();