]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher.pm
adding "merge" and "remote_update" methods. these are propogated to all writable...
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Storage / Publisher.pm
1 package OpenILS::Application::Storage::Publisher;
2 use base qw/OpenILS::Application::Storage/;
3 our $VERSION = 1;
4
5 use Digest::MD5 qw/md5_hex/;
6 use OpenSRF::EX qw/:try/;
7 use OpenSRF::Utils;
8 use OpenSRF::Utils::Logger qw/:level/;
9 use OpenILS::Utils::Fieldmapper;
10
11 my $log = 'OpenSRF::Utils::Logger';
12
13
14 sub register_method {
15         my $class = shift;
16         my %args = @_;
17         my %dup_args = %args;
18
19         $class = ref($class) || $class;
20
21         $args{package} ||= $class;
22         __PACKAGE__->SUPER::register_method( %args );
23
24         if (exists($dup_args{cachable}) and $dup_args{cachable}) {
25                 (my $name = $dup_args{api_name}) =~ s/^open-ils\.storage/open-ils.storage.cachable/o;
26                 if ($name ne $dup_args{api_name}) {
27                         $dup_args{real_api_name} = $dup_args{api_name};
28                         $dup_args{method} = 'cachable_wrapper';
29                         $dup_args{api_name} = $name;
30                         $dup_args{package} = __PACKAGE__;
31                         __PACKAGE__->SUPER::register_method( %dup_args );
32                 }
33         }
34 }
35
36 sub cachable_wrapper {
37         my $self = shift;
38         my $client = shift;
39         my @args = @_;
40
41         my %cache_args = (
42                 limit           => 100,
43                 offset          => 0,
44                 timeout         => 7200,
45                 cache_page_size => 1000,
46         );
47
48         my @real_args;
49         my $key_string = $self->api_name;
50         for (my $ind = 0; $ind < scalar(@args); $ind++) {
51                 if (    $args[$ind] eq 'limit' ||
52                         $args[$ind] eq 'offset' ||
53                         $args[$ind] eq 'cache_page_size' ||
54                         $args[$ind] eq 'timeout' ) {
55
56                         my $key_ind = $ind;
57                         $ind++;
58                         my $value_ind = $ind;
59                         $cache_args{$args[$key_ind]} = $args[$value_ind];
60                         $log->debug("Cache limiter value for $args[$key_ind] is $args[$value_ind]", INTERNAL);
61                         next;
62                 }
63                 $key_string .= $args[$ind];
64                 $log->debug("Partial cache key value is $args[$ind]", INTERNAL);
65                 push @real_args, $args[$ind];
66         }
67
68         my $cache_page = int($cache_args{offset} / $cache_args{cache_page_size});
69         my $cache_key;
70         {       use bytes;
71                 $cache_key = md5_hex($key_string.$cache_page);
72         }
73
74         $log->debug("Key string for cache lookup is $key_string -> $cache_key", DEBUG);
75         $log->debug("Cache page is $cache_page", DEBUG);
76
77         my $cached_res = OpenSRF::Utils::Cache->new->get_cache( $cache_key );
78         if (defined $cached_res) {
79                 $log->debug("Found ".scalar(@$cached_res)." records in the cache", INFO);
80                 $log->debug("Values from cache: ".join(', ', @$cached_res), INTERNAL);
81                 my $start = int($cache_args{offset} - ($cache_page * $cache_args{cache_page_size}));
82                 my $end = int($start + $cache_args{limit} - 1);
83                 $log->debug("Responding with values from ".$start.' to '.$end,DEBUG);
84                 $client->respond( $_ ) for ( grep { defined } @$cached_res[ $start .. $end ]);
85                 return undef;
86         }
87
88         my $method = $self->method_lookup($self->{real_api_name});
89         my @res = $method->run(@real_args);
90
91
92         $client->respond( $_ ) for ( grep { defined } @res[$cache_args{offset} .. int($cache_args{offset} + $cache_args{limit} - 1)] );
93
94         $log->debug("Saving values from ".int($cache_page * $cache_args{cache_page_size})." to ".
95                 int(($cache_page + 1) * $cache_args{cache_page_size}). "to the cache", INTERNAL);
96         try {
97                 OpenSRF::Utils::Cache->new->put_cache(
98                         $cache_key =>
99                         [@res[int($cache_page * $cache_args{cache_page_size}) .. int(($cache_page + 1) * $cache_args{cache_page_size}) ]] =>
100                         OpenSRF::Utils->interval_to_seconds( $cache_args{timeout} )
101                 );
102         } catch Error with {
103                 my $e = shift;
104                 $log->error("Cache seems to be down, $e");
105         };
106
107         return undef;
108 }
109
110 sub retrieve_node {
111         my $self = shift;
112         my $client = shift;
113         my @ids = @_;
114
115         my $cdbi = $self->{cdbi};
116
117         for my $id ( @ids ) {
118                 next unless ($id);
119
120                 my ($rec) = $cdbi->fast_fieldmapper($id);
121                 if ($self->api_name !~ /batch/o) {
122                         return $rec if ($rec);
123                 }
124                 $client->respond($rec);
125         }
126         return undef;
127 }
128
129 sub search_where {
130         my $self = shift;
131         my $client = shift;
132         my @args = @_;
133
134         my $cdbi = $self->{cdbi};
135
136         for my $obj ($cdbi->search_where(@args)) {
137                 next unless ref($obj);
138                 $client->respond( $obj->to_fieldmapper );
139         }
140         return undef;
141 }
142
143 sub search {
144         my $self = shift;
145         my $client = shift;
146         my @args = @_;
147
148         my $cdbi = $self->{cdbi};
149
150         (my $search_type = $self->api_name) =~ s/.*\.(search[^.]*).*/$1/o;
151
152         for my $obj ($cdbi->$search_type(@args)) {
153                 next unless ref($obj);
154                 $client->respond( $obj->to_fieldmapper );
155         }
156         return undef;
157 }
158
159 sub search_one_field {
160         my $self = shift;
161         my $client = shift;
162         my @args = @_;
163
164         (my $field = $self->api_name) =~ s/.*\.([^\.]+)$/$1/o;
165
166         return search( $self, $client, $field, @args );
167 }
168
169 sub old_search_one_field {
170         my $self = shift;
171         my $client = shift;
172         my @terms = @_;
173
174         (my $search_type = $self->api_name) =~ s/.*\.(search[^.]*).*/$1/o;
175         (my $col = $self->api_name) =~ s/.*\.$search_type\.([^.]+).*/$1/;
176         my $cdbi = $self->{cdbi};
177
178         my $like = 0;
179         $like = 1 if ($search_type =~ /like$/o);
180         $like = 2 if ($search_type =~ /fts$/o);
181         $like = 3 if ($search_type =~ /regex$/o);
182
183         for my $term (@terms) {
184                 $log->debug("Searching $cdbi for $col using type $search_type, value '$term'",DEBUG);
185                 if (@terms == 1) {
186                         return [ $cdbi->fast_fieldmapper($term,$col,$like) ];
187                 }
188                 $client->respond( [ $cdbi->fast_fieldmapper($term,$col,$like) ] );
189         }
190         return undef;
191 }
192
193
194 sub create_node {
195         my $self = shift;
196         my $client = shift;
197         my $node = shift;
198
199         my $cdbi = $self->{cdbi};
200
201         my $success;
202         try {
203                 my $rec = $cdbi->create($node);
204                 $success = $rec->id if ($rec);
205         } catch Error with {
206                 $success = 0;
207         };
208
209         return $success;
210 }
211
212 sub update_node {
213         my $self = shift;
214         my $client = shift;
215         my $node = shift;
216
217         my $cdbi = $self->{cdbi};
218
219         return $cdbi->update($node);
220 }
221
222 sub mass_delete {
223         my $self = shift;
224         my $client = shift;
225         my $search = shift;
226
227         my $where = 'WHERE ';
228
229         my $cdbi = $self->{cdbi};
230         my $table = $cdbi->table;
231
232         my @keys = sort keys %$search;
233         
234         my @binds;
235         my @wheres;
236         for my $col ( @keys ) {
237                 if (ref($$search{$col}) and ref($$search{$col}) =~ /ARRAY/o) {
238                         push @wheres, "$col IN (" . join(',', map { '?' } @{ $$search{$col} }) . ')';
239                         push @binds, map { "$_" } @{ $$search{$col} };
240                 } else {
241                         push @wheres, "$col = ?";
242                         push @binds, $$search{$col};
243                 }
244         }
245         $where .= join ' AND ', @wheres;
246
247         my $delete = "DELETE FROM $table $where";
248
249         $log->debug("Performing MASS deletion : $delete",DEBUG);
250
251         my $dbh = $cdbi->db_Main;
252         my $success = 1;
253         try {
254                 my $sth = $dbh->prepare($delete);
255                 $sth->execute( @binds );
256                 $sth->finish;
257                 $log->debug("MASS Delete succeeded",DEBUG);
258         } catch Error with {
259                 $log->debug("MASS Delete FAILED : ".shift(),DEBUG);
260                 $success = 0;
261         };
262         return $success;
263 }
264
265 sub remote_update_node {
266         my $self = shift;
267         my $client = shift;
268         my $node = shift;
269
270         my $cdbi = $self->{cdbi};
271
272         my $success = 1;
273         try {
274                 $success = $cdbi->remote_update($node);
275         } catch Error with {
276                 $success = 0;
277         };
278         return $success;
279 }
280
281 sub merge_node {
282         my $self = shift;
283         my $client = shift;
284         my $node = shift;
285
286         my $cdbi = $self->{cdbi};
287
288         my $success = 1;
289         try {
290                 $success = $cdbi->merge($node);
291         } catch Error with {
292                 $success = 0;
293         };
294         return $success;
295 }
296
297 sub delete_node {
298         my $self = shift;
299         my $client = shift;
300         my $node = shift;
301
302         my $cdbi = $self->{cdbi};
303
304         my $success = 1;
305         try {
306                 $success = $cdbi->delete($node);
307         } catch Error with {
308                 $success = 0;
309         };
310         return $success;
311 }
312
313 sub batch_call {
314         my $self = shift;
315         my $client = shift;
316         my @nodes = @_;
317
318         my $cdbi = $self->{cdbi};
319         my $api_name = $self->api_name;
320         (my $single_call_api_name = $api_name) =~ s/batch\.//o;
321
322         $log->debug("Default $api_name looking up $single_call_api_name...",INTERNAL);
323         my $method = $self->method_lookup($single_call_api_name);
324
325         my @success;
326         while ( my $node = shift(@nodes) ) {
327                 my ($res) = $method->run( $node ); 
328                 push(@success, 1) if ($res >= 0);
329         }
330
331         my $insert_total = 0;
332         $insert_total += $_ for (@success);
333
334         return $insert_total;
335 }
336
337 eval '
338 use OpenILS::Application::Storage::Publisher::actor;
339 use OpenILS::Application::Storage::Publisher::action;
340 use OpenILS::Application::Storage::Publisher::asset;
341 use OpenILS::Application::Storage::Publisher::biblio;
342 use OpenILS::Application::Storage::Publisher::config;
343 use OpenILS::Application::Storage::Publisher::metabib;
344 use OpenILS::Application::Storage::Publisher::money;
345 use OpenILS::Application::Storage::Publisher::permission;
346 ';
347
348 if ($@) {
349         $log->debug("ARG! Couldn't load (at least one) class Publisher: $@", ERROR);
350         throw OpenSRF::EX::ERROR ("ARG! Couldn't load (at least one) class Publisher: $@");
351 }
352
353
354 for my $fmclass ( (Fieldmapper->classes) ) {
355
356         next if ($fmclass->is_virtual);
357
358         $log->debug("Generating methods for Fieldmapper class $fmclass", DEBUG);
359
360         (my $cdbi = $fmclass) =~ s/^Fieldmapper:://o;
361         (my $class = $cdbi) =~ s/::.*//o;
362         (my $api_class = $cdbi) =~ s/::/./go;
363         my $registration_class = __PACKAGE__ . "::$class";
364         my $api_prefix = 'open-ils.storage.direct.'.$api_class;
365
366         # Create the search methods
367         unless ( __PACKAGE__->is_registered( $api_prefix.'.search' ) ) {
368                 __PACKAGE__->register_method(
369                         api_name        => $api_prefix.'.search',
370                         method          => 'search',
371                         api_level       => 1,
372                         stream          => 1,
373                         cdbi            => $cdbi,
374                         cachable        => 1,
375                 );
376         }
377
378         unless ( __PACKAGE__->is_registered( $api_prefix.'.search_where' ) ) {
379                 __PACKAGE__->register_method(
380                         api_name        => $api_prefix.'.search_where',
381                         method          => 'search_where',
382                         api_level       => 1,
383                         stream          => 1,
384                         cdbi            => $cdbi,
385                         cachable        => 1,
386                 );
387         }
388
389         unless ( __PACKAGE__->is_registered( $api_prefix.'.search_like' ) ) {
390                 __PACKAGE__->register_method(
391                         api_name        => $api_prefix.'.search_like',
392                         method          => 'search',
393                         api_level       => 1,
394                         stream          => 1,
395                         cdbi            => $cdbi,
396                         cachable        => 1,
397                 );
398         }
399
400         if (\&Class::DBI::search_fts) {
401                 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_fts' ) ) {
402                         __PACKAGE__->register_method(
403                                 api_name        => $api_prefix.'.search_fts',
404                                 method          => 'search',
405                                 api_level       => 1,
406                                 stream          => 1,
407                                 cdbi            => $cdbi,
408                                 cachable        => 1,
409                         );
410                 }
411         }
412
413         if (\&Class::DBI::search_regex) {
414                 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_regex' ) ) {
415                         __PACKAGE__->register_method(
416                                 api_name        => $api_prefix.'.search_regex',
417                                 method          => 'search',
418                                 api_level       => 1,
419                                 stream          => 1,
420                                 cdbi            => $cdbi,
421                                 cachable        => 1,
422                         );
423                 }
424         }
425
426         if (\&Class::DBI::search_ilike) {
427                 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_ilike' ) ) {
428                         __PACKAGE__->register_method(
429                                 api_name        => $api_prefix.'.search_ilike',
430                                 method          => 'search',
431                                 api_level       => 1,
432                                 stream          => 1,
433                                 cdbi            => $cdbi,
434                                 cachable        => 1,
435                         );
436                 }
437         }
438
439         # Create the retrieve method
440         unless ( __PACKAGE__->is_registered( $api_prefix.'.retrieve' ) ) {
441                 __PACKAGE__->register_method(
442                         api_name        => $api_prefix.'.retrieve',
443                         method          => 'retrieve_node',
444                         api_level       => 1,
445                         cdbi            => $cdbi,
446                         cachable        => 1,
447                 );
448         }
449
450         # Create the batch retrieve method
451         unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.retrieve' ) ) {
452                 __PACKAGE__->register_method(
453                         api_name        => $api_prefix.'.batch.retrieve',
454                         method          => 'retrieve_node',
455                         api_level       => 1,
456                         stream          => 1,
457                         cdbi            => $cdbi,
458                         cachable        => 1,
459                 );
460         }
461
462         for my $field ($fmclass->real_fields) {
463                 unless ( __PACKAGE__->is_registered( $api_prefix.'.search.'.$field ) ) {
464                         __PACKAGE__->register_method(
465                                 api_name        => $api_prefix.'.search.'.$field,
466                                 method          => 'search_one_field',
467                                 api_level       => 1,
468                                 cdbi            => $cdbi,
469                                 cachable        => 1,
470                                 stream          => 1,
471                         );
472                 }
473                 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_like.'.$field ) ) {
474                         __PACKAGE__->register_method(
475                                 api_name        => $api_prefix.'.search_like.'.$field,
476                                 method          => 'search_one_field',
477                                 api_level       => 1,
478                                 cdbi            => $cdbi,
479                                 cachable        => 1,
480                                 stream          => 1,
481                         );
482                 }
483                 if (\&Class::DBI::search_fts) {
484                         unless ( __PACKAGE__->is_registered( $api_prefix.'.search_fts.'.$field ) ) {
485                                 __PACKAGE__->register_method(
486                                         api_name        => $api_prefix.'.search_fts.'.$field,
487                                         method          => 'search_one_field',
488                                         api_level       => 1,
489                                         cdbi            => $cdbi,
490                                         cachable        => 1,
491                                         stream          => 1,
492                                 );
493                         }
494                 }
495                 if (\&Class::DBI::search_regex) {
496                         unless ( __PACKAGE__->is_registered( $api_prefix.'.search_regex.'.$field ) ) {
497                                 __PACKAGE__->register_method(
498                                         api_name        => $api_prefix.'.search_regex.'.$field,
499                                         method          => 'search_one_field',
500                                         api_level       => 1,
501                                         cdbi            => $cdbi,
502                                         cachable        => 1,
503                                         stream          => 1,
504                                 );
505                         }
506                 }
507                 if (\&Class::DBI::search_ilike) {
508                         unless ( __PACKAGE__->is_registered( $api_prefix.'.search_ilike.'.$field ) ) {
509                                 __PACKAGE__->register_method(
510                                         api_name        => $api_prefix.'.search_ilike.'.$field,
511                                         method          => 'search_one_field',
512                                         api_level       => 1,
513                                         cdbi            => $cdbi,
514                                         cachable        => 1,
515                                         stream          => 1,
516                                 );
517                         }
518                 }
519         }
520
521
522         unless ($fmclass->is_readonly) {
523                 # Create the create method
524                 unless ( __PACKAGE__->is_registered( $api_prefix.'.create' ) ) {
525                         __PACKAGE__->register_method(
526                                 api_name        => $api_prefix.'.create',
527                                 method          => 'create_node',
528                                 api_level       => 1,
529                                 cdbi            => $cdbi,
530                         );
531                 }
532
533                 # Create the batch create method
534                 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.create' ) ) {
535                         __PACKAGE__->register_method(
536                                 api_name        => $api_prefix.'.batch.create',
537                                 method          => 'batch_call',
538                                 api_level       => 1,
539                                 cdbi            => $cdbi,
540                         );
541                 }
542
543                 # Create the update method
544                 unless ( __PACKAGE__->is_registered( $api_prefix.'.update' ) ) {
545                         __PACKAGE__->register_method(
546                                 api_name        => $api_prefix.'.update',
547                                 method          => 'update_node',
548                                 api_level       => 1,
549                                 cdbi            => $cdbi,
550                         );
551                 }
552
553                 # Create the batch update method
554                 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.update' ) ) {
555                         __PACKAGE__->register_method(
556                                 api_name        => $api_prefix.'.batch.update',
557                                 method          => 'batch_call',
558                                 api_level       => 1,
559                                 cdbi            => $cdbi,
560                         );
561                 }
562
563                 # Create the delete method
564                 unless ( __PACKAGE__->is_registered( $api_prefix.'.delete' ) ) {
565                         __PACKAGE__->register_method(
566                                 api_name        => $api_prefix.'.delete',
567                                 method          => 'delete_node',
568                                 api_level       => 1,
569                                 cdbi            => $cdbi,
570                         );
571                 }
572
573                 # Create the batch delete method
574                 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.delete' ) ) {
575                         __PACKAGE__->register_method(
576                                 api_name        => $api_prefix.'.batch.delete',
577                                 method          => 'batch_call',
578                                 api_level       => 1,
579                                 cdbi            => $cdbi,
580                         );
581                 }
582
583                 # Create the merge method
584                 unless ( __PACKAGE__->is_registered( $api_prefix.'.merge' ) ) {
585                         __PACKAGE__->register_method(
586                                 api_name        => $api_prefix.'.merge',
587                                 method          => 'merge_node',
588                                 api_level       => 1,
589                                 cdbi            => $cdbi,
590                         );
591                 }
592
593                 # Create the batch merge method
594                 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.merge' ) ) {
595                         __PACKAGE__->register_method(
596                                 api_name        => $api_prefix.'.batch.merge',
597                                 method          => 'batch_call',
598                                 api_level       => 1,
599                                 cdbi            => $cdbi,
600                         );
601                 }
602
603                 # Create the remote_update method
604                 unless ( __PACKAGE__->is_registered( $api_prefix.'.remote_update' ) ) {
605                         __PACKAGE__->register_method(
606                                 api_name        => $api_prefix.'.remote_update',
607                                 method          => 'remote_update_node',
608                                 api_level       => 1,
609                                 cdbi            => $cdbi,
610                         );
611                 }
612
613                 # Create the batch remote_update method
614                 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.remote_update' ) ) {
615                         __PACKAGE__->register_method(
616                                 api_name        => $api_prefix.'.batch.remote_update',
617                                 method          => 'batch_call',
618                                 api_level       => 1,
619                                 cdbi            => $cdbi,
620                         );
621                 }
622
623                 # Create the search-based mass delete method
624                 unless ( __PACKAGE__->is_registered( $api_prefix.'.mass_delete' ) ) {
625                         __PACKAGE__->register_method(
626                                 api_name        => $api_prefix.'.mass_delete',
627                                 method          => 'mass_delete',
628                                 api_level       => 1,
629                                 cdbi            => $cdbi,
630                         );
631                 }
632         }
633 }
634
635 1;