@Override public Optional<RyaStatement> merge(final Optional<RyaStatement> parent, final Optional<RyaStatement> child) throws MergerException { if(parent.isPresent()) { final RyaStatement parentStatement = parent.get(); if(child.isPresent()) { final RyaStatement childStatement = child.get(); final String pVis = new String(parentStatement.getColumnVisibility(), StandardCharsets.UTF_8); final String cVis = new String(childStatement.getColumnVisibility(), StandardCharsets.UTF_8); String visibility = ""; final Joiner join = Joiner.on(")&("); if(pVis.isEmpty() || cVis.isEmpty()) { visibility = (pVis + cVis).trim(); } else { visibility = "(" + join.join(pVis, cVis) + ")"; } parentStatement.setColumnVisibility(visibility.getBytes(StandardCharsets.UTF_8)); return Optional.of(parentStatement); } return parent; } else if(child.isPresent()) { return child; } return Optional.absent(); } }
@Override public Optional<RyaStatement> merge(final Optional<RyaStatement> parent, final Optional<RyaStatement> child) throws MergerException { if(parent.isPresent()) { final RyaStatement parentStatement = parent.get(); if(child.isPresent()) { final RyaStatement childStatement = child.get(); final String pVis = new String(parentStatement.getColumnVisibility(), StandardCharsets.UTF_8); final String cVis = new String(childStatement.getColumnVisibility(), StandardCharsets.UTF_8); String visibility = ""; final Joiner join = Joiner.on(")&("); if(pVis.isEmpty() || cVis.isEmpty()) { visibility = (pVis + cVis).trim(); } else { visibility = "(" + join.join(pVis, cVis) + ")"; } parentStatement.setColumnVisibility(visibility.getBytes(StandardCharsets.UTF_8)); return Optional.of(parentStatement); } return parent; } else if(child.isPresent()) { return child; } return Optional.absent(); } }
private void insertTriples(TransactionBase tx, final Collection<RyaStatement> triples) { for (final RyaStatement triple : triples) { Optional<byte[]> visibility = Optional.fromNullable(triple.getColumnVisibility()); try { tx.set(spoFormat(triple), FluoQueryColumns.TRIPLES, Bytes.of(visibility.or(new byte[0]))); } catch (final TripleRowResolverException e) { log.error("Could not convert a Triple into the SPO format: " + triple); } } }
private void insertTriples(TransactionBase tx, final Collection<RyaStatement> triples) { for (final RyaStatement triple : triples) { Optional<byte[]> visibility = Optional.fromNullable(triple.getColumnVisibility()); try { tx.set(spoFormat(triple), FluoQueryColumns.TRIPLES, Bytes.of(visibility.or(new byte[0]))); } catch (final TripleRowResolverException e) { log.error("Could not convert a Triple into the SPO format: " + triple); } } }
private void simplifyVisibilities(final RyaSubGraph subgraph) throws UnsupportedEncodingException { final Set<RyaStatement> statements = subgraph.getStatements(); if (statements.size() > 0) { final byte[] visibilityBytes = statements.iterator().next().getColumnVisibility(); // Simplify the result's visibilities and cache new simplified // visibilities final String visibility = new String(visibilityBytes, "UTF-8"); if (!simplifiedVisibilities.containsKey(visibility)) { final String simplified = VisibilitySimplifier.simplify(visibility); simplifiedVisibilities.put(visibility, simplified); } for (final RyaStatement statement : statements) { statement.setColumnVisibility(simplifiedVisibilities.get(visibility).getBytes("UTF-8")); } subgraph.setStatements(statements); } }
private void simplifyVisibilities(final RyaSubGraph subgraph) throws UnsupportedEncodingException { final Set<RyaStatement> statements = subgraph.getStatements(); if (statements.size() > 0) { final byte[] visibilityBytes = statements.iterator().next().getColumnVisibility(); // Simplify the result's visibilities and cache new simplified // visibilities final String visibility = new String(visibilityBytes, "UTF-8"); if (!simplifiedVisibilities.containsKey(visibility)) { final String simplified = VisibilitySimplifier.simplify(visibility); simplifiedVisibilities.put(visibility, simplified); } for (final RyaStatement statement : statements) { statement.setColumnVisibility(simplifiedVisibilities.get(visibility).getBytes("UTF-8")); } subgraph.setStatements(statements); } }
public VisibilityStatementSet(Set<RyaStatement> statements) { this.statements = new HashSet<>(); statements.forEach(x -> { this.statements.add(RyaToRdfConversions.convertStatement(x)); if (visibility == null) { if (x.getColumnVisibility() != null) { visibility = new String(x.getColumnVisibility()); } else { this.visibility = ""; } } }); }
public VisibilityStatementSet(Set<RyaStatement> statements) { this.statements = new HashSet<>(); statements.forEach(x -> { this.statements.add(RyaToRdfConversions.convertStatement(x)); if (visibility == null) { if (x.getColumnVisibility() != null) { visibility = new String(x.getColumnVisibility()); } else { this.visibility = ""; } } }); }
/** * Insert a batch of RyaStatements into Fluo. * * @param fluo - A connection to the Fluo table that will be updated. (not null) * @param triples - The triples to insert. (not null) */ public void insert(final FluoClient fluo, final Collection<RyaStatement> triples) { checkNotNull(fluo); checkNotNull(triples); try(Transaction tx = fluo.newTransaction()) { for(final RyaStatement triple : triples) { Optional<byte[]> visibility = Optional.fromNullable(triple.getColumnVisibility()); try { tx.set(spoFormat(triple), FluoQueryColumns.TRIPLES, Bytes.of(visibility.or(new byte[0]))); } catch (final TripleRowResolverException e) { log.error("Could not convert a Triple into the SPO format: " + triple); } } tx.commit(); } }
/** * Insert a batch of RyaStatements into Fluo. * * @param fluo - A connection to the Fluo table that will be updated. (not null) * @param triples - The triples to insert. (not null) */ public void insert(final FluoClient fluo, final Collection<RyaStatement> triples) { checkNotNull(fluo); checkNotNull(triples); try(Transaction tx = fluo.newTransaction()) { for(final RyaStatement triple : triples) { Optional<byte[]> visibility = Optional.fromNullable(triple.getColumnVisibility()); try { tx.set(spoFormat(triple), FluoQueryColumns.TRIPLES, Bytes.of(visibility.or(new byte[0]))); } catch (final TripleRowResolverException e) { log.error("Could not convert a Triple into the SPO format: " + triple); } } tx.commit(); } }
private void writeRyaMutations(final RyaStatement ryaStatement, final Context context, final boolean isDelete) throws IOException, InterruptedException { if (ryaStatement.getColumnVisibility() == null) { ryaStatement.setColumnVisibility(AccumuloRdfConstants.EMPTY_CV.getExpression()); } final Map<TABLE_LAYOUT, Collection<Mutation>> mutationMap = ryaTableMutationFactory.serialize(ryaStatement); final Collection<Mutation> spoMutations = mutationMap.get(TABLE_LAYOUT.SPO); final Collection<Mutation> poMutations = mutationMap.get(TABLE_LAYOUT.PO); final Collection<Mutation> ospMutations = mutationMap.get(TABLE_LAYOUT.OSP); for (final Mutation mutation : spoMutations) { writeMutation(spoTable, mutation, context, isDelete); } for (final Mutation mutation : poMutations) { writeMutation(poTable, mutation, context, isDelete); } for (final Mutation mutation : ospMutations) { writeMutation(ospTable, mutation, context, isDelete); } }
@Override public void add(final Iterator<RyaStatement> statementIter) throws RyaDAOException { final List<DBObject> dbInserts = new ArrayList<>(); while (statementIter.hasNext()){ final RyaStatement ryaStatement = statementIter.next(); final boolean canAdd = DocumentVisibilityUtil.doesUserHaveDocumentAccess(auths, ryaStatement.getColumnVisibility()); if (canAdd) { final DBObject insert = storageStrategy.serialize(ryaStatement); dbInserts.add(insert); try { for (final RyaSecondaryIndexer index : secondaryIndexers) { index.storeStatement(ryaStatement); } } catch (final IOException e) { log.error("Failed to add: " + ryaStatement.toString() + " to the indexer"); } } else { throw new RyaDAOException("User does not have the required authorizations to add statement"); } } try { mongoDbBatchWriter.addObjectsToQueue(dbInserts); if (flushEachUpdate.get()) { flush(); } } catch (final MongoDbBatchWriterException e) { throw new RyaDAOException("Error adding statements", e); } }
@Override public void delete(final Iterator<RyaStatement> statements, final StatefulMongoDBRdfConfiguration conf) throws RyaDAOException { while (statements.hasNext()){ final RyaStatement ryaStatement = statements.next(); final boolean canDelete = DocumentVisibilityUtil.doesUserHaveDocumentAccess(auths, ryaStatement.getColumnVisibility()); if (canDelete) { coll.remove(storageStrategy.getQuery(ryaStatement)); for (final RyaSecondaryIndexer index : secondaryIndexers) { try { index.deleteStatement(ryaStatement); } catch (final IOException e) { log.error("Unable to remove statement: " + ryaStatement.toString() + " from secondary indexer: " + index.getTableName(), e); } } } else { throw new RyaDAOException("User does not have the required authorizations to delete statement"); } } }
@Override public void delete(final Iterator<RyaStatement> statements, final StatefulMongoDBRdfConfiguration conf) throws RyaDAOException { while (statements.hasNext()){ final RyaStatement ryaStatement = statements.next(); final boolean canDelete = DocumentVisibilityUtil.doesUserHaveDocumentAccess(auths, ryaStatement.getColumnVisibility()); if (canDelete) { coll.remove(storageStrategy.getQuery(ryaStatement)); for (final RyaSecondaryIndexer index : secondaryIndexers) { try { index.deleteStatement(ryaStatement); } catch (final IOException e) { log.error("Unable to remove statement: " + ryaStatement.toString() + " from secondary indexer: " + index.getTableName(), e); } } } else { throw new RyaDAOException("User does not have the required authorizations to delete statement"); } } }
/** * Writes a (key,value) pair to Rya. Adds the statement to a buffer, and * flushes the statement buffer to the database if full. * @param key Arbitrary Writable, not used. * @param value Contains statement to insert to Rya. * @throws IOException if writing to Accumulo fails. */ @Override public void write(final Writable key, final RyaStatementWritable value) throws IOException { final RyaStatement ryaStatement = value.getRyaStatement(); if (ryaStatement.getColumnVisibility() == null) { ryaStatement.setColumnVisibility(cv); } if (ryaStatement.getContext() == null) { ryaStatement.setContext(defaultContext); } buffer.add(ryaStatement); bufferCurrentSize += statementSize(ryaStatement); if (bufferCurrentSize >= bufferSizeLimit) { flushBuffer(); } }
/** * Writes a (key,value) pair to Rya. Adds the statement to a buffer, and * flushes the statement buffer to the database if full. * @param key Arbitrary Writable, not used. * @param value Contains statement to insert to Rya. * @throws IOException if writing to Accumulo fails. */ @Override public void write(final Writable key, final RyaStatementWritable value) throws IOException { final RyaStatement ryaStatement = value.getRyaStatement(); if (ryaStatement.getColumnVisibility() == null) { ryaStatement.setColumnVisibility(cv); } if (ryaStatement.getContext() == null) { ryaStatement.setContext(defaultContext); } buffer.add(ryaStatement); bufferCurrentSize += statementSize(ryaStatement); if (bufferCurrentSize >= bufferSizeLimit) { flushBuffer(); } }
@Override public void delete(final RyaStatement statement, final StatefulMongoDBRdfConfiguration conf) throws RyaDAOException { final boolean canDelete = DocumentVisibilityUtil.doesUserHaveDocumentAccess(auths, statement.getColumnVisibility()); if (canDelete) { final DBObject obj = storageStrategy.getQuery(statement); coll.remove(obj); for (final RyaSecondaryIndexer index : secondaryIndexers) { try { index.deleteStatement(statement); } catch (final IOException e) { log.error("Unable to remove statement: " + statement.toString() + " from secondary indexer: " + index.getTableName(), e); } } } else { throw new RyaDAOException("User does not have the required authorizations to delete statement"); } }
@Override public void delete(final RyaStatement statement, final StatefulMongoDBRdfConfiguration conf) throws RyaDAOException { final boolean canDelete = DocumentVisibilityUtil.doesUserHaveDocumentAccess(auths, statement.getColumnVisibility()); if (canDelete) { final DBObject obj = storageStrategy.getQuery(statement); coll.remove(obj); for (final RyaSecondaryIndexer index : secondaryIndexers) { try { index.deleteStatement(statement); } catch (final IOException e) { log.error("Unable to remove statement: " + statement.toString() + " from secondary indexer: " + index.getTableName(), e); } } } else { throw new RyaDAOException("User does not have the required authorizations to delete statement"); } }
private static List<TripleRow> serializeStatement(final RyaStatement stmt) throws RyaTypeResolverException { final RyaURI subject = stmt.getSubject(); final RyaURI predicate = stmt.getPredicate(); final RyaType object = stmt.getObject(); final RyaURI context = stmt.getContext(); final Long timestamp = stmt.getTimestamp(); final byte[] columnVisibility = stmt.getColumnVisibility(); final byte[] value = stmt.getValue(); assert subject != null && predicate != null && object != null; final byte[] cf = (context == null) ? EMPTY_BYTES : context.getData().getBytes(StandardCharsets.UTF_8); final byte[] subjBytes = subject.getData().getBytes(StandardCharsets.UTF_8); final byte[] predBytes = predicate.getData().getBytes(StandardCharsets.UTF_8); final byte[][] objBytes = RyaContext.getInstance().serializeType(object); return Lists.newArrayList(new TripleRow(subjBytes, predBytes, Bytes.concat(cf, DELIM_BYTES, OBJECT.getBytes(StandardCharsets.UTF_8), DELIM_BYTES, objBytes[0], objBytes[1]), timestamp, columnVisibility, value), new TripleRow(objBytes[0], predBytes, Bytes.concat(cf, DELIM_BYTES, SUBJECT.getBytes(StandardCharsets.UTF_8), DELIM_BYTES, subjBytes, objBytes[1]), timestamp, columnVisibility, value)); }
/** * Comparison method for natural ordering. Compares based on the logical * triple (the s/p/o/context information in the underlying RyaStatement) * and then by the metadata contained in the RyaStatement if the triples are * the same. * @return Zero if both RyaStatementWritables contain equivalent statements * or both have null statements; otherwise, an integer whose sign * corresponds to a consistent ordering. */ @Override public int compareTo(RyaStatementWritable other) { CompareToBuilder builder = new CompareToBuilder(); RyaStatement rsThis = this.getRyaStatement(); RyaStatement rsOther = other.getRyaStatement(); // should throw NPE if other is null, as per Comparable contract builder.append(rsThis == null, rsOther == null); if (rsThis != null && rsOther != null) { builder.append(rsThis.getSubject(), rsOther.getSubject()); builder.append(rsThis.getPredicate(), rsOther.getPredicate()); builder.append(rsThis.getObject(), rsOther.getObject()); builder.append(rsThis.getContext(), rsOther.getContext()); builder.append(rsThis.getQualifer(), rsOther.getQualifer()); builder.append(rsThis.getColumnVisibility(), rsOther.getColumnVisibility()); builder.append(rsThis.getValue(), rsOther.getValue()); builder.append(rsThis.getTimestamp(), rsOther.getTimestamp()); } return builder.toComparison(); }