Search in sources :

Example 1 with RyaTypeResolverException

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

the class HashedSpoWholeRowTriplePatternStrategy method defineRange.

@Override
public Map.Entry<TABLE_LAYOUT, ByteRange> defineRange(final RyaURI subject, final RyaURI predicate, final RyaType object, final RyaURI context, final RdfCloudTripleStoreConfiguration conf) throws IOException {
    try {
        // s_r(p)(ng)
        if (!handles(subject, predicate, object, context)) {
            return null;
        }
        final MessageDigest md = MessageDigest.getInstance("MD5");
        final RyaContext ryaContext = RyaContext.getInstance();
        final TABLE_LAYOUT table_layout = TABLE_LAYOUT.SPO;
        byte[] start;
        byte[] stop;
        if (predicate != null) {
            if (object != null) {
                if (object instanceof RyaRange) {
                    // sp_r(o)
                    // range = sp_r(o.s)->sp_r(o.e) (remove last byte to remove type info)
                    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[] subjBytes = subject.getData().getBytes(StandardCharsets.UTF_8);
                    final byte[] hashSubj = Hex.encodeHexString(md.digest(subjBytes)).getBytes(StandardCharsets.UTF_8);
                    final byte[] predBytes = predicate.getData().getBytes(StandardCharsets.UTF_8);
                    start = Bytes.concat(hashSubj, DELIM_BYTES, subjBytes, DELIM_BYTES, predBytes, DELIM_BYTES, objStartBytes);
                    stop = Bytes.concat(hashSubj, DELIM_BYTES, subjBytes, DELIM_BYTES, predBytes, DELIM_BYTES, objEndBytes, DELIM_BYTES, LAST_BYTES);
                } else {
                    // spo
                    // range = spo->spo (remove last byte to remove type info)
                    // TODO: There must be a better way than creating multiple byte[]
                    final byte[] subjBytes = subject.getData().getBytes(StandardCharsets.UTF_8);
                    final byte[] hashSubj = Hex.encodeHexString(md.digest(subjBytes)).getBytes(StandardCharsets.UTF_8);
                    final byte[] objBytes = ryaContext.serializeType(object)[0];
                    start = Bytes.concat(hashSubj, DELIM_BYTES, subjBytes, DELIM_BYTES, predicate.getData().getBytes(StandardCharsets.UTF_8), DELIM_BYTES, objBytes, TYPE_DELIM_BYTES);
                    stop = Bytes.concat(start, LAST_BYTES);
                }
            } else if (predicate instanceof RyaRange) {
                // s_r(p)
                // range = s_r(p.s)->s_r(p.e)
                RyaRange rv = (RyaRange) predicate;
                rv = ryaContext.transformRange(rv);
                final byte[] subjBytes = subject.getData().getBytes(StandardCharsets.UTF_8);
                final byte[] hashSubj = Hex.encodeHexString(md.digest(subjBytes)).getBytes(StandardCharsets.UTF_8);
                final byte[] predStartBytes = rv.getStart().getData().getBytes(StandardCharsets.UTF_8);
                final byte[] predStopBytes = rv.getStop().getData().getBytes(StandardCharsets.UTF_8);
                start = Bytes.concat(hashSubj, DELIM_BYTES, subjBytes, DELIM_BYTES, predStartBytes);
                stop = Bytes.concat(hashSubj, DELIM_BYTES, subjBytes, DELIM_BYTES, predStopBytes, DELIM_BYTES, LAST_BYTES);
            } else {
                // sp
                // range = sp
                final byte[] subjBytes = subject.getData().getBytes(StandardCharsets.UTF_8);
                final byte[] hashSubj = Hex.encodeHexString(md.digest(subjBytes)).getBytes(StandardCharsets.UTF_8);
                start = Bytes.concat(hashSubj, DELIM_BYTES, subjBytes, DELIM_BYTES, predicate.getData().getBytes(StandardCharsets.UTF_8), DELIM_BYTES);
                stop = Bytes.concat(start, LAST_BYTES);
            }
        } else {
            // s
            // range = s
            final byte[] subjBytes = subject.getData().getBytes(StandardCharsets.UTF_8);
            final byte[] hashSubj = Hex.encodeHexString(md.digest(subjBytes)).getBytes(StandardCharsets.UTF_8);
            start = Bytes.concat(hashSubj, DELIM_BYTES, subjBytes, DELIM_BYTES);
            stop = Bytes.concat(start, LAST_BYTES);
        }
        return new RdfCloudTripleStoreUtils.CustomEntry<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 : TABLE_LAYOUT(org.apache.rya.api.RdfCloudTripleStoreConstants.TABLE_LAYOUT) 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)

Example 2 with RyaTypeResolverException

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

the class OspWholeRowTriplePatternStrategy method defineRange.

@Override
public Map.Entry<TABLE_LAYOUT, ByteRange> defineRange(final RyaURI subject, final RyaURI predicate, final RyaType object, final RyaURI context, final RdfCloudTripleStoreConfiguration conf) throws IOException {
    try {
        // r(o)
        if (!handles(subject, predicate, object, context)) {
            return null;
        }
        final RyaContext ryaContext = RyaContext.getInstance();
        final TABLE_LAYOUT table_layout = TABLE_LAYOUT.OSP;
        byte[] start, stop;
        if (subject != null) {
            if (subject instanceof RyaRange) {
                // o_r(s)
                RyaRange ru = (RyaRange) subject;
                ru = ryaContext.transformRange(ru);
                final byte[] subjStartBytes = ru.getStart().getData().getBytes(StandardCharsets.UTF_8);
                final byte[] subjEndBytes = ru.getStop().getData().getBytes(StandardCharsets.UTF_8);
                final byte[] objBytes = ryaContext.serializeType(object)[0];
                start = Bytes.concat(objBytes, DELIM_BYTES, subjStartBytes);
                stop = Bytes.concat(objBytes, DELIM_BYTES, subjEndBytes, DELIM_BYTES, LAST_BYTES);
            } else {
                // os
                final byte[] objBytes = ryaContext.serializeType(object)[0];
                start = Bytes.concat(objBytes, DELIM_BYTES, subject.getData().getBytes(StandardCharsets.UTF_8), DELIM_BYTES);
                stop = Bytes.concat(start, LAST_BYTES);
            }
        } else {
            if (object instanceof RyaRange) {
                // r(o)
                RyaRange rv = (RyaRange) object;
                rv = ryaContext.transformRange(rv);
                start = ryaContext.serializeType(rv.getStart())[0];
                stop = Bytes.concat(ryaContext.serializeType(rv.getStop())[0], DELIM_BYTES, LAST_BYTES);
            } else {
                // o
                start = Bytes.concat(ryaContext.serializeType(object)[0], DELIM_BYTES);
                stop = Bytes.concat(start, LAST_BYTES);
            }
        }
        return new RdfCloudTripleStoreUtils.CustomEntry<TABLE_LAYOUT, ByteRange>(table_layout, new ByteRange(start, stop));
    } catch (final RyaTypeResolverException e) {
        throw new IOException(e);
    }
}
Also used : TABLE_LAYOUT(org.apache.rya.api.RdfCloudTripleStoreConstants.TABLE_LAYOUT) 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)

Example 3 with RyaTypeResolverException

use of org.apache.rya.api.resolver.RyaTypeResolverException 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 RyaTypeResolverException

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

the class RyaTypeResolverImpl method deserialize.

@Override
public RyaType deserialize(final byte[] bytes) throws RyaTypeResolverException {
    if (!deserializable(bytes)) {
        throw new RyaTypeResolverException("Bytes not deserializable");
    }
    final RyaType rt = newInstance();
    rt.setDataType(getRyaDataType());
    final String data = new String(bytes, 0, bytes.length - 2, StandardCharsets.UTF_8);
    rt.setData(deserializeData(data));
    return rt;
}
Also used : RyaTypeResolverException(org.apache.rya.api.resolver.RyaTypeResolverException) RyaType(org.apache.rya.api.domain.RyaType)

Example 5 with RyaTypeResolverException

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

the class AccumuloPcjSerializer method convert.

@Override
public byte[] convert(BindingSet bindingSet, VariableOrder varOrder) throws BindingSetConversionException {
    checkNotNull(bindingSet);
    checkNotNull(varOrder);
    // A list that holds all of the byte segments that will be concatenated at the end.
    // This minimizes byte[] construction.
    final List<byte[]> byteSegments = new LinkedList<>();
    try {
        for (final String varName : varOrder) {
            // Only write information for a variable name if the binding set contains it.
            if (bindingSet.hasBinding(varName)) {
                final RyaType rt = RdfToRyaConversions.convertValue(bindingSet.getBinding(varName).getValue());
                final byte[][] serializedVal = RyaContext.getInstance().serializeType(rt);
                byteSegments.add(serializedVal[0]);
                byteSegments.add(serializedVal[1]);
            }
            // But always write the value delimiter. If a value is missing, you'll see two delimiters next to each-other.
            byteSegments.add(DELIM_BYTES);
        }
        return concat(byteSegments);
    } catch (RyaTypeResolverException e) {
        throw new BindingSetConversionException("Could not convert the BindingSet into a byte[].", e);
    }
}
Also used : RyaTypeResolverException(org.apache.rya.api.resolver.RyaTypeResolverException) RyaType(org.apache.rya.api.domain.RyaType) LinkedList(java.util.LinkedList)

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