Search in sources :

Example 6 with RyaTypeResolverException

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

the class WholeRowTripleResolver method deserialize.

@Override
public RyaStatement deserialize(final TABLE_LAYOUT table_layout, final TripleRow tripleRow) throws TripleRowResolverException {
    try {
        assert tripleRow != null && table_layout != null;
        final byte[] row = tripleRow.getRow();
        final int firstIndex = Bytes.indexOf(row, DELIM_BYTE);
        final int secondIndex = Bytes.lastIndexOf(row, DELIM_BYTE);
        final int typeIndex = Bytes.indexOf(row, TYPE_DELIM_BYTE);
        final byte[] first = Arrays.copyOf(row, firstIndex);
        final byte[] second = Arrays.copyOfRange(row, firstIndex + 1, secondIndex);
        final byte[] third = Arrays.copyOfRange(row, secondIndex + 1, typeIndex);
        final byte[] type = Arrays.copyOfRange(row, typeIndex, row.length);
        final byte[] columnFamily = tripleRow.getColumnFamily();
        final boolean contextExists = columnFamily != null && columnFamily.length > 0;
        final RyaURI context = (contextExists) ? (new RyaURI(new String(columnFamily, StandardCharsets.UTF_8))) : null;
        final byte[] columnQualifier = tripleRow.getColumnQualifier();
        final String qualifier = columnQualifier != null && columnQualifier.length > 0 ? new String(columnQualifier, StandardCharsets.UTF_8) : null;
        final Long timestamp = tripleRow.getTimestamp();
        final byte[] columnVisibility = tripleRow.getColumnVisibility();
        final byte[] value = tripleRow.getValue();
        switch(table_layout) {
            case SPO:
                {
                    final byte[] obj = Bytes.concat(third, type);
                    return new RyaStatement(new RyaURI(new String(first, StandardCharsets.UTF_8)), new RyaURI(new String(second, StandardCharsets.UTF_8)), RyaContext.getInstance().deserialize(obj), context, qualifier, columnVisibility, value, timestamp);
                }
            case PO:
                {
                    final byte[] obj = Bytes.concat(second, type);
                    return new RyaStatement(new RyaURI(new String(third, StandardCharsets.UTF_8)), new RyaURI(new String(first, StandardCharsets.UTF_8)), RyaContext.getInstance().deserialize(obj), context, qualifier, columnVisibility, value, timestamp);
                }
            case OSP:
                {
                    final byte[] obj = Bytes.concat(first, type);
                    return new RyaStatement(new RyaURI(new String(second, StandardCharsets.UTF_8)), new RyaURI(new String(third, StandardCharsets.UTF_8)), RyaContext.getInstance().deserialize(obj), context, qualifier, columnVisibility, value, timestamp);
                }
        }
    } catch (final RyaTypeResolverException e) {
        throw new TripleRowResolverException(e);
    }
    throw new TripleRowResolverException("TripleRow[" + tripleRow + "] with Table layout[" + table_layout + "] is not deserializable");
}
Also used : TripleRowResolverException(org.apache.rya.api.resolver.triple.TripleRowResolverException) RyaURI(org.apache.rya.api.domain.RyaURI) RyaStatement(org.apache.rya.api.domain.RyaStatement) RyaTypeResolverException(org.apache.rya.api.resolver.RyaTypeResolverException)

Example 7 with RyaTypeResolverException

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

the class HashedPoWholeRowTriplePatternStrategy method defineRange.

@Override
public Map.Entry<RdfCloudTripleStoreConstants.TABLE_LAYOUT, ByteRange> defineRange(final RyaURI subject, final RyaURI predicate, final RyaType object, final RyaURI context, final RdfCloudTripleStoreConfiguration conf) throws IOException {
    try {
        // r(p)(ng)
        if (!handles(subject, predicate, object, context)) {
            return null;
        }
        final RyaContext ryaContext = RyaContext.getInstance();
        final MessageDigest md = MessageDigest.getInstance("MD5");
        final RdfCloudTripleStoreConstants.TABLE_LAYOUT table_layout = RdfCloudTripleStoreConstants.TABLE_LAYOUT.PO;
        byte[] start, stop;
        if (object != null) {
            if (object instanceof RyaRange) {
                // p_r(o)
                RyaRange rv = (RyaRange) object;
                rv = ryaContext.transformRange(rv);
                final byte[] objStartBytes = ryaContext.serializeType(rv.getStart())[0];
                final byte[] objEndBytes = ryaContext.serializeType(rv.getStop())[0];
                final byte[] predBytes = predicate.getData().getBytes(StandardCharsets.UTF_8);
                final byte[] predHash = Hex.encodeHexString(md.digest(predBytes)).getBytes(StandardCharsets.UTF_8);
                start = Bytes.concat(predHash, DELIM_BYTES, predBytes, DELIM_BYTES, objStartBytes);
                stop = Bytes.concat(predHash, DELIM_BYTES, predBytes, DELIM_BYTES, objEndBytes, DELIM_BYTES, LAST_BYTES);
            } else {
                if (subject != null && subject instanceof RyaRange) {
                    // po_r(s)
                    RyaRange ru = (RyaRange) subject;
                    ru = ryaContext.transformRange(ru);
                    final byte[] subjStartBytes = ru.getStart().getData().getBytes(StandardCharsets.UTF_8);
                    final byte[] subjStopBytes = ru.getStop().getData().getBytes(StandardCharsets.UTF_8);
                    final byte[] predBytes = predicate.getData().getBytes(StandardCharsets.UTF_8);
                    final byte[] predHash = Hex.encodeHexString(md.digest(predBytes)).getBytes(StandardCharsets.UTF_8);
                    final byte[] objBytes = ryaContext.serializeType(object)[0];
                    start = Bytes.concat(predHash, DELIM_BYTES, predBytes, DELIM_BYTES, objBytes, DELIM_BYTES, subjStartBytes);
                    stop = Bytes.concat(predHash, DELIM_BYTES, predBytes, DELIM_BYTES, objBytes, DELIM_BYTES, subjStopBytes, TYPE_DELIM_BYTES, LAST_BYTES);
                } else {
                    // po
                    // TODO: There must be a better way than creating multiple byte[]
                    final byte[] predBytes = predicate.getData().getBytes(StandardCharsets.UTF_8);
                    final byte[] predHash = Hex.encodeHexString(md.digest(predBytes)).getBytes(StandardCharsets.UTF_8);
                    final byte[] objBytes = ryaContext.serializeType(object)[0];
                    start = Bytes.concat(predHash, DELIM_BYTES, predBytes, DELIM_BYTES, objBytes, DELIM_BYTES);
                    stop = Bytes.concat(start, LAST_BYTES);
                }
            }
        } else {
            // p
            final byte[] predBytes = predicate.getData().getBytes(StandardCharsets.UTF_8);
            final byte[] predHash = Hex.encodeHexString(md.digest(predBytes)).getBytes(StandardCharsets.UTF_8);
            start = Bytes.concat(predHash, DELIM_BYTES, predBytes, DELIM_BYTES);
            stop = Bytes.concat(start, LAST_BYTES);
        }
        return new RdfCloudTripleStoreUtils.CustomEntry<RdfCloudTripleStoreConstants.TABLE_LAYOUT, ByteRange>(table_layout, new ByteRange(start, stop));
    } catch (final RyaTypeResolverException e) {
        throw new IOException(e);
    } catch (final NoSuchAlgorithmException e) {
        throw new IOException(e);
    }
}
Also used : RyaContext(org.apache.rya.api.resolver.RyaContext) ByteRange(org.apache.rya.api.query.strategy.ByteRange) RyaRange(org.apache.rya.api.domain.RyaRange) RyaTypeResolverException(org.apache.rya.api.resolver.RyaTypeResolverException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest) RdfCloudTripleStoreConstants(org.apache.rya.api.RdfCloudTripleStoreConstants)

Example 8 with RyaTypeResolverException

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

the class PoWholeRowTriplePatternStrategy method defineRange.

@Override
public Map.Entry<RdfCloudTripleStoreConstants.TABLE_LAYOUT, ByteRange> defineRange(final RyaURI subject, final RyaURI predicate, final RyaType object, final RyaURI context, final RdfCloudTripleStoreConfiguration conf) throws IOException {
    try {
        // r(p)(ng)
        if (!handles(subject, predicate, object, context)) {
            return null;
        }
        final RyaContext ryaContext = RyaContext.getInstance();
        final RdfCloudTripleStoreConstants.TABLE_LAYOUT table_layout = RdfCloudTripleStoreConstants.TABLE_LAYOUT.PO;
        byte[] start, stop;
        if (object != null) {
            if (object instanceof RyaRange) {
                // p_r(o)
                RyaRange rv = (RyaRange) object;
                rv = ryaContext.transformRange(rv);
                final byte[] objStartBytes = ryaContext.serializeType(rv.getStart())[0];
                final byte[] objEndBytes = ryaContext.serializeType(rv.getStop())[0];
                final byte[] predBytes = predicate.getData().getBytes(StandardCharsets.UTF_8);
                start = Bytes.concat(predBytes, DELIM_BYTES, objStartBytes);
                stop = Bytes.concat(predBytes, DELIM_BYTES, objEndBytes, DELIM_BYTES, LAST_BYTES);
            } else {
                if (subject != null && subject instanceof RyaRange) {
                    // po_r(s)
                    RyaRange ru = (RyaRange) subject;
                    ru = ryaContext.transformRange(ru);
                    final byte[] subjStartBytes = ru.getStart().getData().getBytes(StandardCharsets.UTF_8);
                    final byte[] subjStopBytes = ru.getStop().getData().getBytes(StandardCharsets.UTF_8);
                    final byte[] predBytes = predicate.getData().getBytes(StandardCharsets.UTF_8);
                    final byte[] objBytes = ryaContext.serializeType(object)[0];
                    start = Bytes.concat(predBytes, DELIM_BYTES, objBytes, DELIM_BYTES, subjStartBytes);
                    stop = Bytes.concat(predBytes, DELIM_BYTES, objBytes, DELIM_BYTES, subjStopBytes, TYPE_DELIM_BYTES, LAST_BYTES);
                } else {
                    // po
                    // TODO: There must be a better way than creating multiple byte[]
                    final byte[] objBytes = ryaContext.serializeType(object)[0];
                    start = Bytes.concat(predicate.getData().getBytes(StandardCharsets.UTF_8), DELIM_BYTES, objBytes, DELIM_BYTES);
                    stop = Bytes.concat(start, LAST_BYTES);
                }
            }
        } else if (predicate instanceof RyaRange) {
            // r(p)
            RyaRange rv = (RyaRange) predicate;
            rv = ryaContext.transformRange(rv);
            start = rv.getStart().getData().getBytes(StandardCharsets.UTF_8);
            stop = Bytes.concat(rv.getStop().getData().getBytes(StandardCharsets.UTF_8), DELIM_BYTES, LAST_BYTES);
        } else {
            // p
            start = Bytes.concat(predicate.getData().getBytes(StandardCharsets.UTF_8), DELIM_BYTES);
            stop = Bytes.concat(start, LAST_BYTES);
        }
        return new RdfCloudTripleStoreUtils.CustomEntry<RdfCloudTripleStoreConstants.TABLE_LAYOUT, ByteRange>(table_layout, new ByteRange(start, stop));
    } catch (final RyaTypeResolverException e) {
        throw new IOException(e);
    }
}
Also used : RyaContext(org.apache.rya.api.resolver.RyaContext) ByteRange(org.apache.rya.api.query.strategy.ByteRange) RyaRange(org.apache.rya.api.domain.RyaRange) RyaTypeResolverException(org.apache.rya.api.resolver.RyaTypeResolverException) IOException(java.io.IOException) RdfCloudTripleStoreConstants(org.apache.rya.api.RdfCloudTripleStoreConstants)

Example 9 with RyaTypeResolverException

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

the class AccumuloDocIdIndexer method deserializeKey.

private QueryBindingSet deserializeKey(final Key key, final StarQuery sq, final BindingSet currentBs, final Set<String> unCommonVar) {
    final QueryBindingSet currentSolutionBs = new QueryBindingSet();
    final Text row = key.getRow();
    final Text cq = key.getColumnQualifier();
    final String[] cqArray = cq.toString().split(DocIndexIteratorUtil.DOC_ID_INDEX_DELIM);
    boolean commonVarSet = false;
    // if common Var is constant there is no common variable to assign a value to
    if (sq.commonVarConstant()) {
        commonVarSet = true;
    }
    if (!commonVarSet && sq.isCommonVarURI()) {
        final RyaURI rURI = new RyaURI(row.toString());
        currentSolutionBs.addBinding(sq.getCommonVarName(), RyaToRdfConversions.convertValue(rURI));
        commonVarSet = true;
    }
    for (final String s : sq.getUnCommonVars()) {
        final byte[] cqBytes = cqArray[sq.getVarPos().get(s)].getBytes(StandardCharsets.UTF_8);
        final int firstIndex = Bytes.indexOf(cqBytes, DELIM_BYTE);
        final int secondIndex = Bytes.lastIndexOf(cqBytes, DELIM_BYTE);
        final int typeIndex = Bytes.indexOf(cqBytes, TYPE_DELIM_BYTE);
        final String tripleComponent = new String(Arrays.copyOfRange(cqBytes, firstIndex + 1, secondIndex), StandardCharsets.UTF_8);
        final byte[] cqContent = Arrays.copyOfRange(cqBytes, secondIndex + 1, typeIndex);
        final byte[] objType = Arrays.copyOfRange(cqBytes, typeIndex, cqBytes.length);
        if (tripleComponent.equals("object")) {
            final byte[] object = Bytes.concat(cqContent, objType);
            org.openrdf.model.Value v = null;
            try {
                v = RyaToRdfConversions.convertValue(RyaContext.getInstance().deserialize(object));
            } catch (final RyaTypeResolverException e) {
                e.printStackTrace();
            }
            currentSolutionBs.addBinding(s, v);
        } else if (tripleComponent.equals("subject")) {
            if (!commonVarSet) {
                final byte[] object = Bytes.concat(row.getBytes(), objType);
                org.openrdf.model.Value v = null;
                try {
                    v = RyaToRdfConversions.convertValue(RyaContext.getInstance().deserialize(object));
                } catch (final RyaTypeResolverException e) {
                    e.printStackTrace();
                }
                currentSolutionBs.addBinding(sq.getCommonVarName(), v);
                commonVarSet = true;
            }
            final RyaURI rURI = new RyaURI(new String(cqContent, StandardCharsets.UTF_8));
            currentSolutionBs.addBinding(s, RyaToRdfConversions.convertValue(rURI));
        } else {
            throw new IllegalArgumentException("Invalid row.");
        }
    }
    for (final String s : unCommonVar) {
        currentSolutionBs.addBinding(s, currentBs.getValue(s));
    }
    return currentSolutionBs;
}
Also used : Text(org.apache.hadoop.io.Text) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) RyaURI(org.apache.rya.api.domain.RyaURI) Value(org.apache.accumulo.core.data.Value) RyaTypeResolverException(org.apache.rya.api.resolver.RyaTypeResolverException)

Example 10 with RyaTypeResolverException

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

the class WholeRowTripleResolver 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 byte[] subjBytes = subject.getData().getBytes(StandardCharsets.UTF_8);
        final byte[] predBytes = predicate.getData().getBytes(StandardCharsets.UTF_8);
        final byte[][] objBytes = RyaContext.getInstance().serializeType(object);
        tripleRowMap.put(TABLE_LAYOUT.SPO, new TripleRow(Bytes.concat(subjBytes, DELIM_BYTES, predBytes, DELIM_BYTES, objBytes[0], objBytes[1]), cf, qualBytes, timestamp, columnVisibility, value));
        tripleRowMap.put(TABLE_LAYOUT.PO, new TripleRow(Bytes.concat(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);
    }
}
Also used : 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) HashMap(java.util.HashMap) RyaTypeResolverException(org.apache.rya.api.resolver.RyaTypeResolverException) RyaType(org.apache.rya.api.domain.RyaType)

Aggregations

RyaTypeResolverException (org.apache.rya.api.resolver.RyaTypeResolverException)16 IOException (java.io.IOException)5 TABLE_LAYOUT (org.apache.rya.api.RdfCloudTripleStoreConstants.TABLE_LAYOUT)5 RyaRange (org.apache.rya.api.domain.RyaRange)5 RyaType (org.apache.rya.api.domain.RyaType)5 RyaURI (org.apache.rya.api.domain.RyaURI)5 ByteRange (org.apache.rya.api.query.strategy.ByteRange)5 RyaContext (org.apache.rya.api.resolver.RyaContext)5 TripleRowResolverException (org.apache.rya.api.resolver.triple.TripleRowResolverException)4 MessageDigest (java.security.MessageDigest)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 HashMap (java.util.HashMap)2 RdfCloudTripleStoreConstants (org.apache.rya.api.RdfCloudTripleStoreConstants)2 RyaStatement (org.apache.rya.api.domain.RyaStatement)2 TripleRow (org.apache.rya.api.resolver.triple.TripleRow)2 Value (org.openrdf.model.Value)2 QueryBindingSet (org.openrdf.query.algebra.evaluation.QueryBindingSet)2 Date (java.util.Date)1 LinkedList (java.util.LinkedList)1 Value (org.apache.accumulo.core.data.Value)1