Last active
December 26, 2015 17:29
-
-
Save pepperbob/7187465 to your computer and use it in GitHub Desktop.
"Streaming approach": RDFHandler for Rio will flush the cached STMTs if a (currently hard coded) limit is reached.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| diff --git a/rdfbean-virtuoso/pom.xml b/rdfbean-virtuoso/pom.xml | |
| index b410b7d..5ad612e 100644 | |
| --- a/rdfbean-virtuoso/pom.xml | |
| +++ b/rdfbean-virtuoso/pom.xml | |
| @@ -23,6 +23,11 @@ | |
| <artifactId>rdfbean-core</artifactId> | |
| <version>${project.version}</version> | |
| </dependency> | |
| + <dependency> | |
| + <groupId>com.mysema.rdf</groupId> | |
| + <artifactId>rdfbean-sesame2</artifactId> | |
| + <version>${project.version}</version> | |
| + </dependency> | |
| <dependency> | |
| <groupId>com.openlinksw</groupId> | |
| <artifactId>virtjdbc4</artifactId> | |
| diff --git a/rdfbean-virtuoso/src/main/java/com/mysema/rdfbean/virtuoso/RDFStreamingHandler.java b/rdfbean-virtuoso/src/main/java/com/mysema/rdfbean/virtuoso/RDFStreamingHandler.java | |
| new file mode 100644 | |
| index 0000000..e1784e7 | |
| --- /dev/null | |
| +++ b/rdfbean-virtuoso/src/main/java/com/mysema/rdfbean/virtuoso/RDFStreamingHandler.java | |
| @@ -0,0 +1,79 @@ | |
| +package com.mysema.rdfbean.virtuoso; | |
| + | |
| +import java.util.ArrayList; | |
| +import java.util.List; | |
| + | |
| +import org.openrdf.model.Statement; | |
| +import org.openrdf.model.impl.ValueFactoryImpl; | |
| +import org.openrdf.rio.RDFHandler; | |
| +import org.openrdf.rio.RDFHandlerException; | |
| + | |
| +import com.mysema.rdfbean.model.ID; | |
| +import com.mysema.rdfbean.model.NODE; | |
| +import com.mysema.rdfbean.model.RDFConnection; | |
| +import com.mysema.rdfbean.model.STMT; | |
| +import com.mysema.rdfbean.model.UID; | |
| +import com.mysema.rdfbean.sesame.SesameDialect; | |
| + | |
| +public class RDFStreamingHandler implements RDFHandler { | |
| + | |
| + private final RDFConnection connection; | |
| + private final UID currentContext; | |
| + | |
| + private final SesameDialect dialect; | |
| + private final List<STMT> statements; | |
| + private int currentCount; | |
| + | |
| + public RDFStreamingHandler(RDFConnection connection, UID context) { | |
| + this.connection = connection; | |
| + this.currentContext = context; | |
| + | |
| + this.dialect = new SesameDialect(ValueFactoryImpl.getInstance()); | |
| + this.statements = new ArrayList<STMT>(); | |
| + this.currentCount = 0; | |
| + } | |
| + | |
| + @Override | |
| + public void startRDF() throws RDFHandlerException { | |
| + // nothing to do | |
| + } | |
| + | |
| + @Override | |
| + public void endRDF() throws RDFHandlerException { | |
| + flushAndClearStatements(); | |
| + } | |
| + | |
| + @Override | |
| + public void handleNamespace(String prefix, String uri) | |
| + throws RDFHandlerException { | |
| + // nothing to do | |
| + } | |
| + | |
| + @Override | |
| + public void handleStatement(Statement st) throws RDFHandlerException { | |
| + | |
| + final ID subject = dialect.getID(st.getSubject()); | |
| + final UID predicate = dialect.getUID(st.getPredicate()); | |
| + final NODE object = dialect.getNODE(st.getObject()); | |
| + | |
| + statements.add(new STMT(subject, predicate, object, currentContext)); | |
| + | |
| + currentCount++; | |
| + | |
| + if (currentCount > 5000) { | |
| + flushAndClearStatements(); | |
| + } | |
| + } | |
| + | |
| + private void flushAndClearStatements() { | |
| + connection.update(null, statements); | |
| + statements.clear(); | |
| + currentCount = 0; | |
| + } | |
| + | |
| + @Override | |
| + public void handleComment(String comment) throws RDFHandlerException { | |
| + // nothing to do | |
| + } | |
| + | |
| +} | |
| diff --git a/rdfbean-virtuoso/src/main/java/com/mysema/rdfbean/virtuoso/VirtuosoRepositoryConnection.java b/rdfbean-virtuoso/src/main/java/com/mysema/rdfbean/virtuoso/VirtuosoRepositoryConnection.java | |
| index 0b02b6d..247c409 100644 | |
| --- a/rdfbean-virtuoso/src/main/java/com/mysema/rdfbean/virtuoso/VirtuosoRepositoryConnection.java | |
| +++ b/rdfbean-virtuoso/src/main/java/com/mysema/rdfbean/virtuoso/VirtuosoRepositoryConnection.java | |
| @@ -18,6 +18,11 @@ import java.util.Set; | |
| import javax.annotation.Nullable; | |
| +import org.openrdf.rio.RDFFormat; | |
| +import org.openrdf.rio.RDFHandlerException; | |
| +import org.openrdf.rio.RDFParseException; | |
| +import org.openrdf.rio.RDFParser; | |
| +import org.openrdf.rio.Rio; | |
| import org.slf4j.Logger; | |
| import org.slf4j.LoggerFactory; | |
| @@ -484,27 +489,23 @@ public class VirtuosoRepositoryConnection implements RDFConnection { | |
| } | |
| PreparedStatement stmt = null; | |
| try { | |
| - byte[] bytes = ByteStreams.toByteArray(is); | |
| - if (format == Format.N3 || format == Format.TURTLE || format == Format.NTRIPLES) { // UTF-8 | |
| - // String content = IOUtils.toString(is, format == Format.NTRIPLES ? | |
| - // "US-ASCII" : "UTF-8"); | |
| - String content = new String(bytes, format == Format.NTRIPLES ? Charsets.US_ASCII : Charsets.UTF_8); | |
| + | |
| + if (format == Format.N3 || format == Format.TURTLE || format == Format.NTRIPLES) { | |
| + | |
| + byte[] bytes = ByteStreams.toByteArray(is); | |
| + String content = new String(bytes, format == Format.NTRIPLES ? Charsets.US_ASCII : Charsets.UTF_8); | |
| stmt = connection.prepareStatement("DB.DBA.TTLP(?,'',?,0)"); | |
| stmt.setString(1, content); | |
| stmt.setString(2, context != null ? context.getId() : defaultGraph.getId()); | |
| + stmt.execute(); | |
| + | |
| } else if (format == Format.RDFXML) { | |
| - // String content = IOUtils.toString(is, "UTF-8"); // TODO : | |
| - // proper XML load | |
| - String content = new String(bytes, Charsets.UTF_8); // TODO : | |
| - // propert | |
| - // XML load | |
| - stmt = connection.prepareStatement("DB.DBA.RDF_LOAD_RDFXML(?,'',?,0)"); | |
| - stmt.setString(1, content); | |
| - stmt.setString(2, context != null ? context.getId() : defaultGraph.getId()); | |
| + | |
| + loadRdfXml(is, context); | |
| + | |
| } else { | |
| throw new IllegalArgumentException("Unsupported forma " + format); | |
| } | |
| - stmt.execute(); | |
| } finally { | |
| if (stmt != null) { | |
| stmt.close(); | |
| @@ -512,6 +513,28 @@ public class VirtuosoRepositoryConnection implements RDFConnection { | |
| } | |
| } | |
| + | |
| + private void loadRdfXml(InputStream is, @Nullable UID context) { | |
| + try { | |
| + | |
| + UID currentContext = context != null ? context : defaultGraph; | |
| + | |
| + RDFParser rioParser = Rio.createParser(RDFFormat.RDFXML); | |
| + rioParser.setRDFHandler(new RDFStreamingHandler(this, currentContext)); | |
| + | |
| + // parses and adds triples | |
| + rioParser.parse(is, currentContext.getId()); | |
| + | |
| + | |
| + } catch (RDFParseException e) { | |
| + throw new RepositoryException(e); | |
| + } catch (RDFHandlerException e) { | |
| + throw new RepositoryException(e); | |
| + } catch (IOException e) { | |
| + throw new RepositoryException(e); | |
| + } | |
| + } | |
| + | |
| private void remove(Collection<STMT> removedStatements) throws SQLException { | |
| verifyNotReadOnly(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment