You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by an...@apache.org on 2015/05/28 21:35:12 UTC
[5/7] jena git commit: Tidy code
Tidy code
Project: http://git-wip-us.apache.org/repos/asf/jena/repo
Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/10df2c34
Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/10df2c34
Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/10df2c34
Branch: refs/heads/master
Commit: 10df2c34904a024e3af0748f1af70d40d9fe18d0
Parents: 7f806f7
Author: Andy Seaborne <an...@apache.org>
Authored: Thu May 28 20:20:38 2015 +0100
Committer: Andy Seaborne <an...@apache.org>
Committed: Thu May 28 20:20:38 2015 +0100
----------------------------------------------------------------------
.../apache/jena/query/text/TextIndexSolr.java | 125 +++++++++----------
.../org/apache/jena/query/text/TextQueryPF.java | 68 ++++++----
2 files changed, 101 insertions(+), 92 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/jena/blob/10df2c34/jena-text/src/main/java/org/apache/jena/query/text/TextIndexSolr.java
----------------------------------------------------------------------
diff --git a/jena-text/src/main/java/org/apache/jena/query/text/TextIndexSolr.java b/jena-text/src/main/java/org/apache/jena/query/text/TextIndexSolr.java
index 54a3263..718d4c7 100644
--- a/jena-text/src/main/java/org/apache/jena/query/text/TextIndexSolr.java
+++ b/jena-text/src/main/java/org/apache/jena/query/text/TextIndexSolr.java
@@ -47,71 +47,69 @@ public class TextIndexSolr implements TextIndex
private final EntityDefinition docDef ;
private static final int MAX_N = 10000 ;
- public TextIndexSolr(SolrServer server, EntityDefinition def)
- {
+ public TextIndexSolr(SolrServer server, EntityDefinition def) {
this.solrServer = server ;
this.docDef = def ;
}
@Override
public void updateEntity(Entity entity) {
- throw new RuntimeException("TextIndexSolr.updateEntity not implemented.");
+ throw new RuntimeException("TextIndexSolr.updateEntity not implemented.") ;
}
@Override
- public void prepareCommit() { }
+ public void prepareCommit() {}
@Override
public void commit() {
try {
- solrServer.commit();
+ solrServer.commit() ;
}
catch (SolrServerException e) {
- throw new TextIndexException(e);
+ throw new TextIndexException(e) ;
}
catch (IOException e) {
- throw new TextIndexException(e);
+ throw new TextIndexException(e) ;
}
}
@Override
public void rollback() {
try {
- solrServer.rollback();
+ solrServer.rollback() ;
}
catch (SolrServerException e) {
- throw new TextIndexException(e);
+ throw new TextIndexException(e) ;
}
catch (IOException e) {
- throw new TextIndexException(e);
+ throw new TextIndexException(e) ;
}
}
@Override
- public void close()
- {
- if ( solrServer != null )
+ public void close() {
+ if ( solrServer != null )
solrServer.shutdown() ;
}
@Override
- public void addEntity(Entity entity)
- {
- //log.info("Add entity: "+entity) ;
+ public void addEntity(Entity entity) {
+ // log.info("Add entity: "+entity) ;
try {
SolrInputDocument doc = solrDoc(entity) ;
solrServer.add(doc) ;
- } catch (Exception e) { exception(e) ; }
+ }
+ catch (Exception e) {
+ exception(e) ;
+ }
}
- private SolrInputDocument solrDoc(Entity entity)
- {
+ private SolrInputDocument solrDoc(Entity entity) {
SolrInputDocument doc = new SolrInputDocument() ;
doc.addField(docDef.getEntityField(), entity.getId()) ;
String graphField = docDef.getGraphField() ;
- if ( graphField != null )
- {
+ if ( graphField != null ) {
doc.addField(graphField, entity.getGraph()) ;
}
@@ -119,53 +117,50 @@ public class TextIndexSolr implements TextIndex
// otherwise, if we have multiple fields, each successive
// addition will replace the previous one and we are left
// with only the last field indexed.
- // see http://stackoverflow.com/questions/12183798/solrj-api-for-partial-document-update
- // and https://svn.apache.org/repos/asf/lucene/dev/trunk/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleTests.java
- HashMap<String,Object> map = new HashMap<>();
+ // see
+ // http://stackoverflow.com/questions/12183798/solrj-api-for-partial-document-update
+ // and
+ // https://svn.apache.org/repos/asf/lucene/dev/trunk/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleTests.java
+ HashMap<String, Object> map = new HashMap<>() ;
for ( Entry<String, Object> e : entity.getMap().entrySet() ) {
- map.put("add", e.getValue());
+ map.put("add", e.getValue()) ;
doc.addField(e.getKey(), map) ;
}
return doc ;
}
@Override
- public Map<String, Node> get(String uri)
- {
+ public Map<String, Node> get(String uri) {
String escaped = ClientUtils.escapeQueryChars(uri) ;
- String qs = docDef.getEntityField()+":"+escaped ;
- SolrDocumentList solrResults = solrQuery(qs,1) ;
+ String qs = docDef.getEntityField() + ":" + escaped ;
+ SolrDocumentList solrResults = solrQuery(qs, 1) ;
List<Map<String, Node>> records = process(solrResults) ;
if ( records.size() == 0 )
return null ;
if ( records.size() > 1 )
- log.warn("Multiple docs for one URI: "+uri) ;
+ log.warn("Multiple docs for one URI: " + uri) ;
return records.get(0) ;
}
- private List<Map<String, Node>> process(SolrDocumentList solrResults)
- {
+ private List<Map<String, Node>> process(SolrDocumentList solrResults) {
List<Map<String, Node>> records = new ArrayList<>() ;
- for ( SolrDocument sd : solrResults )
- {
+ for ( SolrDocument sd : solrResults ) {
Map<String, Node> record = new HashMap<>() ;
String uriStr = (String)sd.getFieldValue(docDef.getEntityField()) ;
Node entity = NodeFactory.createURI(uriStr) ;
record.put(docDef.getEntityField(), entity) ;
- for ( String f : docDef.fields() )
- {
- //log.info("Field: "+f) ;
+ for ( String f : docDef.fields() ) {
+ // log.info("Field: "+f) ;
Object obj = sd.getFieldValue(f) ;
- //log.info("Value: "+obj) ;
+ // log.info("Value: "+obj) ;
if ( obj == null )
continue ;
// Multivalued -> array.
- // Null means "not stored" or "not present"
- if ( obj instanceof List<?> )
- {
+ // Null means "not stored" or "not present"
+ if ( obj instanceof List<? > ) {
@SuppressWarnings("unchecked")
List<String> vals = (List<String>)obj ;
continue ;
@@ -176,7 +171,7 @@ public class TextIndexSolr implements TextIndex
record.put(f, n) ;
}
- //log.info("Entity: "+uriStr) ;
+ // log.info("Entity: "+uriStr) ;
records.add(record) ;
}
return records ;
@@ -186,17 +181,15 @@ public class TextIndexSolr implements TextIndex
public List<Node> query(String qs) { return query(qs, 0) ; }
@Override
- public List<Node> query(String qs, int limit)
- {
+ public List<Node> query(String qs, int limit) {
SolrDocumentList solrResults = solrQuery(qs, limit) ;
List<Node> results = new ArrayList<>() ;
- for ( SolrDocument sd : solrResults )
- {
- //** score
+ for ( SolrDocument sd : solrResults ) {
+ // ** score
// 'score' field.
String str = (String)sd.getFieldValue(docDef.getEntityField()) ;
- //log.info("Entity: "+uriStr) ;
+ // log.info("Entity: "+uriStr) ;
Node n = TextQueryFuncs.stringToNode(str) ;
results.add(n) ;
}
@@ -204,43 +197,45 @@ public class TextIndexSolr implements TextIndex
if ( limit > 0 && results.size() > limit )
results = results.subList(0, limit) ;
- return results ;
+ return results ;
}
- private SolrDocumentList solrQuery(String qs, int limit)
- {
+ private SolrDocumentList solrQuery(String qs, int limit) {
SolrQuery sq = new SolrQuery(qs) ;
- //** score
- //sq.setIncludeScore(true) ;
+ // ** score
+ // sq.setIncludeScore(true) ;
if ( limit > 0 )
sq.setRows(limit) ;
- else
- sq.setRows(MAX_N) ; // The Solr default is 10.
+ else
+ sq.setRows(MAX_N) ; // The Solr default is 10.
try {
// Set default field.
sq.add(CommonParams.DF, docDef.getPrimaryField()) ;
- QueryResponse rsp = solrServer.query( sq ) ;
- SolrDocumentList docs = rsp.getResults();
+ QueryResponse rsp = solrServer.query(sq) ;
+ SolrDocumentList docs = rsp.getResults() ;
return docs ;
- } catch (SolrServerException e) { exception(e) ; return null ; }
+ }
+ catch (SolrServerException e) {
+ exception(e) ;
+ return null ;
+ }
}
@Override
- public EntityDefinition getDocDef()
- {
+ public EntityDefinition getDocDef() {
return docDef ;
}
- private Node entryToNode(String v)
- {
+ private Node entryToNode(String v) {
// TEMP
return NodeFactoryExtra.createLiteralNode(v, null, null) ;
}
- public SolrServer getServer() { return solrServer ; }
+ public SolrServer getServer() {
+ return solrServer ;
+ }
- private static Void exception(Exception ex)
- {
+ private static Void exception(Exception ex) {
throw new TextIndexException(ex) ;
}
}
http://git-wip-us.apache.org/repos/asf/jena/blob/10df2c34/jena-text/src/main/java/org/apache/jena/query/text/TextQueryPF.java
----------------------------------------------------------------------
diff --git a/jena-text/src/main/java/org/apache/jena/query/text/TextQueryPF.java b/jena-text/src/main/java/org/apache/jena/query/text/TextQueryPF.java
index f74db79..5a48add 100644
--- a/jena-text/src/main/java/org/apache/jena/query/text/TextQueryPF.java
+++ b/jena-text/src/main/java/org/apache/jena/query/text/TextQueryPF.java
@@ -52,7 +52,7 @@ public class TextQueryPF extends PropertyFunctionBase {
* Look for "//** score" in TextQueryPF, TextIndexLucene and TextIndexSolr
*/
- private TextIndex server = null ;
+ private TextIndex textIndex = null ;
private boolean warningIssued = false ;
public TextQueryPF() {}
@@ -65,25 +65,27 @@ public class TextQueryPF extends PropertyFunctionBase {
//** score
// Subject possibilities become ?foo or (?foo ?score)
DatasetGraph dsg = execCxt.getDataset() ;
- server = chooseTextIndex(dsg) ;
-
+ textIndex = chooseTextIndex(dsg) ;
if (!argSubject.isNode())
throw new QueryBuildException("Subject is not a single node: " + argSubject) ;
if (argObject.isList()) {
- //extract of extra lang arg if present and if is usable.
- //arg is removed from the list to avoid conflict with order and args length
- langArg = extractArg("lang", argObject);
-
- if (langArg != null && server.getDocDef().getLangField() == null)
- log.warn("lang argument is ignored if langField not set in the index configuration");
-
List<Node> list = argObject.getArgList() ;
+
if (list.size() == 0)
throw new QueryBuildException("Zero-length argument list") ;
if (list.size() > 4)
throw new QueryBuildException("Too many arguments in list : " + list) ;
+
+
+ //extract of extra lang arg if present and if is usable.
+ //arg is removed from the list to avoid conflict with order and args length
+ langArg = extractArg("lang", list);
+
+ if (langArg != null && textIndex.getDocDef().getLangField() == null)
+ log.warn("lang argument is ignored if langField not set in the index configuration");
+
}
}
@@ -109,10 +111,10 @@ public class TextQueryPF extends PropertyFunctionBase {
return null ;
}
- private String extractArg(String prefix, PropFuncArg argObject) {
+ private String extractArg(String prefix, List<Node> objArgs) {
String value = null;
int pos = 0;
- for (Node node : argObject.getArgList()) {
+ for (Node node : objArgs) {
if (node.isLiteral()) {
String arg = node.getLiteral().toString();
if (arg.startsWith(prefix + ":")) {
@@ -123,7 +125,7 @@ public class TextQueryPF extends PropertyFunctionBase {
pos++;
}
if (value != null)
- argObject.getArgList().remove(pos);
+ objArgs.remove(pos);
return value;
}
@@ -131,7 +133,7 @@ public class TextQueryPF extends PropertyFunctionBase {
@Override
public QueryIterator exec(Binding binding, PropFuncArg argSubject, Node predicate, PropFuncArg argObject,
ExecutionContext execCxt) {
- if (server == null) {
+ if (textIndex == null) {
if (!warningIssued) {
Log.warn(getClass(), "No text index - no text search performed") ;
warningIssued = true ;
@@ -154,7 +156,7 @@ public class TextQueryPF extends PropertyFunctionBase {
// Does not match
return IterLib.noResults(execCxt) ;
- StrMatch match = objectToStruct(argObject) ;
+ StrMatch match = objectToStruct(argObject, true) ;
if (match == null) {
// can't match
return IterLib.noResults(execCxt) ;
@@ -195,7 +197,7 @@ public class TextQueryPF extends PropertyFunctionBase {
private List<Node> query(String queryString, int limit, ExecutionContext execCxt) {
// use the graph information in the text index if possible
- if (server.getDocDef().getGraphField() != null
+ if (textIndex.getDocDef().getGraphField() != null
&& execCxt.getActiveGraph() instanceof GraphView) {
GraphView activeGraph = (GraphView)execCxt.getActiveGraph() ;
if (!Quad.isUnionGraph(activeGraph.getGraphName())) {
@@ -204,14 +206,14 @@ public class TextQueryPF extends PropertyFunctionBase {
? TextQueryFuncs.graphNodeToString(activeGraph.getGraphName())
: Quad.defaultGraphNodeGenerated.getURI() ;
String escaped = QueryParserBase.escape(uri) ;
- String qs2 = server.getDocDef().getGraphField() + ":" + escaped ;
+ String qs2 = textIndex.getDocDef().getGraphField() + ":" + escaped ;
queryString = "(" + queryString + ") AND " + qs2 ;
}
}
//for language-based search extension
- if (server.getDocDef().getLangField() != null) {
- String field = server.getDocDef().getLangField();
+ if (textIndex.getDocDef().getLangField() != null) {
+ String field = textIndex.getDocDef().getLangField();
if (langArg != null) {
String qs2 = !"none".equals(langArg)?
field + ":" + langArg : "-" + field + ":*";
@@ -222,16 +224,20 @@ public class TextQueryPF extends PropertyFunctionBase {
Explain.explain(execCxt.getContext(), "Text query: "+queryString) ;
if ( log.isDebugEnabled())
log.debug("Text query: {} ({})", queryString,limit) ;
- return server.query(queryString, limit) ;
+ return textIndex.query(queryString, limit) ;
}
- /** Deconstruct the node or list object argument and make a StrMatch */
- private StrMatch objectToStruct(PropFuncArg argObject) {
- EntityDefinition docDef = server.getDocDef() ;
+ /** Deconstruct the node or list object argument and make a StrMatch
+ * The 'executionTime' flag indciates whether this is for a build time
+ * static check, or for runtime execution.
+ */
+ private StrMatch objectToStruct(PropFuncArg argObject, boolean executionTime) {
+ EntityDefinition docDef = textIndex.getDocDef() ;
if (argObject.isNode()) {
Node o = argObject.getArg() ;
if (!o.isLiteral()) {
- log.warn("Object to text query is not a literal") ;
+ if ( executionTime )
+ log.warn("Object to text query is not a literal") ;
return null ;
}
@@ -250,7 +256,7 @@ public class TextQueryPF extends PropertyFunctionBase {
throw new TextIndexException("Change in object list size") ;
Node predicate = null ;
- String field = null ; // Do not prepend the feild name - rely on default field
+ String field = null ; // Do not prepend the field name - rely on default field
int idx = 0 ;
Node x = list.get(0) ;
// Property?
@@ -258,7 +264,7 @@ public class TextQueryPF extends PropertyFunctionBase {
predicate = x ;
idx++ ;
if (idx >= list.size())
- throw new TextIndexException("Property specificied but no query string : " + list) ;
+ throw new TextIndexException("Property specificed but no query string : " + list) ;
x = list.get(idx) ;
field = docDef.getField(predicate) ;
if (field == null) {
@@ -269,9 +275,11 @@ public class TextQueryPF extends PropertyFunctionBase {
// String!
if (!x.isLiteral()) {
- log.warn("Text query string is not a literal " + list) ;
+ if ( executionTime )
+ log.warn("Text query string is not a literal " + list) ;
return null ;
}
+
if (x.getLiteralDatatype() != null && !x.getLiteralDatatype().equals(XSDDatatype.XSDstring)) {
log.warn("Text query is not a string " + list) ;
return null ;
@@ -286,6 +294,12 @@ public class TextQueryPF extends PropertyFunctionBase {
// Limit?
x = list.get(idx) ;
idx++ ;
+ if ( ! x.isLiteral() ) {
+ if ( executionTime )
+ log.warn("Text query limit is not an integer " + x) ;
+ return null ;
+ }
+
int v = NodeFactoryExtra.nodeToInt(x) ;
limit = (v < 0) ? -1 : v ;
}