]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher.pm
new "container" stuff ... where the buckets live
[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 $keys = shift;
269         my $vals = shift;
270
271         my $cdbi = $self->{cdbi};
272
273         my $success = 1;
274         try {
275                 $success = $cdbi->remote_update($keys,$vals);
276         } catch Error with {
277                 $success = 0;
278         };
279         return $success;
280 }
281
282 sub merge_node {
283         my $self = shift;
284         my $client = shift;
285         my $keys = shift;
286         my $vals = shift;
287
288         my $cdbi = $self->{cdbi};
289
290         my $success = 1;
291         try {
292                 $success = $cdbi->merge($keys,$vals)->id;
293         } catch Error with {
294                 $success = 0;
295         };
296         return $success;
297 }
298
299 sub delete_node {
300         my $self = shift;
301         my $client = shift;
302         my $node = shift;
303
304         my $cdbi = $self->{cdbi};
305
306         my $success = 1;
307         try {
308                 $success = $cdbi->delete($node);
309         } catch Error with {
310                 $success = 0;
311         };
312         return $success;
313 }
314
315 sub batch_call {
316         my $self = shift;
317         my $client = shift;
318         my @nodes = @_;
319
320         my $unwrap = $self->{unwrap};
321
322         my $cdbi = $self->{cdbi};
323         my $api_name = $self->api_name;
324         (my $single_call_api_name = $api_name) =~ s/batch\.//o;
325
326         $log->debug("Default $api_name looking up $single_call_api_name...",INTERNAL);
327         my $method = $self->method_lookup($single_call_api_name);
328
329         my @success;
330         while ( my $node = shift(@nodes) ) {
331                 my ($res) = $method->run( ($unwrap ? (@$node) : ($node)) ); 
332                 push(@success, 1) if ($res >= 0);
333         }
334
335         my $insert_total = 0;
336         $insert_total += $_ for (@success);
337
338         return $insert_total;
339 }
340
341 eval '
342 use OpenILS::Application::Storage::Publisher::actor;
343 use OpenILS::Application::Storage::Publisher::action;
344 use OpenILS::Application::Storage::Publisher::asset;
345 use OpenILS::Application::Storage::Publisher::biblio;
346 use OpenILS::Application::Storage::Publisher::config;
347 use OpenILS::Application::Storage::Publisher::metabib;
348 use OpenILS::Application::Storage::Publisher::authority;
349 use OpenILS::Application::Storage::Publisher::money;
350 use OpenILS::Application::Storage::Publisher::permission;
351 use OpenILS::Application::Storage::Publisher::container;
352 ';
353
354 if ($@) {
355         $log->debug("ARG! Couldn't load (at least one) class Publisher: $@", ERROR);
356         throw OpenSRF::EX::ERROR ("ARG! Couldn't load (at least one) class Publisher: $@");
357 }
358
359
360 for my $fmclass ( (Fieldmapper->classes) ) {
361
362         $log->debug("Generating methods for Fieldmapper class $fmclass", DEBUG);
363
364         next if ($fmclass->is_virtual);
365
366         (my $cdbi = $fmclass) =~ s/^Fieldmapper:://o;
367         (my $class = $cdbi) =~ s/::.*//o;
368         (my $api_class = $cdbi) =~ s/::/./go;
369         my $registration_class = __PACKAGE__ . "::$class";
370         my $api_prefix = 'open-ils.storage.direct.'.$api_class;
371
372         # Create the search methods
373         unless ( __PACKAGE__->is_registered( $api_prefix.'.search' ) ) {
374                 __PACKAGE__->register_method(
375                         api_name        => $api_prefix.'.search',
376                         method          => 'search',
377                         api_level       => 1,
378                         stream          => 1,
379                         cdbi            => $cdbi,
380                         cachable        => 1,
381                 );
382         }
383
384         unless ( __PACKAGE__->is_registered( $api_prefix.'.search_where' ) ) {
385                 __PACKAGE__->register_method(
386                         api_name        => $api_prefix.'.search_where',
387                         method          => 'search_where',
388                         api_level       => 1,
389                         stream          => 1,
390                         cdbi            => $cdbi,
391                         cachable        => 1,
392                 );
393         }
394
395         unless ( __PACKAGE__->is_registered( $api_prefix.'.search_like' ) ) {
396                 __PACKAGE__->register_method(
397                         api_name        => $api_prefix.'.search_like',
398                         method          => 'search',
399                         api_level       => 1,
400                         stream          => 1,
401                         cdbi            => $cdbi,
402                         cachable        => 1,
403                 );
404         }
405
406         if (\&Class::DBI::search_fts) {
407                 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_fts' ) ) {
408                         __PACKAGE__->register_method(
409                                 api_name        => $api_prefix.'.search_fts',
410                                 method          => 'search',
411                                 api_level       => 1,
412                                 stream          => 1,
413                                 cdbi            => $cdbi,
414                                 cachable        => 1,
415                         );
416                 }
417         }
418
419         if (\&Class::DBI::search_regex) {
420                 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_regex' ) ) {
421                         __PACKAGE__->register_method(
422                                 api_name        => $api_prefix.'.search_regex',
423                                 method          => 'search',
424                                 api_level       => 1,
425                                 stream          => 1,
426                                 cdbi            => $cdbi,
427                                 cachable        => 1,
428                         );
429                 }
430         }
431
432         if (\&Class::DBI::search_ilike) {
433                 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_ilike' ) ) {
434                         __PACKAGE__->register_method(
435                                 api_name        => $api_prefix.'.search_ilike',
436                                 method          => 'search',
437                                 api_level       => 1,
438                                 stream          => 1,
439                                 cdbi            => $cdbi,
440                                 cachable        => 1,
441                         );
442                 }
443         }
444
445         # Create the retrieve method
446         unless ( __PACKAGE__->is_registered( $api_prefix.'.retrieve' ) ) {
447                 __PACKAGE__->register_method(
448                         api_name        => $api_prefix.'.retrieve',
449                         method          => 'retrieve_node',
450                         api_level       => 1,
451                         cdbi            => $cdbi,
452                         cachable        => 1,
453                 );
454         }
455
456         # Create the batch retrieve method
457         unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.retrieve' ) ) {
458                 __PACKAGE__->register_method(
459                         api_name        => $api_prefix.'.batch.retrieve',
460                         method          => 'retrieve_node',
461                         api_level       => 1,
462                         stream          => 1,
463                         cdbi            => $cdbi,
464                         cachable        => 1,
465                 );
466         }
467
468         for my $field ($fmclass->real_fields) {
469                 unless ( __PACKAGE__->is_registered( $api_prefix.'.search.'.$field ) ) {
470                         __PACKAGE__->register_method(
471                                 api_name        => $api_prefix.'.search.'.$field,
472                                 method          => 'search_one_field',
473                                 api_level       => 1,
474                                 cdbi            => $cdbi,
475                                 cachable        => 1,
476                                 stream          => 1,
477                         );
478                 }
479                 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_like.'.$field ) ) {
480                         __PACKAGE__->register_method(
481                                 api_name        => $api_prefix.'.search_like.'.$field,
482                                 method          => 'search_one_field',
483                                 api_level       => 1,
484                                 cdbi            => $cdbi,
485                                 cachable        => 1,
486                                 stream          => 1,
487                         );
488                 }
489                 if (\&Class::DBI::search_fts) {
490                         unless ( __PACKAGE__->is_registered( $api_prefix.'.search_fts.'.$field ) ) {
491                                 __PACKAGE__->register_method(
492                                         api_name        => $api_prefix.'.search_fts.'.$field,
493                                         method          => 'search_one_field',
494                                         api_level       => 1,
495                                         cdbi            => $cdbi,
496                                         cachable        => 1,
497                                         stream          => 1,
498                                 );
499                         }
500                 }
501                 if (\&Class::DBI::search_regex) {
502                         unless ( __PACKAGE__->is_registered( $api_prefix.'.search_regex.'.$field ) ) {
503                                 __PACKAGE__->register_method(
504                                         api_name        => $api_prefix.'.search_regex.'.$field,
505                                         method          => 'search_one_field',
506                                         api_level       => 1,
507                                         cdbi            => $cdbi,
508                                         cachable        => 1,
509                                         stream          => 1,
510                                 );
511                         }
512                 }
513                 if (\&Class::DBI::search_ilike) {
514                         unless ( __PACKAGE__->is_registered( $api_prefix.'.search_ilike.'.$field ) ) {
515                                 __PACKAGE__->register_method(
516                                         api_name        => $api_prefix.'.search_ilike.'.$field,
517                                         method          => 'search_one_field',
518                                         api_level       => 1,
519                                         cdbi            => $cdbi,
520                                         cachable        => 1,
521                                         stream          => 1,
522                                 );
523                         }
524                 }
525         }
526
527
528         unless ($fmclass->is_readonly) {
529                 # Create the create method
530                 unless ( __PACKAGE__->is_registered( $api_prefix.'.create' ) ) {
531                         __PACKAGE__->register_method(
532                                 api_name        => $api_prefix.'.create',
533                                 method          => 'create_node',
534                                 api_level       => 1,
535                                 cdbi            => $cdbi,
536                         );
537                 }
538
539                 # Create the batch create method
540                 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.create' ) ) {
541                         __PACKAGE__->register_method(
542                                 api_name        => $api_prefix.'.batch.create',
543                                 method          => 'batch_call',
544                                 api_level       => 1,
545                                 cdbi            => $cdbi,
546                         );
547                 }
548
549                 # Create the update method
550                 unless ( __PACKAGE__->is_registered( $api_prefix.'.update' ) ) {
551                         __PACKAGE__->register_method(
552                                 api_name        => $api_prefix.'.update',
553                                 method          => 'update_node',
554                                 api_level       => 1,
555                                 cdbi            => $cdbi,
556                         );
557                 }
558
559                 # Create the batch update method
560                 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.update' ) ) {
561                         __PACKAGE__->register_method(
562                                 api_name        => $api_prefix.'.batch.update',
563                                 method          => 'batch_call',
564                                 api_level       => 1,
565                                 cdbi            => $cdbi,
566                         );
567                 }
568
569                 # Create the delete method
570                 unless ( __PACKAGE__->is_registered( $api_prefix.'.delete' ) ) {
571                         __PACKAGE__->register_method(
572                                 api_name        => $api_prefix.'.delete',
573                                 method          => 'delete_node',
574                                 api_level       => 1,
575                                 cdbi            => $cdbi,
576                         );
577                 }
578
579                 # Create the batch delete method
580                 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.delete' ) ) {
581                         __PACKAGE__->register_method(
582                                 api_name        => $api_prefix.'.batch.delete',
583                                 method          => 'batch_call',
584                                 api_level       => 1,
585                                 cdbi            => $cdbi,
586                         );
587                 }
588
589                 # Create the merge method
590                 unless ( __PACKAGE__->is_registered( $api_prefix.'.merge' ) ) {
591                         __PACKAGE__->register_method(
592                                 api_name        => $api_prefix.'.merge',
593                                 method          => 'merge_node',
594                                 api_level       => 1,
595                                 cdbi            => $cdbi,
596                         );
597                 }
598
599                 # Create the batch merge method
600                 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.merge' ) ) {
601                         __PACKAGE__->register_method(
602                                 api_name        => $api_prefix.'.batch.merge',
603                                 method          => 'batch_call',
604                                 unwrap          => 1,
605                                 api_level       => 1,
606                                 cdbi            => $cdbi,
607                         );
608                 }
609
610                 # Create the remote_update method
611                 unless ( __PACKAGE__->is_registered( $api_prefix.'.remote_update' ) ) {
612                         __PACKAGE__->register_method(
613                                 api_name        => $api_prefix.'.remote_update',
614                                 method          => 'remote_update_node',
615                                 api_level       => 1,
616                                 cdbi            => $cdbi,
617                         );
618                 }
619
620                 # Create the batch remote_update method
621                 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.remote_update' ) ) {
622                         __PACKAGE__->register_method(
623                                 api_name        => $api_prefix.'.batch.remote_update',
624                                 method          => 'batch_call',
625                                 api_level       => 1,
626                                 unwrap          => 1,
627                                 cdbi            => $cdbi,
628                         );
629                 }
630
631                 # Create the search-based mass delete method
632                 unless ( __PACKAGE__->is_registered( $api_prefix.'.mass_delete' ) ) {
633                         __PACKAGE__->register_method(
634                                 api_name        => $api_prefix.'.mass_delete',
635                                 method          => 'mass_delete',
636                                 api_level       => 1,
637                                 cdbi            => $cdbi,
638                         );
639                 }
640         }
641 }
642
643 1;