Search in sources :

Example 1 with RyaRange

use of org.apache.rya.api.domain.RyaRange 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 RyaRange

use of org.apache.rya.api.domain.RyaRange 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 RyaRange

use of org.apache.rya.api.domain.RyaRange in project incubator-rya by apache.

the class AccumuloRyaQueryEngine method query.

@Override
public CloseableIterable<RyaStatement> query(RyaQuery ryaQuery) throws RyaDAOException {
    Preconditions.checkNotNull(ryaQuery);
    RyaStatement stmt = ryaQuery.getQuery();
    Preconditions.checkNotNull(stmt);
    // query configuration
    String[] auths = ryaQuery.getAuths();
    Authorizations authorizations = auths != null ? new Authorizations(auths) : configuration.getAuthorizations();
    Long ttl = ryaQuery.getTtl();
    Long currentTime = ryaQuery.getCurrentTime();
    Long maxResults = ryaQuery.getMaxResults();
    Integer batchSize = ryaQuery.getBatchSize();
    String regexSubject = ryaQuery.getRegexSubject();
    String regexPredicate = ryaQuery.getRegexPredicate();
    String regexObject = ryaQuery.getRegexObject();
    TableLayoutStrategy tableLayoutStrategy = configuration.getTableLayoutStrategy();
    try {
        // find triple pattern range
        TriplePatternStrategy strategy = ryaContext.retrieveStrategy(stmt);
        TABLE_LAYOUT layout;
        Range range;
        RyaURI subject = stmt.getSubject();
        RyaURI predicate = stmt.getPredicate();
        RyaType object = stmt.getObject();
        RyaURI context = stmt.getContext();
        String qualifier = stmt.getQualifer();
        TripleRowRegex tripleRowRegex = null;
        if (strategy != null) {
            // otherwise, full table scan is supported
            Map.Entry<RdfCloudTripleStoreConstants.TABLE_LAYOUT, ByteRange> entry = strategy.defineRange(subject, predicate, object, context, null);
            layout = entry.getKey();
            ByteRange byteRange = entry.getValue();
            range = new Range(new Text(byteRange.getStart()), new Text(byteRange.getEnd()));
        } else {
            range = new Range();
            layout = TABLE_LAYOUT.SPO;
            strategy = ryaContext.retrieveStrategy(layout);
        }
        byte[] objectTypeInfo = null;
        if (object != null) {
            // TODO: Not good to serialize this twice
            if (object instanceof RyaRange) {
                objectTypeInfo = RyaContext.getInstance().serializeType(((RyaRange) object).getStart())[1];
            } else {
                objectTypeInfo = RyaContext.getInstance().serializeType(object)[1];
            }
        }
        tripleRowRegex = strategy.buildRegex(regexSubject, regexPredicate, regexObject, null, objectTypeInfo);
        // use range to set scanner
        // populate scanner based on authorizations, ttl
        String table = layoutToTable(layout, tableLayoutStrategy);
        Scanner scanner = connector.createScanner(table, authorizations);
        scanner.setRange(range);
        if (batchSize != null) {
            scanner.setBatchSize(batchSize);
        }
        fillScanner(scanner, context, qualifier, ttl, currentTime, tripleRowRegex, ryaQuery.getConf());
        FluentCloseableIterable<RyaStatement> results = FluentCloseableIterable.from(new ScannerBaseCloseableIterable(scanner)).transform(keyValueToRyaStatementFunctionMap.get(layout));
        if (maxResults != null) {
            results = results.limit(maxResults.intValue());
        }
        return results;
    } catch (Exception e) {
        throw new RyaDAOException(e);
    }
}
Also used : TableLayoutStrategy(org.apache.rya.api.layout.TableLayoutStrategy) BatchScanner(org.apache.accumulo.core.client.BatchScanner) Scanner(org.apache.accumulo.core.client.Scanner) Authorizations(org.apache.accumulo.core.security.Authorizations) TriplePatternStrategy(org.apache.rya.api.query.strategy.TriplePatternStrategy) ByteRange(org.apache.rya.api.query.strategy.ByteRange) RyaStatement(org.apache.rya.api.domain.RyaStatement) Text(org.apache.hadoop.io.Text) RyaRange(org.apache.rya.api.domain.RyaRange) RyaRange(org.apache.rya.api.domain.RyaRange) Range(org.apache.accumulo.core.data.Range) ByteRange(org.apache.rya.api.query.strategy.ByteRange) RyaType(org.apache.rya.api.domain.RyaType) IOException(java.io.IOException) RyaDAOException(org.apache.rya.api.persist.RyaDAOException) TABLE_LAYOUT(org.apache.rya.api.RdfCloudTripleStoreConstants.TABLE_LAYOUT) RyaURI(org.apache.rya.api.domain.RyaURI) TripleRowRegex(org.apache.rya.api.resolver.triple.TripleRowRegex) RyaDAOException(org.apache.rya.api.persist.RyaDAOException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with RyaRange

use of org.apache.rya.api.domain.RyaRange 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 5 with RyaRange

use of org.apache.rya.api.domain.RyaRange 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)

Aggregations

IOException (java.io.IOException)6 RyaRange (org.apache.rya.api.domain.RyaRange)6 ByteRange (org.apache.rya.api.query.strategy.ByteRange)6 RyaContext (org.apache.rya.api.resolver.RyaContext)5 RyaTypeResolverException (org.apache.rya.api.resolver.RyaTypeResolverException)5 TABLE_LAYOUT (org.apache.rya.api.RdfCloudTripleStoreConstants.TABLE_LAYOUT)4 MessageDigest (java.security.MessageDigest)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 RdfCloudTripleStoreConstants (org.apache.rya.api.RdfCloudTripleStoreConstants)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 BatchScanner (org.apache.accumulo.core.client.BatchScanner)1 Scanner (org.apache.accumulo.core.client.Scanner)1 Range (org.apache.accumulo.core.data.Range)1 Authorizations (org.apache.accumulo.core.security.Authorizations)1 Text (org.apache.hadoop.io.Text)1 RyaStatement (org.apache.rya.api.domain.RyaStatement)1 RyaType (org.apache.rya.api.domain.RyaType)1 RyaURI (org.apache.rya.api.domain.RyaURI)1 TableLayoutStrategy (org.apache.rya.api.layout.TableLayoutStrategy)1