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 if (ref($args[0]) eq 'HASH') {
136 $args[1]{limit_dialect} = $self->{cdbi}->db_Main;
138 $args[1] = {limit_dialect => $self->{cdbi}->db_Main };
141 $args[0] = { @args };
142 $args[1] = {limit_dialect => $self->{cdbi} };
145 my $cdbi = $self->{cdbi};
147 for my $obj ($cdbi->search_where(@args)) {
148 next unless ref($obj);
149 $client->respond( $obj->to_fieldmapper );
159 my $cdbi = $self->{cdbi};
161 (my $search_type = $self->api_name) =~ s/.*\.(search[^.]*).*/$1/o;
163 for my $obj ($cdbi->$search_type(@args)) {
164 next unless ref($obj);
165 $client->respond( $obj->to_fieldmapper );
170 sub search_one_field {
175 (my $field = $self->api_name) =~ s/.*\.([^\.]+)$/$1/o;
177 return search( $self, $client, $field, @args );
180 sub old_search_one_field {
185 (my $search_type = $self->api_name) =~ s/.*\.(search[^.]*).*/$1/o;
186 (my $col = $self->api_name) =~ s/.*\.$search_type\.([^.]+).*/$1/;
187 my $cdbi = $self->{cdbi};
190 $like = 1 if ($search_type =~ /like$/o);
191 $like = 2 if ($search_type =~ /fts$/o);
192 $like = 3 if ($search_type =~ /regex$/o);
194 for my $term (@terms) {
195 $log->debug("Searching $cdbi for $col using type $search_type, value '$term'",DEBUG);
197 return [ $cdbi->fast_fieldmapper($term,$col,$like) ];
199 $client->respond( [ $cdbi->fast_fieldmapper($term,$col,$like) ] );
210 my $cdbi = $self->{cdbi};
214 my $rec = $cdbi->create($node);
215 $success = $rec->id if ($rec);
228 my $cdbi = $self->{cdbi};
230 return $cdbi->update($node);
238 my $where = 'WHERE ';
240 my $cdbi = $self->{cdbi};
241 my $table = $cdbi->table;
243 my @keys = sort keys %$search;
247 for my $col ( @keys ) {
248 if (ref($$search{$col}) and ref($$search{$col}) =~ /ARRAY/o) {
249 push @wheres, "$col IN (" . join(',', map { '?' } @{ $$search{$col} }) . ')';
250 push @binds, map { "$_" } @{ $$search{$col} };
252 push @wheres, "$col = ?";
253 push @binds, $$search{$col};
256 $where .= join ' AND ', @wheres;
258 my $delete = "DELETE FROM $table $where";
260 $log->debug("Performing MASS deletion : $delete",DEBUG);
262 my $dbh = $cdbi->db_Main;
265 my $sth = $dbh->prepare($delete);
266 $sth->execute( @binds );
268 $log->debug("MASS Delete succeeded",DEBUG);
270 $log->debug("MASS Delete FAILED : ".shift(),DEBUG);
276 sub remote_update_node {
282 my $cdbi = $self->{cdbi};
286 $success = $cdbi->remote_update($keys,$vals);
299 my $cdbi = $self->{cdbi};
303 $success = $cdbi->merge($keys,$vals)->id;
315 my $cdbi = $self->{cdbi};
319 $success = $cdbi->delete($node);
331 my $unwrap = $self->{unwrap};
333 my $cdbi = $self->{cdbi};
334 my $api_name = $self->api_name;
335 (my $single_call_api_name = $api_name) =~ s/batch\.//o;
337 $log->debug("Default $api_name looking up $single_call_api_name...",INTERNAL);
338 my $method = $self->method_lookup($single_call_api_name);
341 while ( my $node = shift(@nodes) ) {
342 my ($res) = $method->run( ($unwrap ? (@$node) : ($node)) );
343 push(@success, 1) if ($res >= 0);
346 my $insert_total = 0;
347 $insert_total += $_ for (@success);
349 return $insert_total;
352 OpenILS::Application::Storage::Publisher::actor->use;
354 $log->debug("ARG! Couldn't load actor class Publisher: $@", ERROR);
355 throw OpenSRF::EX::ERROR ("ARG! Couldn't load actor class Publisher: $@");
358 OpenILS::Application::Storage::Publisher::action->use;
360 $log->debug("ARG! Couldn't load action class Publisher: $@", ERROR);
361 throw OpenSRF::EX::ERROR ("ARG! Couldn't load action class Publisher: $@");
364 OpenILS::Application::Storage::Publisher::asset->use;
366 $log->debug("ARG! Couldn't load asset class Publisher: $@", ERROR);
367 throw OpenSRF::EX::ERROR ("ARG! Couldn't load asset class Publisher: $@");
370 OpenILS::Application::Storage::Publisher::biblio->use;
372 $log->debug("ARG! Couldn't load biblio class Publisher: $@", ERROR);
373 throw OpenSRF::EX::ERROR ("ARG! Couldn't load biblio class Publisher: $@");
376 OpenILS::Application::Storage::Publisher::config->use;
378 $log->debug("ARG! Couldn't load config class Publisher: $@", ERROR);
379 throw OpenSRF::EX::ERROR ("ARG! Couldn't load config class Publisher: $@");
382 OpenILS::Application::Storage::Publisher::metabib->use;
384 $log->debug("ARG! Couldn't load metabib class Publisher: $@", ERROR);
385 throw OpenSRF::EX::ERROR ("ARG! Couldn't load metabib class Publisher: $@");
388 OpenILS::Application::Storage::Publisher::authority->use;
390 $log->debug("ARG! Couldn't load authority class Publisher: $@", ERROR);
391 throw OpenSRF::EX::ERROR ("ARG! Couldn't load authority class Publisher: $@");
394 OpenILS::Application::Storage::Publisher::money->use;
396 $log->debug("ARG! Couldn't load money class Publisher: $@", ERROR);
397 throw OpenSRF::EX::ERROR ("ARG! Couldn't load money class Publisher: $@");
400 OpenILS::Application::Storage::Publisher::permission->use;
402 $log->debug("ARG! Couldn't load permission class Publisher: $@", ERROR);
403 throw OpenSRF::EX::ERROR ("ARG! Couldn't load permission class Publisher: $@");
406 OpenILS::Application::Storage::Publisher::container->use;
408 $log->debug("ARG! Couldn't load container class Publisher: $@", ERROR);
409 throw OpenSRF::EX::ERROR ("ARG! Couldn't load container class Publisher: $@");
414 for my $fmclass ( (Fieldmapper->classes) ) {
416 $log->debug("Generating methods for Fieldmapper class $fmclass", DEBUG);
418 next if ($fmclass->is_virtual);
420 (my $cdbi = $fmclass) =~ s/^Fieldmapper:://o;
421 (my $class = $cdbi) =~ s/::.*//o;
422 (my $api_class = $cdbi) =~ s/::/./go;
423 my $registration_class = __PACKAGE__ . "::$class";
424 my $api_prefix = 'open-ils.storage.direct.'.$api_class;
426 # Create the search methods
427 unless ( __PACKAGE__->is_registered( $api_prefix.'.search' ) ) {
428 __PACKAGE__->register_method(
429 api_name => $api_prefix.'.search',
439 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_where' ) ) {
440 __PACKAGE__->register_method(
441 api_name => $api_prefix.'.search_where',
442 method => 'search_where',
451 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_like' ) ) {
452 __PACKAGE__->register_method(
453 api_name => $api_prefix.'.search_like',
463 if (\&Class::DBI::search_fts) {
464 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_fts' ) ) {
465 __PACKAGE__->register_method(
466 api_name => $api_prefix.'.search_fts',
477 if (\&Class::DBI::search_regex) {
478 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_regex' ) ) {
479 __PACKAGE__->register_method(
480 api_name => $api_prefix.'.search_regex',
491 if (\&Class::DBI::search_ilike) {
492 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_ilike' ) ) {
493 __PACKAGE__->register_method(
494 api_name => $api_prefix.'.search_ilike',
505 # Create the retrieve method
506 unless ( __PACKAGE__->is_registered( $api_prefix.'.retrieve' ) ) {
507 __PACKAGE__->register_method(
508 api_name => $api_prefix.'.retrieve',
509 method => 'retrieve_node',
517 # Create the batch retrieve method
518 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.retrieve' ) ) {
519 __PACKAGE__->register_method(
520 api_name => $api_prefix.'.batch.retrieve',
521 method => 'retrieve_node',
530 for my $field ($fmclass->real_fields) {
531 unless ( __PACKAGE__->is_registered( $api_prefix.'.search.'.$field ) ) {
532 __PACKAGE__->register_method(
533 api_name => $api_prefix.'.search.'.$field,
534 method => 'search_one_field',
542 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_like.'.$field ) ) {
543 __PACKAGE__->register_method(
544 api_name => $api_prefix.'.search_like.'.$field,
545 method => 'search_one_field',
553 if (\&Class::DBI::search_fts) {
554 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_fts.'.$field ) ) {
555 __PACKAGE__->register_method(
556 api_name => $api_prefix.'.search_fts.'.$field,
557 method => 'search_one_field',
566 if (\&Class::DBI::search_regex) {
567 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_regex.'.$field ) ) {
568 __PACKAGE__->register_method(
569 api_name => $api_prefix.'.search_regex.'.$field,
570 method => 'search_one_field',
579 if (\&Class::DBI::search_ilike) {
580 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_ilike.'.$field ) ) {
581 __PACKAGE__->register_method(
582 api_name => $api_prefix.'.search_ilike.'.$field,
583 method => 'search_one_field',
595 unless ($fmclass->is_readonly) {
596 # Create the create method
597 unless ( __PACKAGE__->is_registered( $api_prefix.'.create' ) ) {
598 __PACKAGE__->register_method(
599 api_name => $api_prefix.'.create',
600 method => 'create_node',
607 # Create the batch create method
608 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.create' ) ) {
609 __PACKAGE__->register_method(
610 api_name => $api_prefix.'.batch.create',
611 method => 'batch_call',
618 # Create the update method
619 unless ( __PACKAGE__->is_registered( $api_prefix.'.update' ) ) {
620 __PACKAGE__->register_method(
621 api_name => $api_prefix.'.update',
622 method => 'update_node',
629 # Create the batch update method
630 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.update' ) ) {
631 __PACKAGE__->register_method(
632 api_name => $api_prefix.'.batch.update',
633 method => 'batch_call',
640 # Create the delete method
641 unless ( __PACKAGE__->is_registered( $api_prefix.'.delete' ) ) {
642 __PACKAGE__->register_method(
643 api_name => $api_prefix.'.delete',
644 method => 'delete_node',
651 # Create the batch delete method
652 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.delete' ) ) {
653 __PACKAGE__->register_method(
654 api_name => $api_prefix.'.batch.delete',
655 method => 'batch_call',
662 # Create the merge method
663 unless ( __PACKAGE__->is_registered( $api_prefix.'.merge' ) ) {
664 __PACKAGE__->register_method(
665 api_name => $api_prefix.'.merge',
666 method => 'merge_node',
673 # Create the batch merge method
674 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.merge' ) ) {
675 __PACKAGE__->register_method(
676 api_name => $api_prefix.'.batch.merge',
677 method => 'batch_call',
685 # Create the remote_update method
686 unless ( __PACKAGE__->is_registered( $api_prefix.'.remote_update' ) ) {
687 __PACKAGE__->register_method(
688 api_name => $api_prefix.'.remote_update',
689 method => 'remote_update_node',
696 # Create the batch remote_update method
697 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.remote_update' ) ) {
698 __PACKAGE__->register_method(
699 api_name => $api_prefix.'.batch.remote_update',
700 method => 'batch_call',
708 # Create the search-based mass delete method
709 unless ( __PACKAGE__->is_registered( $api_prefix.'.mass_delete' ) ) {
710 __PACKAGE__->register_method(
711 api_name => $api_prefix.'.mass_delete',
712 method => 'mass_delete',