| 1 | package App::CSE::Command::Index; | ||
| 2 | $App::CSE::Command::Index::VERSION = '0.016'; | ||
| 3 | use Moose; | ||
| 4 | extends qw/App::CSE::Command/; | ||
| 5 | with qw/App::CSE::Role::DirIndex/; | ||
| 6 | |||
| 7 | use App::CSE::Command::Unwatch; | ||
| 8 | |||
| 9 | use App::CSE::File; | ||
| 10 | |||
| 11 | use File::Basename; | ||
| 12 | use File::Find; | ||
| 13 | use File::Path; | ||
| 14 | use File::stat; | ||
| 15 | |||
| 16 | use Path::Class::Dir; | ||
| 17 | use Lucy::Plan::Schema; | ||
| 18 | |||
| 19 | use String::CamelCase; | ||
| 20 | use Term::ProgressBar; | ||
| 21 | use Time::HiRes; | ||
| 22 | |||
| 23 | ## Note that using File::Slurp is done at the CSE level, | ||
| 24 | ## avoiding undefined warnings, | ||
| 25 | |||
| 26 | use Log::Log4perl; | ||
| 27 | my $LOGGER = Log::Log4perl->get_logger(); | ||
| 28 | |||
| 29 | |||
| 30 | |||
| 31 | sub execute{ | ||
| 32 | my ($self) = @_; | ||
| 33 | |||
| 34 | |||
| 35 | ## We need to unwatch before indexing. Always. | ||
| 36 | my $unwatch_cmd = App::CSE::Command::Unwatch->new( { cse => $self->cse() } ); | ||
| 37 | $unwatch_cmd->execute(); | ||
| 38 | |||
| 39 | |||
| 40 | ## We will index as a new dir. | ||
| 41 | my $index_dir = $self->cse()->index_dir().'-new'; | ||
| 42 | |||
| 43 | |||
| 44 | my $schema = Lucy::Plan::Schema->new(); | ||
| 45 | |||
| 46 | |||
| 47 | my $case_folder = Lucy::Analysis::CaseFolder->new(); | ||
| 48 | my $tokenizer = Lucy::Analysis::RegexTokenizer->new( pattern => '\w+' ); # ;Lucy::Analysis::StandardTokenizer->new(); | ||
| 49 | |||
| 50 | # Full text analyzer. | ||
| 51 | my $ft_anal = Lucy::Analysis::PolyAnalyzer->new(analyzers => [ $case_folder, $tokenizer ]); | ||
| 52 | my $decl_anal = Lucy::Analysis::PolyAnalyzer | ||
| 53 | ->new( | ||
| 54 | analyzers => [ $case_folder, Lucy::Analysis::RegexTokenizer->new( pattern => '\S+' ) ] | ||
| 55 | ); | ||
| 56 | |||
| 57 | # Full text types. | ||
| 58 | my $path_type = Lucy::Plan::FullTextType->new(analyzer => $ft_anal, sortable => 1, boost => 2.0); | ||
| 59 | my $body_type = Lucy::Plan::FullTextType->new(analyzer => $ft_anal, highlightable => 1 , boost => 1.0); | ||
| 60 | my $declare_type = Lucy::Plan::FullTextType->new( analyzer => $decl_anal, highlightable => 1 , boost => 4.0 ); | ||
| 61 | my $call_type = Lucy::Plan::FullTextType->new( analyzer => $decl_anal, highlightable => 1 , boost => 3.0 ); | ||
| 62 | |||
| 63 | |||
| 64 | # String type | ||
| 65 | my $plain_sortable_string = Lucy::Plan::StringType->new( sortable => 1 ); | ||
| 66 | |||
| 67 | # Plain string type | ||
| 68 | my $plain_string = Lucy::Plan::StringType->new(); | ||
| 69 | |||
| 70 | |||
| 71 | $schema->spec_field( name => 'content' , type => $body_type ); | ||
| 72 | $schema->spec_field( name => 'decl' , type => $declare_type ); | ||
| 73 | $schema->spec_field( name => 'call' , type => $call_type ); | ||
| 74 | $schema->spec_field( name => 'dir' , type => $plain_sortable_string ); | ||
| 75 | $schema->spec_field( name => 'mtime' , type => $plain_sortable_string); | ||
| 76 | $schema->spec_field( name => 'mime' , type => $plain_sortable_string ); | ||
| 77 | $schema->spec_field( name => 'path' , type => $path_type ); | ||
| 78 | $schema->spec_field( name => 'path.raw' , type => $plain_string ); | ||
| 79 | |||
| 80 | |||
| 81 | ## Ok Schema has been built | ||
| 82 | $LOGGER->info("Building index ".$index_dir); | ||
| 83 | my $indexer = Lucy::Index::Indexer->new(schema => $schema, | ||
| 84 | index => $index_dir, | ||
| 85 | create => 1, | ||
| 86 | ); | ||
| 87 | |||
| 88 | $LOGGER->info("Indexing files from ".$self->dir_index()); | ||
| 89 | |||
| 90 | my $dir_index = $self->dir_index(); | ||
| 91 | |||
| 92 | my $cse = $self->cse(); | ||
| 93 | |||
| 94 | ## Wrapper to build File::find wanter subs | ||
| 95 | my $wanted_wrapper = sub{ | ||
| 96 | my ($wrapped) = @_; | ||
| 97 | |||
| 98 | return sub{ | ||
| 99 | my $file_name = $File::Find::name; | ||
| 100 | |||
| 101 | my $valid = $cse->is_file_valid($file_name , { | ||
| 102 | on_hidden => sub{ | ||
| 103 | $File::Find::prune = 1; | ||
| 104 | return undef; | ||
| 105 | }, | ||
| 106 | on_skip => sub{ | ||
| 107 | $File::Find::prune = 1; | ||
| 108 | return undef; | ||
| 109 | } | ||
| 110 | }); | ||
| 111 | unless( $valid ){ | ||
| 112 | return; | ||
| 113 | } | ||
| 114 | |||
| 115 | my $stat = File::stat::stat($file_name.''); | ||
| 116 | &$wrapped($file_name, $stat); | ||
| 117 | }; | ||
| 118 | }; | ||
| 119 | |||
| 120 | |||
| 121 | |||
| 122 | $LOGGER->info("Estimating size. Please wait"); | ||
| 123 | my $ESTIMATED_SIZE = 0; | ||
| 124 | File::Find::find({ wanted => &$wanted_wrapper(sub{ | ||
| 125 | my ($file_name, $stat) = @_; | ||
| 126 | unless( -d $file_name ){ | ||
| 127 | $ESTIMATED_SIZE += $stat->size(); | ||
| 128 | } | ||
| 129 | }), | ||
| 130 | no_chdir => 1, | ||
| 131 | follow => 0, | ||
| 132 | }, $dir_index ); | ||
| 133 | |||
| 134 | my $PROGRESS_BAR = Term::ProgressBar->new({name => 'Indexing', | ||
| 135 | count => $ESTIMATED_SIZE, | ||
| 136 | ETA => 'linear', | ||
| 137 | silent => ! $self->cse()->interactive(), | ||
| 138 | }); | ||
| 139 | |||
| 140 | my $START_TIME = Time::HiRes::time(); | ||
| 141 | my $NUM_INDEXED = 0; | ||
| 142 | my $SIZE_LOOKEDAT = 0; | ||
| 143 | my $TOTAL_SIZE = 0; | ||
| 144 | |||
| 145 | |||
| 146 | my $wanted = sub{ | ||
| 147 | my ($file_name, $stat) = @_; | ||
| 148 | |||
| 149 | |||
| 150 | unless( -d $file_name.'' ){ | ||
| 151 | $SIZE_LOOKEDAT += $stat->size(); | ||
| 152 | $PROGRESS_BAR->update($SIZE_LOOKEDAT); | ||
| 153 | } | ||
| 154 | |||
| 155 | |||
| 156 | my $mime_type = $cse->valid_mime_type($file_name); | ||
| 157 | unless( $mime_type ){ | ||
| 158 | return; | ||
| 159 | } | ||
| 160 | |||
| 161 | my $file_class = App::CSE::File->class_for_mime($mime_type, $file_name.''); | ||
| 162 | unless( $file_class ){ | ||
| 163 | return; | ||
| 164 | } | ||
| 165 | |||
| 166 | ## Build a file instance. | ||
| 167 | my $file = $file_class->new({cse => $cse, | ||
| 168 | mime_type => $mime_type, | ||
| 169 | stat => $stat, | ||
| 170 | file_path => $file_name.'' })->effective_object(); | ||
| 171 | |||
| 172 | |||
| 173 | $LOGGER->debug("Indexing ".$file->file_path().' as '.$file->mime_type()); | ||
| 174 | |||
| 175 | my $content = $file->content(); | ||
| 176 | $indexer->add_doc({ | ||
| 177 | path => $file->file_path(), | ||
| 178 | 'path.raw' => $file->file_path(), | ||
| 179 | decl => join(' ', @{$file->decl()}), | ||
| 180 | call => join(' ', @{$file->call()}), | ||
| 181 | dir => $file->dir(), | ||
| 182 | mime => $file->mime_type(), | ||
| 183 | mtime => $file->mtime->iso8601(), | ||
| 184 | $content ? ( content => $content ) : () | ||
| 185 | }); | ||
| 186 | $NUM_INDEXED++; | ||
| 187 | $TOTAL_SIZE+= $file->stat->size(); | ||
| 188 | }; | ||
| 189 | |||
| 190 | File::Find::find({ wanted => &$wanted_wrapper($wanted), | ||
| 191 | no_chdir => 1, | ||
| 192 | follow => 0, | ||
| 193 | }, $dir_index ); | ||
| 194 | |||
| 195 | $indexer->commit(); | ||
| 196 | $PROGRESS_BAR->update($ESTIMATED_SIZE); | ||
| 197 | |||
| 198 | |||
| 199 | rmtree $cse->index_dir()->stringify(); | ||
| 200 | rename $index_dir , $self->cse->index_dir()->stringify(); | ||
| 201 | |||
| 202 | $self->cse->index_meta->{index_time} = DateTime->now()->iso8601(); | ||
| 203 | # Also inject the right version | ||
| 204 | $self->cse->index_meta->{version} = $self->cse->version();; | ||
| 205 | |||
| 206 | $self->cse->save_index_meta(); | ||
| 207 | |||
| 208 | my $END_TIME = Time::HiRes::time(); | ||
| 209 | |||
| 210 | $LOGGER->info($self->cse->colorizer->colored("Index is ".$self->cse()->index_dir()->stringify(), 'green bold')); | ||
| 211 | $LOGGER->info("$NUM_INDEXED files ($TOTAL_SIZE Bytes) indexed in ".sprintf('%.03f', ( $END_TIME - $START_TIME )).' seconds'); | ||
| 212 | |||
| 213 | return 0; | ||
| 214 | } | ||
| 215 | |||
| 216 | __PACKAGE__->meta->make_immutable(); | ||
| 217 | |||
| 218 | =head1 NAME | ||
| 219 | |||
| 220 | App::CSE::Command::Index - Indexes a directory | ||
| 221 | |||
| 222 | =cut |