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;
286 for my $fmclass ( (Fieldmapper->classes) ) {
287 $log->debug("Generating methods for Fieldmapper class $fmclass", DEBUG);
289 (my $cdbi = $fmclass) =~ s/^Fieldmapper:://o;
290 (my $class = $cdbi) =~ s/::.*//o;
291 (my $api_class = $cdbi) =~ s/::/./go;
292 my $registration_class = __PACKAGE__ . "::$class";
293 my $api_prefix = 'open-ils.storage.direct.'.$api_class;
295 # Create the search method
296 unless ( __PACKAGE__->is_registered( $api_prefix.'.search' ) ) {
297 __PACKAGE__->register_method(
298 api_name => $api_prefix.'.search',
307 # Create the retrieve method
308 unless ( __PACKAGE__->is_registered( $api_prefix.'.retrieve' ) ) {
309 __PACKAGE__->register_method(
310 api_name => $api_prefix.'.retrieve',
311 method => 'retrieve_node',
318 # Create the batch retrieve method
319 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.retrieve' ) ) {
320 __PACKAGE__->register_method(
321 api_name => $api_prefix.'.batch.retrieve',
322 method => 'retrieve_node',
330 unless ($fmclass->is_virtual) {
331 for my $field ($fmclass->real_fields) {
332 unless ( __PACKAGE__->is_registered( $api_prefix.'.search.'.$field ) ) {
333 __PACKAGE__->register_method(
334 api_name => $api_prefix.'.search.'.$field,
335 method => 'search_one_field',
341 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_like.'.$field ) ) {
342 __PACKAGE__->register_method(
343 api_name => $api_prefix.'.search_like.'.$field,
344 method => 'search_one_field',
354 # Create the create method
355 unless ( __PACKAGE__->is_registered( $api_prefix.'.create' ) ) {
356 __PACKAGE__->register_method(
357 api_name => $api_prefix.'.create',
358 method => 'create_node',
364 # Create the batch create method
365 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.create' ) ) {
366 __PACKAGE__->register_method(
367 api_name => $api_prefix.'.batch.create',
368 method => 'batch_call',
374 # Create the update method
375 unless ( __PACKAGE__->is_registered( $api_prefix.'.update' ) ) {
376 __PACKAGE__->register_method(
377 api_name => $api_prefix.'.update',
378 method => 'update_node',
384 # Create the batch update method
385 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.update' ) ) {
386 __PACKAGE__->register_method(
387 api_name => $api_prefix.'.batch.update',
388 method => 'batch_call',
394 # Create the delete method
395 unless ( __PACKAGE__->is_registered( $api_prefix.'.delete' ) ) {
396 __PACKAGE__->register_method(
397 api_name => $api_prefix.'.delete',
398 method => 'delete_node',
404 # Create the batch delete method
405 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.delete' ) ) {
406 __PACKAGE__->register_method(
407 api_name => $api_prefix.'.batch.delete',
408 method => 'batch_call',
414 # Create the search-based mass delete method
415 unless ( __PACKAGE__->is_registered( $api_prefix.'.mass_delete' ) ) {
416 __PACKAGE__->register_method(
417 api_name => $api_prefix.'.mass_delete',
418 method => 'mass_delete',