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;
316 for my $fmclass ( (Fieldmapper->classes) ) {
318 next if ($fmclass->is_virtual);
320 $log->debug("Generating methods for Fieldmapper class $fmclass", DEBUG);
322 (my $cdbi = $fmclass) =~ s/^Fieldmapper:://o;
323 (my $class = $cdbi) =~ s/::.*//o;
324 (my $api_class = $cdbi) =~ s/::/./go;
325 my $registration_class = __PACKAGE__ . "::$class";
326 my $api_prefix = 'open-ils.storage.direct.'.$api_class;
328 # Create the search methods
329 unless ( __PACKAGE__->is_registered( $api_prefix.'.search' ) ) {
330 __PACKAGE__->register_method(
331 api_name => $api_prefix.'.search',
340 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_where' ) ) {
341 __PACKAGE__->register_method(
342 api_name => $api_prefix.'.search_where',
343 method => 'search_where',
351 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_like' ) ) {
352 __PACKAGE__->register_method(
353 api_name => $api_prefix.'.search_like',
362 if (\&Class::DBI::search_fts) {
363 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_fts' ) ) {
364 __PACKAGE__->register_method(
365 api_name => $api_prefix.'.search_fts',
375 if (\&Class::DBI::search_regex) {
376 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_regex' ) ) {
377 __PACKAGE__->register_method(
378 api_name => $api_prefix.'.search_regex',
388 if (\&Class::DBI::search_ilike) {
389 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_ilike' ) ) {
390 __PACKAGE__->register_method(
391 api_name => $api_prefix.'.search_ilike',
401 # Create the retrieve method
402 unless ( __PACKAGE__->is_registered( $api_prefix.'.retrieve' ) ) {
403 __PACKAGE__->register_method(
404 api_name => $api_prefix.'.retrieve',
405 method => 'retrieve_node',
412 # Create the batch retrieve method
413 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.retrieve' ) ) {
414 __PACKAGE__->register_method(
415 api_name => $api_prefix.'.batch.retrieve',
416 method => 'retrieve_node',
424 for my $field ($fmclass->real_fields) {
425 unless ( __PACKAGE__->is_registered( $api_prefix.'.search.'.$field ) ) {
426 __PACKAGE__->register_method(
427 api_name => $api_prefix.'.search.'.$field,
428 method => 'search_one_field',
435 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_like.'.$field ) ) {
436 __PACKAGE__->register_method(
437 api_name => $api_prefix.'.search_like.'.$field,
438 method => 'search_one_field',
445 if (\&Class::DBI::search_fts) {
446 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_fts.'.$field ) ) {
447 __PACKAGE__->register_method(
448 api_name => $api_prefix.'.search_fts.'.$field,
449 method => 'search_one_field',
457 if (\&Class::DBI::search_regex) {
458 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_regex.'.$field ) ) {
459 __PACKAGE__->register_method(
460 api_name => $api_prefix.'.search_regex.'.$field,
461 method => 'search_one_field',
469 if (\&Class::DBI::search_ilike) {
470 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_ilike.'.$field ) ) {
471 __PACKAGE__->register_method(
472 api_name => $api_prefix.'.search_ilike.'.$field,
473 method => 'search_one_field',
484 # Create the create method
485 unless ( __PACKAGE__->is_registered( $api_prefix.'.create' ) ) {
486 __PACKAGE__->register_method(
487 api_name => $api_prefix.'.create',
488 method => 'create_node',
494 # Create the batch create method
495 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.create' ) ) {
496 __PACKAGE__->register_method(
497 api_name => $api_prefix.'.batch.create',
498 method => 'batch_call',
504 # Create the update method
505 unless ( __PACKAGE__->is_registered( $api_prefix.'.update' ) ) {
506 __PACKAGE__->register_method(
507 api_name => $api_prefix.'.update',
508 method => 'update_node',
514 # Create the batch update method
515 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.update' ) ) {
516 __PACKAGE__->register_method(
517 api_name => $api_prefix.'.batch.update',
518 method => 'batch_call',
524 # Create the delete method
525 unless ( __PACKAGE__->is_registered( $api_prefix.'.delete' ) ) {
526 __PACKAGE__->register_method(
527 api_name => $api_prefix.'.delete',
528 method => 'delete_node',
534 # Create the batch delete method
535 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.delete' ) ) {
536 __PACKAGE__->register_method(
537 api_name => $api_prefix.'.batch.delete',
538 method => 'batch_call',
544 # Create the search-based mass delete method
545 unless ( __PACKAGE__->is_registered( $api_prefix.'.mass_delete' ) ) {
546 __PACKAGE__->register_method(
547 api_name => $api_prefix.'.mass_delete',
548 method => 'mass_delete',