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 );
35 if ($dup_args{real_api_name} =~ /^open-ils\.storage\.direct\..+\.search.+/o ||
36 $dup_args{api_name} =~ /^open-ils\.storage\.direct\..+\.search.+/o) {
37 $dup_args{api_name} = $dup_args{real_api_name} if ($dup_args{real_api_name});
39 (my $name = $dup_args{api_name}) =~ s/\.direct\./.id_list./o;
41 $dup_args{notes} = $dup_args{real_api_name};
42 $dup_args{real_api_name} = $dup_args{api_name};
43 $dup_args{method} = 'search_ids';
44 $dup_args{api_name} = $name;
45 $dup_args{package} = __PACKAGE__;
47 __PACKAGE__->SUPER::register_method( %dup_args );
51 sub cachable_wrapper {
60 cache_page_size => 1000,
64 my $key_string = $self->api_name;
65 for (my $ind = 0; $ind < scalar(@args); $ind++) {
66 if ( $args[$ind] eq 'limit' ||
67 $args[$ind] eq 'offset' ||
68 $args[$ind] eq 'cache_page_size' ||
69 $args[$ind] eq 'timeout' ) {
74 $cache_args{$args[$key_ind]} = $args[$value_ind];
75 $log->debug("Cache limiter value for $args[$key_ind] is $args[$value_ind]", INTERNAL);
78 $key_string .= $args[$ind];
79 $log->debug("Partial cache key value is $args[$ind]", INTERNAL);
80 push @real_args, $args[$ind];
83 my $cache_page = int($cache_args{offset} / $cache_args{cache_page_size});
86 $cache_key = md5_hex($key_string.$cache_page);
89 $log->debug("Key string for cache lookup is $key_string -> $cache_key", DEBUG);
90 $log->debug("Cache page is $cache_page", DEBUG);
92 my $cached_res = OpenSRF::Utils::Cache->new->get_cache( $cache_key );
93 if (defined $cached_res) {
94 $log->debug("Found ".scalar(@$cached_res)." records in the cache", INFO);
95 $log->debug("Values from cache: ".join(', ', @$cached_res), INTERNAL);
96 my $start = int($cache_args{offset} - ($cache_page * $cache_args{cache_page_size}));
97 my $end = int($start + $cache_args{limit} - 1);
98 $log->debug("Responding with values from ".$start.' to '.$end,DEBUG);
99 $client->respond( $_ ) for ( grep { defined } @$cached_res[ $start .. $end ]);
103 my $method = $self->method_lookup($self->{real_api_name});
104 my @res = $method->run(@real_args);
107 $client->respond( $_ ) for ( grep { defined } @res[$cache_args{offset} .. int($cache_args{offset} + $cache_args{limit} - 1)] );
109 $log->debug("Saving values from ".int($cache_page * $cache_args{cache_page_size})." to ".
110 int(($cache_page + 1) * $cache_args{cache_page_size}). "to the cache", INTERNAL);
112 OpenSRF::Utils::Cache->new->put_cache(
114 [@res[int($cache_page * $cache_args{cache_page_size}) .. int(($cache_page + 1) * $cache_args{cache_page_size}) ]] =>
115 OpenSRF::Utils->interval_to_seconds( $cache_args{timeout} )
119 $log->error("Cache seems to be down, $e");
129 my $cdbi = $self->{cdbi};
130 my $table = $cdbi->table;
134 WHERE id IN (( SELECT (RANDOM() * (SELECT MAX(id) FROM $table))::INT LIMIT 1 ));
140 my $id = $cdbi->db_Main->selectcol_arrayref($sql);
143 return ($cdbi->fast_fieldmapper(@$id))[0];
153 my $cdbi = $self->{cdbi};
155 for my $id ( @ids ) {
158 my ($rec) = $cdbi->fast_fieldmapper($id);
159 if ($self->api_name !~ /batch/o) {
160 return $rec if ($rec);
162 $client->respond($rec);
172 my @res = $self->method_lookup($self->{real_api_name})->run(@args);
174 if (ref($res[0]) eq 'ARRAY') {
175 return [ map { $_->id } @{ $res[0] } ];
178 $client->respond($_) for ( map { $_->id } @res );
187 if (ref($args[0]) eq 'HASH') {
189 $args[1]{limit_dialect} = $self->{cdbi}->db_Main;
191 $args[1] = {limit_dialect => $self->{cdbi}->db_Main };
194 $args[0] = { @args };
195 $args[1] = {limit_dialect => $self->{cdbi} };
198 my $cdbi = $self->{cdbi};
200 for my $obj ($cdbi->search_where(@args)) {
201 next unless ref($obj);
202 $client->respond( $obj->to_fieldmapper );
212 my $cdbi = $self->{cdbi};
214 (my $search_type = $self->api_name) =~ s/.*\.(search[^.]*).*/$1/o;
216 for my $obj ($cdbi->$search_type(@args)) {
217 next unless ref($obj);
218 $client->respond( $obj->to_fieldmapper );
223 sub search_one_field {
228 (my $field = $self->api_name) =~ s/.*\.([^\.]+)$/$1/o;
230 return search( $self, $client, $field, @args );
233 sub old_search_one_field {
238 (my $search_type = $self->api_name) =~ s/.*\.(search[^.]*).*/$1/o;
239 (my $col = $self->api_name) =~ s/.*\.$search_type\.([^.]+).*/$1/;
240 my $cdbi = $self->{cdbi};
243 $like = 1 if ($search_type =~ /like$/o);
244 $like = 2 if ($search_type =~ /fts$/o);
245 $like = 3 if ($search_type =~ /regex$/o);
247 for my $term (@terms) {
248 $log->debug("Searching $cdbi for $col using type $search_type, value '$term'",DEBUG);
250 return [ $cdbi->fast_fieldmapper($term,$col,$like) ];
252 $client->respond( [ $cdbi->fast_fieldmapper($term,$col,$like) ] );
263 local $OpenILS::Application::Storage::WRITE = 1;
265 my $cdbi = $self->{cdbi};
269 my $rec = $cdbi->create($node);
270 $success = $rec->id if ($rec);
283 local $OpenILS::Application::Storage::WRITE = 1;
285 my $cdbi = $self->{cdbi};
287 return $cdbi->update($node);
295 local $OpenILS::Application::Storage::WRITE = 1;
297 my $where = 'WHERE ';
299 my $cdbi = $self->{cdbi};
300 my $table = $cdbi->table;
302 my @keys = sort keys %$search;
306 for my $col ( @keys ) {
307 if (ref($$search{$col}) and ref($$search{$col}) =~ /ARRAY/o) {
308 push @wheres, "$col IN (" . join(',', map { '?' } @{ $$search{$col} }) . ')';
309 push @binds, map { "$_" } @{ $$search{$col} };
311 push @wheres, "$col = ?";
312 push @binds, $$search{$col};
315 $where .= join ' AND ', @wheres;
317 my $delete = "DELETE FROM $table $where";
319 $log->debug("Performing MASS deletion : $delete",DEBUG);
321 my $dbh = $cdbi->db_Main;
324 my $sth = $dbh->prepare($delete);
325 $sth->execute( @binds );
327 $log->debug("MASS Delete succeeded",DEBUG);
329 $log->debug("MASS Delete FAILED : ".shift(),DEBUG);
335 sub remote_update_node {
341 local $OpenILS::Application::Storage::WRITE = 1;
343 my $cdbi = $self->{cdbi};
347 $success = $cdbi->remote_update($keys,$vals);
360 local $OpenILS::Application::Storage::WRITE = 1;
362 my $cdbi = $self->{cdbi};
366 $success = $cdbi->merge($keys,$vals)->id;
378 local $OpenILS::Application::Storage::WRITE = 1;
380 my $cdbi = $self->{cdbi};
384 $success = $cdbi->delete($node);
396 my $unwrap = $self->{unwrap};
398 my $cdbi = $self->{cdbi};
399 my $api_name = $self->api_name;
400 (my $single_call_api_name = $api_name) =~ s/batch\.//o;
402 $log->debug("Default $api_name looking up $single_call_api_name...",INTERNAL);
403 my $method = $self->method_lookup($single_call_api_name);
406 while ( my $node = shift(@nodes) ) {
407 my ($res) = $method->run( ($unwrap ? (@$node) : ($node)) );
408 push(@success, 1) if ($res >= 0);
411 my $insert_total = 0;
412 $insert_total += $_ for (@success);
414 return $insert_total;
418 # --------------------- End of generic methods -----------------------
421 for my $pkg ( qw/actor action asset biblio config metabib authority money permission container/ ) {
422 "OpenILS::Application::Storage::Publisher::$pkg"->use;
424 $log->debug("ARG! Couldn't load $pkg class Publisher: $@", ERROR);
425 throw OpenSRF::EX::ERROR ("ARG! Couldn't load $pkg class Publisher: $@");
429 for my $fmclass ( (Fieldmapper->classes) ) {
431 $log->debug("Generating methods for Fieldmapper class $fmclass", DEBUG);
433 next if ($fmclass->is_virtual);
435 (my $cdbi = $fmclass) =~ s/^Fieldmapper:://o;
436 (my $class = $cdbi) =~ s/::.*//o;
437 (my $api_class = $cdbi) =~ s/::/./go;
438 my $registration_class = __PACKAGE__ . "::$class";
439 my $api_prefix = 'open-ils.storage.direct.'.$api_class;
441 # Create the search methods
442 unless ( __PACKAGE__->is_registered( $api_prefix.'.search' ) ) {
443 __PACKAGE__->register_method(
444 api_name => $api_prefix.'.search',
454 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_where' ) ) {
455 __PACKAGE__->register_method(
456 api_name => $api_prefix.'.search_where',
457 method => 'search_where',
468 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_like' ) ) {
469 __PACKAGE__->register_method(
470 api_name => $api_prefix.'.search_like',
480 if (\&Class::DBI::search_fts and $cdbi->columns('FTS')) {
481 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_fts' ) ) {
482 __PACKAGE__->register_method(
483 api_name => $api_prefix.'.search_fts',
494 if (\&Class::DBI::search_regex) {
495 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_regex' ) ) {
496 __PACKAGE__->register_method(
497 api_name => $api_prefix.'.search_regex',
508 if (\&Class::DBI::search_ilike) {
509 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_ilike' ) ) {
510 __PACKAGE__->register_method(
511 api_name => $api_prefix.'.search_ilike',
524 # Create the random method
525 unless ( __PACKAGE__->is_registered( $api_prefix.'.random' ) ) {
526 __PACKAGE__->register_method(
527 api_name => $api_prefix.'.random',
528 method => 'random_object',
535 # Create the retrieve method
536 unless ( __PACKAGE__->is_registered( $api_prefix.'.retrieve' ) ) {
537 __PACKAGE__->register_method(
538 api_name => $api_prefix.'.retrieve',
539 method => 'retrieve_node',
547 # Create the batch retrieve method
548 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.retrieve' ) ) {
549 __PACKAGE__->register_method(
550 api_name => $api_prefix.'.batch.retrieve',
551 method => 'retrieve_node',
560 for my $field ($fmclass->real_fields) {
561 unless ( __PACKAGE__->is_registered( $api_prefix.'.search.'.$field ) ) {
562 __PACKAGE__->register_method(
563 api_name => $api_prefix.'.search.'.$field,
564 method => 'search_one_field',
575 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_like.'.$field ) ) {
576 __PACKAGE__->register_method(
577 api_name => $api_prefix.'.search_like.'.$field,
578 method => 'search_one_field',
586 if (\&Class::DBI::search_fts and grep { $field eq $_ } $cdbi->columns('FTS')) {
587 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_fts.'.$field ) ) {
588 __PACKAGE__->register_method(
589 api_name => $api_prefix.'.search_fts.'.$field,
590 method => 'search_one_field',
599 if (\&Class::DBI::search_regex) {
600 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_regex.'.$field ) ) {
601 __PACKAGE__->register_method(
602 api_name => $api_prefix.'.search_regex.'.$field,
603 method => 'search_one_field',
612 if (\&Class::DBI::search_ilike) {
613 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_ilike.'.$field ) ) {
614 __PACKAGE__->register_method(
615 api_name => $api_prefix.'.search_ilike.'.$field,
616 method => 'search_one_field',
631 unless ($fmclass->is_readonly) {
632 # Create the create method
633 unless ( __PACKAGE__->is_registered( $api_prefix.'.create' ) ) {
634 __PACKAGE__->register_method(
635 api_name => $api_prefix.'.create',
636 method => 'create_node',
643 # Create the batch create method
644 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.create' ) ) {
645 __PACKAGE__->register_method(
646 api_name => $api_prefix.'.batch.create',
647 method => 'batch_call',
654 # Create the update method
655 unless ( __PACKAGE__->is_registered( $api_prefix.'.update' ) ) {
656 __PACKAGE__->register_method(
657 api_name => $api_prefix.'.update',
658 method => 'update_node',
665 # Create the batch update method
666 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.update' ) ) {
667 __PACKAGE__->register_method(
668 api_name => $api_prefix.'.batch.update',
669 method => 'batch_call',
676 # Create the delete method
677 unless ( __PACKAGE__->is_registered( $api_prefix.'.delete' ) ) {
678 __PACKAGE__->register_method(
679 api_name => $api_prefix.'.delete',
680 method => 'delete_node',
687 # Create the batch delete method
688 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.delete' ) ) {
689 __PACKAGE__->register_method(
690 api_name => $api_prefix.'.batch.delete',
691 method => 'batch_call',
698 # Create the merge method
699 unless ( __PACKAGE__->is_registered( $api_prefix.'.merge' ) ) {
700 __PACKAGE__->register_method(
701 api_name => $api_prefix.'.merge',
702 method => 'merge_node',
709 # Create the batch merge method
710 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.merge' ) ) {
711 __PACKAGE__->register_method(
712 api_name => $api_prefix.'.batch.merge',
713 method => 'batch_call',
721 # Create the remote_update method
722 unless ( __PACKAGE__->is_registered( $api_prefix.'.remote_update' ) ) {
723 __PACKAGE__->register_method(
724 api_name => $api_prefix.'.remote_update',
725 method => 'remote_update_node',
732 # Create the batch remote_update method
733 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.remote_update' ) ) {
734 __PACKAGE__->register_method(
735 api_name => $api_prefix.'.batch.remote_update',
736 method => 'batch_call',
744 # Create the search-based mass delete method
745 unless ( __PACKAGE__->is_registered( $api_prefix.'.mass_delete' ) ) {
746 __PACKAGE__->register_method(
747 api_name => $api_prefix.'.mass_delete',
748 method => 'mass_delete',