Indexed/Indexer/Xapian.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Indexed/Indexer/KSx.pm ↑ Summary
1 package Rose::DBx::Object::Indexed::Indexer::Xapian;
2  
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use base qw( Rose::DBx::Object::Indexed::Indexer );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 use Carp;
7 use Class::C3;
8  
9 our $VERSION = '0.009';
10  
11 =head1 NAME
12  
13 Rose::DBx::Object::Indexed::Indexer::Xapian - Xapian indexer
14  
15 =head1 SYNOPSIS
16  
17 # from a Rose::DBx::Object::Indexed object
18 my $thing = MyThing->new( id => 123 )->load;
19 $thing->write_index('insert');
20
21 # standalone
22 my $indexer = MyThing->init_indexer;
23 while (my $thing = $thing_iterator->next) {
24 $indexer->insert($thing);
25 }
26  
27 =cut
28  
29 =head2 init_indexer_class
30  
31 Returns 'SWISH::Prog::Xapian::Indexer'.
32  
33 =cut
34  
35 sub init_indexer_class {'SWISH::Prog::Xapian::Indexer'}
36  
37 sub __seed_index {
38 my $self = shift;
39  
40 # no op for now
41  
42 }
43  
44 =head2 make_doc( I<rdbo_obj> )
45  
46 Returns a SWISH::Prog::Doc instance for I<rdbo_obj>.
47  
48 =cut
49  
50 sub make_doc {
51 my $self = shift;
52 my $obj = shift or croak "RDBO object required";
53 my $xml = $self->to_xml( $self->serialize_object($obj), $obj );
54 return SWISH::Prog::Doc->new(
55 content => $xml,
56 url => $self->get_primary_key($obj),
57 modtime => time(),
58 parser => 'XML*',
59 type => 'application/x-rdbo-indexed', # TODO ??
60 version => 3, # SWISH::3 headers
61 );
62 }
63  
64 =head2 run( I<args> )
65  
66 Calls the superclass method and then finish() on the swish_indexer().
67 Note that the explicit call to finish() means that a new indexer
68 is spawned for each insert(), update() or delete(). If you are trying
69 to do bulk index updates, avoid this kind of overhead and do not
70 call run(). Instead, do something like:
71  
72 my $swish_indexer = $object->swish_indexer;
73 my $indexer = $object->indexer;
74
75 foreach my $obj (@list_of_objects) {
76 my $doc = $indexer->make_doc($obj);
77 $swish_indexer->process($doc);
78 }
79
80 $swish_indexer->finish(); # must do this to commit the index transaction.
81  
82 =cut
83  
84 sub run {
85 my $self = shift;
86 $self->next::method(@_);
87  
88 # note that this invalidates the indexer,
89 # so explicitly destroy it to avoid race conditions.
90 $self->swish_indexer->finish();
91 $self->swish_indexer(undef);
92 }
93  
94 =head2 insert( I<rdbo_obj> )
95  
96 Calls run() with the appropriate arguments.
97  
98 =cut
99  
100 sub insert {
101 my $self = shift;
102 my $obj = shift or croak "RDBO object required";
103 $self->run($obj); # no action. the default is to 'Index' (add)
104 }
105  
106 =head2 update( I<rdbo_obj> )
107  
108 Calls run() with the appropriate arguments.
109  
110 =cut
111  
112 sub update {
113 my $self = shift;
114 my $obj = shift or croak "RDBO object required";
115 $self->run( $obj, 'Update' );
116 }
117  
118 =head2 delete( I<rdbo_obj> )
119  
120 Calls run() with the appropriate arguments.
121  
122 =cut
123  
124 sub delete {
125 my $self = shift;
126 my $obj = shift or croak "RDBO object required";
127 $self->run( $obj, 'Remove' );
128 }
129  
130 1;
131  
132 __END__
« Indexed/Indexer/KSx.pm ↑ Summary