use of org.openrdf.sail.SailConnection in project blueprints by tinkerpop.
the class SailGraph method loadRDF.
/**
* Load RDF data into the SailGraph. Supported formats include rdf-xml, n-triples, turtle, n3, trix, or trig.
* Before loading data, the current transaction is successfully committed.
*
* @param input The InputStream of RDF data.
* @param baseURI The baseURI for RDF data.
* @param rdfParser The {@link RDFParser} to use. It's {@link RDFHandler} will be
* changed to an internal one. The main purpose of this is to use
* a custom {@link ValueFactory}. It is recommended to use
* <code>Rio.createParser(getFormat(format))</code> and set the
* {@link ValueFactory} of the parser to something that
* <code>extends</code> the {@link ValueFactory} of the
* {@link Sail} used to initialize this class.
* <p>
* For example, <code>extend {@link ValueFactoryImpl}</code> if
* you used a <code>GraphSail</code> to initialize this class.
* @param baseGraph The baseGraph to insert the data into. May be null, in which case data is added to the default graph.
* @param rdfHandlers Any number of {@link RDFHandler}s into which to pass parsed RDF statements <b>after</b>
* they have been added to the graph.
* These may be used, for example, for implementing your own logging.
* Can be <code>null</code> if you only want to use the default
* {@link RDFHandler} created internally.
*/
public void loadRDF(final InputStream input, final String baseURI, final RDFParser rdfParser, final String baseGraph, final RDFHandler... rdfHandlers) {
try {
this.commit();
final SailConnection c = this.rawGraph.getConnection();
try {
c.begin();
RDFHandler h = null == baseGraph ? new SailAdder(c) : new SailAdder(c, new URIImpl(baseGraph));
if (rdfHandlers != null) {
RDFHandler[] handlers = new RDFHandler[rdfHandlers.length + 1];
handlers[0] = h;
System.arraycopy(rdfHandlers, 0, handlers, 1, rdfHandlers.length);
h = new RDFHandlerWrapper(handlers);
}
rdfParser.setRDFHandler(h);
rdfParser.parse(input, baseURI);
c.commit();
} finally {
c.rollback();
c.close();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of org.openrdf.sail.SailConnection in project blueprints by tinkerpop.
the class SailGraph method closeAllConnections.
private void closeAllConnections() throws SailException {
for (SailConnection sc : connections) {
if (null != sc) {
if (sc.isOpen()) {
sc.rollback();
sc.close();
}
}
}
}
use of org.openrdf.sail.SailConnection in project blueprints by tinkerpop.
the class SailLoader method load.
/**
* Loads an RDF file, or a directory containing RDF files, into a Sail
*
* @param fileOrDirectory a file in a supported RDF format or a directory which contains (at any level)
* one or more files in a supported RDF format.
* A directory should contain all or mostly RDF files.
* Files must be named with a format-appropriate extension, e.g. *.rdf, *.ttl, *.nt
* or a format-appropriate extension followed by .gz if they are compressed with Gzip.
*/
public synchronized void load(final File fileOrDirectory) throws Exception {
LOGGER.info("loading from " + fileOrDirectory);
SailConnection c = sail.getConnection();
try {
c.begin();
long startTime = System.currentTimeMillis();
long count = loadFile(fileOrDirectory, c);
// commit leftover statements
c.commit();
long endTime = System.currentTimeMillis();
LOGGER.info("loaded " + count + " statements in " + (endTime - startTime) + "ms");
} finally {
c.rollback();
c.close();
}
}
use of org.openrdf.sail.SailConnection in project blueprints by tinkerpop.
the class SailTest method testSetNamespace.
@Test
public void testSetNamespace() throws Exception {
SailConnection sc = sail.getConnection();
try {
sc.begin();
String prefix = "foo";
String emptyPrefix = "";
String name = "http://example.org/foo";
String otherName = "http://example.org/bar";
sc.removeNamespace(prefix);
sc.removeNamespace(emptyPrefix);
sc.commit();
sc.begin();
// Namespace initially absent?
assertNull(sc.getNamespace(prefix));
assertNull(sc.getNamespace(emptyPrefix));
// Can we set the namespace?
sc.setNamespace(prefix, name);
sc.commit();
sc.begin();
assertEquals(sc.getNamespace(prefix), name);
// Can we reset the namespace?
sc.setNamespace(prefix, otherName);
sc.commit();
sc.begin();
assertEquals(sc.getNamespace(prefix), otherName);
// Can we use an empty namespace prefix?
sc.setNamespace(emptyPrefix, name);
sc.commit();
sc.begin();
assertEquals(sc.getNamespace(emptyPrefix), name);
} finally {
sc.rollback();
sc.close();
}
}
use of org.openrdf.sail.SailConnection in project blueprints by tinkerpop.
the class SailTest method testBlankNodes.
// blank nodes /////////////////////////////////////////////////////////////
@Test
public void testBlankNodes() throws Throwable {
URI uriA = sail.getValueFactory().createURI("http://example.org/test/S_POG#a");
URI uriB = sail.getValueFactory().createURI("http://example.org/test/S_POG#b");
SailConnection sc = sail.getConnection();
try {
sc.begin();
ValueFactory factory = sail.getValueFactory();
BNode bNode = factory.createBNode();
try {
sc.addStatement(uriA, uriA, bNode);
} catch (SailException se) {
// FIXME: not supporting blank nodes ATM
assertTrue(se.getCause() instanceof UnsupportedOperationException);
}
sc.commit();
} finally {
sc.rollback();
sc.close();
}
}
Aggregations