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});
68 my $cache_key = md5_hex($key_string.$cache_page);
70 $log->debug("Key string for cache lookup is $key_string -> $cache_key", DEBUG);
72 my $cached_res = OpenSRF::Utils::Cache->new->get_cache( $cache_key );
73 if (defined $cached_res) {
74 $log->debug("Found ".scalar(@$cached_res)." records in the cache", INFO);
75 $log->debug("Values from cache: ".join(', ', @$cached_res), INTERNAL);
76 my $start = int($cache_args{offset} - ($cache_page * $cache_args{cache_page_size}));
77 my $end = int(($cache_args{offset} + $cache_args{limit} - 1) - (($cache_page + 1) * $cache_args{cache_page_size}));
78 $client->respond( $_ ) for ( grep { defined } @$cached_res[ $start .. $end ]);
82 my $method = $self->method_lookup($self->{real_api_name});
83 my @res = $method->run(@real_args);
86 $client->respond( $_ ) for ( grep { defined } @res[$cache_args{offset} .. int($cache_args{offset} + $cache_args{limit} - 1)] );
88 OpenSRF::Utils::Cache->new->put_cache(
90 [@res[int($cache_page * $cache_args{cache_page_size}) .. int(($cache_page + 1) * $cache_args{cache_page_size}) ]] =>
102 my $cdbi = $self->{cdbi};
104 for my $id ( @ids ) {
107 my ($rec) = $cdbi->fast_fieldmapper($id);
108 if ($self->api_name !~ /batch/o) {
109 return $rec if ($rec);
111 $client->respond($rec);
119 my $searches = shift;
121 my $cdbi = $self->{cdbi};
123 $log->debug("Searching $cdbi for { ".join(',', map { "$_ => $$searches{$_}" } keys %$searches).' }',DEBUG);
125 for my $obj ($cdbi->search($searches)) {
126 $client->respond( $obj->to_fieldmapper );
131 sub search_one_field {
136 (my $search_type = $self->api_name) =~ s/.*\.(search[^.]*).*/$1/o;
137 (my $col = $self->api_name) =~ s/.*\.$search_type\.([^.]+).*/$1/;
138 my $cdbi = $self->{cdbi};
141 $like = 1 if ($search_type =~ /like$/o);
143 for my $term (@terms) {
144 $log->debug("Searching $cdbi for $col using type $search_type, value '$term'",DEBUG);
146 return [ $cdbi->fast_fieldmapper($term,$col,$like) ];
148 $client->respond( [ $cdbi->fast_fieldmapper($term,$col,$like) ] );
159 my $cdbi = $self->{cdbi};
163 my $rec = $cdbi->create($node);
164 $success = $rec->id if ($rec);
177 my $cdbi = $self->{cdbi};
179 return $cdbi->update($node);
187 my $where = 'WHERE ';
189 my $cdbi = $self->{cdbi};
190 my $table = $cdbi->table;
192 my @keys = sort keys %$search;
196 for my $col ( @keys ) {
197 if (ref($$search{$col}) and ref($$search{$col}) =~ /ARRAY/o) {
198 push @wheres, "$col IN (" . join(',', map { '?' } @{ $$search{$col} }) . ')';
199 push @binds, map { "$_" } @{ $$search{$col} };
201 push @wheres, "$col = ?";
202 push @binds, $$search{$col};
205 $where .= join ' AND ', @wheres;
207 my $delete = "DELETE FROM $table $where";
209 $log->debug("Performing MASS deletion : $delete",DEBUG);
211 my $dbh = $cdbi->db_Main;
214 my $sth = $dbh->prepare($delete);
215 $sth->execute( @binds );
217 $log->debug("MASS Delete succeeded",DEBUG);
219 $log->debug("MASS Delete FAILED : ".shift(),DEBUG);
230 my $cdbi = $self->{cdbi};
234 $success = $cdbi->delete($node);
246 my $cdbi = $self->{cdbi};
247 my $api_name = $self->api_name;
248 (my $single_call_api_name = $api_name) =~ s/batch\.//o;
250 $log->debug("Default $api_name looking up $single_call_api_name...",INTERNAL);
251 my $method = $self->method_lookup($single_call_api_name);
254 while ( my $node = shift(@nodes) ) {
255 my ($res) = $method->run( $node );
256 push(@success, 1) if ($res >= 0);
259 my $insert_total = 0;
260 $insert_total += $_ for (@success);
262 return $insert_total;
266 use OpenILS::Application::Storage::Publisher::actor;
267 use OpenILS::Application::Storage::Publisher::action;
268 use OpenILS::Application::Storage::Publisher::asset;
269 use OpenILS::Application::Storage::Publisher::biblio;
270 use OpenILS::Application::Storage::Publisher::config;
271 use OpenILS::Application::Storage::Publisher::metabib;
274 for my $fmclass ( (Fieldmapper->classes) ) {
275 $log->debug("Generating methods for Fieldmapper class $fmclass", DEBUG);
277 (my $cdbi = $fmclass) =~ s/^Fieldmapper:://o;
278 (my $class = $cdbi) =~ s/::.*//o;
279 (my $api_class = $cdbi) =~ s/::/./go;
280 my $registration_class = __PACKAGE__ . "::$class";
281 my $api_prefix = 'open-ils.storage.direct.'.$api_class;
283 # Create the search method
284 unless ( __PACKAGE__->is_registered( $api_prefix.'.search' ) ) {
285 __PACKAGE__->register_method(
286 api_name => $api_prefix.'.search',
295 # Create the retrieve method
296 unless ( __PACKAGE__->is_registered( $api_prefix.'.retrieve' ) ) {
297 __PACKAGE__->register_method(
298 api_name => $api_prefix.'.retrieve',
299 method => 'retrieve_node',
306 # Create the batch retrieve method
307 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.retrieve' ) ) {
308 __PACKAGE__->register_method(
309 api_name => $api_prefix.'.batch.retrieve',
310 method => 'retrieve_node',
318 unless ($fmclass->is_virtual) {
319 for my $field ($fmclass->real_fields) {
320 unless ( __PACKAGE__->is_registered( $api_prefix.'.search.'.$field ) ) {
321 __PACKAGE__->register_method(
322 api_name => $api_prefix.'.search.'.$field,
323 method => 'search_one_field',
329 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_like.'.$field ) ) {
330 __PACKAGE__->register_method(
331 api_name => $api_prefix.'.search_like.'.$field,
332 method => 'search_one_field',
342 # Create the create method
343 unless ( __PACKAGE__->is_registered( $api_prefix.'.create' ) ) {
344 __PACKAGE__->register_method(
345 api_name => $api_prefix.'.create',
346 method => 'create_node',
352 # Create the batch create method
353 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.create' ) ) {
354 __PACKAGE__->register_method(
355 api_name => $api_prefix.'.batch.create',
356 method => 'batch_call',
362 # Create the update method
363 unless ( __PACKAGE__->is_registered( $api_prefix.'.update' ) ) {
364 __PACKAGE__->register_method(
365 api_name => $api_prefix.'.update',
366 method => 'update_node',
372 # Create the batch update method
373 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.update' ) ) {
374 __PACKAGE__->register_method(
375 api_name => $api_prefix.'.batch.update',
376 method => 'batch_call',
382 # Create the delete method
383 unless ( __PACKAGE__->is_registered( $api_prefix.'.delete' ) ) {
384 __PACKAGE__->register_method(
385 api_name => $api_prefix.'.delete',
386 method => 'delete_node',
392 # Create the batch delete method
393 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.delete' ) ) {
394 __PACKAGE__->register_method(
395 api_name => $api_prefix.'.batch.delete',
396 method => 'batch_call',
402 # Create the search-based mass delete method
403 unless ( __PACKAGE__->is_registered( $api_prefix.'.mass_delete' ) ) {
404 __PACKAGE__->register_method(
405 api_name => $api_prefix.'.mass_delete',
406 method => 'mass_delete',