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 $log->debug("Searching $cdbi for { ".join(',', map { "$_ => $$searches{$_}" } keys %$searches).' }',DEBUG);
137 for my $obj ($cdbi->search($searches)) {
138 $client->respond( $obj->to_fieldmapper );
143 sub search_one_field {
148 (my $search_type = $self->api_name) =~ s/.*\.(search[^.]*).*/$1/o;
149 (my $col = $self->api_name) =~ s/.*\.$search_type\.([^.]+).*/$1/;
150 my $cdbi = $self->{cdbi};
153 $like = 1 if ($search_type =~ /like$/o);
155 for my $term (@terms) {
156 $log->debug("Searching $cdbi for $col using type $search_type, value '$term'",DEBUG);
158 return [ $cdbi->fast_fieldmapper($term,$col,$like) ];
160 $client->respond( [ $cdbi->fast_fieldmapper($term,$col,$like) ] );
171 my $cdbi = $self->{cdbi};
175 my $rec = $cdbi->create($node);
176 $success = $rec->id if ($rec);
189 my $cdbi = $self->{cdbi};
191 return $cdbi->update($node);
199 my $where = 'WHERE ';
201 my $cdbi = $self->{cdbi};
202 my $table = $cdbi->table;
204 my @keys = sort keys %$search;
208 for my $col ( @keys ) {
209 if (ref($$search{$col}) and ref($$search{$col}) =~ /ARRAY/o) {
210 push @wheres, "$col IN (" . join(',', map { '?' } @{ $$search{$col} }) . ')';
211 push @binds, map { "$_" } @{ $$search{$col} };
213 push @wheres, "$col = ?";
214 push @binds, $$search{$col};
217 $where .= join ' AND ', @wheres;
219 my $delete = "DELETE FROM $table $where";
221 $log->debug("Performing MASS deletion : $delete",DEBUG);
223 my $dbh = $cdbi->db_Main;
226 my $sth = $dbh->prepare($delete);
227 $sth->execute( @binds );
229 $log->debug("MASS Delete succeeded",DEBUG);
231 $log->debug("MASS Delete FAILED : ".shift(),DEBUG);
242 my $cdbi = $self->{cdbi};
246 $success = $cdbi->delete($node);
258 my $cdbi = $self->{cdbi};
259 my $api_name = $self->api_name;
260 (my $single_call_api_name = $api_name) =~ s/batch\.//o;
262 $log->debug("Default $api_name looking up $single_call_api_name...",INTERNAL);
263 my $method = $self->method_lookup($single_call_api_name);
266 while ( my $node = shift(@nodes) ) {
267 my ($res) = $method->run( $node );
268 push(@success, 1) if ($res >= 0);
271 my $insert_total = 0;
272 $insert_total += $_ for (@success);
274 return $insert_total;
278 use OpenILS::Application::Storage::Publisher::actor;
279 use OpenILS::Application::Storage::Publisher::action;
280 use OpenILS::Application::Storage::Publisher::asset;
281 use OpenILS::Application::Storage::Publisher::biblio;
282 use OpenILS::Application::Storage::Publisher::config;
283 use OpenILS::Application::Storage::Publisher::metabib;
284 use OpenILS::Application::Storage::Publisher::permission;
287 for my $fmclass ( (Fieldmapper->classes) ) {
289 next if ($fmclass->is_virtual);
291 $log->debug("Generating methods for Fieldmapper class $fmclass", DEBUG);
293 (my $cdbi = $fmclass) =~ s/^Fieldmapper:://o;
294 (my $class = $cdbi) =~ s/::.*//o;
295 (my $api_class = $cdbi) =~ s/::/./go;
296 my $registration_class = __PACKAGE__ . "::$class";
297 my $api_prefix = 'open-ils.storage.direct.'.$api_class;
299 # Create the search method
300 unless ( __PACKAGE__->is_registered( $api_prefix.'.search' ) ) {
301 __PACKAGE__->register_method(
302 api_name => $api_prefix.'.search',
311 # Create the retrieve method
312 unless ( __PACKAGE__->is_registered( $api_prefix.'.retrieve' ) ) {
313 __PACKAGE__->register_method(
314 api_name => $api_prefix.'.retrieve',
315 method => 'retrieve_node',
322 # Create the batch retrieve method
323 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.retrieve' ) ) {
324 __PACKAGE__->register_method(
325 api_name => $api_prefix.'.batch.retrieve',
326 method => 'retrieve_node',
334 for my $field ($fmclass->real_fields) {
335 unless ( __PACKAGE__->is_registered( $api_prefix.'.search.'.$field ) ) {
336 __PACKAGE__->register_method(
337 api_name => $api_prefix.'.search.'.$field,
338 method => 'search_one_field',
344 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_like.'.$field ) ) {
345 __PACKAGE__->register_method(
346 api_name => $api_prefix.'.search_like.'.$field,
347 method => 'search_one_field',
357 # Create the create method
358 unless ( __PACKAGE__->is_registered( $api_prefix.'.create' ) ) {
359 __PACKAGE__->register_method(
360 api_name => $api_prefix.'.create',
361 method => 'create_node',
367 # Create the batch create method
368 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.create' ) ) {
369 __PACKAGE__->register_method(
370 api_name => $api_prefix.'.batch.create',
371 method => 'batch_call',
377 # Create the update method
378 unless ( __PACKAGE__->is_registered( $api_prefix.'.update' ) ) {
379 __PACKAGE__->register_method(
380 api_name => $api_prefix.'.update',
381 method => 'update_node',
387 # Create the batch update method
388 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.update' ) ) {
389 __PACKAGE__->register_method(
390 api_name => $api_prefix.'.batch.update',
391 method => 'batch_call',
397 # Create the delete method
398 unless ( __PACKAGE__->is_registered( $api_prefix.'.delete' ) ) {
399 __PACKAGE__->register_method(
400 api_name => $api_prefix.'.delete',
401 method => 'delete_node',
407 # Create the batch delete method
408 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.delete' ) ) {
409 __PACKAGE__->register_method(
410 api_name => $api_prefix.'.batch.delete',
411 method => 'batch_call',
417 # Create the search-based mass delete method
418 unless ( __PACKAGE__->is_registered( $api_prefix.'.mass_delete' ) ) {
419 __PACKAGE__->register_method(
420 api_name => $api_prefix.'.mass_delete',
421 method => 'mass_delete',