]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher.pm
39faec1f7d3932db7fb7e0d3a0b78749e7b30261
[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 use OpenILS::Application::Storage::Publisher::permission;
285 ';
286
287 for my $fmclass ( (Fieldmapper->classes) ) {
288
289         next if ($fmclass->is_virtual);
290
291         $log->debug("Generating methods for Fieldmapper class $fmclass", DEBUG);
292
293         (my $cdbi = $fmclass) =~ s/^Fieldmapper:://o;
294         (my $class = $cdbi) =~ s/::.*//o;
295         (my $api_class = $cdbi) =~ s/::/./go;
296         my $registration_class = __PACKAGE__ . "::$class";
297         my $api_prefix = 'open-ils.storage.direct.'.$api_class;
298
299         # Create the search method
300         unless ( __PACKAGE__->is_registered( $api_prefix.'.search' ) ) {
301                 __PACKAGE__->register_method(
302                         api_name        => $api_prefix.'.search',
303                         method          => 'search',
304                         api_level       => 1,
305                         stream          => 1,
306                         cdbi            => $cdbi,
307                         cachable        => 1,
308                 );
309         }
310
311         # Create the retrieve method
312         unless ( __PACKAGE__->is_registered( $api_prefix.'.retrieve' ) ) {
313                 __PACKAGE__->register_method(
314                         api_name        => $api_prefix.'.retrieve',
315                         method          => 'retrieve_node',
316                         api_level       => 1,
317                         cdbi            => $cdbi,
318                         cachable        => 1,
319                 );
320         }
321
322         # Create the batch retrieve method
323         unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.retrieve' ) ) {
324                 __PACKAGE__->register_method(
325                         api_name        => $api_prefix.'.batch.retrieve',
326                         method          => 'retrieve_node',
327                         api_level       => 1,
328                         stream          => 1,
329                         cdbi            => $cdbi,
330                         cachable        => 1,
331                 );
332         }
333
334         for my $field ($fmclass->real_fields) {
335                 unless ( __PACKAGE__->is_registered( $api_prefix.'.search.'.$field ) ) {
336                         __PACKAGE__->register_method(
337                                 api_name        => $api_prefix.'.search.'.$field,
338                                 method          => 'search_one_field',
339                                 api_level       => 1,
340                                 cdbi            => $cdbi,
341                                 cachable        => 1,
342                         );
343                 }
344                 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_like.'.$field ) ) {
345                         __PACKAGE__->register_method(
346                                 api_name        => $api_prefix.'.search_like.'.$field,
347                                 method          => 'search_one_field',
348                                 api_level       => 1,
349                                 cdbi            => $cdbi,
350                                 cachable        => 1,
351                         );
352                 }
353         }
354
355
356         # Create the create method
357         unless ( __PACKAGE__->is_registered( $api_prefix.'.create' ) ) {
358                 __PACKAGE__->register_method(
359                         api_name        => $api_prefix.'.create',
360                         method          => 'create_node',
361                         api_level       => 1,
362                         cdbi            => $cdbi,
363                 );
364         }
365
366         # Create the batch create method
367         unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.create' ) ) {
368                 __PACKAGE__->register_method(
369                         api_name        => $api_prefix.'.batch.create',
370                         method          => 'batch_call',
371                         api_level       => 1,
372                         cdbi            => $cdbi,
373                 );
374         }
375
376         # Create the update method
377         unless ( __PACKAGE__->is_registered( $api_prefix.'.update' ) ) {
378                 __PACKAGE__->register_method(
379                         api_name        => $api_prefix.'.update',
380                         method          => 'update_node',
381                         api_level       => 1,
382                         cdbi            => $cdbi,
383                 );
384         }
385
386         # Create the batch update method
387         unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.update' ) ) {
388                 __PACKAGE__->register_method(
389                         api_name        => $api_prefix.'.batch.update',
390                         method          => 'batch_call',
391                         api_level       => 1,
392                         cdbi            => $cdbi,
393                 );
394         }
395
396         # Create the delete method
397         unless ( __PACKAGE__->is_registered( $api_prefix.'.delete' ) ) {
398                 __PACKAGE__->register_method(
399                         api_name        => $api_prefix.'.delete',
400                         method          => 'delete_node',
401                         api_level       => 1,
402                         cdbi            => $cdbi,
403                 );
404         }
405
406         # Create the batch delete method
407         unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.delete' ) ) {
408                 __PACKAGE__->register_method(
409                         api_name        => $api_prefix.'.batch.delete',
410                         method          => 'batch_call',
411                         api_level       => 1,
412                         cdbi            => $cdbi,
413                 );
414         }
415
416         # Create the search-based mass delete method
417         unless ( __PACKAGE__->is_registered( $api_prefix.'.mass_delete' ) ) {
418                 __PACKAGE__->register_method(
419                         api_name        => $api_prefix.'.mass_delete',
420                         method          => 'mass_delete',
421                         api_level       => 1,
422                         cdbi            => $cdbi,
423                 );
424         }
425
426 }
427
428 1;