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