]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/metabib.pm
big changes... big
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Storage / Publisher / metabib.pm
1 package OpenILS::Application::Storage::Publisher::metabib;
2 use base qw/OpenILS::Application::Storage::Publisher/;
3 use vars qw/$VERSION/;
4 use OpenSRF::EX qw/:try/;
5 use OpenILS::Application::Storage::FTS;
6 use OpenILS::Utils::Fieldmapper;
7 use OpenSRF::Utils::Logger qw/:level/;
8 use OpenSRF::Utils::Cache;
9 use Data::Dumper;
10 use Digest::MD5 qw/md5_hex/;
11
12 my $log = 'OpenSRF::Utils::Logger';
13
14 $VERSION = 1;
15
16 sub metarecord_copy_count {
17         my $self = shift;
18         my $client = shift;
19
20         my %args = @_;
21
22         my $sm_table = metabib::metarecord_source_map->table;
23         my $cn_table = asset::call_number->table;
24         my $cp_table = asset::copy->table;
25         my $out_table = actor::org_unit_type->table;
26         my $descendants = "actor.org_unit_descendants(u.id)";
27         my $ancestors = "actor.org_unit_ancestors(?)";
28
29         my $sql = <<"   SQL";
30                 SELECT  t.depth,
31                         u.id AS org_unit,
32                         sum(
33                                 (SELECT count(cp.id)
34                                   FROM  $sm_table r
35                                         JOIN $cn_table cn ON (cn.record = r.source)
36                                         JOIN $cp_table cp ON (cn.id = cp.call_number)
37                                         JOIN $descendants a ON (cp.circ_lib = a.id)
38                                   WHERE r.metarecord = ?)
39                         ) AS count,
40                         sum(
41                                 (SELECT count(cp.id)
42                                   FROM  $sm_table r
43                                         JOIN $cn_table cn ON (cn.record = r.source)
44                                         JOIN $cp_table cp ON (cn.id = cp.call_number)
45                                         JOIN $descendants a ON (cp.circ_lib = a.id)
46                                   WHERE r.metarecord = ?
47                                         AND cp.status = 0)
48                         ) AS available
49
50                   FROM  $ancestors u
51                         JOIN $out_table t ON (u.ou_type = t.id)
52                   GROUP BY 1,2
53         SQL
54
55         my $sth = metabib::metarecord_source_map->db_Main->prepare_cached($sql);
56         $sth->execute(''.$args{metarecord}, ''.$args{metarecord}, ''.$args{org_unit});
57         while ( my $row = $sth->fetchrow_hashref ) {
58                 $client->respond( $row );
59         }
60         return undef;
61 }
62 __PACKAGE__->register_method(
63         api_name        => 'open-ils.storage.metabib.metarecord.copy_count',
64         method          => 'metarecord_copy_count',
65         api_level       => 1,
66         stream          => 1,
67         cachable        => 1,
68 );
69
70 sub search_full_rec {
71         my $self = shift;
72         my $client = shift;
73
74         my %args = @_;
75         
76         my $term = $args{term};
77         my $limiters = $args{restrict};
78
79         my ($index_col) = metabib::full_rec->columns('FTS');
80         $index_col ||= 'value';
81         my $search_table = metabib::full_rec->table;
82
83         my $fts = OpenILS::Application::Storage::FTS->compile($term, 'value',"$index_col");
84
85         my $fts_where = $fts->sql_where_clause();
86         my @fts_ranks = $fts->fts_rank;
87
88         my $rank = join(' + ', @fts_ranks);
89
90         my @binds;
91         my @wheres;
92         for my $limit (@$limiters) {
93                 push @wheres, "( tag = ? AND subfield LIKE ? AND $fts_where )";
94                 push @binds, $$limit{tag}, $$limit{subfield};
95                 $log->debug("Limiting query using { tag => $$limit{tag}, subfield => $$limit{subfield} }", DEBUG);
96         }
97         my $where = join(' OR ', @wheres);
98
99         my $select = "SELECT record, sum($rank) FROM $search_table WHERE $where GROUP BY 1 ORDER BY 2 DESC;";
100
101         $log->debug("Search SQL :: [$select]",DEBUG);
102
103         my $recs = metabib::full_rec->db_Main->selectall_arrayref($select, {}, @binds);
104         $log->debug("Search yielded ".scalar(@$recs)." results.",DEBUG);
105
106         $client->respond($_) for (@$recs);
107         return undef;
108 }
109 __PACKAGE__->register_method(
110         api_name        => 'open-ils.storage.direct.metabib.full_rec.search_fts.value',
111         method          => 'search_full_rec',
112         api_level       => 1,
113         stream          => 1,
114         cachable        => 1,
115 );
116 __PACKAGE__->register_method(
117         api_name        => 'open-ils.storage.direct.metabib.full_rec.search_fts.index_vector',
118         method          => 'search_full_rec',
119         api_level       => 1,
120         stream          => 1,
121         cachable        => 1,
122 );
123
124
125 # XXX factored most of the PG dependant stuff out of here... need to find a way to do "dependants".
126 sub search_class_fts {
127         my $self = shift;
128         my $client = shift;
129         my %args = @_;
130         
131         my $term = $args{term};
132         my $ou = $args{org_unit};
133         my $ou_type = $args{depth};
134
135
136         my $descendants = defined($ou_type) ?
137                                 "actor.org_unit_descendants($ou, $ou_type)" :
138                                 "actor.org_unit_descendants($ou)";
139
140         my $class = $self->{cdbi};
141         my $search_table = $class->table;
142
143         my $metabib_metarecord_source_map_table = metabib::metarecord_source_map->table;
144         my $asset_call_number_table = asset::call_number->table;
145
146         my ($index_col) = $class->columns('FTS');
147         $index_col ||= 'value';
148
149         my $fts = OpenILS::Application::Storage::FTS->compile($term, 'value', "$index_col");
150
151         my $fts_where = $fts->sql_where_clause;
152         my @fts_ranks = $fts->fts_rank;
153
154         my $rank = join(' + ', @fts_ranks);
155
156         my $select = <<"        SQL";
157                 SELECT  m.metarecord, sum($rank)/count(m.source)
158                   FROM  $search_table f,
159                         $metabib_metarecord_source_map_table m,
160                         $asset_call_number_table cn,
161                         $descendants d
162                   WHERE $fts_where
163                         AND m.source = f.source
164                         AND cn.record = m.source
165                         AND cn.owning_lib = d.id
166                   GROUP BY 1
167                   ORDER BY 2 DESC;
168         SQL
169
170         $log->debug("Field Search SQL :: [$select]",DEBUG);
171
172         my $recs = $class->db_Main->selectall_arrayref($select);
173         
174         $log->debug("Search yielded ".scalar(@$recs)." results.",DEBUG);
175
176         $client->respond($_) for (@$recs);
177         return undef;
178 }
179 __PACKAGE__->register_method(
180         api_name        => 'open-ils.storage.metabib.title.search_fts.metarecord',
181         method          => 'search_class_fts',
182         api_level       => 1,
183         stream          => 1,
184         cdbi            => 'metabib::title_field_entry',
185         cachable        => 1,
186 );
187 __PACKAGE__->register_method(
188         api_name        => 'open-ils.storage.metabib.author.search_fts.metarecord',
189         method          => 'search_class_fts',
190         api_level       => 1,
191         stream          => 1,
192         cdbi            => 'metabib::author_field_entry',
193         cachable        => 1,
194 );
195 __PACKAGE__->register_method(
196         api_name        => 'open-ils.storage.metabib.subject.search_fts.metarecord',
197         method          => 'search_class_fts',
198         api_level       => 1,
199         stream          => 1,
200         cdbi            => 'metabib::subject_field_entry',
201         cachable        => 1,
202 );
203 __PACKAGE__->register_method(
204         api_name        => 'open-ils.storage.metabib.keyword.search_fts.metarecord',
205         method          => 'search_class_fts',
206         api_level       => 1,
207         stream          => 1,
208         cdbi            => 'metabib::keyword_field_entry',
209         cachable        => 1,
210 );
211
212 # XXX factored most of the PG dependant stuff out of here... need to find a way to do "dependants".
213 sub search_class_fts_count {
214         my $self = shift;
215         my $client = shift;
216         my %args = @_;
217         
218         my $term = $args{term};
219         my $ou = $args{org_unit};
220         my $ou_type = $args{depth};
221         my $limit = $args{limit} || 100;
222         my $offset = $args{offset} || 0;
223
224         my $descendants = defined($ou_type) ?
225                                 "actor.org_unit_descendants($ou, $ou_type)" :
226                                 "actor.org_unit_descendants($ou)";
227                 
228
229         (my $search_class = $self->api_name) =~ s/.*metabib.(\w+).search_fts.*/$1/o;
230         my $cache_key = md5_hex($search_class.$term.$ou.$ou_type.'_COUNT_');
231
232         my $cached_recs = OpenSRF::Utils::Cache->new->get_cache( $cache_key );
233         return $cached_recs if (defined $cached_recs);
234
235         my $class = $self->{cdbi};
236         my $search_table = $class->table;
237
238         my $metabib_metarecord_source_map_table = metabib::metarecord_source_map->table;
239         my $asset_call_number_table = asset::call_number->table;
240
241         my ($index_col) = $class->columns('FTS');
242         $index_col ||= 'value';
243
244         my $fts = OpenILS::Application::Storage::FTS->compile($term, 'value',"$index_col");
245
246         my $fts_where = $fts->sql_where_clause;
247
248         # XXX test an "EXISTS version of descendant checking...
249         my $select = <<"        SQL";
250                 SELECT  count(distinct  m.metarecord)
251                   FROM  $search_table f,
252                         $metabib_metarecord_source_map_table m,
253                         $asset_call_number_table cn,
254                         $descendants d
255                   WHERE $fts_where
256                         AND m.source = f.source
257                         AND cn.record = m.source
258                         AND cn.owning_lib = d.id;
259         SQL
260
261         $log->debug("Field Search Count SQL :: [$select]",DEBUG);
262
263         my $recs = $class->db_Main->selectrow_arrayref($select)->[0];
264         
265         $log->debug("Count Search yielded $recs results.",DEBUG);
266
267         OpenSRF::Utils::Cache->new->put_cache( $cache_key => $recs );
268
269         return $recs;
270
271 }
272 __PACKAGE__->register_method(
273         api_name        => 'open-ils.storage.metabib.title.search_fts.metarecord_count',
274         method          => 'search_class_fts_count',
275         api_level       => 1,
276         stream          => 1,
277         cdbi            => 'metabib::title_field_entry',
278         cachable        => 1,
279 );
280 __PACKAGE__->register_method(
281         api_name        => 'open-ils.storage.metabib.author.search_fts.metarecord_count',
282         method          => 'search_class_fts_count',
283         api_level       => 1,
284         stream          => 1,
285         cdbi            => 'metabib::author_field_entry',
286         cachable        => 1,
287 );
288 __PACKAGE__->register_method(
289         api_name        => 'open-ils.storage.metabib.subject.search_fts.metarecord_count',
290         method          => 'search_class_fts_count',
291         api_level       => 1,
292         stream          => 1,
293         cdbi            => 'metabib::subject_field_entry',
294         cachable        => 1,
295 );
296 __PACKAGE__->register_method(
297         api_name        => 'open-ils.storage.metabib.keyword.search_fts.metarecord_count',
298         method          => 'search_class_fts_count',
299         api_level       => 1,
300         stream          => 1,
301         cdbi            => 'metabib::keyword_field_entry',
302         cachable        => 1,
303 );
304
305 1;