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");
130 my $cdbi = $self->{cdbi};
132 for my $id ( @ids ) {
135 my ($rec) = $cdbi->fast_fieldmapper($id);
136 if ($self->api_name !~ /batch/o) {
137 return $rec if ($rec);
139 $client->respond($rec);
149 my @res = $self->method_lookup($self->{real_api_name})->run(@args);
151 if (ref($res[0]) eq 'ARRAY') {
152 return [ map { $_->id } @{ $res[0] } ];
155 $client->respond($_) for ( map { $_->id } @res );
164 if (ref($args[0]) eq 'HASH') {
166 $args[1]{limit_dialect} = $self->{cdbi}->db_Main;
168 $args[1] = {limit_dialect => $self->{cdbi}->db_Main };
171 $args[0] = { @args };
172 $args[1] = {limit_dialect => $self->{cdbi} };
175 my $cdbi = $self->{cdbi};
177 for my $obj ($cdbi->search_where(@args)) {
178 next unless ref($obj);
179 $client->respond( $obj->to_fieldmapper );
189 my $cdbi = $self->{cdbi};
191 (my $search_type = $self->api_name) =~ s/.*\.(search[^.]*).*/$1/o;
193 for my $obj ($cdbi->$search_type(@args)) {
194 next unless ref($obj);
195 $client->respond( $obj->to_fieldmapper );
200 sub search_one_field {
205 (my $field = $self->api_name) =~ s/.*\.([^\.]+)$/$1/o;
207 return search( $self, $client, $field, @args );
210 sub old_search_one_field {
215 (my $search_type = $self->api_name) =~ s/.*\.(search[^.]*).*/$1/o;
216 (my $col = $self->api_name) =~ s/.*\.$search_type\.([^.]+).*/$1/;
217 my $cdbi = $self->{cdbi};
220 $like = 1 if ($search_type =~ /like$/o);
221 $like = 2 if ($search_type =~ /fts$/o);
222 $like = 3 if ($search_type =~ /regex$/o);
224 for my $term (@terms) {
225 $log->debug("Searching $cdbi for $col using type $search_type, value '$term'",DEBUG);
227 return [ $cdbi->fast_fieldmapper($term,$col,$like) ];
229 $client->respond( [ $cdbi->fast_fieldmapper($term,$col,$like) ] );
240 local $OpenILS::Application::Storage::WRITE = 1;
242 my $cdbi = $self->{cdbi};
246 my $rec = $cdbi->create($node);
247 $success = $rec->id if ($rec);
260 local $OpenILS::Application::Storage::WRITE = 1;
262 my $cdbi = $self->{cdbi};
264 return $cdbi->update($node);
272 local $OpenILS::Application::Storage::WRITE = 1;
274 my $where = 'WHERE ';
276 my $cdbi = $self->{cdbi};
277 my $table = $cdbi->table;
279 my @keys = sort keys %$search;
283 for my $col ( @keys ) {
284 if (ref($$search{$col}) and ref($$search{$col}) =~ /ARRAY/o) {
285 push @wheres, "$col IN (" . join(',', map { '?' } @{ $$search{$col} }) . ')';
286 push @binds, map { "$_" } @{ $$search{$col} };
288 push @wheres, "$col = ?";
289 push @binds, $$search{$col};
292 $where .= join ' AND ', @wheres;
294 my $delete = "DELETE FROM $table $where";
296 $log->debug("Performing MASS deletion : $delete",DEBUG);
298 my $dbh = $cdbi->db_Main;
301 my $sth = $dbh->prepare($delete);
302 $sth->execute( @binds );
304 $log->debug("MASS Delete succeeded",DEBUG);
306 $log->debug("MASS Delete FAILED : ".shift(),DEBUG);
312 sub remote_update_node {
318 local $OpenILS::Application::Storage::WRITE = 1;
320 my $cdbi = $self->{cdbi};
324 $success = $cdbi->remote_update($keys,$vals);
337 local $OpenILS::Application::Storage::WRITE = 1;
339 my $cdbi = $self->{cdbi};
343 $success = $cdbi->merge($keys,$vals)->id;
355 local $OpenILS::Application::Storage::WRITE = 1;
357 my $cdbi = $self->{cdbi};
361 $success = $cdbi->delete($node);
373 my $unwrap = $self->{unwrap};
375 my $cdbi = $self->{cdbi};
376 my $api_name = $self->api_name;
377 (my $single_call_api_name = $api_name) =~ s/batch\.//o;
379 $log->debug("Default $api_name looking up $single_call_api_name...",INTERNAL);
380 my $method = $self->method_lookup($single_call_api_name);
383 while ( my $node = shift(@nodes) ) {
384 my ($res) = $method->run( ($unwrap ? (@$node) : ($node)) );
385 push(@success, 1) if ($res >= 0);
388 my $insert_total = 0;
389 $insert_total += $_ for (@success);
391 return $insert_total;
395 # --------------------- End of generic methods -----------------------
398 for my $pkg ( qw/actor action asset biblio config metabib authority money permission container/ ) {
399 "OpenILS::Application::Storage::Publisher::$pkg"->use;
401 $log->debug("ARG! Couldn't load $pkg class Publisher: $@", ERROR);
402 throw OpenSRF::EX::ERROR ("ARG! Couldn't load $pkg class Publisher: $@");
406 for my $fmclass ( (Fieldmapper->classes) ) {
408 $log->debug("Generating methods for Fieldmapper class $fmclass", DEBUG);
410 next if ($fmclass->is_virtual);
412 (my $cdbi = $fmclass) =~ s/^Fieldmapper:://o;
413 (my $class = $cdbi) =~ s/::.*//o;
414 (my $api_class = $cdbi) =~ s/::/./go;
415 my $registration_class = __PACKAGE__ . "::$class";
416 my $api_prefix = 'open-ils.storage.direct.'.$api_class;
418 # Create the search methods
419 unless ( __PACKAGE__->is_registered( $api_prefix.'.search' ) ) {
420 __PACKAGE__->register_method(
421 api_name => $api_prefix.'.search',
431 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_where' ) ) {
432 __PACKAGE__->register_method(
433 api_name => $api_prefix.'.search_where',
434 method => 'search_where',
445 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_like' ) ) {
446 __PACKAGE__->register_method(
447 api_name => $api_prefix.'.search_like',
457 if (\&Class::DBI::search_fts and $cdbi->columns('FTS')) {
458 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_fts' ) ) {
459 __PACKAGE__->register_method(
460 api_name => $api_prefix.'.search_fts',
471 if (\&Class::DBI::search_regex) {
472 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_regex' ) ) {
473 __PACKAGE__->register_method(
474 api_name => $api_prefix.'.search_regex',
485 if (\&Class::DBI::search_ilike) {
486 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_ilike' ) ) {
487 __PACKAGE__->register_method(
488 api_name => $api_prefix.'.search_ilike',
501 # Create the retrieve method
502 unless ( __PACKAGE__->is_registered( $api_prefix.'.retrieve' ) ) {
503 __PACKAGE__->register_method(
504 api_name => $api_prefix.'.retrieve',
505 method => 'retrieve_node',
513 # Create the batch retrieve method
514 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.retrieve' ) ) {
515 __PACKAGE__->register_method(
516 api_name => $api_prefix.'.batch.retrieve',
517 method => 'retrieve_node',
526 for my $field ($fmclass->real_fields) {
527 unless ( __PACKAGE__->is_registered( $api_prefix.'.search.'.$field ) ) {
528 __PACKAGE__->register_method(
529 api_name => $api_prefix.'.search.'.$field,
530 method => 'search_one_field',
541 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_like.'.$field ) ) {
542 __PACKAGE__->register_method(
543 api_name => $api_prefix.'.search_like.'.$field,
544 method => 'search_one_field',
552 if (\&Class::DBI::search_fts and grep { $field eq $_ } $cdbi->columns('FTS')) {
553 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_fts.'.$field ) ) {
554 __PACKAGE__->register_method(
555 api_name => $api_prefix.'.search_fts.'.$field,
556 method => 'search_one_field',
565 if (\&Class::DBI::search_regex) {
566 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_regex.'.$field ) ) {
567 __PACKAGE__->register_method(
568 api_name => $api_prefix.'.search_regex.'.$field,
569 method => 'search_one_field',
578 if (\&Class::DBI::search_ilike) {
579 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_ilike.'.$field ) ) {
580 __PACKAGE__->register_method(
581 api_name => $api_prefix.'.search_ilike.'.$field,
582 method => 'search_one_field',
597 unless ($fmclass->is_readonly) {
598 # Create the create method
599 unless ( __PACKAGE__->is_registered( $api_prefix.'.create' ) ) {
600 __PACKAGE__->register_method(
601 api_name => $api_prefix.'.create',
602 method => 'create_node',
609 # Create the batch create method
610 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.create' ) ) {
611 __PACKAGE__->register_method(
612 api_name => $api_prefix.'.batch.create',
613 method => 'batch_call',
620 # Create the update method
621 unless ( __PACKAGE__->is_registered( $api_prefix.'.update' ) ) {
622 __PACKAGE__->register_method(
623 api_name => $api_prefix.'.update',
624 method => 'update_node',
631 # Create the batch update method
632 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.update' ) ) {
633 __PACKAGE__->register_method(
634 api_name => $api_prefix.'.batch.update',
635 method => 'batch_call',
642 # Create the delete method
643 unless ( __PACKAGE__->is_registered( $api_prefix.'.delete' ) ) {
644 __PACKAGE__->register_method(
645 api_name => $api_prefix.'.delete',
646 method => 'delete_node',
653 # Create the batch delete method
654 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.delete' ) ) {
655 __PACKAGE__->register_method(
656 api_name => $api_prefix.'.batch.delete',
657 method => 'batch_call',
664 # Create the merge method
665 unless ( __PACKAGE__->is_registered( $api_prefix.'.merge' ) ) {
666 __PACKAGE__->register_method(
667 api_name => $api_prefix.'.merge',
668 method => 'merge_node',
675 # Create the batch merge method
676 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.merge' ) ) {
677 __PACKAGE__->register_method(
678 api_name => $api_prefix.'.batch.merge',
679 method => 'batch_call',
687 # Create the remote_update method
688 unless ( __PACKAGE__->is_registered( $api_prefix.'.remote_update' ) ) {
689 __PACKAGE__->register_method(
690 api_name => $api_prefix.'.remote_update',
691 method => 'remote_update_node',
698 # Create the batch remote_update method
699 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.remote_update' ) ) {
700 __PACKAGE__->register_method(
701 api_name => $api_prefix.'.batch.remote_update',
702 method => 'batch_call',
710 # Create the search-based mass delete method
711 unless ( __PACKAGE__->is_registered( $api_prefix.'.mass_delete' ) ) {
712 __PACKAGE__->register_method(
713 api_name => $api_prefix.'.mass_delete',
714 method => 'mass_delete',