@Override public void close() { for (RDFConnection connection : connections) { connection.close(); } }
@Override public RDFBeanTransaction beginTransaction(boolean readOnly, int txTimeout, int isolationLevel) { if (transaction != null) { throw new IllegalStateException("Transaction exists already"); } transaction = connection.beginTransaction(readOnly, txTimeout, isolationLevel); return transaction; }
@Override public boolean exists(ID subject, UID predicate, NODE object, UID context, boolean includeInferred) { return connections[0].exists(subject, predicate, object, context, includeInferred); }
@Override public <RT> RT execute(RDFConnectionCallback<RT> operation) { RDFConnection connection = openConnection(); try { RDFBeanTransaction tx = connection.beginTransaction(false, RDFBeanTransaction.TIMEOUT, RDFBeanTransaction.ISOLATION); try { RT retVal = operation.doInConnection(connection); tx.commit(); return retVal; } catch (IOException io) { tx.rollback(); throw new RepositoryException(io); } } finally { connection.close(); } }
@Override public void export(Format format, Map<String, String> ns2prefix, UID context, OutputStream out) { RDFWriter writer = WriterUtils.createWriter(format, out, ns2prefix); RDFConnection conn = openConnection(); try { CloseableIterator<STMT> stmts = conn.findStatements(null, null, null, context, false); try { writer.begin(); while (stmts.hasNext()) { writer.handle(stmts.next()); } writer.end(); } finally { stmts.close(); } } finally { conn.close(); } }
@BeforeClass public static void beforeClass() throws IOException { repository = new MiniRepository(); repository.initialize(); // enums Set<STMT> added = new HashSet<STMT>(); for (NoteType nt : NoteType.values()) { added.add(new STMT( new UID(TEST.NS, nt.name()), CORE.enumOrdinal, new LIT(String.valueOf(nt.ordinal()), XSD.integerType))); } RDFConnection connection = repository.openConnection(); connection.update(Collections.<STMT> emptySet(), added); connection.close(); }
@Override public void update(Collection<STMT> removedStatements, Collection<STMT> addedStatements) { if (!readonlyTnx) { for (RDFConnection connection : connections) { connection.update(removedStatements, addedStatements); } } }
@Test public void Delete_From_From_Where() throws IOException { connection().update(null, Collections.singleton(new STMT(RDFS.Resource, RDF.type, RDFS.Class, ex1))); connection().update(null, Collections.singleton(new STMT(RDFS.Resource, RDF.type, RDFS.Class, ex2))); RDFUpdate delete = update().delete(s.a(RDFS.Class)).from(ex1, ex2).where(s.has(p, o)); delete.execute(); assertFalse(connection().exists(null, null, null, ex1, false)); assertFalse(connection().exists(null, null, null, ex2, false)); assertTrue(connection().exists(null, null, null, null, false)); }
private List<STMT> findStatements(@Nullable ID subject, @Nullable UID predicate, @Nullable NODE object, @Nullable UID context, boolean includeInferred) { if (logger.isDebugEnabled()) { logger.debug("findStatements " + subject + " " + predicate + " " + object + " " + context); } // rdf type inference if (RDF.type.equals(predicate) && subject == null && object != null && connection.getInferenceOptions().subClassOf()) { Collection<UID> types = ontology.getSubtypes(object.asURI()); if (types.size() > 1) { RDFQuery query = new RDFQueryImpl(connection); CloseableIterator<STMT> stmts = query.where( Blocks.SPOC, QNODE.o.in(types)) .set(QNODE.p, predicate) .construct(Blocks.SPOC); return IteratorAdapter.asList(stmts); } } return IteratorAdapter.asList(connection.findStatements(subject, predicate, object, context, includeInferred)); }
@Override public void remove(ID subject, UID predicate, NODE object, UID context) { connections[0].remove(subject, predicate, object, context); }
@Override public CloseableIterator<STMT> findStatements(ID subject, UID predicate, NODE object, UID context, boolean includeInferred) { return connections[0].findStatements(subject, predicate, object, context, includeInferred); }
@Override public <RT> RT execute(RDFConnectionCallback<RT> operation) { RDFConnection connection = openConnection(); try { RDFBeanTransaction tx = connection.beginTransaction(false, RDFBeanTransaction.TIMEOUT, RDFBeanTransaction.ISOLATION); try { RT retVal = operation.doInConnection(connection); tx.commit(); return retVal; } catch (IOException io) { tx.rollback(); throw new RepositoryException(io); } } finally { connection.close(); } }
public void flush() { if (!removedStatements.isEmpty() || !addedStatements.isEmpty()) { connection.update(removedStatements, addedStatements); } removedStatements = new LinkedHashSet<STMT>(); addedStatements = new LinkedHashSet<STMT>(); }
@Test public void Delete_From_Where() throws IOException { connection().update(null, Collections.singleton(new STMT(RDFS.Resource, RDF.type, RDFS.Class, ex1))); RDFUpdate delete = update().delete(s.a(RDFS.Class)).from(ex1).where(s.has(p, o)); delete.execute(); assertFalse(connection().exists(null, null, null, ex1, false)); assertTrue(connection().exists(null, null, null, null, false)); }
@Override public void execute() { switch (clause.getType()) { case CLEAR: case DROP: connection.remove(null, null, null, clause.getSource()); case CREATE: case LOAD: // TODO break; case DELETE: executeDelete(); break; case INSERT: executeInsert(); break; case MODIFY: executeModify(); break; default: throw new IllegalStateException("Unknown clause " + clause.getType()); } }
@Nullable private STMT find( @Nullable ID subject, @Nullable UID predicate, @Nullable NODE object) { CloseableIterator<STMT> stmts = connection.findStatements(subject, predicate, object, null, false); try { return stmts.hasNext() ? stmts.next() : null; } finally { stmts.close(); } } }
@Override public <RT> RT execute(RDFConnectionCallback<RT> operation) { RDFConnection connection = openConnection(); try{ RDFBeanTransaction tx = connection.beginTransaction(false, RDFBeanTransaction.TIMEOUT, RDFBeanTransaction.ISOLATION); try{ RT retVal = operation.doInConnection(connection); tx.commit(); return retVal; }catch(IOException io){ tx.rollback(); throw new RepositoryException(io); } }finally{ connection.close(); } }
@Override public void close() { connection.close(); }
private void add(ID subject, UID predicate, NODE object) { connection.update( null, Collections.singleton(new STMT(subject, predicate, object))); }
@Override public RDFBeanTransaction beginTransaction(boolean readOnly, int txTimeout, int isolationLevel) { RDFBeanTransaction[] transactions = new RDFBeanTransaction[connections.length]; for (int i = 0; i < transactions.length; i++) { transactions[i] = connections[i].beginTransaction(readOnly, txTimeout, isolationLevel); } this.readonlyTnx = readOnly; localTxn = new MultiTransaction(this, transactions); return localTxn; }