88package org .eclipse .rdf4j .lucene .spin ;
99
1010import 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 ;
1117import 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 ;
1222import org .eclipse .rdf4j .sail .NotifyingSailConnection ;
1323import org .eclipse .rdf4j .sail .SailException ;
1424import org .eclipse .rdf4j .sail .evaluation .TupleFunctionEvaluationMode ;
1525import org .eclipse .rdf4j .sail .helpers .NotifyingSailWrapper ;
1626import org .eclipse .rdf4j .sail .lucene .LuceneSail ;
27+ import static org .eclipse .rdf4j .sail .lucene .LuceneSail .INDEXEDFIELDS ;
1728import org .eclipse .rdf4j .sail .lucene .SearchIndex ;
1829import org .eclipse .rdf4j .sail .lucene .SearchIndexQueryContextInitializer ;
1930import 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}
0 commit comments