commonsrdf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From st...@apache.org
Subject [37/50] incubator-commonsrdf git commit: Split out ConvertedStatements iterable
Date Fri, 07 Oct 2016 15:01:40 GMT
Split out ConvertedStatements iterable


Project: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/commit/90e3e500
Tree: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/tree/90e3e500
Diff: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/diff/90e3e500

Branch: refs/heads/master
Commit: 90e3e50067112a947a0e5147940bb04854e93d96
Parents: a47ad36
Author: Stian Soiland-Reyes <stain@apache.org>
Authored: Fri Oct 7 00:36:36 2016 +0100
Committer: Stian Soiland-Reyes <stain@apache.org>
Committed: Fri Oct 7 00:43:50 2016 +0100

----------------------------------------------------------------------
 .../apache/commons/rdf/rdf4j/RDF4JDataset.java  | 47 +++++++++++++
 .../apache/commons/rdf/rdf4j/RDF4JGraph.java    |  4 +-
 .../rdf/rdf4j/impl/ConvertedStatements.java     | 71 ++++++++++++++++++++
 .../rdf/rdf4j/impl/RepositoryDatasetImpl.java   | 19 ++++++
 .../rdf/rdf4j/impl/RepositoryGraphImpl.java     | 41 ++---------
 5 files changed, 142 insertions(+), 40 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/90e3e500/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/RDF4JDataset.java
----------------------------------------------------------------------
diff --git a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/RDF4JDataset.java b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/RDF4JDataset.java
index d1aea4c..8ed5d4d 100644
--- a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/RDF4JDataset.java
+++ b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/RDF4JDataset.java
@@ -25,6 +25,7 @@ import org.apache.commons.rdf.api.Dataset;
 import org.apache.commons.rdf.api.IRI;
 import org.apache.commons.rdf.api.Quad;
 import org.apache.commons.rdf.api.RDFTerm;
+import org.apache.commons.rdf.api.Triple;
 import org.eclipse.rdf4j.repository.RepositoryConnection;
 
 
@@ -88,4 +89,50 @@ public interface RDF4JDataset extends Dataset, RDF4JGraphLike<Quad>
{
 	@Override
 	Stream<BlankNodeOrIRI> getGraphNames();
 	
+
+	/**
+	 * {@inheritDoc}
+	 * <p>
+	 * Note that the iterable <strong>must be closed</strong> with
+	 * {@link ClosableIterable#close()}. 
+	 * call 
+	 * <p>
+	 * This can generally achieved using a try-with-resources block, e.g.:
+	 * <pre>
+	 * try (ClosableIterable&lt;Quad&gt; s : graph.iterate()) {
+     *   for (Quad q : quads) {
+     *       return q; // OK to terminate for-loop early
+     *   }
+	 * }
+	 * </pre>
+	 * If you don't use a try-with-resources block, the iterator will 
+	 * attempt to close the ClosableIterable 
+	 * when reaching the end of the iteration.
+	 */	
+	@Override
+	ClosableIterable<Quad> iterate();
+	
+
+	/**
+	 * {@inheritDoc}
+	 * <p>
+	 * Note that the iterable <strong>must be closed</strong> with
+	 * {@link ClosableIterable#close()}. 
+	 * call 
+	 * <p>
+	 * This can generally achieved using a try-with-resources block, e.g.:
+	 * <pre>
+	 * try (ClosableIterable&lt;Quad&gt; s : graph.iterate(g,s,p,o)) {
+     *   for (Quad q : quads) {
+     *       return q; // OK to terminate for-loop early
+     *   }
+	 * }
+	 * </pre>
+	 * If you don't use a try-with-resources block, the iterator will 
+	 * attempt to close the ClosableIterable 
+	 * when reaching the end of the iteration.
+	 */	
+	@Override
+	ClosableIterable<Quad> iterate(Optional<BlankNodeOrIRI> graphName, BlankNodeOrIRI
subject, IRI predicate, RDFTerm object);	
+	
 }

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/90e3e500/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/RDF4JGraph.java
----------------------------------------------------------------------
diff --git a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/RDF4JGraph.java b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/RDF4JGraph.java
index 8030723..c19e14a 100644
--- a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/RDF4JGraph.java
+++ b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/RDF4JGraph.java
@@ -116,7 +116,6 @@ public interface RDF4JGraph extends Graph, RDF4JGraphLike<Triple>
{
 	 * <p>
 	 * This can generally achieved using a try-with-resources block, e.g.:
 	 * <pre>
-	 * int subjects;
 	 * try (ClosableIterable&lt;Triple&gt; s : graph.iterate()) {
      *   for (Triple t : triples) {
      *       return t; // OK to terminate for-loop early
@@ -139,7 +138,6 @@ public interface RDF4JGraph extends Graph, RDF4JGraphLike<Triple>
{
 	 * <p>
 	 * This can generally achieved using a try-with-resources block, e.g.:
 	 * <pre>
-	 * int subjects;
 	 * try (ClosableIterable&lt;Triple&gt; s : graph.iterate(s,p,o)) {
      *   for (Triple t : triples) {
      *       return t; // OK to terminate for-loop early
@@ -151,5 +149,5 @@ public interface RDF4JGraph extends Graph, RDF4JGraphLike<Triple>
{
 	 * when reaching the end of the iteration.
 	 */	
 	@Override
-	Iterable<Triple> iterate(BlankNodeOrIRI subject, IRI predicate, RDFTerm object);
+	ClosableIterable<Triple> iterate(BlankNodeOrIRI subject, IRI predicate, RDFTerm object);
 }

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/90e3e500/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/ConvertedStatements.java
----------------------------------------------------------------------
diff --git a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/ConvertedStatements.java
b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/ConvertedStatements.java
new file mode 100644
index 0000000..4c8478a
--- /dev/null
+++ b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/ConvertedStatements.java
@@ -0,0 +1,71 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.rdf.rdf4j.impl;
+
+import java.util.Iterator;
+import java.util.function.Function;
+import java.util.function.Supplier;
+
+import org.apache.commons.rdf.rdf4j.ClosableIterable;
+import org.eclipse.rdf4j.model.Resource;
+import org.eclipse.rdf4j.model.Statement;
+import org.eclipse.rdf4j.model.Value;
+import org.eclipse.rdf4j.repository.RepositoryConnection;
+import org.eclipse.rdf4j.repository.RepositoryResult;
+
+final class ConvertedStatements<T> implements ClosableIterable<T> {
+	
+	private RepositoryConnection conn;
+	private RepositoryResult<Statement> results;
+	private Function<Statement, T> statementAdapter;
+
+	ConvertedStatements(Supplier<RepositoryConnection> repositoryConnector, Function<Statement,
T> statementAdapter,
+			Resource subj, org.eclipse.rdf4j.model.IRI pred, Value obj, Resource... contexts) {
+		this.statementAdapter = statementAdapter;
+		this.conn = repositoryConnector.get();
+		this.results = conn.getStatements(subj, pred, obj, contexts);
+	}
+
+	@Override
+	public Iterator<T> iterator() {
+		return new ConvertedIterator();
+	}
+
+	@Override
+	public void close() {
+		results.close();
+		conn.close();
+	}
+
+	private final class ConvertedIterator implements Iterator<T> {
+		@Override
+		public boolean hasNext() {
+			boolean hasNext = results.hasNext();
+			if (!hasNext) {
+				close();
+			}
+			return hasNext;
+		}
+
+		@Override
+		public T next() {
+			return statementAdapter.apply(results.next());
+		}
+	}
+
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/90e3e500/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/RepositoryDatasetImpl.java
----------------------------------------------------------------------
diff --git a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/RepositoryDatasetImpl.java
b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/RepositoryDatasetImpl.java
index eca52fe..2efef42 100644
--- a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/RepositoryDatasetImpl.java
+++ b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/RepositoryDatasetImpl.java
@@ -17,6 +17,7 @@
  */
 package org.apache.commons.rdf.rdf4j.impl;
 
+import java.util.ConcurrentModificationException;
 import java.util.Optional;
 import java.util.stream.Stream;
 
@@ -26,6 +27,7 @@ import org.apache.commons.rdf.api.Graph;
 import org.apache.commons.rdf.api.IRI;
 import org.apache.commons.rdf.api.Quad;
 import org.apache.commons.rdf.api.RDFTerm;
+import org.apache.commons.rdf.rdf4j.ClosableIterable;
 import org.apache.commons.rdf.rdf4j.RDF4JDataset;
 import org.apache.commons.rdf.rdf4j.RDF4JQuad;
 import org.eclipse.rdf4j.common.iteration.Iterations;
@@ -170,6 +172,23 @@ class RepositoryDatasetImpl extends AbstractRepositoryGraphLike<Quad>
implements
 
 	}
 
+
+	@Override
+	public ClosableIterable<Quad> iterate() throws ConcurrentModificationException, IllegalStateException
{
+		return iterate(null, null, null, null);
+	}
+	
+	@Override
+	public ClosableIterable<Quad> iterate(Optional<BlankNodeOrIRI> graphName, BlankNodeOrIRI
subject, IRI predicate, RDFTerm object)
+			throws ConcurrentModificationException, IllegalStateException {
+		Resource[] contexts = asContexts(graphName);
+		Resource subj = (Resource) rdf4jTermFactory.asValue(subject);
+		org.eclipse.rdf4j.model.IRI pred = (org.eclipse.rdf4j.model.IRI) rdf4jTermFactory.asValue(predicate);
+		Value obj = rdf4jTermFactory.asValue(object);
+		return new ConvertedStatements<Quad>(this::getRepositoryConnection, 
+				rdf4jTermFactory::asQuad, subj, pred, obj, contexts);
+	}	
+	
 	@Override
 	protected RDF4JQuad asTripleLike(Statement s) {
 		return rdf4jTermFactory.asQuad(s);

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/90e3e500/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/RepositoryGraphImpl.java
----------------------------------------------------------------------
diff --git a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/RepositoryGraphImpl.java
b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/RepositoryGraphImpl.java
index ce0add1..03fede3 100644
--- a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/RepositoryGraphImpl.java
+++ b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/RepositoryGraphImpl.java
@@ -20,9 +20,9 @@ package org.apache.commons.rdf.rdf4j.impl;
 import java.util.Collections;
 import java.util.ConcurrentModificationException;
 import java.util.HashSet;
-import java.util.Iterator;
 import java.util.Objects;
 import java.util.Set;
+import java.util.function.Function;
 import java.util.stream.Stream;
 
 import org.apache.commons.rdf.api.BlankNodeOrIRI;
@@ -43,41 +43,7 @@ import org.eclipse.rdf4j.repository.RepositoryConnection;
 import org.eclipse.rdf4j.repository.RepositoryResult;
 
 class RepositoryGraphImpl extends AbstractRepositoryGraphLike<Triple> implements Graph,
RDF4JGraph {
-
-	private final class TripleIteration implements ClosableIterable<Triple> {
-		private RepositoryConnection conn;
-		private RepositoryResult<Statement> results;
-		private TripleIteration(Resource subj, org.eclipse.rdf4j.model.IRI pred, Value obj) {
-			conn = getRepositoryConnection();
-			results = conn.getStatements(subj, pred, obj, contextMask);
-		}
-		
-		@Override
-		public Iterator<Triple> iterator() {
-			return new Iterator<Triple>(){
-				@Override
-				public boolean hasNext() {
-					boolean hasNext = results.hasNext();
-					if (! hasNext) {
-						close();
-					}
-					return hasNext;
-				}
-				@Override
-				public Triple next() {					
-					Statement statement = results.next();
-					return rdf4jTermFactory.asTriple(statement);
-				}
-			};
-		}
-
-		@Override
-		public void close() {
-			results.close();
-			conn.close();
-		}
-	}
-
+	
 	private final Resource[] contextMask;
 
 	RepositoryGraphImpl(Repository repository, boolean handleInitAndShutdown, boolean includeInferred,
Resource... contextMask) {
@@ -177,7 +143,8 @@ class RepositoryGraphImpl extends AbstractRepositoryGraphLike<Triple>
implements
 		Resource subj = (Resource) rdf4jTermFactory.asValue(subject);
 		org.eclipse.rdf4j.model.IRI pred = (org.eclipse.rdf4j.model.IRI) rdf4jTermFactory.asValue(predicate);
 		Value obj = rdf4jTermFactory.asValue(object);
-		return new TripleIteration(subj, pred, obj);
+		return new ConvertedStatements<Triple>(this::getRepositoryConnection, rdf4jTermFactory::asTriple,

+				subj, pred, obj, contextMask);
 	}
 
 	@Override


Mime
View raw message