]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher.pm
adding search_fts and search_regex
[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         (my $search_type = $self->api_name) =~ s/.*\.(search[^.]*).*/$1/o;
136
137         $log->debug("Searching $cdbi for { ".
138                 join(',', map { "$_ => $$searches{$_}" } keys %$searches).
139                 " } using $search_type",DEBUG);
140
141         for my $obj ($cdbi->$search_type($searches)) {
142                 $client->respond( $obj->to_fieldmapper );
143         }
144         return undef;
145 }
146
147 sub search_one_field {
148         my $self = shift;
149         my $client = shift;
150         my @terms = @_;
151
152         (my $search_type = $self->api_name) =~ s/.*\.(search[^.]*).*/$1/o;
153         (my $col = $self->api_name) =~ s/.*\.$search_type\.([^.]+).*/$1/;
154         my $cdbi = $self->{cdbi};
155
156         my $like = 0;
157         $like = 1 if ($search_type =~ /like$/o);
158         $like = 2 if ($search_type =~ /fts$/o);
159         $like = 3 if ($search_type =~ /regex$/o);
160
161         for my $term (@terms) {
162                 $log->debug("Searching $cdbi for $col using type $search_type, value '$term'",DEBUG);
163                 if (@terms == 1) {
164                         return [ $cdbi->fast_fieldmapper($term,$col,$like) ];
165                 }
166                 $client->respond( [ $cdbi->fast_fieldmapper($term,$col,$like) ] );
167         }
168         return undef;
169 }
170
171
172 sub create_node {
173         my $self = shift;
174         my $client = shift;
175         my $node = shift;
176
177         my $cdbi = $self->{cdbi};
178
179         my $success;
180         try {
181                 my $rec = $cdbi->create($node);
182                 $success = $rec->id if ($rec);
183         } catch Error with {
184                 $success = 0;
185         };
186
187         return $success;
188 }
189
190 sub update_node {
191         my $self = shift;
192         my $client = shift;
193         my $node = shift;
194
195         my $cdbi = $self->{cdbi};
196
197         return $cdbi->update($node);
198 }
199
200 sub mass_delete {
201         my $self = shift;
202         my $client = shift;
203         my $search = shift;
204
205         my $where = 'WHERE ';
206
207         my $cdbi = $self->{cdbi};
208         my $table = $cdbi->table;
209
210         my @keys = sort keys %$search;
211         
212         my @binds;
213         my @wheres;
214         for my $col ( @keys ) {
215                 if (ref($$search{$col}) and ref($$search{$col}) =~ /ARRAY/o) {
216                         push @wheres, "$col IN (" . join(',', map { '?' } @{ $$search{$col} }) . ')';
217                         push @binds, map { "$_" } @{ $$search{$col} };
218                 } else {
219                         push @wheres, "$col = ?";
220                         push @binds, $$search{$col};
221                 }
222         }
223         $where .= join ' AND ', @wheres;
224
225         my $delete = "DELETE FROM $table $where";
226
227         $log->debug("Performing MASS deletion : $delete",DEBUG);
228
229         my $dbh = $cdbi->db_Main;
230         my $success = 1;
231         try {
232                 my $sth = $dbh->prepare($delete);
233                 $sth->execute( @binds );
234                 $sth->finish;
235                 $log->debug("MASS Delete succeeded",DEBUG);
236         } catch Error with {
237                 $log->debug("MASS Delete FAILED : ".shift(),DEBUG);
238                 $success = 0;
239         };
240         return $success;
241 }
242
243 sub delete_node {
244         my $self = shift;
245         my $client = shift;
246         my $node = shift;
247
248         my $cdbi = $self->{cdbi};
249
250         my $success = 1;
251         try {
252                 $success = $cdbi->delete($node);
253         } catch Error with {
254                 $success = 0;
255         };
256         return $success;
257 }
258
259 sub batch_call {
260         my $self = shift;
261         my $client = shift;
262         my @nodes = @_;
263
264         my $cdbi = $self->{cdbi};
265         my $api_name = $self->api_name;
266         (my $single_call_api_name = $api_name) =~ s/batch\.//o;
267
268         $log->debug("Default $api_name looking up $single_call_api_name...",INTERNAL);
269         my $method = $self->method_lookup($single_call_api_name);
270
271         my @success;
272         while ( my $node = shift(@nodes) ) {
273                 my ($res) = $method->run( $node ); 
274                 push(@success, 1) if ($res >= 0);
275         }
276
277         my $insert_total = 0;
278         $insert_total += $_ for (@success);
279
280         return $insert_total;
281 }
282
283 eval '
284 use OpenILS::Application::Storage::Publisher::actor;
285 use OpenILS::Application::Storage::Publisher::action;
286 use OpenILS::Application::Storage::Publisher::asset;
287 use OpenILS::Application::Storage::Publisher::biblio;
288 use OpenILS::Application::Storage::Publisher::config;
289 use OpenILS::Application::Storage::Publisher::metabib;
290 use OpenILS::Application::Storage::Publisher::permission;
291 ';
292
293 for my $fmclass ( (Fieldmapper->classes) ) {
294
295         next if ($fmclass->is_virtual);
296
297         $log->debug("Generating methods for Fieldmapper class $fmclass", DEBUG);
298
299         (my $cdbi = $fmclass) =~ s/^Fieldmapper:://o;
300         (my $class = $cdbi) =~ s/::.*//o;
301         (my $api_class = $cdbi) =~ s/::/./go;
302         my $registration_class = __PACKAGE__ . "::$class";
303         my $api_prefix = 'open-ils.storage.direct.'.$api_class;
304
305         # Create the search methods
306         unless ( __PACKAGE__->is_registered( $api_prefix.'.search' ) ) {
307                 __PACKAGE__->register_method(
308                         api_name        => $api_prefix.'.search',
309                         method          => 'search',
310                         api_level       => 1,
311                         stream          => 1,
312                         cdbi            => $cdbi,
313                         cachable        => 1,
314                 );
315         }
316
317         unless ( __PACKAGE__->is_registered( $api_prefix.'.search_like' ) ) {
318                 __PACKAGE__->register_method(
319                         api_name        => $api_prefix.'.search_like',
320                         method          => 'search',
321                         api_level       => 1,
322                         stream          => 1,
323                         cdbi            => $cdbi,
324                         cachable        => 1,
325                 );
326         }
327
328         if (\&Class::DBI::search_fts) {
329                 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_fts' ) ) {
330                         __PACKAGE__->register_method(
331                                 api_name        => $api_prefix.'.search_fts',
332                                 method          => 'search',
333                                 api_level       => 1,
334                                 stream          => 1,
335                                 cdbi            => $cdbi,
336                                 cachable        => 1,
337                         );
338                 }
339         }
340
341         if (\&Class::DBI::search_regex) {
342                 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_regex' ) ) {
343                         __PACKAGE__->register_method(
344                                 api_name        => $api_prefix.'.search_regex',
345                                 method          => 'search',
346                                 api_level       => 1,
347                                 stream          => 1,
348                                 cdbi            => $cdbi,
349                                 cachable        => 1,
350                         );
351                 }
352         }
353
354         # Create the retrieve method
355         unless ( __PACKAGE__->is_registered( $api_prefix.'.retrieve' ) ) {
356                 __PACKAGE__->register_method(
357                         api_name        => $api_prefix.'.retrieve',
358                         method          => 'retrieve_node',
359                         api_level       => 1,
360                         cdbi            => $cdbi,
361                         cachable        => 1,
362                 );
363         }
364
365         # Create the batch retrieve method
366         unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.retrieve' ) ) {
367                 __PACKAGE__->register_method(
368                         api_name        => $api_prefix.'.batch.retrieve',
369                         method          => 'retrieve_node',
370                         api_level       => 1,
371                         stream          => 1,
372                         cdbi            => $cdbi,
373                         cachable        => 1,
374                 );
375         }
376
377         for my $field ($fmclass->real_fields) {
378                 unless ( __PACKAGE__->is_registered( $api_prefix.'.search.'.$field ) ) {
379                         __PACKAGE__->register_method(
380                                 api_name        => $api_prefix.'.search.'.$field,
381                                 method          => 'search_one_field',
382                                 api_level       => 1,
383                                 cdbi            => $cdbi,
384                                 cachable        => 1,
385                         );
386                 }
387                 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_like.'.$field ) ) {
388                         __PACKAGE__->register_method(
389                                 api_name        => $api_prefix.'.search_like.'.$field,
390                                 method          => 'search_one_field',
391                                 api_level       => 1,
392                                 cdbi            => $cdbi,
393                                 cachable        => 1,
394                         );
395                 }
396                 if (\&Class::DBI::search_fts) {
397                         unless ( __PACKAGE__->is_registered( $api_prefix.'.search_fts.'.$field ) ) {
398                                 __PACKAGE__->register_method(
399                                         api_name        => $api_prefix.'.search_fts.'.$field,
400                                         method          => 'search_one_field',
401                                         api_level       => 1,
402                                         cdbi            => $cdbi,
403                                         cachable        => 1,
404                                 );
405                         }
406                 }
407                 if (\&Class::DBI::search_regex) {
408                         unless ( __PACKAGE__->is_registered( $api_prefix.'.search_regex.'.$field ) ) {
409                                 __PACKAGE__->register_method(
410                                         api_name        => $api_prefix.'.search_regex.'.$field,
411                                         method          => 'search_one_field',
412                                         api_level       => 1,
413                                         cdbi            => $cdbi,
414                                         cachable        => 1,
415                                 );
416                         }
417                 }
418         }
419
420
421         # Create the create method
422         unless ( __PACKAGE__->is_registered( $api_prefix.'.create' ) ) {
423                 __PACKAGE__->register_method(
424                         api_name        => $api_prefix.'.create',
425                         method          => 'create_node',
426                         api_level       => 1,
427                         cdbi            => $cdbi,
428                 );
429         }
430
431         # Create the batch create method
432         unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.create' ) ) {
433                 __PACKAGE__->register_method(
434                         api_name        => $api_prefix.'.batch.create',
435                         method          => 'batch_call',
436                         api_level       => 1,
437                         cdbi            => $cdbi,
438                 );
439         }
440
441         # Create the update method
442         unless ( __PACKAGE__->is_registered( $api_prefix.'.update' ) ) {
443                 __PACKAGE__->register_method(
444                         api_name        => $api_prefix.'.update',
445                         method          => 'update_node',
446                         api_level       => 1,
447                         cdbi            => $cdbi,
448                 );
449         }
450
451         # Create the batch update method
452         unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.update' ) ) {
453                 __PACKAGE__->register_method(
454                         api_name        => $api_prefix.'.batch.update',
455                         method          => 'batch_call',
456                         api_level       => 1,
457                         cdbi            => $cdbi,
458                 );
459         }
460
461         # Create the delete method
462         unless ( __PACKAGE__->is_registered( $api_prefix.'.delete' ) ) {
463                 __PACKAGE__->register_method(
464                         api_name        => $api_prefix.'.delete',
465                         method          => 'delete_node',
466                         api_level       => 1,
467                         cdbi            => $cdbi,
468                 );
469         }
470
471         # Create the batch delete method
472         unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.delete' ) ) {
473                 __PACKAGE__->register_method(
474                         api_name        => $api_prefix.'.batch.delete',
475                         method          => 'batch_call',
476                         api_level       => 1,
477                         cdbi            => $cdbi,
478                 );
479         }
480
481         # Create the search-based mass delete method
482         unless ( __PACKAGE__->is_registered( $api_prefix.'.mass_delete' ) ) {
483                 __PACKAGE__->register_method(
484                         api_name        => $api_prefix.'.mass_delete',
485                         method          => 'mass_delete',
486                         api_level       => 1,
487                         cdbi            => $cdbi,
488                 );
489         }
490
491 }
492
493 1;