01: package org.apache.lucene.queryParser.surround.query;
02:
03: /**
04: * Licensed to the Apache Software Foundation (ASF) under one or more
05: * contributor license agreements. See the NOTICE file distributed with
06: * this work for additional information regarding copyright ownership.
07: * The ASF licenses this file to You under the Apache License, Version 2.0
08: * (the "License"); you may not use this file except in compliance with
09: * the License. You may obtain a copy of the License at
10: *
11: * http://www.apache.org/licenses/LICENSE-2.0
12: *
13: * Unless required by applicable law or agreed to in writing, software
14: * distributed under the License is distributed on an "AS IS" BASIS,
15: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16: * See the License for the specific language governing permissions and
17: * limitations under the License.
18: */
19:
20: /* Create basic queries to be used during rewrite.
21: * The basic queries are TermQuery and SpanTermQuery.
22: * An exception can be thrown when too many of these are used.
23: * SpanTermQuery and TermQuery use IndexReader.termEnum(Term), which causes the buffer usage.
24: *
25: * Use this class to limit the buffer usage for reading terms from an index.
26: * Default is 1024, the same as the max. number of subqueries for a BooleanQuery.
27: */
28:
29: import org.apache.lucene.index.Term;
30: import org.apache.lucene.search.TermQuery;
31: import org.apache.lucene.search.spans.SpanTermQuery;
32:
33: public class BasicQueryFactory {
34: public BasicQueryFactory(int maxBasicQueries) {
35: this .maxBasicQueries = maxBasicQueries;
36: this .queriesMade = 0;
37: }
38:
39: public BasicQueryFactory() {
40: this (1024);
41: }
42:
43: private int maxBasicQueries;
44: private int queriesMade;
45:
46: public int getNrQueriesMade() {
47: return queriesMade;
48: }
49:
50: public int getMaxBasicQueries() {
51: return maxBasicQueries;
52: }
53:
54: private synchronized void checkMax() throws TooManyBasicQueries {
55: if (queriesMade >= maxBasicQueries)
56: throw new TooManyBasicQueries(getMaxBasicQueries());
57: queriesMade++;
58: }
59:
60: public TermQuery newTermQuery(Term term) throws TooManyBasicQueries {
61: checkMax();
62: return new TermQuery(term);
63: }
64:
65: public SpanTermQuery newSpanTermQuery(Term term)
66: throws TooManyBasicQueries {
67: checkMax();
68: return new SpanTermQuery(term);
69: }
70: }
|