| 1 | package Rose::DBx::Object::Indexed::Indexer; | ||
| 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::Object ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | use Carp; | ||
| 7 | use Class::C3; | ||
| 8 | use SWISH::Prog; | ||
| 9 | use SWISH::Prog::Config; | ||
| 10 | use SWISH::Prog::Doc; | ||
| 11 | use SWISH::Prog::Utils; | ||
| 12 | |||
| 13 | use Rose::Object::MakeMethods::Generic ( | ||
| 14 | 'scalar --get_set_init' => 'config', | ||
| 15 | 'scalar --get_set_init' => 'invindex', | ||
| 16 | 'scalar --get_set_init' => 'swish_indexer', | ||
| 17 | 'scalar --get_set_init' => 'indexer_class', | ||
| 18 | 'scalar --get_set_init' => 'prune', | ||
| 19 | 'scalar --get_set_init' => 'force_load', | ||
| 20 | 'scalar --get_set_init' => 'tree_opts', | ||
| 21 | 'scalar' => 'xml_root_element', | ||
| 22 | 'scalar --get_set_init' => 'max_depth', | ||
| 23 | 'scalar --get_set_init' => 'debug', | ||
| 24 | ); | ||
| 25 | |||
| 26 | our $VERSION = '0.009'; | ||
| 27 | |||
| 28 | =head1 NAME | ||
| 29 | |||
| 30 | Rose::DBx::Object::Indexed::Indexer - Indexer base class | ||
| 31 | |||
| 32 | =head1 SYNOPSIS | ||
| 33 | |||
| 34 | # from a Rose::DBx::Object::Indexed object | ||
| 35 | my $thing = MyThing->new( id => 123 )->load; | ||
| 36 | $thing->write_index('insert'); | ||
| 37 | |||
| 38 | # standalone | ||
| 39 | my $indexer = MyThing->init_indexer; | ||
| 40 | while (my $thing = $thing_iterator->next) { | ||
| 41 | $indexer->insert($thing); | ||
| 42 | } | ||
| 43 | |||
| 44 | =head1 DESCRIPTION | ||
| 45 | |||
| 46 | Rose::DBx::Object::Indexed::Indexer uses SWISH::Prog to create | ||
| 47 | and maintain full-text indexes of Rose::DB::Object instances. | ||
| 48 | |||
| 49 | This class is typically accessed via Rose::DBx::Object::Indexed instances. | ||
| 50 | |||
| 51 | =head1 METHODS | ||
| 52 | |||
| 53 | Only new or overridden method are documented here. | ||
| 54 | |||
| 55 | =cut | ||
| 56 | |||
| 57 | =head2 init_invindex | ||
| 58 | |||
| 59 | Should return either a path to the index directory or a SWISH::Prog::InvIndex | ||
| 60 | object. The default is a no-op, which will tell the indexer to | ||
| 61 | generate a default SWISH::Prog::InvIndex object. | ||
| 62 | |||
| 63 | =cut | ||
| 64 | |||
| 65 | sub init_invindex { } | ||
| 66 | |||
| 67 | =head2 init_config | ||
| 68 | |||
| 69 | Should return a SWISH::Prog::Config instance. The default is a new Config instance. | ||
| 70 | |||
| 71 | =cut | ||
| 72 | |||
| 73 | sub init_config { | ||
| 74 | return SWISH::Prog::Config->new; | ||
| 75 | } | ||
| 76 | |||
| 77 | =head2 init_indexer_class | ||
| 78 | |||
| 79 | The default is SWISH::Prog::Native::Indexer. | ||
| 80 | |||
| 81 | =cut | ||
| 82 | |||
| 83 | sub init_indexer_class {'SWISH::Prog::Native::Indexer'} | ||
| 84 | |||
| 85 | =head2 init | ||
| 86 | |||
| 87 | Just calls next::method(). | ||
| 88 | |||
| 89 | =cut | ||
| 90 | |||
| 91 | sub init { | ||
| 92 | my $self = shift; | ||
| 93 | $self->next::method(@_); | ||
| 94 | return $self; | ||
| 95 | } | ||
| 96 | |||
| 97 | =head2 init_swish_indexer | ||
| 98 | |||
| 99 | Loads and returns an instance of indexer_class(). | ||
| 100 | |||
| 101 | =cut | ||
| 102 | |||
| 103 | sub init_swish_indexer { | ||
| 104 | my $self = shift; | ||
| 105 | my $class = $self->indexer_class; | ||
| 106 | |||
| 107 | eval "require $class"; | ||
| 108 | |||
| 109 | my $indexer = $class->new( | ||
| 110 | config => $self->config, | ||
| 111 | invindex => $self->invindex, | ||
| 112 | @_ | ||
| 113 | ); | ||
| 114 | return $indexer; | ||
| 115 | } | ||
| 116 | |||
| 117 | =head2 init_prune | ||
| 118 | |||
| 119 | Should return a hash ref of relationship names to skip in serialize_object(). | ||
| 120 | |||
| 121 | The default is an empty hash ref (skip nothing). | ||
| 122 | |||
| 123 | =cut | ||
| 124 | |||
| 125 | sub init_prune { {} } | ||
| 126 | |||
| 127 | =head2 init_tree_opts | ||
| 128 | |||
| 129 | Should return array ref of key/value pairs to pass into the as_tree() | ||
| 130 | method in Rose::DB::Object::Helpers. Default is an empty array. | ||
| 131 | |||
| 132 | =cut | ||
| 133 | |||
| 134 | sub init_tree_opts { [] } | ||
| 135 | |||
| 136 | =head2 get_primary_key( I<rdbo_obj> ) | ||
| 137 | |||
| 138 | Should return the primary key to be used as the "URL" of the indexed "document". | ||
| 139 | The default return value is primary_key_uri_escaped() from the Rose::DBx::Object::MoreHelpers | ||
| 140 | class. | ||
| 141 | |||
| 142 | =cut | ||
| 143 | |||
| 144 | sub get_primary_key { | ||
| 145 | $_[1]->primary_key_uri_escaped; | ||
| 146 | } | ||
| 147 | |||
| 148 | =head2 init_max_depth | ||
| 149 | |||
| 150 | Used by serialize_object(). The default value is 1. | ||
| 151 | |||
| 152 | =cut | ||
| 153 | |||
| 154 | sub init_max_depth {1} | ||
| 155 | |||
| 156 | =head2 init_force_load | ||
| 157 | |||
| 158 | Used by serialize_object(). The default value is 1 (which is B<not> | ||
| 159 | the default value in as_tree()). | ||
| 160 | |||
| 161 | =cut | ||
| 162 | |||
| 163 | sub init_force_load {1} | ||
| 164 | |||
| 165 | =head2 init_debug | ||
| 166 | |||
| 167 | Some messages on stderr if true. Default is false. | ||
| 168 | |||
| 169 | =cut | ||
| 170 | |||
| 171 | sub init_debug {0} | ||
| 172 | |||
| 173 | =head2 serialize_object( I<rdbo_object> ) | ||
| 174 | |||
| 175 | Returns I<rdbo_object> as a hash ref, using the as_tree() Helper method. | ||
| 176 | |||
| 177 | =cut | ||
| 178 | |||
| 179 | sub serialize_object { | ||
| 180 | my $self = shift; | ||
| 181 | my $obj = shift; | ||
| 182 | unless ( defined $obj and ref $obj ) { | ||
| 183 | croak | ||
| 184 | "must load or pass a RDBO object before calling serialize_object"; | ||
| 185 | } | ||
| 186 | my $prune = $self->prune; | ||
| 187 | my $hash = $obj->as_tree( | ||
| 188 | force_load => $self->force_load, | ||
| 189 | max_depth => $self->max_depth, | ||
| 190 | prune => sub { | ||
| 191 | my ( $rel_meta, $object, $depth ) = @_; | ||
| 192 | |||
| 193 | if ( $self->debug ) { | ||
| 194 | warn sprintf( | ||
| 195 | "eval prune for rel %s for object %s at depth %s\n", | ||
| 196 | $rel_meta->name, $object, $depth ); | ||
| 197 | } | ||
| 198 | |||
| 199 | return exists $prune->{ $rel_meta->name }; | ||
| 200 | }, | ||
| 201 | @{ $self->tree_opts }, | ||
| 202 | |||
| 203 | ); | ||
| 204 | return $hash; | ||
| 205 | } | ||
| 206 | |||
| 207 | =head2 xml_root_element( [I<tagname>] ) | ||
| 208 | |||
| 209 | Get/set the root tag name to use when serializing to XML. Default | ||
| 210 | is to use the return value of get_xml_root_element(). | ||
| 211 | |||
| 212 | =head2 get_xml_root_element( I<rdbo_obj> ) | ||
| 213 | |||
| 214 | Returns the name of the element to use as the top-level XML tag, | ||
| 215 | by default the RDBO class name, XML-escaped. | ||
| 216 | |||
| 217 | =cut | ||
| 218 | |||
| 219 | sub get_xml_root_element { | ||
| 220 | my $el = $_[1]->meta->class; | ||
| 221 | $el =~ s/\W/_/g; | ||
| 222 | return $el; | ||
| 223 | } | ||
| 224 | |||
| 225 | =head2 to_xml( I<hash>, I<rdbo_obj> [, I<strip_plurals>] ) | ||
| 226 | |||
| 227 | Returns I<hash> as XML, using xml_root_element() as the top-level tag. | ||
| 228 | |||
| 229 | =cut | ||
| 230 | |||
| 231 | sub to_xml { | ||
| 232 | my $self = shift; | ||
| 233 | my $hash = shift or croak "hash ref required"; | ||
| 234 | my $obj = shift or croak "RDBO object required"; | ||
| 235 | my $root = $self->xml_root_element || $self->get_xml_root_element($obj); | ||
| 236 | return SWISH::Prog::Utils->perl_to_xml( $hash, $root, @_ ); | ||
| 237 | } | ||
| 238 | |||
| 239 | =head2 make_doc( I<rdbo_obj> ) | ||
| 240 | |||
| 241 | Returns a SWISH::Prog::Doc instance for I<rdbo_obj>. | ||
| 242 | |||
| 243 | =cut | ||
| 244 | |||
| 245 | sub make_doc { | ||
| 246 | my $self = shift; | ||
| 247 | my $obj = shift or croak "RDBO object required"; | ||
| 248 | my $xml = $self->to_xml( $self->serialize_object($obj), $obj ); | ||
| 249 | return SWISH::Prog::Doc->new( | ||
| 250 | content => $xml, | ||
| 251 | url => $self->get_primary_key($obj), | ||
| 252 | modtime => time(), | ||
| 253 | parser => 'XML*', | ||
| 254 | type => 'application/x-rdbo-indexed', # TODO ?? | ||
| 255 | ); | ||
| 256 | } | ||
| 257 | |||
| 258 | =head2 run( I<rdbo_obj> [, I<action>] ) | ||
| 259 | |||
| 260 | The main method. Serializes I<rdbo_obj> and hands it to the swish_indexer() | ||
| 261 | process() method. | ||
| 262 | |||
| 263 | =cut | ||
| 264 | |||
| 265 | sub run { | ||
| 266 | my $self = shift; | ||
| 267 | my $obj = shift or croak "RDBO object required"; | ||
| 268 | my $action = shift; | ||
| 269 | my $indexer = $self->swish_indexer; | ||
| 270 | |||
| 271 | # the very first time we run we must seed the index | ||
| 272 | # with a dummy "document" so that we can pass the -u | ||
| 273 | # option in both insert() and update(). | ||
| 274 | # the -u option to the native swish-e indexer means | ||
| 275 | # that the index *as a whole* should be updated, | ||
| 276 | # not just a particular "document". | ||
| 277 | if ( !-s $indexer->invindex->path->file('swish.xml') ) { | ||
| 278 | $self->__seed_index(); | ||
| 279 | } | ||
| 280 | my $doc = $self->make_doc($obj); | ||
| 281 | $doc->action($action) if $action; | ||
| 282 | $indexer->process($doc); | ||
| 283 | } | ||
| 284 | |||
| 285 | sub __seed_index { | ||
| 286 | my $self = shift; | ||
| 287 | my $dummy = SWISH::Prog::Doc->new( | ||
| 288 | content => "000dummy000", # assume no one ever searches for this.. | ||
| 289 | url => "000-dummy-000", | ||
| 290 | modtime => time(), | ||
| 291 | parser => 'TXT*', | ||
| 292 | type => 'application/x-rdbo-indexed', # TODO ?? | ||
| 293 | ); | ||
| 294 | |||
| 295 | # must init a new indexer since it will write the swish.xml file | ||
| 296 | # only when it is destroyed. | ||
| 297 | my $indexer = $self->init_swish_indexer(); | ||
| 298 | my $opts = $indexer->opts; | ||
| 299 | $indexer->opts(''); | ||
| 300 | $indexer->process($dummy); | ||
| 301 | $indexer->opts($opts); | ||
| 302 | |||
| 303 | } | ||
| 304 | |||
| 305 | =head2 insert( I<rdbo_obj> ) | ||
| 306 | |||
| 307 | Calls run() with the appropriate arguments. | ||
| 308 | |||
| 309 | =cut | ||
| 310 | |||
| 311 | sub insert { | ||
| 312 | my $self = shift; | ||
| 313 | my $obj = shift or croak "RDBO object required"; | ||
| 314 | $self->swish_indexer->opts('-u'); # TODO header should be enough | ||
| 315 | $self->run($obj); # no action. the default is to 'Index' (add) | ||
| 316 | } | ||
| 317 | |||
| 318 | =head2 update( I<rdbo_obj> ) | ||
| 319 | |||
| 320 | Calls run() with the appropriate arguments. | ||
| 321 | |||
| 322 | =cut | ||
| 323 | |||
| 324 | sub update { | ||
| 325 | my $self = shift; | ||
| 326 | my $obj = shift or croak "RDBO object required"; | ||
| 327 | $self->swish_indexer->opts('-u'); # TODO header should be enough | ||
| 328 | $self->run( $obj, 'Update' ); | ||
| 329 | } | ||
| 330 | |||
| 331 | =head2 delete( I<rdbo_obj> ) | ||
| 332 | |||
| 333 | Calls run() with the appropriate arguments. | ||
| 334 | |||
| 335 | =cut | ||
| 336 | |||
| 337 | sub delete { | ||
| 338 | my $self = shift; | ||
| 339 | my $obj = shift or croak "RDBO object required"; | ||
| 340 | $self->swish_indexer->opts('-r'); | ||
| 341 | $self->run( $obj, 'Remove' ); | ||
| 342 | } | ||
| 343 | |||
| 344 | 1; | ||
| 345 | |||
| 346 | __END__ | ||