public static void flushTriples(Iterable<KiWiTriple> tripleBacklog, OutputStream out) throws IOException { CsvListWriter writer = new CsvListWriter(new OutputStreamWriter(out), CsvPreference.STANDARD_PREFERENCE); // reuse the same array to avoid unnecessary object allocation Object[] rowArray = new Object[10]; List<Object> row = Arrays.asList(rowArray); for(KiWiTriple t : tripleBacklog) { rowArray[0] = t.getId(); rowArray[1] = t.getSubject(); rowArray[2] = t.getPredicate(); rowArray[3] = t.getObject(); rowArray[4] = t.getContext(); rowArray[5] = t.getCreator(); rowArray[6] = t.isInferred(); rowArray[7] = t.isDeleted(); rowArray[8] = t.getCreated(); rowArray[9] = t.getDeletedAt(); writer.write(row, tripleProcessors); } writer.close(); }
@Override public Void run() throws SQLException { PreparedStatement insertTriple = getPreparedStatement("store.triple"); insertTriple.clearParameters(); insertTriple.clearBatch(); synchronized (tripleBatch) { for(KiWiTriple triple : tripleBatch) { // retrieve a new triple ID and set it in the object if(triple.getId() < 0) { triple.setId(getNextSequence()); } insertTriple.setLong(1,triple.getId()); insertTriple.setLong(2,triple.getSubject().getId()); insertTriple.setLong(3,triple.getPredicate().getId()); insertTriple.setLong(4,triple.getObject().getId()); if(triple.getContext() != null) { insertTriple.setLong(5,triple.getContext().getId()); } else { insertTriple.setNull(5, Types.BIGINT); } insertTriple.setBoolean(6,triple.isInferred()); insertTriple.setTimestamp(7, new Timestamp(triple.getCreated().getTime())); insertTriple.addBatch(); } } insertTriple.executeBatch(); tripleBatch.clear(); return null; } });
/** * Remove a statement from the triple registry. Called when the statement is deleted and the transaction commits. * @param triple */ protected void removeStatement(KiWiTriple triple) { if(triple.getId() >= 0) { IntArray cacheKey = IntArray.createSPOCKey(triple.getSubject(), triple.getPredicate(), triple.getObject(), triple.getContext()); synchronized (registry) { registry.deleteKey(cacheKey); } } triple.setDeleted(true); }
@Override public int hashCode() { return 961 * getSubject().hashCode() + 31 * getPredicate().hashCode() + getObject().hashCode(); }
KiWiTriple result = new KiWiTriple(); result.setId(id); result.setSubject((KiWiResource) batch[0]); result.setPredicate((KiWiUriResource) batch[1]); result.setObject(batch[2]); result.setContext((KiWiResource) batch[3]); result.setCreator((KiWiResource)loadNodeById(row.getLong(8))); result.setDeleted(row.getBoolean(6)); result.setInferred(row.getBoolean(7)); result.setCreated(new Date(row.getTimestamp(9).getTime())); try { if (row.getDate(10) != null) { result.setDeletedAt(new Date(row.getTimestamp(10).getTime()));
if (triple.getId() < 0) { triple.setId(getNextSequence()); if (deletedStatementsLog.mightContain(triple.getId())) { Preconditions.checkNotNull(triple.getSubject().getId()); Preconditions.checkNotNull(triple.getPredicate().getId()); Preconditions.checkNotNull(triple.getObject().getId());
KiWiResource kcontext = convert(context); KiWiTriple result = new KiWiTriple(ksubject,kpredicate,kobject,kcontext); result.setId(tripleId); registry.registerKey(cacheKey, connection.getTransactionId(), result.getId()); } else { result.setId(connection.getTripleId(ksubject,kpredicate,kobject,kcontext)); if(result.getId() < 0) { synchronized (registry) { result.setId(tripleId); } else { result.setId(connection.getNextSequence()); result.setNewTriple(true); registry.registerKey(cacheKey, connection.getTransactionId(), result.getId());
result = ((ResourceField)pattern.getSubject()).getResource().equals(triple.getSubject()); } else if(pattern.getSubject().isVariableField()) { KiWiNode binding = match.getBindings().get(pattern.getSubject()); if(binding != null) { result = binding.equals(triple.getSubject()); } else { match.getBindings().put((VariableField) pattern.getSubject(), triple.getSubject()); result = ((ResourceField)pattern.getProperty()).getResource().equals(triple.getPredicate()); } else if(result && pattern.getProperty().isVariableField()) { KiWiNode binding = match.getBindings().get(pattern.getProperty()); if(binding != null) { result = binding.equals(triple.getPredicate()); } else { match.getBindings().put((VariableField) pattern.getProperty(), triple.getPredicate()); result = ((ResourceField)pattern.getContext()).getResource().equals(triple.getContext()); } else if(result && pattern.getContext() != null && pattern.getContext().isVariableField()) { KiWiNode binding = match.getBindings().get(pattern.getContext()); if(binding != null) { result = binding.equals(triple.getContext()); } else { match.getBindings().put((VariableField) pattern.getContext(), triple.getContext()); result = ((ResourceField)pattern.getObject()).getResource().equals(triple.getObject()); } else if(result && pattern.getObject().isLiteralField()) { result = ((LiteralField)pattern.getObject()).getLiteral().equals(triple.getObject()); } else if(result && pattern.getObject().isVariableField()) { KiWiNode binding = match.getBindings().get(pattern.getObject());
connection.storeNode(context); KiWiTriple triple1 = new KiWiTriple(subject,pred_1,object_1,context); KiWiTriple triple2 = new KiWiTriple(subject,pred_2,object_2,context); Assert.assertEquals((long) triple1.getId(), dbResult1.getLong("id")); Assert.assertEquals((long) triple1.getSubject().getId(), dbResult1.getLong("subject")); Assert.assertEquals((long) triple1.getPredicate().getId(), dbResult1.getLong("predicate")); Assert.assertEquals((long)triple1.getObject().getId(),dbResult1.getLong("object")); Assert.assertEquals((long)triple2.getId(),dbResult1.getLong("id")); Assert.assertEquals((long)triple2.getSubject().getId(),dbResult1.getLong("subject")); Assert.assertEquals((long)triple2.getPredicate().getId(),dbResult1.getLong("predicate")); Assert.assertEquals((long)triple2.getObject().getId(),dbResult1.getLong("object"));
/** * Removes an inferred statement from a specific context. * * @throws org.openrdf.sail.SailException If the statement could not be removed. * @throws IllegalStateException If the connection has been closed. */ public boolean removeInferredStatement(KiWiTriple triple) throws SailException { try { if(triple.getId() >= 0 && triple.isInferred()) { databaseConnection.deleteTriple(triple); triplesRemoved = true; notifyStatementRemoved(triple); } } catch(SQLException ex) { throw new SailException("database error while deleting statement",ex); } return true; }
/** * Mark the triple passed as argument as not deleted, setting the "deleted" flag to false and * clearing the timestamp value of "deletedAt". * <p/> * Note that this operation should only be called if the triple was deleted before in the same * transaction! * * @param triple the KiWiTriple to restore */ public void undeleteTriple(KiWiTriple triple) throws SQLException { if(triple.getId() < 0) { log.warn("attempting to undelete non-persistent triple: {}",triple); return; } requireJDBCConnection(); // make sure the triple is not marked as deleted in case some service still holds a reference triple.setDeleted(false); triple.setDeletedAt(null); synchronized (triple) { if(!triple.isDeleted()) { log.warn("attempting to undelete triple that was not deleted: {}",triple); } PreparedStatement undeleteTriple = getPreparedStatement("undelete.triple"); undeleteTriple.setLong(1, triple.getId()); undeleteTriple.executeUpdate(); if(!persistence.getConfiguration().isClustered()) { cacheTriple(triple); } } }
triple.setDeleted(true); triple.setDeletedAt(new Date()); if (triple.getId() < 0) { log.warn("attempting to remove non-persistent triple: {}", triple); } else { deleteTriple.setLong(1, triple.getId()); deleteTriple.executeUpdate(); deletedStatementsLog.put(triple.getId()); deleteTriple.setLong(1, triple.getId()); deleteTriple.executeUpdate(); deletedStatementsLog.put(triple.getId());
/** * Creates a new statement with the supplied subject, predicate and object * and associated context. * * @param subject The statement's subject. * @param predicate The statement's predicate. * @param object The statement's object. * @param context The statement's context. * @return The created statement. */ @Override public Statement createStatement(Resource subject, URI predicate, Value object, Resource context) { Preconditions.checkArgument(subject instanceof KiWiNode); Preconditions.checkArgument(predicate instanceof KiWiNode); Preconditions.checkArgument(object instanceof KiWiNode); Preconditions.checkArgument(context == null || context instanceof KiWiNode); KiWiTriple t = new KiWiTriple((KiWiResource)subject,(KiWiUriResource)predicate,(KiWiNode)object,(KiWiResource)context, new Date()); t.setId(idGenerator.getId()); return t; } }
for(Statement stmt : data.getAddedTriples()) { KiWiTriple t = (KiWiTriple)stmt; if(t.isNewTriple()) { newTriples.add(t); t.setNewTriple(false);
/** * Undoes all changes made in the current transaction * and releases any database locks currently held * by this <code>Connection</code> object. This method should be * used only when auto-commit mode has been disabled. * * @exception java.sql.SQLException if a database access error occurs, * this method is called while participating in a distributed transaction, * this method is called on a closed connection or this * <code>Connection</code> object is in auto-commit mode * @see #setAutoCommit */ public void rollback() throws SQLException { if (tripleBatch != null && tripleBatch.size() > 0) { synchronized (tripleBatch) { for(KiWiTriple triple : tripleBatch) { triple.setId(-1L); } tripleBatch.clear(); } } deletedStatementsLog = BloomFilter.create(Funnels.longFunnel(), 100000); if(connection != null && !connection.isClosed()) { connection.rollback(); } this.transactionId = getNextSequence(); }
if (it.next().getContext().equals(ctx)) { it.remove();
if(support.isInferred()) { Collection<Justification> supportJustifications = justificationCache.get(support);
@Override public Boolean run() throws SQLException { PreparedStatement insertTriple = getPreparedStatement("store.triple"); insertTriple.setLong(1,triple.getId()); insertTriple.setLong(2,triple.getSubject().getId()); insertTriple.setLong(3,triple.getPredicate().getId()); insertTriple.setLong(4,triple.getObject().getId()); if (triple.getContext() != null) { insertTriple.setLong(5,triple.getContext().getId()); } else { insertTriple.setNull(5, Types.BIGINT); } insertTriple.setBoolean(6,triple.isInferred()); insertTriple.setTimestamp(7, new Timestamp(triple.getCreated().getTime())); int count = insertTriple.executeUpdate(); cacheTriple(triple); return count > 0; } });
KiWiTriple result = new KiWiTriple(); result.setId(id); result.setSubject((KiWiResource) batch[0]); result.setPredicate((KiWiUriResource) batch[1]); result.setObject(batch[2]); result.setContext((KiWiResource) batch[3]); result.setCreator((KiWiResource)loadNodeById(row.getLong(8))); result.setDeleted(row.getBoolean(6)); result.setInferred(row.getBoolean(7)); result.setCreated(new Date(row.getTimestamp(9).getTime())); try { if (row.getDate(10) != null) { result.setDeletedAt(new Date(row.getTimestamp(10).getTime()));