From c019f92b9084b05f31a3199a73880fb5a9fc7dfc Mon Sep 17 00:00:00 2001 From: "Eric J. Bowersox" Date: Sat, 14 Jun 2003 07:21:07 +0000 Subject: [PATCH] added methods and a storage object for precompiling queries --- .../dynamo/iface/IndexService.java | 9 + .../dynamo/index/CompiledQuery.java | 53 ++++++ .../dynamo/index/IndexServiceImpl.java | 179 ++++++++++-------- 3 files changed, 167 insertions(+), 74 deletions(-) create mode 100644 src/dynamo-framework/com/silverwrist/dynamo/index/CompiledQuery.java diff --git a/src/dynamo-framework/com/silverwrist/dynamo/iface/IndexService.java b/src/dynamo-framework/com/silverwrist/dynamo/iface/IndexService.java index 9abbf0f..64a6d98 100644 --- a/src/dynamo-framework/com/silverwrist/dynamo/iface/IndexService.java +++ b/src/dynamo-framework/com/silverwrist/dynamo/iface/IndexService.java @@ -20,6 +20,7 @@ package com.silverwrist.dynamo.iface; import java.util.Date; import java.util.List; import com.silverwrist.dynamo.except.IndexException; +import com.silverwrist.dynamo.index.CompiledQuery; public interface IndexService { @@ -28,10 +29,18 @@ public interface IndexService public boolean deleteItem(String item_namespace, String item_name, Object item) throws IndexException; + public CompiledQuery compileQuery(String query_string, java.util.Date date_low, java.util.Date date_high, + DynamoUser match_owner, String match_scope) throws IndexException; + public List query(String query_string, java.util.Date date_low, java.util.Date date_high, DynamoUser match_owner, String match_scope, int offset, int count) throws IndexException; + public List query(CompiledQuery query, int offset, int count) throws IndexException; + public int queryCount(String query_string, java.util.Date date_low, java.util.Date date_high, DynamoUser match_owner, String match_scope) throws IndexException; + public int queryCount(CompiledQuery query) throws IndexException; + } // end interface IndexService + diff --git a/src/dynamo-framework/com/silverwrist/dynamo/index/CompiledQuery.java b/src/dynamo-framework/com/silverwrist/dynamo/index/CompiledQuery.java new file mode 100644 index 0000000..54908b0 --- /dev/null +++ b/src/dynamo-framework/com/silverwrist/dynamo/index/CompiledQuery.java @@ -0,0 +1,53 @@ +/* + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at . + * + * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT + * WARRANTY OF ANY KIND, either express or implied. See the License for the specific + * language governing rights and limitations under the License. + * + * The Original Code is the Venice Web Communities System. + * + * The Initial Developer of the Original Code is Eric J. Bowersox , + * for Silverwrist Design Studios. Portions created by Eric J. Bowersox are + * Copyright (C) 2003 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved. + * + * Contributor(s): + */ +package com.silverwrist.dynamo.index; + +import org.apache.lucene.search.Query; + +public class CompiledQuery +{ + /*-------------------------------------------------------------------------------- + * Attributes + *-------------------------------------------------------------------------------- + */ + + private Query m_query; + + /*-------------------------------------------------------------------------------- + * Constructor + *-------------------------------------------------------------------------------- + */ + + CompiledQuery(Query query) + { + m_query = query; + + } // end constructor + + /*-------------------------------------------------------------------------------- + * External getters + *-------------------------------------------------------------------------------- + */ + + Query getQuery() + { + return m_query; + + } // end getQuery + +} // end class CompiledQuery diff --git a/src/dynamo-framework/com/silverwrist/dynamo/index/IndexServiceImpl.java b/src/dynamo-framework/com/silverwrist/dynamo/index/IndexServiceImpl.java index 6f78ba6..b108c3b 100644 --- a/src/dynamo-framework/com/silverwrist/dynamo/index/IndexServiceImpl.java +++ b/src/dynamo-framework/com/silverwrist/dynamo/index/IndexServiceImpl.java @@ -250,8 +250,8 @@ class IndexServiceImpl implements IndexService } // end createTag - private final Query compileQuery(String query_string, java.util.Date date_low, java.util.Date date_high, - DynamoUser match_owner, String match_scope) throws IndexException + private final Query compileInternal(String query_string, java.util.Date date_low, java.util.Date date_high, + DynamoUser match_owner, String match_scope) throws IndexException { ArrayList queries = new ArrayList(); if (query_string!=null) @@ -312,7 +312,89 @@ class IndexServiceImpl implements IndexService rc.add((Query)(queries.get(i)),true,false); return rc; - } // end compileQuery + } // end compileInternal + + private final List doQuery(Query query, int offset, int count) throws IndexException + { + SubsetCollector subc = new SubsetCollector(offset,count); + List rc = null; + IndexReader irdr = null; + IndexSearcher srch = null; + try + { // run that puppy! + irdr = IndexReader.open(m_directory); + srch = new IndexSearcher(irdr); + if (query==null) + visitAllDocuments(srch,subc); + else + srch.search(query,subc); + rc = subc.outputItems(irdr); + + } // end try + catch (IOException e) + { // the query failed somehow - throw an error + throw new IndexException(IndexServiceImpl.class,"IndexMessages","query.fail",e); + + } // end catch + finally + { // make sure we close down OK + try + { // close the search and index reader + if (srch!=null) + srch.close(); + if (irdr!=null) + irdr.close(); + + } // end try + catch (IOException e) + { // shouldn't happen + logger.warn("query(): error closing stuff",e); + + } // end catch + + } // end finally + + return rc; + + } // end doQuery + + private final int doQueryCount(Query query) throws IndexException + { + CountingCollector cc = new CountingCollector(); + IndexSearcher srch = null; + try + { // run that puppy! + srch = new IndexSearcher(m_directory); + if (query==null) + visitAllDocuments(srch,cc); + else + srch.search(query,cc); + + } // end try + catch (IOException e) + { // the query failed somehow - throw an error + throw new IndexException(IndexServiceImpl.class,"IndexMessages","query.fail",e); + + } // end catch + finally + { // make sure we close down OK + try + { // close the search and index reader + if (srch!=null) + srch.close(); + + } // end try + catch (IOException e) + { // shouldn't happen + logger.warn("queryCount(): error closing stuff",e); + + } // end catch + + } // end finally + + return cc.getCount(); + + } // end doQueryCount /*-------------------------------------------------------------------------------- * Implementations from interface IndexService @@ -374,90 +456,39 @@ class IndexServiceImpl implements IndexService } // end deleteItem + public CompiledQuery compileQuery(String query_string, java.util.Date date_low, java.util.Date date_high, + DynamoUser match_owner, String match_scope) throws IndexException + { + Query query = compileInternal(query_string,date_low,date_high,match_owner,match_scope); + return new CompiledQuery(query); + + } // end compileQuery + public List query(String query_string, java.util.Date date_low, java.util.Date date_high, DynamoUser match_owner, String match_scope, int offset, int count) throws IndexException { - Query query = compileQuery(query_string,date_low,date_high,match_owner,match_scope); - SubsetCollector subc = new SubsetCollector(offset,count); + Query query = compileInternal(query_string,date_low,date_high,match_owner,match_scope); + return doQuery(query,offset,count); - List rc = null; - IndexReader irdr = null; - IndexSearcher srch = null; - try - { // run that puppy! - irdr = IndexReader.open(m_directory); - srch = new IndexSearcher(irdr); - if (query==null) - visitAllDocuments(srch,subc); - else - srch.search(query,subc); - rc = subc.outputItems(irdr); + } // end query - } // end try - catch (IOException e) - { // the query failed somehow - throw an error - throw new IndexException(IndexServiceImpl.class,"IndexMessages","query.fail",e); - - } // end catch - finally - { // make sure we close down OK - try - { // close the search and index reader - if (srch!=null) - srch.close(); - if (irdr!=null) - irdr.close(); - - } // end try - catch (IOException e) - { // shouldn't happen - logger.warn("query(): error closing stuff",e); - - } // end catch - - } // end finally - - return rc; + public List query(CompiledQuery query, int offset, int count) throws IndexException + { + return doQuery(query.getQuery(),offset,count); } // end query public int queryCount(String query_string, java.util.Date date_low, java.util.Date date_high, DynamoUser match_owner, String match_scope) throws IndexException { - Query query = compileQuery(query_string,date_low,date_high,match_owner,match_scope); - CountingCollector cc = new CountingCollector(); - IndexSearcher srch = null; - try - { // run that puppy! - srch = new IndexSearcher(m_directory); - if (query==null) - visitAllDocuments(srch,cc); - else - srch.search(query,cc); + Query query = compileInternal(query_string,date_low,date_high,match_owner,match_scope); + return doQueryCount(query); - } // end try - catch (IOException e) - { // the query failed somehow - throw an error - throw new IndexException(IndexServiceImpl.class,"IndexMessages","query.fail",e); + } // end queryCount - } // end catch - finally - { // make sure we close down OK - try - { // close the search and index reader - if (srch!=null) - srch.close(); - - } // end try - catch (IOException e) - { // shouldn't happen - logger.warn("queryCount(): error closing stuff",e); - - } // end catch - - } // end finally - - return cc.getCount(); + public int queryCount(CompiledQuery query) throws IndexException + { + return doQueryCount(query.getQuery()); } // end queryCount