Skip to content

Commit

Permalink
[ issue #41 ] Cache language term queries
Browse files Browse the repository at this point in the history
  • Loading branch information
agazzarini committed Mar 16, 2015
1 parent 749a01a commit 7249cc1
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions solrdf/src/main/java/org/gazzax/labs/solrdf/graph/SolRDFGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import static org.gazzax.labs.solrdf.NTriples.asNtURI;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.lucene.index.Term;
import org.apache.lucene.search.MatchAllDocsQuery;
Expand Down Expand Up @@ -48,9 +50,9 @@
*/
public final class SolRDFGraph extends GraphBase {
static final int DEFAULT_QUERY_FETCH_SIZE = 1000;
private final static Map<String, TermQuery> languageTermQueries = new HashMap<String, TermQuery>();

private FieldInjectorRegistry registry = new FieldInjectorRegistry();

final UpdateRequestProcessor updateProcessor;
final AddUpdateCommand updateCommand;
final SolrQueryRequest request;
Expand All @@ -63,7 +65,7 @@ public final class SolRDFGraph extends GraphBase {

final int queryFetchSize;

final GraphEventConsumer listener;
final GraphEventConsumer consumer;

/**
* Creates a Read / Write {@link Graph}.
Expand All @@ -72,15 +74,16 @@ public final class SolRDFGraph extends GraphBase {
* @param request the current Solr request.
* @param response the current Solr response.
* @param qParser the query parser associated with the current request.
* @param consumer the Graph event consumer that will be notified on relevant events.
* @return a RW {@link Graph} that can be used both for adding and querying data.
*/
public static SolRDFGraph readableAndWritableGraph(
final Node graphNode,
final SolrQueryRequest request,
final SolrQueryResponse response,
final QParser qParser,
final GraphEventConsumer listener) {
return new SolRDFGraph(graphNode, request, response, qParser, DEFAULT_QUERY_FETCH_SIZE, listener);
final GraphEventConsumer consumer) {
return new SolRDFGraph(graphNode, request, response, qParser, DEFAULT_QUERY_FETCH_SIZE, consumer);
}

/**
Expand All @@ -91,6 +94,7 @@ public static SolRDFGraph readableAndWritableGraph(
* @param response the current Solr response.
* @param qParser the query parser associated with the current request.
* @param fetchSize the read fetch size.
* @param consumer the Graph event consumer that will be notified on relevant events.
* @return a RW {@link Graph} that can be used both for adding and querying data.
*/
public static SolRDFGraph readableAndWritableGraph(
Expand All @@ -99,8 +103,8 @@ public static SolRDFGraph readableAndWritableGraph(
final SolrQueryResponse response,
final QParser qParser,
final int fetchSize,
final GraphEventConsumer listener) {
return new SolRDFGraph(graphNode, request, response, qParser, fetchSize, listener);
final GraphEventConsumer consumer) {
return new SolRDFGraph(graphNode, request, response, qParser, fetchSize, consumer);
}

/**
Expand All @@ -111,14 +115,15 @@ public static SolRDFGraph readableAndWritableGraph(
* @param response the Solr query response.
* @param qparser the query parser.
* @param fetchSize the fetch size that will be used in reads.
* @param consumer the Graph event consumer that will be notified on relevant events.
*/
private SolRDFGraph(
final Node graphNode,
final SolrQueryRequest request,
final SolrQueryResponse response,
final QParser qparser,
final int fetchSize,
final GraphEventConsumer listener) {
final GraphEventConsumer consumer) {
this.graphNode = graphNode;
this.graphNodeStringified = (graphNode != null) ? asNtURI(graphNode) : null;
this.request = request;
Expand All @@ -128,7 +133,7 @@ private SolRDFGraph(
this.searcher = request.getSearcher();
this.qParser = qparser;
this.queryFetchSize = fetchSize;
this.listener = listener;
this.consumer = consumer;
}

@Override
Expand Down Expand Up @@ -259,7 +264,7 @@ Iterator<Triple> query(final TripleMatch pattern) throws SyntaxError {
if (o.isLiteral()) {
final String language = o.getLiteralLanguage();
if (Strings.isNotNullOrEmptyString(language)) {
filters.add(new TermQuery(new Term(Field.LANG, language)));
filters.add(languageTermQuery(language));
}

final String literalValue = o.getLiteralLexicalForm();
Expand All @@ -276,7 +281,7 @@ Iterator<Triple> query(final TripleMatch pattern) throws SyntaxError {

cmd.setFilterList(filters);

return new DeepPagingIterator(searcher, cmd, sortSpec, listener);
return new DeepPagingIterator(searcher, cmd, sortSpec, consumer);
}

/**
Expand Down Expand Up @@ -329,4 +334,21 @@ String deleteQuery(final Triple triple) {

return builder.toString();
}

/**
* Returns a language {@link TermQuery} from the cache.
* If the cache doesn't contain a query for a specific language, it
* will be created, cached and returned.
*
* @param language the language.
* @return a language {@link TermQuery} from the cache.
*/
TermQuery languageTermQuery(final String language) {
TermQuery query = languageTermQueries.get(language);
if (query == null) {
query = new TermQuery(new Term(Field.LANG, language));
languageTermQueries.put(language, query);
}
return query;
}
}

0 comments on commit 7249cc1

Please sign in to comment.