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);
95 OpenSRF::Utils::Cache->new->put_cache(
97 [@res[int($cache_page * $cache_args{cache_page_size}) .. int(($cache_page + 1) * $cache_args{cache_page_size}) ]] =>
109 my $cdbi = $self->{cdbi};
111 for my $id ( @ids ) {
114 my ($rec) = $cdbi->fast_fieldmapper($id);
115 if ($self->api_name !~ /batch/o) {
116 return $rec if ($rec);
118 $client->respond($rec);
126 my $searches = shift;
128 my $cdbi = $self->{cdbi};
130 $log->debug("Searching $cdbi for { ".join(',', map { "$_ => $$searches{$_}" } keys %$searches).' }',DEBUG);
132 for my $obj ($cdbi->search($searches)) {
133 $client->respond( $obj->to_fieldmapper );
138 sub search_one_field {
143 (my $search_type = $self->api_name) =~ s/.*\.(search[^.]*).*/$1/o;
144 (my $col = $self->api_name) =~ s/.*\.$search_type\.([^.]+).*/$1/;
145 my $cdbi = $self->{cdbi};
148 $like = 1 if ($search_type =~ /like$/o);
150 for my $term (@terms) {
151 $log->debug("Searching $cdbi for $col using type $search_type, value '$term'",DEBUG);
153 return [ $cdbi->fast_fieldmapper($term,$col,$like) ];
155 $client->respond( [ $cdbi->fast_fieldmapper($term,$col,$like) ] );
166 my $cdbi = $self->{cdbi};
170 my $rec = $cdbi->create($node);
171 $success = $rec->id if ($rec);
184 my $cdbi = $self->{cdbi};
186 return $cdbi->update($node);
194 my $where = 'WHERE ';
196 my $cdbi = $self->{cdbi};
197 my $table = $cdbi->table;
199 my @keys = sort keys %$search;
203 for my $col ( @keys ) {
204 if (ref($$search{$col}) and ref($$search{$col}) =~ /ARRAY/o) {
205 push @wheres, "$col IN (" . join(',', map { '?' } @{ $$search{$col} }) . ')';
206 push @binds, map { "$_" } @{ $$search{$col} };
208 push @wheres, "$col = ?";
209 push @binds, $$search{$col};
212 $where .= join ' AND ', @wheres;
214 my $delete = "DELETE FROM $table $where";
216 $log->debug("Performing MASS deletion : $delete",DEBUG);
218 my $dbh = $cdbi->db_Main;
221 my $sth = $dbh->prepare($delete);
222 $sth->execute( @binds );
224 $log->debug("MASS Delete succeeded",DEBUG);
226 $log->debug("MASS Delete FAILED : ".shift(),DEBUG);
237 my $cdbi = $self->{cdbi};
241 $success = $cdbi->delete($node);
253 my $cdbi = $self->{cdbi};
254 my $api_name = $self->api_name;
255 (my $single_call_api_name = $api_name) =~ s/batch\.//o;
257 $log->debug("Default $api_name looking up $single_call_api_name...",INTERNAL);
258 my $method = $self->method_lookup($single_call_api_name);
261 while ( my $node = shift(@nodes) ) {
262 my ($res) = $method->run( $node );
263 push(@success, 1) if ($res >= 0);
266 my $insert_total = 0;
267 $insert_total += $_ for (@success);
269 return $insert_total;
273 use OpenILS::Application::Storage::Publisher::actor;
274 use OpenILS::Application::Storage::Publisher::action;
275 use OpenILS::Application::Storage::Publisher::asset;
276 use OpenILS::Application::Storage::Publisher::biblio;
277 use OpenILS::Application::Storage::Publisher::config;
278 use OpenILS::Application::Storage::Publisher::metabib;
281 for my $fmclass ( (Fieldmapper->classes) ) {
282 $log->debug("Generating methods for Fieldmapper class $fmclass", DEBUG);
284 (my $cdbi = $fmclass) =~ s/^Fieldmapper:://o;
285 (my $class = $cdbi) =~ s/::.*//o;
286 (my $api_class = $cdbi) =~ s/::/./go;
287 my $registration_class = __PACKAGE__ . "::$class";
288 my $api_prefix = 'open-ils.storage.direct.'.$api_class;
290 # Create the search method
291 unless ( __PACKAGE__->is_registered( $api_prefix.'.search' ) ) {
292 __PACKAGE__->register_method(
293 api_name => $api_prefix.'.search',
302 # Create the retrieve method
303 unless ( __PACKAGE__->is_registered( $api_prefix.'.retrieve' ) ) {
304 __PACKAGE__->register_method(
305 api_name => $api_prefix.'.retrieve',
306 method => 'retrieve_node',
313 # Create the batch retrieve method
314 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.retrieve' ) ) {
315 __PACKAGE__->register_method(
316 api_name => $api_prefix.'.batch.retrieve',
317 method => 'retrieve_node',
325 unless ($fmclass->is_virtual) {
326 for my $field ($fmclass->real_fields) {
327 unless ( __PACKAGE__->is_registered( $api_prefix.'.search.'.$field ) ) {
328 __PACKAGE__->register_method(
329 api_name => $api_prefix.'.search.'.$field,
330 method => 'search_one_field',
336 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_like.'.$field ) ) {
337 __PACKAGE__->register_method(
338 api_name => $api_prefix.'.search_like.'.$field,
339 method => 'search_one_field',
349 # Create the create method
350 unless ( __PACKAGE__->is_registered( $api_prefix.'.create' ) ) {
351 __PACKAGE__->register_method(
352 api_name => $api_prefix.'.create',
353 method => 'create_node',
359 # Create the batch create method
360 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.create' ) ) {
361 __PACKAGE__->register_method(
362 api_name => $api_prefix.'.batch.create',
363 method => 'batch_call',
369 # Create the update method
370 unless ( __PACKAGE__->is_registered( $api_prefix.'.update' ) ) {
371 __PACKAGE__->register_method(
372 api_name => $api_prefix.'.update',
373 method => 'update_node',
379 # Create the batch update method
380 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.update' ) ) {
381 __PACKAGE__->register_method(
382 api_name => $api_prefix.'.batch.update',
383 method => 'batch_call',
389 # Create the delete method
390 unless ( __PACKAGE__->is_registered( $api_prefix.'.delete' ) ) {
391 __PACKAGE__->register_method(
392 api_name => $api_prefix.'.delete',
393 method => 'delete_node',
399 # Create the batch delete method
400 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.delete' ) ) {
401 __PACKAGE__->register_method(
402 api_name => $api_prefix.'.batch.delete',
403 method => 'batch_call',
409 # Create the search-based mass delete method
410 unless ( __PACKAGE__->is_registered( $api_prefix.'.mass_delete' ) ) {
411 __PACKAGE__->register_method(
412 api_name => $api_prefix.'.mass_delete',
413 method => 'mass_delete',