blob: 07a1598c5d4d89f6a718ceae0dffa3a259f2470a [file] [log] [blame]
Nils Diewaldea969502015-02-16 21:10:54 +00001package de.ids_mannheim.korap.collection;
Nils Diewaldf399a672013-11-18 17:55:22 +00002
margaretha16323812018-09-03 16:43:30 +02003import java.io.File;
4import java.io.FileInputStream;
Akron176c9b12015-07-29 19:53:40 +02005import java.io.IOException;
margaretha8efa3752018-07-24 17:46:43 +02006import java.util.ArrayList;
margarethafe252802018-07-30 14:59:50 +02007import java.util.HashMap;
margaretha8efa3752018-07-24 17:46:43 +02008import java.util.Iterator;
margarethafe252802018-07-30 14:59:50 +02009import java.util.Map;
Akronb59f40e2018-08-23 17:15:43 +020010import java.util.Properties;
Akron176c9b12015-07-29 19:53:40 +020011
margaretha16323812018-09-03 16:43:30 +020012import org.apache.commons.io.IOUtils;
Akron408ae352018-03-28 16:47:41 +020013import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
margaretha8efa3752018-07-24 17:46:43 +020014import org.apache.lucene.queries.TermsFilter;
15import org.apache.lucene.search.Filter;
16import org.apache.lucene.search.NumericRangeFilter;
17import org.apache.lucene.search.PhraseQuery;
18import org.apache.lucene.search.QueryWrapperFilter;
19import org.apache.lucene.search.RegexpQuery;
Nils Diewaldf399a672013-11-18 17:55:22 +000020import org.slf4j.Logger;
21import org.slf4j.LoggerFactory;
Nils Diewaldf399a672013-11-18 17:55:22 +000022
Akron176c9b12015-07-29 19:53:40 +020023import de.ids_mannheim.korap.KrillCollection;
margaretha8efa3752018-07-24 17:46:43 +020024import de.ids_mannheim.korap.index.TextPrependedTokenStream;
25import de.ids_mannheim.korap.util.KrillDate;
Akronb59f40e2018-08-23 17:15:43 +020026import de.ids_mannheim.korap.util.KrillProperties;
margaretha16323812018-09-03 16:43:30 +020027import de.ids_mannheim.korap.util.QueryException;
Akronb59f40e2018-08-23 17:15:43 +020028import net.sf.ehcache.Cache;
29import net.sf.ehcache.CacheManager;
30import net.sf.ehcache.Element;
31
Akron176c9b12015-07-29 19:53:40 +020032
Akronaa74ec62015-07-31 17:22:55 +020033/*
34 * TODO: Optimize!
Akron408ae352018-03-28 16:47:41 +020035 * - Remove identical object in Boolean groups
36 * - Flatten boolean groups
37 * - create "between" ranges for multiple date objects
38 *
39 * TODO:
40 * - Filters are deprecated, they should be ported to queries
Akronaa74ec62015-07-31 17:22:55 +020041 */
42
Nils Diewaldea969502015-02-16 21:10:54 +000043public class CollectionBuilder {
Nils Diewaldf399a672013-11-18 17:55:22 +000044
Akronb59f40e2018-08-23 17:15:43 +020045 public final static CacheManager cacheManager = CacheManager.newInstance();
46 public final static Cache cache = cacheManager.getCache("named_vc");
47
48
Nils Diewaldf399a672013-11-18 17:55:22 +000049 // Logger
Akron40550172015-08-04 03:06:12 +020050 private final static Logger log = LoggerFactory
51 .getLogger(KrillCollection.class);
Nils Diewaldf399a672013-11-18 17:55:22 +000052
Nils Diewaldfb4d7b02014-04-09 17:56:17 +000053 // This advices the java compiler to ignore all loggings
54 public static final boolean DEBUG = false;
Nils Diewaldbb33da22015-03-04 16:24:25 +000055
Akron40550172015-08-04 03:06:12 +020056
Akron60dfa7e2015-08-03 22:15:17 +020057 public CollectionBuilder.Interface term (String field, String term) {
58 return new CollectionBuilder.Term(field, term);
Nils Diewaldfb4d7b02014-04-09 17:56:17 +000059 };
Nils Diewaldbb33da22015-03-04 16:24:25 +000060
Akron40550172015-08-04 03:06:12 +020061
Akron60dfa7e2015-08-03 22:15:17 +020062 public CollectionBuilder.Interface re (String field, String term) {
63 return new CollectionBuilder.Term(field, term, true);
Nils Diewaldf399a672013-11-18 17:55:22 +000064 };
65
Akron40550172015-08-04 03:06:12 +020066
Akron408ae352018-03-28 16:47:41 +020067 public CollectionBuilder.Interface text (String field, String text) {
68 return new CollectionBuilder.Text(field, text);
69 };
70
71
Akron60dfa7e2015-08-03 22:15:17 +020072 public CollectionBuilder.Interface since (String field, String date) {
Akron176c9b12015-07-29 19:53:40 +020073 int since = new KrillDate(date).floor();
Nils Diewaldbb33da22015-03-04 16:24:25 +000074
Akron176c9b12015-07-29 19:53:40 +020075 if (since == 0 || since == KrillDate.BEGINNING)
76 return null;
77
Akron60dfa7e2015-08-03 22:15:17 +020078 return new CollectionBuilder.Range(field, since, KrillDate.END);
Nils Diewaldf399a672013-11-18 17:55:22 +000079 };
80
Akron5e3436f2017-07-04 15:28:03 +020081 public CollectionBuilder.Interface nothing () {
82
83 // Requires that a field with name "0---" does not exist
84 return new CollectionBuilder.Term("0---", "0");
85 };
86
Akron40550172015-08-04 03:06:12 +020087
Akron60dfa7e2015-08-03 22:15:17 +020088 public CollectionBuilder.Interface till (String field, String date) {
Akron176c9b12015-07-29 19:53:40 +020089 try {
90 int till = new KrillDate(date).ceil();
91 if (till == 0 || till == KrillDate.END)
92 return null;
Nils Diewaldbb33da22015-03-04 16:24:25 +000093
Eliza Margaretha6f989202016-10-14 21:48:29 +020094 return new CollectionBuilder.Range(field, KrillDate.BEGINNING,
95 till);
Akron176c9b12015-07-29 19:53:40 +020096 }
97 catch (NumberFormatException e) {
98 log.warn("Parameter of till(date) is invalid");
99 };
100 return null;
Nils Diewaldf399a672013-11-18 17:55:22 +0000101 };
102
Akron40550172015-08-04 03:06:12 +0200103
Akron60dfa7e2015-08-03 22:15:17 +0200104 // This will be optimized away in future versions
Akron40550172015-08-04 03:06:12 +0200105 public CollectionBuilder.Interface between (String field, String start,
106 String end) {
Akron60dfa7e2015-08-03 22:15:17 +0200107 CollectionBuilder.Interface startObj = this.since(field, start);
108 if (startObj == null)
109 return null;
110
111 CollectionBuilder.Interface endObj = this.till(field, end);
112 if (endObj == null)
113 return null;
114
115 return this.andGroup().with(startObj).with(endObj);
116 };
117
Akron40550172015-08-04 03:06:12 +0200118
Akron60dfa7e2015-08-03 22:15:17 +0200119 public CollectionBuilder.Interface date (String field, String date) {
Akron176c9b12015-07-29 19:53:40 +0200120 KrillDate dateDF = new KrillDate(date);
Nils Diewaldbb33da22015-03-04 16:24:25 +0000121
Akron176c9b12015-07-29 19:53:40 +0200122 if (dateDF.year == 0)
123 return null;
124
125 if (dateDF.day == 0 || dateDF.month == 0) {
126 int begin = dateDF.floor();
127 int end = dateDF.ceil();
128
129 if (end == 0
Akron40550172015-08-04 03:06:12 +0200130 || (begin == KrillDate.BEGINNING && end == KrillDate.END))
Akron176c9b12015-07-29 19:53:40 +0200131 return null;
132
Akron60dfa7e2015-08-03 22:15:17 +0200133 return new CollectionBuilder.Range(field, begin, end);
Akron176c9b12015-07-29 19:53:40 +0200134 };
135
Eliza Margaretha6f989202016-10-14 21:48:29 +0200136 return new CollectionBuilder.Range(field, dateDF.floor(),
137 dateDF.ceil());
Nils Diewaldf399a672013-11-18 17:55:22 +0000138 };
139
Akronb59f40e2018-08-23 17:15:43 +0200140 public CollectionBuilder.Interface referTo (String reference) {
141 return new CollectionBuilder.Reference(reference);
142 };
143
Akron40550172015-08-04 03:06:12 +0200144
Akron60dfa7e2015-08-03 22:15:17 +0200145 public CollectionBuilder.Group andGroup () {
146 return new CollectionBuilder.Group(false);
Nils Diewaldf399a672013-11-18 17:55:22 +0000147 };
148
Akron40550172015-08-04 03:06:12 +0200149
Akron60dfa7e2015-08-03 22:15:17 +0200150 public CollectionBuilder.Group orGroup () {
151 return new CollectionBuilder.Group(true);
Nils Diewaldf399a672013-11-18 17:55:22 +0000152 };
153
Akron60dfa7e2015-08-03 22:15:17 +0200154 public interface Interface {
Akron176c9b12015-07-29 19:53:40 +0200155 public String toString ();
Akron40550172015-08-04 03:06:12 +0200156
157
Akronb59f40e2018-08-23 17:15:43 +0200158 public Filter toFilter () throws QueryException;
Akron40550172015-08-04 03:06:12 +0200159
160
Akron176c9b12015-07-29 19:53:40 +0200161 public boolean isNegative ();
Akron40550172015-08-04 03:06:12 +0200162
163
Akron60dfa7e2015-08-03 22:15:17 +0200164 public CollectionBuilder.Interface not ();
Nils Diewaldbaf68c52013-11-20 13:22:19 +0000165 };
Nils Diewaldf399a672013-11-18 17:55:22 +0000166
Akron60dfa7e2015-08-03 22:15:17 +0200167 public class Term implements CollectionBuilder.Interface {
Akron176c9b12015-07-29 19:53:40 +0200168 private boolean isNegative = false;
169 private boolean regex = false;
170 private String field;
171 private String term;
Nils Diewaldbb33da22015-03-04 16:24:25 +0000172
Akron40550172015-08-04 03:06:12 +0200173
Akron60dfa7e2015-08-03 22:15:17 +0200174 public Term (String field, String term) {
Akron176c9b12015-07-29 19:53:40 +0200175 this.field = field;
176 this.term = term;
177 };
178
Akron40550172015-08-04 03:06:12 +0200179
Akron60dfa7e2015-08-03 22:15:17 +0200180 public Term (String field, String term, boolean regex) {
Akron176c9b12015-07-29 19:53:40 +0200181 this.field = field;
182 this.term = term;
183 this.regex = regex;
184 };
185
Akron40550172015-08-04 03:06:12 +0200186
Akron176c9b12015-07-29 19:53:40 +0200187 public Filter toFilter () {
188 // Regular expression
189 if (this.regex)
190 return new QueryWrapperFilter(
Akron40550172015-08-04 03:06:12 +0200191 new RegexpQuery(new org.apache.lucene.index.Term(
192 this.field, this.term)));
193
Akron176c9b12015-07-29 19:53:40 +0200194 // Simple term
Eliza Margaretha6f989202016-10-14 21:48:29 +0200195 return new TermsFilter(
196 new org.apache.lucene.index.Term(this.field, this.term));
Akron176c9b12015-07-29 19:53:40 +0200197 };
198
Akron40550172015-08-04 03:06:12 +0200199
Akron176c9b12015-07-29 19:53:40 +0200200 public String toString () {
Akron60dfa7e2015-08-03 22:15:17 +0200201 Filter filter = this.toFilter();
202 if (filter == null)
203 return "";
204 return filter.toString();
Akron176c9b12015-07-29 19:53:40 +0200205 };
206
Akron40550172015-08-04 03:06:12 +0200207
Akron176c9b12015-07-29 19:53:40 +0200208 public boolean isNegative () {
209 return this.isNegative;
210 };
211
212
Akron60dfa7e2015-08-03 22:15:17 +0200213 public CollectionBuilder.Interface not () {
Akron176c9b12015-07-29 19:53:40 +0200214 this.isNegative = true;
215 return this;
216 };
Nils Diewaldbaf68c52013-11-20 13:22:19 +0000217 };
Nils Diewaldf399a672013-11-18 17:55:22 +0000218
Akron408ae352018-03-28 16:47:41 +0200219
220 public class Text implements CollectionBuilder.Interface {
221 private boolean isNegative = false;
222 // private boolean regex = false;
223 private String field;
224 private String text;
225
226
227 public Text (String field, String text) {
228 this.field = field;
229 this.text = text;
230 };
231
232 // TODO:
233 // Currently this treatment is language specific and
Akronb59f40e2018-08-23 17:15:43 +0200234 // does too much, I guess.
Akron408ae352018-03-28 16:47:41 +0200235 public Filter toFilter () {
Akron408ae352018-03-28 16:47:41 +0200236 PhraseQuery pq = new PhraseQuery();
237 int pos = 0;
238 try {
Akron26207572018-04-04 20:21:42 +0200239 TextPrependedTokenStream tpts = new TextPrependedTokenStream(this.text);
240 tpts.doNotPrepend();
Akron408ae352018-03-28 16:47:41 +0200241 CharTermAttribute term;
Akron26207572018-04-04 20:21:42 +0200242 tpts.reset();
243 while (tpts.incrementToken()) {
244 term = tpts.getAttribute(CharTermAttribute.class);
Akron408ae352018-03-28 16:47:41 +0200245 pq.add(new org.apache.lucene.index.Term(this.field, term.toString()), pos++);
246 };
Akron26207572018-04-04 20:21:42 +0200247 tpts.close();
Akron408ae352018-03-28 16:47:41 +0200248 }
249 catch (IOException ie) {
250 System.err.println(ie);
251 return null;
252 };
Akron26207572018-04-04 20:21:42 +0200253
Akron408ae352018-03-28 16:47:41 +0200254 return new QueryWrapperFilter(pq);
255 };
256
257
258 public String toString () {
259 Filter filter = this.toFilter();
260 if (filter == null)
261 return "";
262 return filter.toString();
263 };
264
265
266 public boolean isNegative () {
267 return this.isNegative;
268 };
269
270
271 public CollectionBuilder.Interface not () {
272 this.isNegative = true;
273 return this;
274 };
275 };
276
Akronb59f40e2018-08-23 17:15:43 +0200277
278 public class Reference implements CollectionBuilder.Interface {
279 private boolean isNegative = false;
280 private String reference;
281 private Map<Integer, DocBits> docIdMap =
282 new HashMap<Integer, DocBits>();
283
284 public Reference (String reference) {
285 this.reference = reference;
286 };
287
288 public Filter toFilter () throws QueryException {
Akronb59f40e2018-08-23 17:15:43 +0200289 Element element = KrillCollection.cache.get(this.reference);
margaretha856d4502018-09-04 14:47:45 +0200290
Akronb59f40e2018-08-23 17:15:43 +0200291 if (element == null) {
margarethaf96b5032018-12-17 11:43:26 +0100292 if (DEBUG) {
293 log.debug(reference + " is NOT found in the cache");
294 }
Akronb59f40e2018-08-23 17:15:43 +0200295 KrillCollection kc = new KrillCollection();
296
Akron65d57e92018-08-24 19:25:56 +0200297 kc.fromStore(this.reference);
Akronb59f40e2018-08-23 17:15:43 +0200298
Akron65d57e92018-08-24 19:25:56 +0200299 if (kc.hasErrors()) {
300 throw new QueryException(
Akronb59f40e2018-08-23 17:15:43 +0200301 kc.getError(0).getCode(),
302 kc.getError(0).getMessage()
303 );
304 };
305
306 return new ToCacheVCFilter(
307 this.reference,
308 docIdMap,
309 kc.getBuilder(),
310 kc.toFilter()
311 );
312 }
313 else {
margarethaf96b5032018-12-17 11:43:26 +0100314 if (DEBUG) {
315 log.debug(reference + " is FOUND in the cache.");
316 }
Akronb59f40e2018-08-23 17:15:43 +0200317 CachedVCData cc = (CachedVCData) element.getObjectValue();
318 return new CachedVCFilter(this.reference, cc);
319 }
320 };
321
322
323 public String toString () {
324 return "referTo(" + this.reference + ")";
325 };
326
327
328 public boolean isNegative () {
329 return this.isNegative;
330 };
331
332
333 public CollectionBuilder.Interface not () {
334 this.isNegative = true;
335 return this;
336 };
337
338 private String loadVCFile (String ref) {
339 Properties prop = KrillProperties.loadDefaultProperties();
340 if (prop == null){
341 /*
342 this.addError(StatusCodes.MISSING_KRILL_PROPERTIES,
343 "krill.properties is not found.");
344 */
345 return null;
346 }
347
348 String namedVCPath = prop.getProperty("krill.namedVC");
349 if (!namedVCPath.endsWith("/")){
350 namedVCPath += "/";
351 }
352 File file = new File(namedVCPath+ref+".jsonld");
353
354 String json = null;
355 try {
356 FileInputStream fis = new FileInputStream(file);
357 json = IOUtils.toString(fis);
358 }
359 catch (IOException e) {
360 /*
361 this.addError(StatusCodes.MISSING_COLLECTION,
362 "Collection is not found.");
363 */
364 return null;
365 }
366 return json;
367 }
368 };
369
Akron408ae352018-03-28 16:47:41 +0200370
Akron60dfa7e2015-08-03 22:15:17 +0200371 public class Group implements CollectionBuilder.Interface {
Akron176c9b12015-07-29 19:53:40 +0200372 private boolean isOptional = false;
margaretha8a8c4272018-08-21 17:39:27 +0200373 private boolean isNegative = false;
Nils Diewaldbb33da22015-03-04 16:24:25 +0000374
Akron40550172015-08-04 03:06:12 +0200375
Akron176c9b12015-07-29 19:53:40 +0200376 public boolean isNegative () {
377 return this.isNegative;
378 };
379
margaretha8a8c4272018-08-21 17:39:27 +0200380 public void setNegative (boolean isNegative) {
381 this.isNegative = isNegative;
382 }
383
Akron176c9b12015-07-29 19:53:40 +0200384 public boolean isOptional () {
385 return this.isOptional;
386 };
387
Akron60dfa7e2015-08-03 22:15:17 +0200388 private ArrayList<CollectionBuilder.Interface> operands;
Akron176c9b12015-07-29 19:53:40 +0200389
Akron40550172015-08-04 03:06:12 +0200390
Akron60dfa7e2015-08-03 22:15:17 +0200391 public Group (boolean optional) {
Akron176c9b12015-07-29 19:53:40 +0200392 this.isOptional = optional;
Akron60dfa7e2015-08-03 22:15:17 +0200393 this.operands = new ArrayList<CollectionBuilder.Interface>(3);
Akron176c9b12015-07-29 19:53:40 +0200394 };
395
Akron40550172015-08-04 03:06:12 +0200396
Akron60dfa7e2015-08-03 22:15:17 +0200397 public Group with (CollectionBuilder.Interface cb) {
Akronfd05f502015-07-30 18:34:26 +0200398 if (cb == null)
399 return this;
400
Akron176c9b12015-07-29 19:53:40 +0200401 this.operands.add(cb);
402 return this;
403 };
404
Akron40550172015-08-04 03:06:12 +0200405
Akron60dfa7e2015-08-03 22:15:17 +0200406 public Group with (String field, String term) {
407 if (field == null || term == null)
408 return this;
409 return this.with(new CollectionBuilder.Term(field, term));
410 };
Akron176c9b12015-07-29 19:53:40 +0200411
Akron40550172015-08-04 03:06:12 +0200412
Akronb59f40e2018-08-23 17:15:43 +0200413 public Filter toFilter () throws QueryException {
Akron176c9b12015-07-29 19:53:40 +0200414 if (this.operands == null || this.operands.isEmpty())
415 return null;
416
417 if (this.operands.size() == 1)
418 return this.operands.get(0).toFilter();
419
420 // BooleanFilter bool = new BooleanFilter();
421 BooleanGroupFilter bool = new BooleanGroupFilter(this.isOptional);
422
Akron60dfa7e2015-08-03 22:15:17 +0200423 Iterator<CollectionBuilder.Interface> i = this.operands.iterator();
Akron176c9b12015-07-29 19:53:40 +0200424 while (i.hasNext()) {
Akron60dfa7e2015-08-03 22:15:17 +0200425 CollectionBuilder.Interface cb = i.next();
Akron176c9b12015-07-29 19:53:40 +0200426 if (cb.isNegative()) {
427 bool.without(cb.toFilter());
428 }
429 else {
430 bool.with(cb.toFilter());
431 };
432 };
433
434 return bool;
435 };
436
Akron40550172015-08-04 03:06:12 +0200437
Akron176c9b12015-07-29 19:53:40 +0200438 public String toString () {
Akronb59f40e2018-08-23 17:15:43 +0200439 try {
440 Filter filter = this.toFilter();
441 if (filter == null)
442 return "";
443 return filter.toString();
444 }
445 catch (QueryException qe) {
446 log.warn(qe.getLocalizedMessage());
447 };
448 return "";
Akron176c9b12015-07-29 19:53:40 +0200449 };
450
Akron40550172015-08-04 03:06:12 +0200451
Akron60dfa7e2015-08-03 22:15:17 +0200452 public CollectionBuilder.Interface not () {
Akron176c9b12015-07-29 19:53:40 +0200453 this.isNegative = true;
454 return this;
455 };
Nils Diewaldbaf68c52013-11-20 13:22:19 +0000456 };
Nils Diewaldfb4d7b02014-04-09 17:56:17 +0000457
Akron60dfa7e2015-08-03 22:15:17 +0200458 public class Range implements CollectionBuilder.Interface {
Akron176c9b12015-07-29 19:53:40 +0200459 private boolean isNegative = false;
460 private String field;
461 private int start, end;
Nils Diewaldbb33da22015-03-04 16:24:25 +0000462
Akron40550172015-08-04 03:06:12 +0200463
Akron60dfa7e2015-08-03 22:15:17 +0200464 public Range (String field, int start, int end) {
Akron176c9b12015-07-29 19:53:40 +0200465 this.field = field;
466 this.start = start;
467 this.end = end;
468 };
Nils Diewaldfb4d7b02014-04-09 17:56:17 +0000469
Akron40550172015-08-04 03:06:12 +0200470
Akron176c9b12015-07-29 19:53:40 +0200471 public boolean isNegative () {
472 return this.isNegative;
473 };
Nils Diewaldbb33da22015-03-04 16:24:25 +0000474
Akron40550172015-08-04 03:06:12 +0200475
Akron176c9b12015-07-29 19:53:40 +0200476 public String toString () {
Akron60dfa7e2015-08-03 22:15:17 +0200477 Filter filter = this.toFilter();
478 if (filter == null)
479 return "";
480 return filter.toString();
Akron176c9b12015-07-29 19:53:40 +0200481 };
Nils Diewald8db8f922014-10-24 17:43:13 +0000482
Akron40550172015-08-04 03:06:12 +0200483
Akron176c9b12015-07-29 19:53:40 +0200484 public Filter toFilter () {
Akron40550172015-08-04 03:06:12 +0200485 return NumericRangeFilter.newIntRange(this.field, this.start,
486 this.end, true, true);
Akron176c9b12015-07-29 19:53:40 +0200487 };
Nils Diewaldbb33da22015-03-04 16:24:25 +0000488
Akron40550172015-08-04 03:06:12 +0200489
Akron60dfa7e2015-08-03 22:15:17 +0200490 public CollectionBuilder.Interface not () {
Akron176c9b12015-07-29 19:53:40 +0200491 this.isNegative = true;
492 return this;
493 };
Nils Diewaldfb4d7b02014-04-09 17:56:17 +0000494 };
margaretha8efa3752018-07-24 17:46:43 +0200495
margarethadf0e9d12018-07-30 16:22:59 +0200496 /** Builder for virtual corpus / collection existing in the cache
497 *
498 * @author margaretha
499 *
500 */
margaretha85ee2ac2018-07-25 17:58:09 +0200501 public class CachedVC implements CollectionBuilder.Interface {
margaretha8efa3752018-07-24 17:46:43 +0200502
Akronb59f40e2018-08-23 17:15:43 +0200503 private String cacheKey;
margaretha85ee2ac2018-07-25 17:58:09 +0200504 private CachedVCData cachedCollection;
margaretha8efa3752018-07-24 17:46:43 +0200505 private boolean isNegative = false;
506
Akronb59f40e2018-08-23 17:15:43 +0200507 public CachedVC (String vcRef, CachedVCData cc) {
508 this.cacheKey = vcRef;
509 this.cachedCollection = cc;
margaretha8efa3752018-07-24 17:46:43 +0200510 }
511
512 @Override
513 public Filter toFilter () {
Akronb59f40e2018-08-23 17:15:43 +0200514 return new CachedVCFilter(this.cacheKey, cachedCollection);
margaretha8efa3752018-07-24 17:46:43 +0200515 }
516
517 @Override
518 public boolean isNegative () {
519 return this.isNegative;
520 }
521
522 @Override
523 public CollectionBuilder.Interface not () {
524 this.isNegative = true;
525 return this;
526 }
527
528 }
margarethafe252802018-07-30 14:59:50 +0200529
530 /** Wraps a sub CollectionBuilder.Interface to allows VC caching
531 *
532 * @author margaretha
533 *
534 */
535 public class ToCacheVC implements CollectionBuilder.Interface {
margaretha8efa3752018-07-24 17:46:43 +0200536
margarethafe252802018-07-30 14:59:50 +0200537 private CollectionBuilder.Interface child;
538 private String cacheKey;
539
margarethadf0e9d12018-07-30 16:22:59 +0200540 private Map<Integer, DocBits> docIdMap;
margarethafe252802018-07-30 14:59:50 +0200541
542 public ToCacheVC (String vcRef, Interface cbi) {
543 this.child = cbi;
544 this.cacheKey = vcRef;
margarethadf0e9d12018-07-30 16:22:59 +0200545 this.docIdMap = new HashMap<Integer, DocBits>();
margarethafe252802018-07-30 14:59:50 +0200546 }
547
548 @Override
Akronb59f40e2018-08-23 17:15:43 +0200549 public Filter toFilter () throws QueryException {
margarethafe252802018-07-30 14:59:50 +0200550 return new ToCacheVCFilter(cacheKey,docIdMap, child, child.toFilter());
551 }
552
553 @Override
554 public boolean isNegative () {
555 return child.isNegative();
556 }
557
558 @Override
559 public CollectionBuilder.Interface not () {
560 // not supported
561 return this;
562 }
563 }
Akronb59f40e2018-08-23 17:15:43 +0200564
565 // Maybe irrelevant
566 public Interface namedVC (String vcRef, CachedVCData cc) {
567 return new CollectionBuilder.CachedVC(vcRef, cc);
margaretha8efa3752018-07-24 17:46:43 +0200568 }
margarethafe252802018-07-30 14:59:50 +0200569
570 public Interface toCacheVC (String vcRef, Interface cbi) {
571 return new CollectionBuilder.ToCacheVC(vcRef, cbi);
572 }
Nils Diewaldf399a672013-11-18 17:55:22 +0000573};