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);
270 my $cdbi = $self->{cdbi};
274 $success = $cdbi->delete($node);
286 my $cdbi = $self->{cdbi};
287 my $api_name = $self->api_name;
288 (my $single_call_api_name = $api_name) =~ s/batch\.//o;
290 $log->debug("Default $api_name looking up $single_call_api_name...",INTERNAL);
291 my $method = $self->method_lookup($single_call_api_name);
294 while ( my $node = shift(@nodes) ) {
295 my ($res) = $method->run( $node );
296 push(@success, 1) if ($res >= 0);
299 my $insert_total = 0;
300 $insert_total += $_ for (@success);
302 return $insert_total;
306 use OpenILS::Application::Storage::Publisher::actor;
307 use OpenILS::Application::Storage::Publisher::action;
308 use OpenILS::Application::Storage::Publisher::asset;
309 use OpenILS::Application::Storage::Publisher::biblio;
310 use OpenILS::Application::Storage::Publisher::config;
311 use OpenILS::Application::Storage::Publisher::metabib;
312 use OpenILS::Application::Storage::Publisher::money;
313 use OpenILS::Application::Storage::Publisher::permission;
317 $log->debug("ARG! Couldn't load (at least one) class Publisher: $@", ERROR);
318 throw OpenSRF::EX::ERROR ("ARG! Couldn't load (at least one) class Publisher: $@");
322 for my $fmclass ( (Fieldmapper->classes) ) {
324 next if ($fmclass->is_virtual);
326 $log->debug("Generating methods for Fieldmapper class $fmclass", DEBUG);
328 (my $cdbi = $fmclass) =~ s/^Fieldmapper:://o;
329 (my $class = $cdbi) =~ s/::.*//o;
330 (my $api_class = $cdbi) =~ s/::/./go;
331 my $registration_class = __PACKAGE__ . "::$class";
332 my $api_prefix = 'open-ils.storage.direct.'.$api_class;
334 # Create the search methods
335 unless ( __PACKAGE__->is_registered( $api_prefix.'.search' ) ) {
336 __PACKAGE__->register_method(
337 api_name => $api_prefix.'.search',
346 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_where' ) ) {
347 __PACKAGE__->register_method(
348 api_name => $api_prefix.'.search_where',
349 method => 'search_where',
357 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_like' ) ) {
358 __PACKAGE__->register_method(
359 api_name => $api_prefix.'.search_like',
368 if (\&Class::DBI::search_fts) {
369 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_fts' ) ) {
370 __PACKAGE__->register_method(
371 api_name => $api_prefix.'.search_fts',
381 if (\&Class::DBI::search_regex) {
382 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_regex' ) ) {
383 __PACKAGE__->register_method(
384 api_name => $api_prefix.'.search_regex',
394 if (\&Class::DBI::search_ilike) {
395 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_ilike' ) ) {
396 __PACKAGE__->register_method(
397 api_name => $api_prefix.'.search_ilike',
407 # Create the retrieve method
408 unless ( __PACKAGE__->is_registered( $api_prefix.'.retrieve' ) ) {
409 __PACKAGE__->register_method(
410 api_name => $api_prefix.'.retrieve',
411 method => 'retrieve_node',
418 # Create the batch retrieve method
419 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.retrieve' ) ) {
420 __PACKAGE__->register_method(
421 api_name => $api_prefix.'.batch.retrieve',
422 method => 'retrieve_node',
430 for my $field ($fmclass->real_fields) {
431 unless ( __PACKAGE__->is_registered( $api_prefix.'.search.'.$field ) ) {
432 __PACKAGE__->register_method(
433 api_name => $api_prefix.'.search.'.$field,
434 method => 'search_one_field',
441 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_like.'.$field ) ) {
442 __PACKAGE__->register_method(
443 api_name => $api_prefix.'.search_like.'.$field,
444 method => 'search_one_field',
451 if (\&Class::DBI::search_fts) {
452 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_fts.'.$field ) ) {
453 __PACKAGE__->register_method(
454 api_name => $api_prefix.'.search_fts.'.$field,
455 method => 'search_one_field',
463 if (\&Class::DBI::search_regex) {
464 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_regex.'.$field ) ) {
465 __PACKAGE__->register_method(
466 api_name => $api_prefix.'.search_regex.'.$field,
467 method => 'search_one_field',
475 if (\&Class::DBI::search_ilike) {
476 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_ilike.'.$field ) ) {
477 __PACKAGE__->register_method(
478 api_name => $api_prefix.'.search_ilike.'.$field,
479 method => 'search_one_field',
490 unless ($fmclass->is_readonly) {
491 # Create the create method
492 unless ( __PACKAGE__->is_registered( $api_prefix.'.create' ) ) {
493 __PACKAGE__->register_method(
494 api_name => $api_prefix.'.create',
495 method => 'create_node',
501 # Create the batch create method
502 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.create' ) ) {
503 __PACKAGE__->register_method(
504 api_name => $api_prefix.'.batch.create',
505 method => 'batch_call',
511 # Create the update method
512 unless ( __PACKAGE__->is_registered( $api_prefix.'.update' ) ) {
513 __PACKAGE__->register_method(
514 api_name => $api_prefix.'.update',
515 method => 'update_node',
521 # Create the batch update method
522 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.update' ) ) {
523 __PACKAGE__->register_method(
524 api_name => $api_prefix.'.batch.update',
525 method => 'batch_call',
531 # Create the delete method
532 unless ( __PACKAGE__->is_registered( $api_prefix.'.delete' ) ) {
533 __PACKAGE__->register_method(
534 api_name => $api_prefix.'.delete',
535 method => 'delete_node',
541 # Create the batch delete method
542 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.delete' ) ) {
543 __PACKAGE__->register_method(
544 api_name => $api_prefix.'.batch.delete',
545 method => 'batch_call',
551 # Create the search-based mass delete method
552 unless ( __PACKAGE__->is_registered( $api_prefix.'.mass_delete' ) ) {
553 __PACKAGE__->register_method(
554 api_name => $api_prefix.'.mass_delete',
555 method => 'mass_delete',