1 # Copyright (C) 2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
3 # based on notmuch, but with no concept of folders, files or flags
4 package PublicInbox::SearchIdx;
7 use base qw(PublicInbox::Search);
8 use PublicInbox::MID qw/mid_clean mid_compress/;
9 *xpfx = *PublicInbox::Search::xpfx;
14 OLD_PERM_EVERYBODY => 2,
16 PERM_EVERYBODY => 0664,
20 my ($class, $git_dir, $writable) = @_;
21 my $dir = $class->xdir($git_dir);
22 require Search::Xapian::WritableDatabase;
23 my $flag = Search::Xapian::DB_OPEN;
24 my $self = bless { git_dir => $git_dir }, $class;
25 my $perm = $self->_git_config_perm;
26 my $umask = _umask_for($perm);
27 $self->{umask} = $umask;
28 $self->{xdb} = $self->with_umask(sub {
31 File::Path::mkpath($dir);
32 $flag = Search::Xapian::DB_CREATE_OR_OPEN;
34 Search::Xapian::WritableDatabase->new($dir, $flag);
40 my ($self, $mime) = @_; # mime = Email::MIME object
41 my $db = $self->{xdb};
44 my $mid = mid_clean($mime->header('Message-ID'));
46 my $ct_msg = $mime->header('Content-Type') || 'text/plain';
49 my $smsg = $self->lookup_message($mid);
53 $smsg->ensure_metadata;
54 # convert a ghost to a regular message
55 # it will also clobber any existing regular message
59 my $type = xpfx('type');
61 $doc->remove_term($type . 'ghost');
65 # probably does not exist:
66 eval { $doc->remove_term($type . 'mail') };
67 $doc->add_term($type . 'mail');
69 $smsg = PublicInbox::SearchMsg->new($mime);
71 $doc->add_term(xpfx('mid') . $mid);
74 my $subj = $smsg->subject;
77 $doc->add_term(xpfx('subject') . $subj);
79 my $path = $self->subject_path($subj);
80 $doc->add_term(xpfx('path') . mid_compress($path));
83 my $ts = Search::Xapian::sortable_serialise($smsg->ts);
84 $doc->add_value(PublicInbox::Search::TS, $ts);
86 my $tg = $self->term_generator;
88 $tg->set_document($doc);
89 $tg->index_text($subj, 1, 'S') if $subj;
90 $tg->increase_termpos;
91 $tg->index_text($subj) if $subj;
92 $tg->increase_termpos;
94 $tg->index_text($smsg->from->format);
95 $tg->increase_termpos;
97 $mime->walk_parts(sub {
99 return if $part->subparts; # walk_parts already recurses
100 my $ct = $part->content_type || $ct_msg;
102 # account for filter bugs...
103 $ct =~ m!\btext/plain\b!i or return;
106 my $body = $part->body;
108 my @lines = split(/\n/, $body);
109 while (defined(my $l = shift @lines)) {
117 $tg->index_text(join("\n", @quot), 0);
119 $tg->increase_termpos;
122 $tg->index_text(join("\n", @orig));
124 $tg->increase_termpos;
129 $doc_id = $smsg->doc_id;
130 $self->link_message($smsg, 0);
131 $doc->set_data($smsg->to_doc_data);
132 $db->replace_document($doc_id, $doc);
134 $self->link_message($smsg, 0);
135 $doc->set_data($smsg->to_doc_data);
136 $doc_id = $db->add_document($doc);
141 warn "failed to index message <$mid>: $@\n";
147 # returns deleted doc_id on success, undef on missing
149 my ($self, $mid) = @_;
150 my $db = $self->{xdb};
152 $mid = mid_clean($mid);
155 $doc_id = $self->find_unique_doc_id('mid', $mid);
156 $db->delete_document($doc_id) if defined $doc_id;
160 warn "failed to remove message <$mid>: $@\n";
166 sub term_generator { # write-only
169 my $tg = $self->{term_generator};
172 $tg = Search::Xapian::TermGenerator->new;
173 $tg->set_stemmer($self->stemmer);
175 $self->{term_generator} = $tg;
178 sub next_doc_id { $_[0]->{xdb}->get_lastdocid + 1 }
180 # increments last_thread_id counter
181 # returns a 64-bit integer represented as a hex string
184 my $db = $self->{xdb};
185 my $last_thread_id = int($db->get_metadata('last_thread_id') || 0);
187 $db->set_metadata('last_thread_id', ++$last_thread_id);
193 my ($self, $smsg, $is_ghost) = @_;
196 $smsg->ensure_metadata;
198 $self->link_message_to_parents($smsg);
202 sub link_message_to_parents {
203 my ($self, $smsg) = @_;
204 my $doc = $smsg->{doc};
205 my $mid = $smsg->mid;
206 my $mime = $smsg->mime;
207 my $refs = $mime->header('References');
208 my @refs = $refs ? ($refs =~ /<([^>]+)>/g) : ();
209 if (my $irt = $mime->header('In-Reply-To')) {
210 # last References should be $irt
211 # we will de-dupe later
212 push @refs, mid_clean($irt);
217 my %uniq = ($mid => 1);
218 my @orig_refs = @refs;
221 # prevent circular references via References: here:
222 foreach my $ref (@orig_refs) {
229 $smsg->{references_sorted} = '<'.join('><', @refs).'>';
231 # first ref *should* be the thread root,
232 # but we can never trust clients to do the right thing
233 my $ref = shift @refs;
234 $tid = $self->_resolve_mid_to_tid($ref);
236 # the rest of the refs should point to this tid:
237 foreach $ref (@refs) {
238 my $ptid = $self->_resolve_mid_to_tid($ref);
240 $self->merge_threads($tid, $ptid);
244 $tid = $self->next_thread_id;
246 $doc->add_term(xpfx('thread') . $tid);
250 my ($self, $git, $blob) = @_;
251 my $mime = do_cat_mail($git, $blob) or return;
252 eval { $self->add_message($mime) };
253 warn "W: index_blob $blob: $@\n" if $@;
257 my ($self, $git, $blob) = @_;
258 my $mime = do_cat_mail($git, $blob) or return;
259 my $mid = $mime->header('Message-ID');
260 eval { $self->remove_message($mid) } if defined $mid;
261 warn "W: unindex_blob $blob: $@\n" if $@;
265 my ($git, $blob) = @_;
267 my $str = $git->cat_file($blob);
268 Email::MIME->new($str);
274 my ($self, $head) = @_;
275 $self->with_umask(sub { $self->_index_sync($head) });
279 my ($self, $range, $add_cb, $del_cb) = @_;
280 my $hex = '[a-f0-9]';
281 my $h40 = $hex .'{40}';
282 my $addmsg = qr!^:000000 100644 \S+ ($h40) A\t${hex}{2}/${hex}{38}$!;
283 my $delmsg = qr!^:100644 000000 ($h40) \S+ D\t${hex}{2}/${hex}{38}$!;
284 my $git_dir = $self->{git_dir};
285 require PublicInbox::GitCatFile;
286 my $git = PublicInbox::GitCatFile->new($git_dir);
287 my @cmd = ('git', "--git-dir=$git_dir", "log",
288 qw/--reverse --no-notes --no-color --raw -r --no-abbrev/,
291 my $pid = open(my $log, '-|', @cmd) or
292 die('open` '.join(' ', @cmd) . " pipe failed: $!\n");
293 while (my $line = <$log>) {
294 if ($line =~ /$addmsg/o) {
295 $add_cb->($self, $git, $1);
296 } elsif ($line =~ /$delmsg/o) {
297 $del_cb->($self, $git, $1);
298 } elsif ($line =~ /^commit ($h40)/o) {
306 # indexes all unindexed messages
308 my ($self, $head) = @_;
309 my $db = $self->{xdb};
312 $db->begin_transaction;
314 my $latest = $db->get_metadata('last_commit');
315 my $range = $latest eq '' ? $head : "$latest..$head";
316 $latest = $self->rlog($range, *index_blob, *unindex_blob);
317 $db->set_metadata('last_commit', $latest) if defined $latest;
320 warn "indexing failed: $@\n";
321 $db->cancel_transaction;
323 $db->commit_transaction;
327 # this will create a ghost as necessary
328 sub _resolve_mid_to_tid {
329 my ($self, $mid) = @_;
331 my $smsg = $self->lookup_message($mid) || $self->create_ghost($mid);
336 my ($self, $mid, $tid) = @_;
338 $tid = $self->next_thread_id unless defined $tid;
340 my $doc = Search::Xapian::Document->new;
341 $doc->add_term(xpfx('mid') . $mid);
342 $doc->add_term(xpfx('thread') . $tid);
343 $doc->add_term(xpfx('type') . 'ghost');
345 my $smsg = PublicInbox::SearchMsg->wrap($doc, $mid);
346 $self->link_message($smsg, 1);
347 $self->{xdb}->add_document($doc);
353 my ($self, $winner_tid, $loser_tid) = @_;
354 my ($head, $tail) = $self->find_doc_ids('thread', $loser_tid);
355 my $thread_pfx = xpfx('thread');
356 my $db = $self->{xdb};
358 for (; $head != $tail; $head->inc) {
359 my $docid = $head->get_docid;
360 my $doc = $db->get_document($docid);
361 $doc->remove_term($thread_pfx . $loser_tid);
362 $doc->add_term($thread_pfx . $winner_tid);
363 $db->replace_document($docid, $doc);
367 sub _read_git_config_perm {
369 my @cmd = ('git', "--git-dir=$self->{git_dir}",
370 qw(config core.sharedRepository));
371 my $pid = open(my $fh, '-|', @cmd) or
372 die('open `'.join(' ', @cmd) . " pipe failed: $!\n");
375 chomp $perm if defined $perm;
379 sub _git_config_perm {
381 my $perm = scalar @_ ? $_[0] : _read_git_config_perm($self);
382 return PERM_GROUP if (!defined($perm) || $perm eq '');
383 return PERM_UMASK if ($perm eq 'umask');
384 return PERM_GROUP if ($perm eq 'group');
385 if ($perm =~ /\A(?:all|world|everybody)\z/) {
386 return PERM_EVERYBODY;
388 return PERM_GROUP if ($perm =~ /\A(?:true|yes|on|1)\z/);
389 return PERM_UMASK if ($perm =~ /\A(?:false|no|off|0)\z/);
392 return PERM_UMASK if ($i == PERM_UMASK);
393 return PERM_GROUP if ($i == OLD_PERM_GROUP);
394 return PERM_EVERYBODY if ($i == OLD_PERM_EVERYBODY);
396 if (($i & 0600) != 0600) {
397 die "core.sharedRepository mode invalid: ".
398 sprintf('%.3o', $i) . "\nOwner must have permissions\n";
404 my ($perm) = @_; # _git_config_perm return value
406 return umask if $rv == 0;
408 # set +x bit if +r or +w were set
409 $rv |= 0100 if ($rv & 0600);
410 $rv |= 0010 if ($rv & 0060);
411 $rv |= 0001 if ($rv & 0006);
416 my ($self, $cb) = @_;
417 my $old = umask $self->{umask};
418 my $rv = eval { $cb->() };