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/;;
7 use OpenSRF::Utils::Logger qw/:level/;
8 use OpenILS::Utils::Fieldmapper;
10 my $log = 'OpenSRF::Utils::Logger';
18 $class = ref($class) || $class;
20 $args{package} ||= $class;
21 __PACKAGE__->SUPER::register_method( %args );
23 if (exists($dup_args{cachable}) and $dup_args{cachable}) {
24 (my $name = $dup_args{api_name}) =~ s/^open-ils\.storage/open-ils.storage.cachable/o;
25 if ($name ne $dup_args{api_name}) {
26 $dup_args{real_api_name} = $dup_args{api_name};
27 $dup_args{method} = 'cachable_wrapper';
28 $dup_args{api_name} = $name;
29 $dup_args{package} = __PACKAGE__;
30 __PACKAGE__->SUPER::register_method( %dup_args );
35 sub cachable_wrapper {
44 cache_page_size => 1000,
48 my $key_string = $self->api_name;
49 for (my $ind = 0; $ind < scalar(@args); $ind++) {
50 if ( $args[$ind] eq 'limit' ||
51 $args[$ind] eq 'offset' ||
52 $args[$ind] eq 'cache_page_size' ||
53 $args[$ind] eq 'timeout' ) {
58 $cache_args{$args[$key_ind]} = $args[$value_ind];
59 $log->debug("Cache limiter value for $args[$key_ind] is $args[$value_ind]", INTERNAL);
62 $key_string .= $args[$ind];
63 $log->debug("Partial cache key value is $args[$ind]", INTERNAL);
64 push @real_args, $args[$ind];
67 my $cache_page = int($cache_args{offset} / $cache_args{cache_page_size});
70 $cache_key = md5_hex($key_string.$cache_page);
73 $log->debug("Key string for cache lookup is $key_string -> $cache_key", DEBUG);
74 $log->debug("Cache page is $cache_page", DEBUG);
76 my $cached_res = OpenSRF::Utils::Cache->new->get_cache( $cache_key );
77 if (defined $cached_res) {
78 $log->debug("Found ".scalar(@$cached_res)." records in the cache", INFO);
79 $log->debug("Values from cache: ".join(', ', @$cached_res), INTERNAL);
80 my $start = int($cache_args{offset} - ($cache_page * $cache_args{cache_page_size}));
81 my $end = int($start + $cache_args{limit} - 1);
82 $log->debug("Responding with values from ".$start.' to '.$end,DEBUG);
83 $client->respond( $_ ) for ( grep { defined } @$cached_res[ $start .. $end ]);
87 my $method = $self->method_lookup($self->{real_api_name});
88 my @res = $method->run(@real_args);
91 $client->respond( $_ ) for ( grep { defined } @res[$cache_args{offset} .. int($cache_args{offset} + $cache_args{limit} - 1)] );
93 $log->debug("Saving values from ".int($cache_page * $cache_args{cache_page_size})." to ".
94 int(($cache_page + 1) * $cache_args{cache_page_size}). "to the cache", INTERNAL);
96 OpenSRF::Utils::Cache->new->put_cache(
98 [@res[int($cache_page * $cache_args{cache_page_size}) .. int(($cache_page + 1) * $cache_args{cache_page_size}) ]] =>
103 $log->error("Cache seems to be down, $e");
114 my $cdbi = $self->{cdbi};
116 for my $id ( @ids ) {
119 my ($rec) = $cdbi->fast_fieldmapper($id);
120 if ($self->api_name !~ /batch/o) {
121 return $rec if ($rec);
123 $client->respond($rec);
131 my $searches = shift;
133 my $cdbi = $self->{cdbi};
135 (my $search_type = $self->api_name) =~ s/.*\.(search[^.]*).*/$1/o;
137 $log->debug("Searching $cdbi for { ".
138 join(',', map { "$_ => $$searches{$_}" } keys %$searches).
139 " } using $search_type",DEBUG);
141 for my $obj ($cdbi->$search_type($searches)) {
142 $client->respond( $obj->to_fieldmapper );
147 sub search_one_field {
152 (my $search_type = $self->api_name) =~ s/.*\.(search[^.]*).*/$1/o;
153 (my $col = $self->api_name) =~ s/.*\.$search_type\.([^.]+).*/$1/;
154 my $cdbi = $self->{cdbi};
157 $like = 1 if ($search_type =~ /like$/o);
158 $like = 2 if ($search_type =~ /fts$/o);
159 $like = 3 if ($search_type =~ /regex$/o);
161 for my $term (@terms) {
162 $log->debug("Searching $cdbi for $col using type $search_type, value '$term'",DEBUG);
164 return [ $cdbi->fast_fieldmapper($term,$col,$like) ];
166 $client->respond( [ $cdbi->fast_fieldmapper($term,$col,$like) ] );
177 my $cdbi = $self->{cdbi};
181 my $rec = $cdbi->create($node);
182 $success = $rec->id if ($rec);
195 my $cdbi = $self->{cdbi};
197 return $cdbi->update($node);
205 my $where = 'WHERE ';
207 my $cdbi = $self->{cdbi};
208 my $table = $cdbi->table;
210 my @keys = sort keys %$search;
214 for my $col ( @keys ) {
215 if (ref($$search{$col}) and ref($$search{$col}) =~ /ARRAY/o) {
216 push @wheres, "$col IN (" . join(',', map { '?' } @{ $$search{$col} }) . ')';
217 push @binds, map { "$_" } @{ $$search{$col} };
219 push @wheres, "$col = ?";
220 push @binds, $$search{$col};
223 $where .= join ' AND ', @wheres;
225 my $delete = "DELETE FROM $table $where";
227 $log->debug("Performing MASS deletion : $delete",DEBUG);
229 my $dbh = $cdbi->db_Main;
232 my $sth = $dbh->prepare($delete);
233 $sth->execute( @binds );
235 $log->debug("MASS Delete succeeded",DEBUG);
237 $log->debug("MASS Delete FAILED : ".shift(),DEBUG);
248 my $cdbi = $self->{cdbi};
252 $success = $cdbi->delete($node);
264 my $cdbi = $self->{cdbi};
265 my $api_name = $self->api_name;
266 (my $single_call_api_name = $api_name) =~ s/batch\.//o;
268 $log->debug("Default $api_name looking up $single_call_api_name...",INTERNAL);
269 my $method = $self->method_lookup($single_call_api_name);
272 while ( my $node = shift(@nodes) ) {
273 my ($res) = $method->run( $node );
274 push(@success, 1) if ($res >= 0);
277 my $insert_total = 0;
278 $insert_total += $_ for (@success);
280 return $insert_total;
284 use OpenILS::Application::Storage::Publisher::actor;
285 use OpenILS::Application::Storage::Publisher::action;
286 use OpenILS::Application::Storage::Publisher::asset;
287 use OpenILS::Application::Storage::Publisher::biblio;
288 use OpenILS::Application::Storage::Publisher::config;
289 use OpenILS::Application::Storage::Publisher::metabib;
290 use OpenILS::Application::Storage::Publisher::permission;
293 for my $fmclass ( (Fieldmapper->classes) ) {
295 next if ($fmclass->is_virtual);
297 $log->debug("Generating methods for Fieldmapper class $fmclass", DEBUG);
299 (my $cdbi = $fmclass) =~ s/^Fieldmapper:://o;
300 (my $class = $cdbi) =~ s/::.*//o;
301 (my $api_class = $cdbi) =~ s/::/./go;
302 my $registration_class = __PACKAGE__ . "::$class";
303 my $api_prefix = 'open-ils.storage.direct.'.$api_class;
305 # Create the search methods
306 unless ( __PACKAGE__->is_registered( $api_prefix.'.search' ) ) {
307 __PACKAGE__->register_method(
308 api_name => $api_prefix.'.search',
317 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_like' ) ) {
318 __PACKAGE__->register_method(
319 api_name => $api_prefix.'.search_like',
328 if (\&Class::DBI::search_fts) {
329 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_fts' ) ) {
330 __PACKAGE__->register_method(
331 api_name => $api_prefix.'.search_fts',
341 if (\&Class::DBI::search_regex) {
342 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_regex' ) ) {
343 __PACKAGE__->register_method(
344 api_name => $api_prefix.'.search_regex',
354 # Create the retrieve method
355 unless ( __PACKAGE__->is_registered( $api_prefix.'.retrieve' ) ) {
356 __PACKAGE__->register_method(
357 api_name => $api_prefix.'.retrieve',
358 method => 'retrieve_node',
365 # Create the batch retrieve method
366 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.retrieve' ) ) {
367 __PACKAGE__->register_method(
368 api_name => $api_prefix.'.batch.retrieve',
369 method => 'retrieve_node',
377 for my $field ($fmclass->real_fields) {
378 unless ( __PACKAGE__->is_registered( $api_prefix.'.search.'.$field ) ) {
379 __PACKAGE__->register_method(
380 api_name => $api_prefix.'.search.'.$field,
381 method => 'search_one_field',
387 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_like.'.$field ) ) {
388 __PACKAGE__->register_method(
389 api_name => $api_prefix.'.search_like.'.$field,
390 method => 'search_one_field',
396 if (\&Class::DBI::search_fts) {
397 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_fts.'.$field ) ) {
398 __PACKAGE__->register_method(
399 api_name => $api_prefix.'.search_fts.'.$field,
400 method => 'search_one_field',
407 if (\&Class::DBI::search_regex) {
408 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_regex.'.$field ) ) {
409 __PACKAGE__->register_method(
410 api_name => $api_prefix.'.search_regex.'.$field,
411 method => 'search_one_field',
421 # Create the create method
422 unless ( __PACKAGE__->is_registered( $api_prefix.'.create' ) ) {
423 __PACKAGE__->register_method(
424 api_name => $api_prefix.'.create',
425 method => 'create_node',
431 # Create the batch create method
432 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.create' ) ) {
433 __PACKAGE__->register_method(
434 api_name => $api_prefix.'.batch.create',
435 method => 'batch_call',
441 # Create the update method
442 unless ( __PACKAGE__->is_registered( $api_prefix.'.update' ) ) {
443 __PACKAGE__->register_method(
444 api_name => $api_prefix.'.update',
445 method => 'update_node',
451 # Create the batch update method
452 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.update' ) ) {
453 __PACKAGE__->register_method(
454 api_name => $api_prefix.'.batch.update',
455 method => 'batch_call',
461 # Create the delete method
462 unless ( __PACKAGE__->is_registered( $api_prefix.'.delete' ) ) {
463 __PACKAGE__->register_method(
464 api_name => $api_prefix.'.delete',
465 method => 'delete_node',
471 # Create the batch delete method
472 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.delete' ) ) {
473 __PACKAGE__->register_method(
474 api_name => $api_prefix.'.batch.delete',
475 method => 'batch_call',
481 # Create the search-based mass delete method
482 unless ( __PACKAGE__->is_registered( $api_prefix.'.mass_delete' ) ) {
483 __PACKAGE__->register_method(
484 api_name => $api_prefix.'.mass_delete',
485 method => 'mass_delete',