user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
8f53c3ef8c59b34df2502ea6342ee53f70b2db7e blob 2398 bytes (raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
 
# Copyright (C) 2021 all contributors <meta@public-inbox.org>
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>

# eXternal keyword index - for dealing with keyword storage on
# read-only local external inboxes and extindex.
#
# Only boolean term prefixes:
#	Q - Message-ID (like SearchIdx)
#	U - git blob OID (URL)
#	K - keywords
package PublicInbox::LeiXkwIdx;
use strict;
use v5.10.1;
use parent qw(PublicInbox::SearchIdx);
use PublicInbox::Search qw(xap_terms);
use PublicInbox::InboxWritable;
use PublicInbox::LeiXkw;

*docids_for = \&PublicInbox::LeiXkw::docids_for;

sub new {
	my ($cls, $dir, $opt) = @_;
	PublicInbox::SearchIdx::load_xapian_writable();
	my $fl = $PublicInbox::SearchIdx::DB_CREATE_OR_OPEN;
	$fl |= $PublicInbox::SearchIdx::DB_NO_SYNC if $opt->{-no_fsync};
	bless {
		xpfx => $dir, xdb_flags => $fl, umask => 077,
		indexlevel => 'full', creat => 1, git => $opt->{git},
	}, $cls;
}

sub set_xkw {
	my ($self, $eml, $smsg, $method) = @_;
	my $kw = $smsg->{kw} // die 'BUG: no {kw}';
	$self->begin_txn_lazy;
	my @docids = docids_for($self, $eml, $smsg);
	if (!@docids) { # brand new
		return () unless scalar(@$kw);
		my $doc = $PublicInbox::Search::X{Document}->new;
		$doc->add_boolean_term('U' . $smsg->{blob});
		$doc->add_boolean_term('K' . $_) for @$kw;
		for my $mid (@{$smsg->{mids4idx}}) {
			$doc->add_boolean_term('Q' . $mid);
		}
		return ($self->{xdb}->add_document($doc));
	# modify existing
	} elsif (scalar @$kw) {
		$method //= 'set_keywords'; # | add_keywords | remove_keywords
		for my $docid (@docids) {
			$self->$method($docid, @$kw);
		}
		# we fell back to mids matching, speed up future matches
		if ($self->{mids4idx}) {
			for my $docid (@docids) {
				my $doc = $self->{xdb}->get_document($docid);
				$doc->add_boolean_term('U' . $smsg->{blob});
				$self->{xdb}->replace_document($docid, $doc);
			}
		}
	} else {
		for my $docid (@docids) {
			$self->{xdb}->delete_document($docid);
		}
	}
	@docids;
}

sub xdb { $_[0]->begin_txn_lazy } # for get_xkw

sub clear_xkw {
	my ($self, $eml, $smsg) = @_;
	$self->begin_txn_lazy;
	my @docids = docids_for($self, $eml, $smsg);
	for my $docid (@docids) {
		$self->{xdb}->delete_document($docid);
	}
	@docids
}

no warnings 'once';
*DESTROY = \&PublicInbox::SearchIdx::commit_txn_lazy;
*with_umask = \&PublicInbox::InboxWritable::with_umask;
*get_xkw = \&PublicInbox::LeiXkw::get_xkw;

1;
debug log:

solving 8f53c3ef ...
found 8f53c3ef in https://public-inbox.org/meta/20210303140139.7637-1-e@80x24.org/

applying [1/1] https://public-inbox.org/meta/20210303140139.7637-1-e@80x24.org/
diff --git a/lib/PublicInbox/LeiXkwIdx.pm b/lib/PublicInbox/LeiXkwIdx.pm
new file mode 100644
index 00000000..8f53c3ef

Checking patch lib/PublicInbox/LeiXkwIdx.pm...
Applied patch lib/PublicInbox/LeiXkwIdx.pm cleanly.

index at:
100644 8f53c3ef8c59b34df2502ea6342ee53f70b2db7e	lib/PublicInbox/LeiXkwIdx.pm

Code repositories for project(s) associated with this inbox:

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).