VIVO-742 Create the interfaces for Application and SearchEngine

Also some stubs for unit tests.
This commit is contained in:
Jim Blake 2014-04-22 10:32:27 -04:00
parent b20445b233
commit 6329343465
16 changed files with 886 additions and 0 deletions

View file

@ -0,0 +1,29 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.application;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import edu.cornell.mannlib.vitro.webapp.modules.Application;
/**
* Tools for working with the current Application instance.
*/
public class ApplicationUtils {
private static final Log log = LogFactory.getLog(ApplicationUtils.class);
private static volatile Application instance;
public static Application instance() {
try {
instance.getClass();
return instance;
} catch (NullPointerException e) {
log.error("Called for Application before it was available", e);
throw new IllegalStateException(
"Called for Application before it was available", e);
}
}
}

View file

@ -0,0 +1,35 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.modules;
import javax.servlet.ServletContext;
import edu.cornell.mannlib.vitro.webapp.modules.searchEngine.SearchEngine;
/**
* The interface that holds the modules and extensions together.
*/
public interface Application {
ServletContext getServletContext();
SearchEngine getSearchEngine();
public interface Component {
enum LifecycleState {
NEW, ACTIVE, STOPPED
}
void startup(Application application, ComponentStartupStatus ss);
void shutdown(Application application);
}
public static interface Module extends Component {
// Nothing except lifecycle so far.
}
public static interface Extension extends Component {
// Nothing except lifecycle so far.
}
}

View file

@ -0,0 +1,21 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.modules;
/**
* A facade for the StartupStatus that already knows who the
* ServletContextListener is.
*/
public interface ComponentStartupStatus {
void info(String message);
void info(String message, Throwable cause);
void warning(String message);
void warning(String message, Throwable cause);
void fatal(String message);
void fatal(String message, Throwable cause);
}

View file

@ -0,0 +1,90 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.modules.searchEngine;
import java.util.Collection;
import edu.cornell.mannlib.vitro.webapp.modules.Application;
/**
* The principle interface for the SearchEngine. All search-related objects are
* created by these methods.
*/
public interface SearchEngine extends Application.Module {
/**
* Check to see whether the SearchEngine is alive.
*
* @throws SearchEngineException
* if the SearchEngine does not respond.
*/
void ping() throws SearchEngineException;
// ----------------------------------------------------------------------
// Indexing operations
// ----------------------------------------------------------------------
/**
* Create a SearchInputDocument that can be populated and added to the
* index.
*/
SearchInputDocument createInputDocument();
/**
* Add documents to the search index.
*/
void add(SearchInputDocument... docs) throws SearchEngineException;
/**
* Add documents to the search index.
*/
void add(Collection<SearchInputDocument> docs) throws SearchEngineException;
/**
* Explicitly commit all pending changes, and wait until they are visible to
* the search.
*/
void commit() throws SearchEngineException;
/**
* Explicitly commit all pending changes, and optionally wait until they are
* visible to the search.
*/
void commit(boolean wait) throws SearchEngineException;
/**
* Delete documents from the search index, by unique ID.
*/
void deleteById(String... ids) throws SearchEngineException;
/**
* Delete documents from the search index, by unique ID.
*/
void deleteById(Collection<String> ids) throws SearchEngineException;
/**
* Delete documents from the search index if they satisfy the query.
*/
void deleteByQuery(String query) throws SearchEngineException;
// ----------------------------------------------------------------------
// Searching operations
// ----------------------------------------------------------------------
/**
* Create a SearchQuery that can be populated and used for searching.
*/
SearchQuery createQuery();
/**
* Convenience method to create a SearchQuery and set the query text in one
* step.
*/
SearchQuery createQuery(String queryText);
/**
* Query the search index and return the results. Response is never null.
*/
SearchResponse query(SearchQuery query) throws SearchEngineException;
}

View file

@ -0,0 +1,26 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.modules.searchEngine;
/**
* Indicates a problem with a request to the SearchEngine.
*/
public class SearchEngineException extends Exception {
public SearchEngineException() {
super();
}
public SearchEngineException(String message) {
super(message);
}
public SearchEngineException(Throwable cause) {
super(cause);
}
public SearchEngineException(String message, Throwable cause) {
super(message, cause);
}
}

View file

@ -0,0 +1,39 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.modules.searchEngine;
import java.util.List;
/**
* Holds the faceting information from a query result.
*/
public interface SearchFacetField {
/**
* The name of the field that was faceted. Never null.
*/
String getName();
/**
* The different facet values. May return an empty list, but never null.
*/
List<Count> getValues();
/**
* Holds one facet from this field.
*/
public interface Count {
/**
* The value of this facet. Never null.
*/
String getName();
/**
* The number of times that the value occurs in the results.
*/
long getCount();
}
}

View file

@ -0,0 +1,68 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.modules.searchEngine;
import java.util.Collection;
import java.util.Map;
/**
* A collection of fields and values that will be used to build a record in the
* search index.
*/
public interface SearchInputDocument {
/**
* Create a field that can be populated and added to the document.
*/
SearchInputField createField(String name);
/**
* Put the field into the document. If a field with this name already exists
* in the document, it will be replaced.
*/
void addField(SearchInputField field);
/**
* Create a field with this name and values, and put it into the document. If
* a field with this name already exists in the document, it will be
* replaced.
*/
void addField(String name, Object... values);
/**
* Create a field with this name and values, and put it into the document. If
* a field with this name already exists in the document, it will be
* replaced.
*/
void addField(String name, Collection<Object> values);
/**
* Create a field with this name, boost level and values, and put it into
* the document. If a field with this name already exists in the document,
* it will be replaced.
*/
void addField(String name, float boost, Object... values);
/**
* Create a field with this name, boost level and values, and put it into
* the document. If a field with this name already exists in the document,
* it will be replaced.
*/
void addField(String name, float boost, Collection<Object> values);
/**
* Set a boost level for the document as a whole.
*/
void setDocumentBoost(float searchBoost);
float getDocumentBoost();
/**
* May return null.
*/
SearchInputField getField(String name);
/**
* May return an empty map, but never null.
*/
Map<String, SearchInputField> getFieldMap();
}

View file

@ -0,0 +1,42 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.modules.searchEngine;
import java.util.Collection;
/**
* A named field with a name and one or more values. This can be added to a
* SearchInputDocument and inserted into the search index.
*/
public interface SearchInputField {
/**
* Add values to this field.
*/
void addValues(Object... values);
/**
* Add values to this field.
*/
void addValues(Collection<? extends Object> values);
/**
* Set the boost level for this field.
*/
void setBoost(float boost);
String getName();
float getBoost();
/**
* May return an empty collection, but never null.
*/
Collection<Object> getValues();
/**
* May return null.
*/
Object getFirstValue();
}

View file

@ -0,0 +1,137 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.modules.searchEngine;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* A collection of search terms that will be used to query the search index.
*
* Each of the "set" and "add" methods will return the searchQuery itself, so
* calls can easily be chained together.
*/
public interface SearchQuery {
public enum Order {
ASC, DESC
}
/**
* Set the text of the query. This will be parsed using Lucene query syntax.
*/
SearchQuery setQuery(String query);
/**
* Where in the ordered list of result documents should the response begin?
* That is, how many of the results should be skipped? (allows paging of
* results)
*/
SearchQuery setStart(int start);
/**
* What is the maximum number of documents that will be returned from the
* query?
*/
SearchQuery setRows(int rows);
/**
* Which fields should be returned from the query?
*/
SearchQuery addFields(String... names);
/**
* Which fields should be returned from the query?
*/
SearchQuery addFields(Collection<String> names);
/**
* What field should be used to sort the results, and in what order?
*/
SearchQuery addSortField(String name, Order order);
/**
* Restrict the results by thisw query.
*/
SearchQuery addFilterQuery(String filterQuery);
/**
* Restrict the results by these queries.
*/
SearchQuery addFilterQueries(String... filterQueries);
/**
* Should the results be faceted?
*/
SearchQuery setFaceting(boolean b);
/**
* What fields should be used to facet the results?
*/
SearchQuery addFacetFields(String... fields);
/**
* Add queries that can be used to facet the results.
*/
SearchQuery addFacetQueries(String... queries);
/**
* Facet having fewer hits will be excluded from the list.
*/
SearchQuery setFacetMinCount(int cnt);
/**
* Add a system-dependent parameter to the query.
*/
SearchQuery addParameter(String name, String... values);
/**
* Get the text of the query.
*/
String getQuery();
int getStart();
/**
* A value of -1 means that no limit has been specified.
*/
int getRows();
/**
* May return an empty set, but never null.
*/
Set<String> getFieldsToReturn();
/**
* May return an empty map, but never null.
*/
Map<String, SearchQuery.Order> getSortFields();
/**
* May return an empty set, but never null.
*/
Set<String> getFilters();
boolean isFaceting();
/**
* May return an empty set, but never null.
*/
Set<String> getFacetFields();
/**
* May return an empty set, but never null.
*/
Set<String> getFacetQueries();
/**
* A value of -1 means that no limit has been specified.
*/
int getFacetMinCount();
/**
* May return an empty map, but never null.
*/
Map<String, List<String>> getParameterMap();
}

View file

@ -0,0 +1,34 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.modules.searchEngine;
import java.util.List;
import java.util.Map;
/**
* The response to a query against the search index. It includes a list of the
* results, as well as an optional collection of facet fields.
*/
public interface SearchResponse {
/**
* May return an empty list, but never null.
*/
SearchResultDocumentList getResults();
/**
* May return an empty map, but never null.
*/
Map<String, Map<String, List<String>>> getHighlighting();
/**
* May return null.
*/
SearchFacetField getFacetField(String name);
/**
* May return an empty list, but never null.
*/
List<SearchFacetField> getFacetFields();
}

View file

@ -0,0 +1,47 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.modules.searchEngine;
import java.util.Collection;
import java.util.Map;
/**
* The concrete representation of a document in the search index. Obtained in
* response to a query.
*/
public interface SearchResultDocument {
/**
* A document identifier that can be used in SearchEngine.deleteById();
* Never null.
*/
public String getUniqueId();
/**
* May return an empty collection, but never null.
*/
public Collection<String> getFieldNames();
/**
* May return null.
*/
public Object getFirstValue(String name);
/**
* Gets the first value for the named field, and converts it to a String.
* May return null.
*/
public String getStringValue(String name);
/**
* Get the values for the named field. May return an empty collection, but
* never null.
*/
public Collection<Object> getFieldValues(String name);
/**
* May return an empty map, but never null. The values collection for any
* key may be empty, but never null.
*/
public Map<String, Collection<Object>> getFieldValuesMap();
}

View file

@ -0,0 +1,23 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.modules.searchEngine;
/**
* A collection of results that are returned from a query.
*/
public interface SearchResultDocumentList extends
Iterable<SearchResultDocument> {
/**
* The number of documents that would satisfy the query
*/
long getNumFound();
/**
* The number of documents that are included in this result.
*/
int size();
SearchResultDocument get(int i);
}

View file

@ -0,0 +1,61 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package stubs.edu.cornell.mannlib.vitro.webapp.modules;
import java.lang.reflect.Field;
import javax.servlet.ServletContext;
import edu.cornell.mannlib.vitro.webapp.application.ApplicationUtils;
import edu.cornell.mannlib.vitro.webapp.modules.Application;
import edu.cornell.mannlib.vitro.webapp.modules.searchEngine.SearchEngine;
/**
* TODO
*/
public class ApplicationStub implements Application {
/**
* Create an ApplicationStub and set it as the instance in ApplicationUtils.
*/
public static void setup(ServletContext ctx, SearchEngine searchEngine) {
ApplicationStub instance = new ApplicationStub(ctx, searchEngine);
try {
Field instanceField = ApplicationUtils.class
.getDeclaredField("instance");
instanceField.setAccessible(true);
instanceField.set(null, instance);
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
// ----------------------------------------------------------------------
// Stub infrastructure
// ----------------------------------------------------------------------
private final ServletContext ctx;
private final SearchEngine searchEngine;
public ApplicationStub(ServletContext ctx, SearchEngine searchEngine) {
this.ctx = ctx;
this.searchEngine = searchEngine;
}
// ----------------------------------------------------------------------
// Stub methods
// ----------------------------------------------------------------------
@Override
public ServletContext getServletContext() {
return ctx;
}
@Override
public SearchEngine getSearchEngine() {
return searchEngine;
}
// ----------------------------------------------------------------------
// Un-implemented methods
// ----------------------------------------------------------------------
}

View file

@ -0,0 +1,136 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package stubs.edu.cornell.mannlib.vitro.webapp.modules.searchEngine;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import edu.cornell.mannlib.vitro.webapp.modules.Application;
import edu.cornell.mannlib.vitro.webapp.modules.ComponentStartupStatus;
import edu.cornell.mannlib.vitro.webapp.modules.searchEngine.SearchEngine;
import edu.cornell.mannlib.vitro.webapp.modules.searchEngine.SearchEngineException;
import edu.cornell.mannlib.vitro.webapp.modules.searchEngine.SearchInputDocument;
import edu.cornell.mannlib.vitro.webapp.modules.searchEngine.SearchQuery;
import edu.cornell.mannlib.vitro.webapp.modules.searchEngine.SearchResponse;
/**
* TODO
*/
public class SearchEngineStub implements SearchEngine {
// ----------------------------------------------------------------------
// Stub infrastructure
// ----------------------------------------------------------------------
Map<String, SearchResponseStub> queryResponses = new HashMap<>();
public void setQueryResponse(String queryText, SearchResponseStub response) {
queryResponses.put(queryText, response);
}
// ----------------------------------------------------------------------
// Stub methods
// ----------------------------------------------------------------------
@Override
public SearchQuery createQuery() {
return new SearchQueryStub();
}
@Override
public SearchQuery createQuery(String queryText) {
return new SearchQueryStub(queryText);
}
@Override
public SearchResponse query(SearchQuery query) throws SearchEngineException {
SearchResponseStub response = queryResponses.get(query.getQuery());
if (response == null) {
return SearchResponseStub.EMPTY_RESPONSE;
} else {
return response;
}
}
// ----------------------------------------------------------------------
// Un-implemented methods
// ----------------------------------------------------------------------
@Override
public void startup(Application application, ComponentStartupStatus ss) {
// TODO Auto-generated method stub
throw new RuntimeException(
"SearchEngineStub.startup() not implemented.");
}
@Override
public void shutdown(Application application) {
// TODO Auto-generated method stub
throw new RuntimeException(
"SearchEngineStub.shutdown() not implemented.");
}
@Override
public void ping() throws SearchEngineException {
// TODO Auto-generated method stub
throw new RuntimeException("SearchEngineStub.ping() not implemented.");
}
@Override
public SearchInputDocument createInputDocument() {
// TODO Auto-generated method stub
throw new RuntimeException(
"SearchEngineStub.createInputDocument() not implemented.");
}
@Override
public void add(SearchInputDocument... docs) throws SearchEngineException {
// TODO Auto-generated method stub
throw new RuntimeException("SearchEngineStub.add() not implemented.");
}
@Override
public void add(Collection<SearchInputDocument> docs)
throws SearchEngineException {
// TODO Auto-generated method stub
throw new RuntimeException("SearchEngineStub.add() not implemented.");
}
@Override
public void commit() throws SearchEngineException {
// TODO Auto-generated method stub
throw new RuntimeException("SearchEngineStub.commit() not implemented.");
}
@Override
public void commit(boolean wait) throws SearchEngineException {
// TODO Auto-generated method stub
throw new RuntimeException("SearchEngineStub.commit() not implemented.");
}
@Override
public void deleteById(String... ids) throws SearchEngineException {
// TODO Auto-generated method stub
throw new RuntimeException(
"SearchEngineStub.deleteById() not implemented.");
}
@Override
public void deleteById(Collection<String> ids) throws SearchEngineException {
// TODO Auto-generated method stub
throw new RuntimeException(
"SearchEngineStub.deleteById() not implemented.");
}
@Override
public void deleteByQuery(String query) throws SearchEngineException {
// TODO Auto-generated method stub
throw new RuntimeException(
"SearchEngineStub.deleteByQuery() not implemented.");
}
}

View file

@ -0,0 +1,38 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package stubs.edu.cornell.mannlib.vitro.webapp.modules.searchEngine;
import edu.cornell.mannlib.vitro.webapp.searchengine.base.BaseSearchQuery;
/**
* TODO
*/
public class SearchQueryStub extends BaseSearchQuery {
// ----------------------------------------------------------------------
// Stub infrastructure
// ----------------------------------------------------------------------
/**
* @param queryText
*/
public SearchQueryStub() {
super();
}
/**
* @param queryText
*/
public SearchQueryStub(String queryText) {
super();
setQuery(queryText);
}
// ----------------------------------------------------------------------
// Stub methods
// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
// Un-implemented methods
// ----------------------------------------------------------------------
}

View file

@ -0,0 +1,60 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package stubs.edu.cornell.mannlib.vitro.webapp.modules.searchEngine;
import java.util.List;
import java.util.Map;
import edu.cornell.mannlib.vitro.webapp.modules.searchEngine.SearchFacetField;
import edu.cornell.mannlib.vitro.webapp.modules.searchEngine.SearchResponse;
import edu.cornell.mannlib.vitro.webapp.modules.searchEngine.SearchResultDocumentList;
/**
* TODO
*/
public class SearchResponseStub implements SearchResponse {
// ----------------------------------------------------------------------
// Stub infrastructure
// ----------------------------------------------------------------------
public static final SearchResponseStub EMPTY_RESPONSE = null;
// ----------------------------------------------------------------------
// Stub methods
// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
// Un-implemented methods
// ----------------------------------------------------------------------
@Override
public SearchResultDocumentList getResults() {
// TODO Auto-generated method stub
throw new RuntimeException(
"SearchResponseStub.getResults() not implemented.");
}
@Override
public Map<String, Map<String, List<String>>> getHighlighting() {
// TODO Auto-generated method stub
throw new RuntimeException(
"SearchResponseStub.getHighlighting() not implemented.");
}
@Override
public SearchFacetField getFacetField(String name) {
// TODO Auto-generated method stub
throw new RuntimeException(
"SearchResponseStub.getFacetField() not implemented.");
}
@Override
public List<SearchFacetField> getFacetFields() {
// TODO Auto-generated method stub
throw new RuntimeException(
"SearchResponseStub.getFacetFields() not implemented.");
}
}