Skip to content

Commit e3ecde0

Browse files
committed
Add support of LuceneSail#INDEXEDFIELDS
- add support of LuceneSail#INDEXEDFIELDS into LuceneSpinSail Signed-off-by:Jacek Grzebyta <grzebyta.dev@gmail.com>
1 parent 29fb372 commit e3ecde0

3 files changed

Lines changed: 97 additions & 8 deletions

File tree

core/sail/fts/lucene-spin/src/main/java/org/eclipse/rdf4j/lucene/spin/LuceneSpinSail.java

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,23 @@
88
package org.eclipse.rdf4j.lucene.spin;
99

1010
import java.io.File;
11+
import java.io.IOException;
12+
import java.io.Reader;
13+
import java.io.StringReader;
14+
import java.util.HashMap;
15+
import java.util.HashSet;
16+
import java.util.Map;
1117
import java.util.Properties;
18+
import java.util.Set;
19+
import org.eclipse.rdf4j.model.IRI;
20+
import org.eclipse.rdf4j.model.Statement;
21+
import org.eclipse.rdf4j.model.ValueFactory;
1222
import org.eclipse.rdf4j.sail.NotifyingSailConnection;
1323
import org.eclipse.rdf4j.sail.SailException;
1424
import org.eclipse.rdf4j.sail.evaluation.TupleFunctionEvaluationMode;
1525
import org.eclipse.rdf4j.sail.helpers.NotifyingSailWrapper;
1626
import org.eclipse.rdf4j.sail.lucene.LuceneSail;
27+
import static org.eclipse.rdf4j.sail.lucene.LuceneSail.INDEXEDFIELDS;
1728
import org.eclipse.rdf4j.sail.lucene.SearchIndex;
1829
import org.eclipse.rdf4j.sail.lucene.SearchIndexQueryContextInitializer;
1930
import org.eclipse.rdf4j.sail.lucene.util.SearchIndexUtils;
@@ -36,6 +47,10 @@ public class LuceneSpinSail extends NotifyingSailWrapper {
3647

3748
private Properties parameters = new Properties();
3849

50+
private Set<IRI> indexedFields;
51+
52+
private Map<IRI, IRI> indexedFieldsMapping;
53+
3954
public LuceneSpinSail() {
4055
}
4156

@@ -58,6 +73,30 @@ public void setParameters(Properties parameters) {
5873
public void initialize()
5974
throws SailException
6075
{
76+
// Add support for indexed fields
77+
if (parameters.containsKey(INDEXEDFIELDS)) {
78+
String indexedfieldsString = parameters.getProperty(INDEXEDFIELDS);
79+
Properties prop = new Properties();
80+
try (Reader reader = new StringReader(indexedfieldsString)) {
81+
prop.load(reader);
82+
}
83+
catch (IOException e) {
84+
throw new SailException("Could read " + INDEXEDFIELDS + ": " + indexedfieldsString, e);
85+
}
86+
ValueFactory vf = getValueFactory();
87+
indexedFields = new HashSet<>();
88+
indexedFieldsMapping = new HashMap<>();
89+
for (Object key : prop.keySet()) {
90+
String keyStr = key.toString();
91+
if (keyStr.startsWith("index.")) {
92+
indexedFields.add(vf.createIRI(prop.getProperty(keyStr)));
93+
}
94+
else {
95+
indexedFieldsMapping.put(vf.createIRI(keyStr), vf.createIRI(prop.getProperty(keyStr)));
96+
}
97+
}
98+
}
99+
61100
((SpinSail)getBaseSail()).setEvaluationMode(TupleFunctionEvaluationMode.TRIPLE_SOURCE);
62101
parameters.setProperty(LuceneSail.INDEX_CLASS_KEY,
63102
getParameters().getProperty(LuceneSail.INDEX_CLASS_KEY, LuceneSail.DEFAULT_INDEX_CLASS));
@@ -94,7 +133,38 @@ public NotifyingSailConnection getConnection()
94133
throw new SailException("Index is not created");
95134
}
96135
// the connection from the super is created only when the index exists
97-
return new LuceneSpinSailConnection(super.getConnection(), si);
136+
return new LuceneSpinSailConnection(super.getConnection(), si, this);
137+
}
138+
139+
/**
140+
* Copy of {@link LuceneSail#mapStatement(org.eclipse.rdf4j.model.Statement) }
141+
*
142+
* @param statement
143+
* @return
144+
*/
145+
public Statement mapStatement(Statement statement) {
146+
IRI p = statement.getPredicate();
147+
boolean predicateChanged = false;
148+
Map<IRI, IRI> nextIndexedFieldsMapping = indexedFieldsMapping;
149+
if (nextIndexedFieldsMapping != null) {
150+
IRI res = nextIndexedFieldsMapping.get(p);
151+
if (res != null) {
152+
p = res;
153+
predicateChanged = true;
154+
}
155+
}
156+
Set<IRI> nextIndexedFields = indexedFields;
157+
if (nextIndexedFields != null && !nextIndexedFields.contains(p)) {
158+
return null;
159+
}
160+
161+
if (predicateChanged) {
162+
return getValueFactory().createStatement(statement.getSubject(), p, statement.getObject(),
163+
statement.getContext());
164+
}
165+
else {
166+
return statement;
167+
}
98168
}
99169

100170
}

core/sail/fts/lucene-spin/src/main/java/org/eclipse/rdf4j/lucene/spin/LuceneSpinSailConnection.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public class LuceneSpinSailConnection extends NotifyingSailConnectionWrapper {
4444

4545
private final SearchIndex luceneIndex;
4646

47+
private final LuceneSpinSail sail;
48+
4749
/**
4850
* the buffer that collects operations
4951
*/
@@ -58,6 +60,14 @@ public class LuceneSpinSailConnection extends NotifyingSailConnectionWrapper {
5860
public void statementAdded(Statement statement) {
5961
// we only consider statements that contain literals
6062
if (statement.getObject() instanceof Literal) {
63+
64+
statement = sail.mapStatement(statement);
65+
66+
// process only mapped/indexable statements
67+
if (statement == null) {
68+
return;
69+
}
70+
6171
// we further only index statements where the Literal's datatype is
6272
// accepted
6373
Literal literal = (Literal)statement.getObject();
@@ -70,6 +80,12 @@ public void statementAdded(Statement statement) {
7080
public void statementRemoved(Statement statement) {
7181
// we only consider statements that contain literals
7282
if (statement.getObject() instanceof Literal) {
83+
84+
// process oly mapped/indexable statements
85+
statement = sail.mapStatement(statement);
86+
if (statement == null) {
87+
return;
88+
}
7389
// we further only indexed statements where the Literal's datatype
7490
// is accepted
7591
Literal literal = (Literal)statement.getObject();
@@ -84,9 +100,12 @@ public void statementRemoved(Statement statement) {
84100
*/
85101
private final AtomicBoolean closed = new AtomicBoolean(false);
86102

87-
public LuceneSpinSailConnection(NotifyingSailConnection wrappedConnection, SearchIndex luceneIndex) {
103+
public LuceneSpinSailConnection(NotifyingSailConnection wrappedConnection, SearchIndex luceneIndex,
104+
LuceneSpinSail sail)
105+
{
88106
super(wrappedConnection);
89107
this.luceneIndex = luceneIndex;
108+
this.sail = sail;
90109

91110
/*
92111
* Using SailConnectionListener, see <a href="#whySailConnectionListener">above</a>

core/sail/fts/lucene-spin/src/test/java/org/eclipse/rdf4j/lucene/spin/AbstractLuceneSailSpinTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
/*
2-
* To change this license header, choose License Headers in Project Properties.
3-
* To change this template file, choose Tools | Templates
4-
* and open the template in the editor.
1+
/**
2+
* Copyright (c) 2017 Eclipse RDF4J contributors.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Distribution License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/org/documents/edl-v10.php.
57
*/
68
package org.eclipse.rdf4j.lucene.spin;
79

8-
import com.google.common.base.Function;
910
import com.google.common.collect.Lists;
1011
import java.util.List;
1112
import org.apache.commons.io.output.ByteArrayOutputStream;
@@ -17,7 +18,6 @@
1718
import org.eclipse.rdf4j.query.BindingSet;
1819
import org.eclipse.rdf4j.query.GraphQuery;
1920
import org.eclipse.rdf4j.query.GraphQueryResult;
20-
import org.eclipse.rdf4j.query.QueryEvaluationException;
2121
import org.eclipse.rdf4j.query.QueryLanguage;
2222
import org.eclipse.rdf4j.query.TupleQuery;
2323
import org.eclipse.rdf4j.query.TupleQueryResult;

0 commit comments

Comments
 (0)