1 package OpenILS::Application::Storage::Publisher;
2 use base qw/OpenILS::Application::Storage/;
5 use Digest::MD5 qw/md5_hex/;
6 use OpenSRF::EX qw/:try/;
8 use OpenSRF::Utils::Logger qw/:level/;
9 use OpenILS::Utils::Fieldmapper;
11 my $log = 'OpenSRF::Utils::Logger';
19 $class = ref($class) || $class;
21 $args{package} ||= $class;
22 __PACKAGE__->SUPER::register_method( %args );
24 if (exists($dup_args{cachable}) and $dup_args{cachable}) {
25 (my $name = $dup_args{api_name}) =~ s/^open-ils\.storage/open-ils.storage.cachable/o;
26 if ($name ne $dup_args{api_name}) {
27 $dup_args{real_api_name} = $dup_args{api_name};
28 $dup_args{method} = 'cachable_wrapper';
29 $dup_args{api_name} = $name;
30 $dup_args{package} = __PACKAGE__;
31 __PACKAGE__->SUPER::register_method( %dup_args );
36 sub cachable_wrapper {
45 cache_page_size => 1000,
49 my $key_string = $self->api_name;
50 for (my $ind = 0; $ind < scalar(@args); $ind++) {
51 if ( $args[$ind] eq 'limit' ||
52 $args[$ind] eq 'offset' ||
53 $args[$ind] eq 'cache_page_size' ||
54 $args[$ind] eq 'timeout' ) {
59 $cache_args{$args[$key_ind]} = $args[$value_ind];
60 $log->debug("Cache limiter value for $args[$key_ind] is $args[$value_ind]", INTERNAL);
63 $key_string .= $args[$ind];
64 $log->debug("Partial cache key value is $args[$ind]", INTERNAL);
65 push @real_args, $args[$ind];
68 my $cache_page = int($cache_args{offset} / $cache_args{cache_page_size});
71 $cache_key = md5_hex($key_string.$cache_page);
74 $log->debug("Key string for cache lookup is $key_string -> $cache_key", DEBUG);
75 $log->debug("Cache page is $cache_page", DEBUG);
77 my $cached_res = OpenSRF::Utils::Cache->new->get_cache( $cache_key );
78 if (defined $cached_res) {
79 $log->debug("Found ".scalar(@$cached_res)." records in the cache", INFO);
80 $log->debug("Values from cache: ".join(', ', @$cached_res), INTERNAL);
81 my $start = int($cache_args{offset} - ($cache_page * $cache_args{cache_page_size}));
82 my $end = int($start + $cache_args{limit} - 1);
83 $log->debug("Responding with values from ".$start.' to '.$end,DEBUG);
84 $client->respond( $_ ) for ( grep { defined } @$cached_res[ $start .. $end ]);
88 my $method = $self->method_lookup($self->{real_api_name});
89 my @res = $method->run(@real_args);
92 $client->respond( $_ ) for ( grep { defined } @res[$cache_args{offset} .. int($cache_args{offset} + $cache_args{limit} - 1)] );
94 $log->debug("Saving values from ".int($cache_page * $cache_args{cache_page_size})." to ".
95 int(($cache_page + 1) * $cache_args{cache_page_size}). "to the cache", INTERNAL);
97 OpenSRF::Utils::Cache->new->put_cache(
99 [@res[int($cache_page * $cache_args{cache_page_size}) .. int(($cache_page + 1) * $cache_args{cache_page_size}) ]] =>
100 OpenSRF::Utils->interval_to_seconds( $cache_args{timeout} )
104 $log->error("Cache seems to be down, $e");
115 my $cdbi = $self->{cdbi};
117 for my $id ( @ids ) {
120 my ($rec) = $cdbi->fast_fieldmapper($id);
121 if ($self->api_name !~ /batch/o) {
122 return $rec if ($rec);
124 $client->respond($rec);
134 my $cdbi = $self->{cdbi};
136 for my $obj ($cdbi->search_where(@args)) {
137 next unless ref($obj);
138 $client->respond( $obj->to_fieldmapper );
148 my $cdbi = $self->{cdbi};
150 (my $search_type = $self->api_name) =~ s/.*\.(search[^.]*).*/$1/o;
152 for my $obj ($cdbi->$search_type(@args)) {
153 next unless ref($obj);
154 $client->respond( $obj->to_fieldmapper );
159 sub search_one_field {
164 (my $field = $self->api_name) =~ s/.*\.([^\.]+)$/$1/o;
166 return search( $self, $client, $field, @args );
169 sub old_search_one_field {
174 (my $search_type = $self->api_name) =~ s/.*\.(search[^.]*).*/$1/o;
175 (my $col = $self->api_name) =~ s/.*\.$search_type\.([^.]+).*/$1/;
176 my $cdbi = $self->{cdbi};
179 $like = 1 if ($search_type =~ /like$/o);
180 $like = 2 if ($search_type =~ /fts$/o);
181 $like = 3 if ($search_type =~ /regex$/o);
183 for my $term (@terms) {
184 $log->debug("Searching $cdbi for $col using type $search_type, value '$term'",DEBUG);
186 return [ $cdbi->fast_fieldmapper($term,$col,$like) ];
188 $client->respond( [ $cdbi->fast_fieldmapper($term,$col,$like) ] );
199 my $cdbi = $self->{cdbi};
203 my $rec = $cdbi->create($node);
204 $success = $rec->id if ($rec);
217 my $cdbi = $self->{cdbi};
219 return $cdbi->update($node);
227 my $where = 'WHERE ';
229 my $cdbi = $self->{cdbi};
230 my $table = $cdbi->table;
232 my @keys = sort keys %$search;
236 for my $col ( @keys ) {
237 if (ref($$search{$col}) and ref($$search{$col}) =~ /ARRAY/o) {
238 push @wheres, "$col IN (" . join(',', map { '?' } @{ $$search{$col} }) . ')';
239 push @binds, map { "$_" } @{ $$search{$col} };
241 push @wheres, "$col = ?";
242 push @binds, $$search{$col};
245 $where .= join ' AND ', @wheres;
247 my $delete = "DELETE FROM $table $where";
249 $log->debug("Performing MASS deletion : $delete",DEBUG);
251 my $dbh = $cdbi->db_Main;
254 my $sth = $dbh->prepare($delete);
255 $sth->execute( @binds );
257 $log->debug("MASS Delete succeeded",DEBUG);
259 $log->debug("MASS Delete FAILED : ".shift(),DEBUG);
265 sub remote_update_node {
271 my $cdbi = $self->{cdbi};
275 $success = $cdbi->remote_update($keys,$vals);
288 my $cdbi = $self->{cdbi};
292 $success = $cdbi->merge($keys,$vals)->id;
304 my $cdbi = $self->{cdbi};
308 $success = $cdbi->delete($node);
320 my $unwrap = $self->{unwrap};
322 my $cdbi = $self->{cdbi};
323 my $api_name = $self->api_name;
324 (my $single_call_api_name = $api_name) =~ s/batch\.//o;
326 $log->debug("Default $api_name looking up $single_call_api_name...",INTERNAL);
327 my $method = $self->method_lookup($single_call_api_name);
330 while ( my $node = shift(@nodes) ) {
331 my ($res) = $method->run( ($unwrap ? (@$node) : ($node)) );
332 push(@success, 1) if ($res >= 0);
335 my $insert_total = 0;
336 $insert_total += $_ for (@success);
338 return $insert_total;
341 OpenILS::Application::Storage::Publisher::actor->use;
343 $log->debug("ARG! Couldn't load actor class Publisher: $@", ERROR);
344 throw OpenSRF::EX::ERROR ("ARG! Couldn't load actor class Publisher: $@");
347 OpenILS::Application::Storage::Publisher::action->use;
349 $log->debug("ARG! Couldn't load action class Publisher: $@", ERROR);
350 throw OpenSRF::EX::ERROR ("ARG! Couldn't load action class Publisher: $@");
353 OpenILS::Application::Storage::Publisher::asset->use;
355 $log->debug("ARG! Couldn't load asset class Publisher: $@", ERROR);
356 throw OpenSRF::EX::ERROR ("ARG! Couldn't load asset class Publisher: $@");
359 OpenILS::Application::Storage::Publisher::biblio->use;
361 $log->debug("ARG! Couldn't load biblio class Publisher: $@", ERROR);
362 throw OpenSRF::EX::ERROR ("ARG! Couldn't load biblio class Publisher: $@");
365 OpenILS::Application::Storage::Publisher::config->use;
367 $log->debug("ARG! Couldn't load config class Publisher: $@", ERROR);
368 throw OpenSRF::EX::ERROR ("ARG! Couldn't load config class Publisher: $@");
371 OpenILS::Application::Storage::Publisher::metabib->use;
373 $log->debug("ARG! Couldn't load metabib class Publisher: $@", ERROR);
374 throw OpenSRF::EX::ERROR ("ARG! Couldn't load metabib class Publisher: $@");
377 OpenILS::Application::Storage::Publisher::authority->use;
379 $log->debug("ARG! Couldn't load authority class Publisher: $@", ERROR);
380 throw OpenSRF::EX::ERROR ("ARG! Couldn't load authority class Publisher: $@");
383 OpenILS::Application::Storage::Publisher::money->use;
385 $log->debug("ARG! Couldn't load money class Publisher: $@", ERROR);
386 throw OpenSRF::EX::ERROR ("ARG! Couldn't load money class Publisher: $@");
389 OpenILS::Application::Storage::Publisher::permission->use;
391 $log->debug("ARG! Couldn't load permission class Publisher: $@", ERROR);
392 throw OpenSRF::EX::ERROR ("ARG! Couldn't load permission class Publisher: $@");
395 OpenILS::Application::Storage::Publisher::container->use;
397 $log->debug("ARG! Couldn't load container class Publisher: $@", ERROR);
398 throw OpenSRF::EX::ERROR ("ARG! Couldn't load container class Publisher: $@");
403 for my $fmclass ( (Fieldmapper->classes) ) {
405 $log->debug("Generating methods for Fieldmapper class $fmclass", DEBUG);
407 next if ($fmclass->is_virtual);
409 (my $cdbi = $fmclass) =~ s/^Fieldmapper:://o;
410 (my $class = $cdbi) =~ s/::.*//o;
411 (my $api_class = $cdbi) =~ s/::/./go;
412 my $registration_class = __PACKAGE__ . "::$class";
413 my $api_prefix = 'open-ils.storage.direct.'.$api_class;
415 # Create the search methods
416 unless ( __PACKAGE__->is_registered( $api_prefix.'.search' ) ) {
417 __PACKAGE__->register_method(
418 api_name => $api_prefix.'.search',
428 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_where' ) ) {
429 __PACKAGE__->register_method(
430 api_name => $api_prefix.'.search_where',
431 method => 'search_where',
440 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_like' ) ) {
441 __PACKAGE__->register_method(
442 api_name => $api_prefix.'.search_like',
452 if (\&Class::DBI::search_fts) {
453 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_fts' ) ) {
454 __PACKAGE__->register_method(
455 api_name => $api_prefix.'.search_fts',
466 if (\&Class::DBI::search_regex) {
467 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_regex' ) ) {
468 __PACKAGE__->register_method(
469 api_name => $api_prefix.'.search_regex',
480 if (\&Class::DBI::search_ilike) {
481 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_ilike' ) ) {
482 __PACKAGE__->register_method(
483 api_name => $api_prefix.'.search_ilike',
494 # Create the retrieve method
495 unless ( __PACKAGE__->is_registered( $api_prefix.'.retrieve' ) ) {
496 __PACKAGE__->register_method(
497 api_name => $api_prefix.'.retrieve',
498 method => 'retrieve_node',
506 # Create the batch retrieve method
507 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.retrieve' ) ) {
508 __PACKAGE__->register_method(
509 api_name => $api_prefix.'.batch.retrieve',
510 method => 'retrieve_node',
519 for my $field ($fmclass->real_fields) {
520 unless ( __PACKAGE__->is_registered( $api_prefix.'.search.'.$field ) ) {
521 __PACKAGE__->register_method(
522 api_name => $api_prefix.'.search.'.$field,
523 method => 'search_one_field',
531 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_like.'.$field ) ) {
532 __PACKAGE__->register_method(
533 api_name => $api_prefix.'.search_like.'.$field,
534 method => 'search_one_field',
542 if (\&Class::DBI::search_fts) {
543 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_fts.'.$field ) ) {
544 __PACKAGE__->register_method(
545 api_name => $api_prefix.'.search_fts.'.$field,
546 method => 'search_one_field',
555 if (\&Class::DBI::search_regex) {
556 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_regex.'.$field ) ) {
557 __PACKAGE__->register_method(
558 api_name => $api_prefix.'.search_regex.'.$field,
559 method => 'search_one_field',
568 if (\&Class::DBI::search_ilike) {
569 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_ilike.'.$field ) ) {
570 __PACKAGE__->register_method(
571 api_name => $api_prefix.'.search_ilike.'.$field,
572 method => 'search_one_field',
584 unless ($fmclass->is_readonly) {
585 # Create the create method
586 unless ( __PACKAGE__->is_registered( $api_prefix.'.create' ) ) {
587 __PACKAGE__->register_method(
588 api_name => $api_prefix.'.create',
589 method => 'create_node',
596 # Create the batch create method
597 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.create' ) ) {
598 __PACKAGE__->register_method(
599 api_name => $api_prefix.'.batch.create',
600 method => 'batch_call',
607 # Create the update method
608 unless ( __PACKAGE__->is_registered( $api_prefix.'.update' ) ) {
609 __PACKAGE__->register_method(
610 api_name => $api_prefix.'.update',
611 method => 'update_node',
618 # Create the batch update method
619 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.update' ) ) {
620 __PACKAGE__->register_method(
621 api_name => $api_prefix.'.batch.update',
622 method => 'batch_call',
629 # Create the delete method
630 unless ( __PACKAGE__->is_registered( $api_prefix.'.delete' ) ) {
631 __PACKAGE__->register_method(
632 api_name => $api_prefix.'.delete',
633 method => 'delete_node',
640 # Create the batch delete method
641 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.delete' ) ) {
642 __PACKAGE__->register_method(
643 api_name => $api_prefix.'.batch.delete',
644 method => 'batch_call',
651 # Create the merge method
652 unless ( __PACKAGE__->is_registered( $api_prefix.'.merge' ) ) {
653 __PACKAGE__->register_method(
654 api_name => $api_prefix.'.merge',
655 method => 'merge_node',
662 # Create the batch merge method
663 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.merge' ) ) {
664 __PACKAGE__->register_method(
665 api_name => $api_prefix.'.batch.merge',
666 method => 'batch_call',
674 # Create the remote_update method
675 unless ( __PACKAGE__->is_registered( $api_prefix.'.remote_update' ) ) {
676 __PACKAGE__->register_method(
677 api_name => $api_prefix.'.remote_update',
678 method => 'remote_update_node',
685 # Create the batch remote_update method
686 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.remote_update' ) ) {
687 __PACKAGE__->register_method(
688 api_name => $api_prefix.'.batch.remote_update',
689 method => 'batch_call',
697 # Create the search-based mass delete method
698 unless ( __PACKAGE__->is_registered( $api_prefix.'.mass_delete' ) ) {
699 __PACKAGE__->register_method(
700 api_name => $api_prefix.'.mass_delete',
701 method => 'mass_delete',