Search in sources :

Example 1 with TripleRow

use of org.apache.rya.api.resolver.triple.TripleRow in project incubator-rya by apache.

the class AccumuloRyaUtils method createRyaStatement.

/**
 * Creates a {@link RyaStatement} from a {@link Key}/{@link Value} pair.
 * @param key the {@link Key}.
 * @param value the {@link Value}.
 * @param ryaTripleContext the {@link RyaTripleContext}.
 * @return the converted {@link RyaStatement}.
 * @throws TripleRowResolverException
 */
public static RyaStatement createRyaStatement(final Key key, final Value value, final RyaTripleContext ryaTripleContext) throws TripleRowResolverException {
    final byte[] row = key.getRowData() != null && key.getRowData().toArray().length > 0 ? key.getRowData().toArray() : null;
    final byte[] columnFamily = key.getColumnFamilyData() != null && key.getColumnFamilyData().toArray().length > 0 ? key.getColumnFamilyData().toArray() : null;
    final byte[] columnQualifier = key.getColumnQualifierData() != null && key.getColumnQualifierData().toArray().length > 0 ? key.getColumnQualifierData().toArray() : null;
    final Long timestamp = key.getTimestamp();
    final byte[] columnVisibility = key.getColumnVisibilityData() != null && key.getColumnVisibilityData().toArray().length > 0 ? key.getColumnVisibilityData().toArray() : null;
    final byte[] valueBytes = value != null && value.get().length > 0 ? value.get() : null;
    final TripleRow tripleRow = new TripleRow(row, columnFamily, columnQualifier, timestamp, columnVisibility, valueBytes);
    final RyaStatement ryaStatement = ryaTripleContext.deserializeTriple(TABLE_LAYOUT.SPO, tripleRow);
    return ryaStatement;
}
Also used : TripleRow(org.apache.rya.api.resolver.triple.TripleRow) RyaStatement(org.apache.rya.api.domain.RyaStatement)

Example 2 with TripleRow

use of org.apache.rya.api.resolver.triple.TripleRow in project incubator-rya by apache.

the class RyaStatementWritable method readFields.

/**
 * Loads a RyaStatementWritable by reading data from an input stream.
 * Creates a new RyaStatement and assigns it to this RyaStatementWritable.
 * @param   dataInput   An stream containing serialized statement data.
 */
@Override
public void readFields(DataInput dataInput) throws IOException {
    byte[] row = read(dataInput);
    byte[] columnFamily = read(dataInput);
    byte[] columnQualifier = read(dataInput);
    byte[] columnVisibility = read(dataInput);
    byte[] value = read(dataInput);
    boolean b = dataInput.readBoolean();
    Long timestamp = null;
    if (b) {
        timestamp = dataInput.readLong();
    }
    try {
        ryaStatement = ryaContext.deserializeTriple(RdfCloudTripleStoreConstants.TABLE_LAYOUT.SPO, new TripleRow(row, columnFamily, columnQualifier));
        ryaStatement.setColumnVisibility(columnVisibility);
        ryaStatement.setValue(value);
        ryaStatement.setTimestamp(timestamp);
    } catch (TripleRowResolverException e) {
        throw new IOException(e);
    }
}
Also used : TripleRowResolverException(org.apache.rya.api.resolver.triple.TripleRowResolverException) TripleRow(org.apache.rya.api.resolver.triple.TripleRow) IOException(java.io.IOException)

Example 3 with TripleRow

use of org.apache.rya.api.resolver.triple.TripleRow in project incubator-rya by apache.

the class WholeRowHashedTripleResolver method serialize.

@Override
public Map<TABLE_LAYOUT, TripleRow> serialize(final RyaStatement stmt) throws TripleRowResolverException {
    try {
        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 String qualifer = stmt.getQualifer();
        final byte[] qualBytes = qualifer == null ? EMPTY_BYTES : qualifer.getBytes(StandardCharsets.UTF_8);
        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 Map<TABLE_LAYOUT, TripleRow> tripleRowMap = new HashMap<TABLE_LAYOUT, TripleRow>();
        final MessageDigest md = MessageDigest.getInstance("MD5");
        final byte[] subjBytes = subject.getData().getBytes(StandardCharsets.UTF_8);
        final byte[] subjHashBytes = md.digest(subjBytes);
        final byte[] predBytes = predicate.getData().getBytes(StandardCharsets.UTF_8);
        final byte[] predHashBytes = md.digest(predBytes);
        final byte[][] objBytes = RyaContext.getInstance().serializeType(object);
        tripleRowMap.put(TABLE_LAYOUT.SPO, new TripleRow(Bytes.concat(Hex.encodeHexString(subjHashBytes).getBytes(StandardCharsets.UTF_8), DELIM_BYTES, subjBytes, DELIM_BYTES, predBytes, DELIM_BYTES, objBytes[0], objBytes[1]), cf, qualBytes, timestamp, columnVisibility, value));
        tripleRowMap.put(TABLE_LAYOUT.PO, new TripleRow(Bytes.concat(Hex.encodeHexString(predHashBytes).getBytes(StandardCharsets.UTF_8), DELIM_BYTES, predBytes, DELIM_BYTES, objBytes[0], DELIM_BYTES, subjBytes, objBytes[1]), cf, qualBytes, timestamp, columnVisibility, value));
        tripleRowMap.put(TABLE_LAYOUT.OSP, new TripleRow(Bytes.concat(objBytes[0], DELIM_BYTES, subjBytes, DELIM_BYTES, predBytes, objBytes[1]), cf, qualBytes, timestamp, columnVisibility, value));
        return tripleRowMap;
    } catch (final RyaTypeResolverException e) {
        throw new TripleRowResolverException(e);
    } catch (final NoSuchAlgorithmException e) {
        throw new TripleRowResolverException(e);
    }
}
Also used : HashMap(java.util.HashMap) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) RyaType(org.apache.rya.api.domain.RyaType) TripleRowResolverException(org.apache.rya.api.resolver.triple.TripleRowResolverException) RyaURI(org.apache.rya.api.domain.RyaURI) TABLE_LAYOUT(org.apache.rya.api.RdfCloudTripleStoreConstants.TABLE_LAYOUT) TripleRow(org.apache.rya.api.resolver.triple.TripleRow) RyaTypeResolverException(org.apache.rya.api.resolver.RyaTypeResolverException) MessageDigest(java.security.MessageDigest)

Example 4 with TripleRow

use of org.apache.rya.api.resolver.triple.TripleRow in project incubator-rya by apache.

the class BaseRuleMapper method map.

@Override
protected void map(final Key key, final Value value, final Context context) throws IOException, InterruptedException {
    final TripleRow row = new TripleRow(key.getRowData().toArray(), key.getColumnFamilyData().toArray(), key.getColumnQualifierData().toArray(), key.getTimestamp(), key.getColumnVisibilityData().toArray(), value == null ? null : value.get());
    try {
        // If there's no layout, copy the row directly
        if (parentLayout == null) {
            copyRow(key, value, context);
            context.getCounter(Counters.DIRECT_ROWS_COPIED).increment(1);
        } else // If there is a layout, deserialize the statement and insert it if it meets the condition
        {
            final RyaStatement rs = resolver.deserialize(parentLayout, row);
            if (condition == null || CopyRule.accept(RyaToRdfConversions.convertStatement(rs), condition, strategy)) {
                copyStatement(rs, context);
                context.getCounter(Counters.STATEMENTS_COPIED).increment(1);
            }
        }
    } catch (final TripleRowResolverException e) {
        throw new IOException("Error deserializing triple", e);
    } catch (final QueryEvaluationException e) {
        throw new IOException("Error evaluating the filter condition", e);
    }
}
Also used : TripleRowResolverException(org.apache.rya.api.resolver.triple.TripleRowResolverException) TripleRow(org.apache.rya.api.resolver.triple.TripleRow) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) RyaStatement(org.apache.rya.api.domain.RyaStatement) IOException(java.io.IOException)

Example 5 with TripleRow

use of org.apache.rya.api.resolver.triple.TripleRow in project incubator-rya by apache.

the class MergeToolMapper method compareKeys.

/**
 * Since both Scanners will return sorted data, if the two key-values are
 * equal, then both Scanners can advance to the next comparison. If the Key
 * from Scanner1 sorts before the Key from Scanner2, then that Key doesn't
 * exist in the table from Scanner2 which means Scanner1 should advance. If
 * the Key from Scanner2 sorts before the Key from Scanner1, then that Key
 * doesn't exist in the table from Scanner1 which means Scanner2 should
 * advance.
 * @param key1 the {@link RyaStatement} from the parent instance table.
 * @param key2 the {@link RyaStatement} from the child instance table.
 * @return the {@link CompareKeysResult}.
 * @throws MutationsRejectedException
 * @throws IOException
 * @throws InterruptedException
 * @throws TripleRowResolverException
 */
private CompareKeysResult compareKeys(final RyaStatement key1, final RyaStatement key2) throws MutationsRejectedException, IOException, InterruptedException, TripleRowResolverException {
    log.trace("key1 = " + key1);
    log.trace("key2 = " + key2);
    if (key1 == null && key2 == null) {
        // Reached the end of the parent and child table.
        return CompareKeysResult.FINISHED;
    } else if (key1 == null) {
        // Reached the end of the parent table so add the remaining child keys if they meet the time criteria.
        final Date t2 = normalizeDate(new Date(key2.getTimestamp()), false);
        // Move on to next comparison (do nothing) or add this child key to parent
        final boolean doNothing = usesStartTime && t2.before(startTime);
        return doNothing ? CompareKeysResult.ADVANCE_CHILD : CompareKeysResult.ADVANCE_CHILD_AND_ADD;
    } else if (key2 == null) {
        // Reached the end of the child table so delete the remaining parent keys if they meet the time criteria.
        final Date t1 = normalizeDate(new Date(key1.getTimestamp()), true);
        // Move on to next comparison (do nothing) or delete this key from parent
        final boolean doNothing = usesStartTime && (copyToolInputTime != null && (t1.before(copyToolInputTime) || (t1.after(copyToolInputTime) && t1.after(startTime))) || (copyToolInputTime == null && t1.after(startTime)));
        return doNothing ? CompareKeysResult.ADVANCE_PARENT : CompareKeysResult.ADVANCE_PARENT_AND_DELETE;
    } else {
        // There are 2 keys to compare
        final Map<TABLE_LAYOUT, TripleRow> map1 = parentRyaContext.serializeTriple(key1);
        final Text row1 = new Text(map1.get(TABLE_LAYOUT.SPO).getRow());
        final Map<TABLE_LAYOUT, TripleRow> map2 = childRyaContext.serializeTriple(key2);
        final Text row2 = new Text(map2.get(TABLE_LAYOUT.SPO).getRow());
        final Date t1 = normalizeDate(new Date(key1.getTimestamp()), true);
        final Date t2 = normalizeDate(new Date(key2.getTimestamp()), false);
        if (row1.compareTo(row2) < 0) {
            // Parent key sort order was before the child key sort order
            // so it doesn't exist in the child table.
            // What does this mean?  Was it added by the parent after the child was cloned? (Meaning we should leave it)
            // Or did the child delete it after it was cloned? (Meaning we should delete it)
            final boolean doNothing = usesStartTime && (copyToolInputTime != null && (t1.before(copyToolInputTime) || (t1.after(copyToolInputTime) && t1.after(startTime))) || (copyToolInputTime == null && t1.after(startTime)));
            return doNothing ? CompareKeysResult.ADVANCE_PARENT : CompareKeysResult.ADVANCE_PARENT_AND_DELETE;
        } else if (row1.compareTo(row2) > 0) {
            // Parent key sort order was after the child key sort order
            // so it doesn't exist in the parent table.
            // What does this mean?  Was it deleted by the parent after the child was cloned? (Meaning we should leave it)
            // Or did the child add it after it was cloned? (Meaning we should add it)
            final boolean doNothing = usesStartTime && t2.before(startTime);
            return doNothing ? CompareKeysResult.ADVANCE_CHILD : CompareKeysResult.ADVANCE_CHILD_AND_ADD;
        } else {
            // move on to the next parent and child keys.
            return CompareKeysResult.ADVANCE_BOTH;
        }
    }
}
Also used : TABLE_LAYOUT(org.apache.rya.api.RdfCloudTripleStoreConstants.TABLE_LAYOUT) TripleRow(org.apache.rya.api.resolver.triple.TripleRow) Text(org.apache.hadoop.io.Text) Date(java.util.Date)

Aggregations

TripleRow (org.apache.rya.api.resolver.triple.TripleRow)55 RyaStatement (org.apache.rya.api.domain.RyaStatement)42 RyaURI (org.apache.rya.api.domain.RyaURI)21 Map (java.util.Map)19 TABLE_LAYOUT (org.apache.rya.api.RdfCloudTripleStoreConstants.TABLE_LAYOUT)17 Key (org.apache.accumulo.core.data.Key)14 Value (org.apache.accumulo.core.data.Value)13 ByteRange (org.apache.rya.api.query.strategy.ByteRange)13 TripleRowResolverException (org.apache.rya.api.resolver.triple.TripleRowResolverException)13 RyaType (org.apache.rya.api.domain.RyaType)11 RdfCloudTripleStoreConstants (org.apache.rya.api.RdfCloudTripleStoreConstants)8 Test (org.junit.Test)8 IOException (java.io.IOException)7 Text (org.apache.hadoop.io.Text)7 Mutation (org.apache.accumulo.core.data.Mutation)6 Scanner (org.apache.accumulo.core.client.Scanner)5 Authorizations (org.apache.accumulo.core.security.Authorizations)5 IntWritable (org.apache.hadoop.io.IntWritable)5 HashMap (java.util.HashMap)4 BatchWriter (org.apache.accumulo.core.client.BatchWriter)4