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',
443 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_like' ) ) {
444 __PACKAGE__->register_method(
445 api_name => $api_prefix.'.search_like',
455 if (\&Class::DBI::search_fts and $cdbi->columns('FTS')) {
456 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_fts' ) ) {
457 __PACKAGE__->register_method(
458 api_name => $api_prefix.'.search_fts',
469 if (\&Class::DBI::search_regex) {
470 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_regex' ) ) {
471 __PACKAGE__->register_method(
472 api_name => $api_prefix.'.search_regex',
483 if (\&Class::DBI::search_ilike) {
484 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_ilike' ) ) {
485 __PACKAGE__->register_method(
486 api_name => $api_prefix.'.search_ilike',
497 # Create the retrieve method
498 unless ( __PACKAGE__->is_registered( $api_prefix.'.retrieve' ) ) {
499 __PACKAGE__->register_method(
500 api_name => $api_prefix.'.retrieve',
501 method => 'retrieve_node',
509 # Create the batch retrieve method
510 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.retrieve' ) ) {
511 __PACKAGE__->register_method(
512 api_name => $api_prefix.'.batch.retrieve',
513 method => 'retrieve_node',
522 for my $field ($fmclass->real_fields) {
523 unless ( __PACKAGE__->is_registered( $api_prefix.'.search.'.$field ) ) {
524 __PACKAGE__->register_method(
525 api_name => $api_prefix.'.search.'.$field,
526 method => 'search_one_field',
534 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_like.'.$field ) ) {
535 __PACKAGE__->register_method(
536 api_name => $api_prefix.'.search_like.'.$field,
537 method => 'search_one_field',
545 if (\&Class::DBI::search_fts and grep { $field eq $_ } $cdbi->columns('FTS')) {
546 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_fts.'.$field ) ) {
547 __PACKAGE__->register_method(
548 api_name => $api_prefix.'.search_fts.'.$field,
549 method => 'search_one_field',
558 if (\&Class::DBI::search_regex) {
559 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_regex.'.$field ) ) {
560 __PACKAGE__->register_method(
561 api_name => $api_prefix.'.search_regex.'.$field,
562 method => 'search_one_field',
571 if (\&Class::DBI::search_ilike) {
572 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_ilike.'.$field ) ) {
573 __PACKAGE__->register_method(
574 api_name => $api_prefix.'.search_ilike.'.$field,
575 method => 'search_one_field',
587 unless ($fmclass->is_readonly) {
588 # Create the create method
589 unless ( __PACKAGE__->is_registered( $api_prefix.'.create' ) ) {
590 __PACKAGE__->register_method(
591 api_name => $api_prefix.'.create',
592 method => 'create_node',
599 # Create the batch create method
600 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.create' ) ) {
601 __PACKAGE__->register_method(
602 api_name => $api_prefix.'.batch.create',
603 method => 'batch_call',
610 # Create the update method
611 unless ( __PACKAGE__->is_registered( $api_prefix.'.update' ) ) {
612 __PACKAGE__->register_method(
613 api_name => $api_prefix.'.update',
614 method => 'update_node',
621 # Create the batch update method
622 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.update' ) ) {
623 __PACKAGE__->register_method(
624 api_name => $api_prefix.'.batch.update',
625 method => 'batch_call',
632 # Create the delete method
633 unless ( __PACKAGE__->is_registered( $api_prefix.'.delete' ) ) {
634 __PACKAGE__->register_method(
635 api_name => $api_prefix.'.delete',
636 method => 'delete_node',
643 # Create the batch delete method
644 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.delete' ) ) {
645 __PACKAGE__->register_method(
646 api_name => $api_prefix.'.batch.delete',
647 method => 'batch_call',
654 # Create the merge method
655 unless ( __PACKAGE__->is_registered( $api_prefix.'.merge' ) ) {
656 __PACKAGE__->register_method(
657 api_name => $api_prefix.'.merge',
658 method => 'merge_node',
665 # Create the batch merge method
666 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.merge' ) ) {
667 __PACKAGE__->register_method(
668 api_name => $api_prefix.'.batch.merge',
669 method => 'batch_call',
677 # Create the remote_update method
678 unless ( __PACKAGE__->is_registered( $api_prefix.'.remote_update' ) ) {
679 __PACKAGE__->register_method(
680 api_name => $api_prefix.'.remote_update',
681 method => 'remote_update_node',
688 # Create the batch remote_update method
689 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.remote_update' ) ) {
690 __PACKAGE__->register_method(
691 api_name => $api_prefix.'.batch.remote_update',
692 method => 'batch_call',
700 # Create the search-based mass delete method
701 unless ( __PACKAGE__->is_registered( $api_prefix.'.mass_delete' ) ) {
702 __PACKAGE__->register_method(
703 api_name => $api_prefix.'.mass_delete',
704 method => 'mass_delete',