]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher.pm
b02e0a5165afe95b49507fd4bfbfd1e2c13c8758
[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::Logger qw/:level/;
8 use OpenILS::Utils::Fieldmapper;
9
10 my $log = 'OpenSRF::Utils::Logger';
11
12
13 sub register_method {
14         my $class = shift;
15         my %args = @_;
16         my %dup_args = %args;
17
18         $class = ref($class) || $class;
19
20         $args{package} ||= $class;
21         __PACKAGE__->SUPER::register_method( %args );
22
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 );
31                 }
32         }
33 }
34
35 sub cachable_wrapper {
36         my $self = shift;
37         my $client = shift;
38         my @args = @_;
39
40         my %cache_args = (
41                 limit           => 100,
42                 offset          => 0,
43                 timeout         => 7200,
44                 cache_page_size => 1000,
45         );
46
47         my @real_args;
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' ) {
54
55                         my $key_ind = $ind;
56                         $ind++;
57                         my $value_ind = $ind;
58                         $cache_args{$args[$key_ind]} = $args[$value_ind];
59                         $log->debug("Cache limiter value for $args[$key_ind] is $args[$value_ind]", INTERNAL);
60                         next;
61                 }
62                 $key_string .= $args[$ind];
63                 $log->debug("Partial cache key value is $args[$ind]", INTERNAL);
64                 push @real_args, $args[$ind];
65         }
66
67         my $cache_page = int($cache_args{offset} / $cache_args{cache_page_size});
68         my $cache_key;
69         {       use bytes;
70                 $cache_key = md5_hex($key_string.$cache_page);
71         }
72
73         $log->debug("Key string for cache lookup is $key_string -> $cache_key", DEBUG);
74         $log->debug("Cache page is $cache_page", DEBUG);
75
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 ]);
84                 return undef;
85         }
86
87         my $method = $self->method_lookup($self->{real_api_name});
88         my @res = $method->run(@real_args);
89
90
91         $client->respond( $_ ) for ( grep { defined } @res[$cache_args{offset} .. int($cache_args{offset} + $cache_args{limit} - 1)] );
92
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);
95         try {
96                 OpenSRF::Utils::Cache->new->put_cache(
97                         $cache_key =>
98                         [@res[int($cache_page * $cache_args{cache_page_size}) .. int(($cache_page + 1) * $cache_args{cache_page_size}) ]] =>
99                         $cache_args{timeout}
100                 );
101         } catch Error with {
102                 my $e = shift;
103                 $log->error("Cache seems to be down, $e");
104         };
105
106         return undef;
107 }
108
109 sub retrieve_node {
110         my $self = shift;
111         my $client = shift;
112         my @ids = @_;
113
114         my $cdbi = $self->{cdbi};
115
116         for my $id ( @ids ) {
117                 next unless ($id);
118
119                 my ($rec) = $cdbi->fast_fieldmapper($id);
120                 if ($self->api_name !~ /batch/o) {
121                         return $rec if ($rec);
122                 }
123                 $client->respond($rec);
124         }
125         return undef;
126 }
127
128 sub search {
129         my $self = shift;
130         my $client = shift;
131         my $searches = shift;
132
133         my $cdbi = $self->{cdbi};
134
135         $log->debug("Searching $cdbi for { ".join(',', map { "$_ => $$searches{$_}" } keys %$searches).' }',DEBUG);
136
137         for my $obj ($cdbi->search($searches)) {
138                 $client->respond( $obj->to_fieldmapper );
139         }
140         return undef;
141 }
142
143 sub search_one_field {
144         my $self = shift;
145         my $client = shift;
146         my @terms = @_;
147
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};
151
152         my $like = 0;
153         $like = 1 if ($search_type =~ /like$/o);
154
155         for my $term (@terms) {
156                 $log->debug("Searching $cdbi for $col using type $search_type, value '$term'",DEBUG);
157                 if (@terms == 1) {
158                         return [ $cdbi->fast_fieldmapper($term,$col,$like) ];
159                 }
160                 $client->respond( [ $cdbi->fast_fieldmapper($term,$col,$like) ] );
161         }
162         return undef;
163 }
164
165
166 sub create_node {
167         my $self = shift;
168         my $client = shift;
169         my $node = shift;
170
171         my $cdbi = $self->{cdbi};
172
173         my $success;
174         try {
175                 my $rec = $cdbi->create($node);
176                 $success = $rec->id if ($rec);
177         } catch Error with {
178                 $success = 0;
179         };
180
181         return $success;
182 }
183
184 sub update_node {
185         my $self = shift;
186         my $client = shift;
187         my $node = shift;
188
189         my $cdbi = $self->{cdbi};
190
191         return $cdbi->update($node);
192 }
193
194 sub mass_delete {
195         my $self = shift;
196         my $client = shift;
197         my $search = shift;
198
199         my $where = 'WHERE ';
200
201         my $cdbi = $self->{cdbi};
202         my $table = $cdbi->table;
203
204         my @keys = sort keys %$search;
205         
206         my @binds;
207         my @wheres;
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} };
212                 } else {
213                         push @wheres, "$col = ?";
214                         push @binds, $$search{$col};
215                 }
216         }
217         $where .= join ' AND ', @wheres;
218
219         my $delete = "DELETE FROM $table $where";
220
221         $log->debug("Performing MASS deletion : $delete",DEBUG);
222
223         my $dbh = $cdbi->db_Main;
224         my $success = 1;
225         try {
226                 my $sth = $dbh->prepare($delete);
227                 $sth->execute( @binds );
228                 $sth->finish;
229                 $log->debug("MASS Delete succeeded",DEBUG);
230         } catch Error with {
231                 $log->debug("MASS Delete FAILED : ".shift(),DEBUG);
232                 $success = 0;
233         };
234         return $success;
235 }
236
237 sub delete_node {
238         my $self = shift;
239         my $client = shift;
240         my $node = shift;
241
242         my $cdbi = $self->{cdbi};
243
244         my $success = 1;
245         try {
246                 $success = $cdbi->delete($node);
247         } catch Error with {
248                 $success = 0;
249         };
250         return $success;
251 }
252
253 sub batch_call {
254         my $self = shift;
255         my $client = shift;
256         my @nodes = @_;
257
258         my $cdbi = $self->{cdbi};
259         my $api_name = $self->api_name;
260         (my $single_call_api_name = $api_name) =~ s/batch\.//o;
261
262         $log->debug("Default $api_name looking up $single_call_api_name...",INTERNAL);
263         my $method = $self->method_lookup($single_call_api_name);
264
265         my @success;
266         while ( my $node = shift(@nodes) ) {
267                 my ($res) = $method->run( $node ); 
268                 push(@success, 1) if ($res >= 0);
269         }
270
271         my $insert_total = 0;
272         $insert_total += $_ for (@success);
273
274         return $insert_total;
275 }
276
277 eval '
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 ';
285
286 for my $fmclass ( (Fieldmapper->classes) ) {
287         $log->debug("Generating methods for Fieldmapper class $fmclass", DEBUG);
288
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;
294
295         # Create the search method
296         unless ( __PACKAGE__->is_registered( $api_prefix.'.search' ) ) {
297                 __PACKAGE__->register_method(
298                         api_name        => $api_prefix.'.search',
299                         method          => 'search',
300                         api_level       => 1,
301                         stream          => 1,
302                         cdbi            => $cdbi,
303                         cachable        => 1,
304                 );
305         }
306
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',
312                         api_level       => 1,
313                         cdbi            => $cdbi,
314                         cachable        => 1,
315                 );
316         }
317
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',
323                         api_level       => 1,
324                         stream          => 1,
325                         cdbi            => $cdbi,
326                         cachable        => 1,
327                 );
328         }
329
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',
336                                         api_level       => 1,
337                                         cdbi            => $cdbi,
338                                         cachable        => 1,
339                                 );
340                         }
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',
345                                         api_level       => 1,
346                                         cdbi            => $cdbi,
347                                         cachable        => 1,
348                                 );
349                         }
350                 }
351         }
352
353
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',
359                         api_level       => 1,
360                         cdbi            => $cdbi,
361                 );
362         }
363
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',
369                         api_level       => 1,
370                         cdbi            => $cdbi,
371                 );
372         }
373
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',
379                         api_level       => 1,
380                         cdbi            => $cdbi,
381                 );
382         }
383
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',
389                         api_level       => 1,
390                         cdbi            => $cdbi,
391                 );
392         }
393
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',
399                         api_level       => 1,
400                         cdbi            => $cdbi,
401                 );
402         }
403
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',
409                         api_level       => 1,
410                         cdbi            => $cdbi,
411                 );
412         }
413
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',
419                         api_level       => 1,
420                         cdbi            => $cdbi,
421                 );
422         }
423
424 }
425
426 1;