Search in sources :

Example 6 with RyaContext

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

the class SpoWholeRowTriplePatternStrategy 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 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[] predBytes = predicate.getData().getBytes(StandardCharsets.UTF_8);
                    start = Bytes.concat(subjBytes, DELIM_BYTES, predBytes, DELIM_BYTES, objStartBytes);
                    stop = Bytes.concat(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[] objBytes = ryaContext.serializeType(object)[0];
                    start = Bytes.concat(subject.getData().getBytes(StandardCharsets.UTF_8), 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[] predStartBytes = rv.getStart().getData().getBytes(StandardCharsets.UTF_8);
                final byte[] predStopBytes = rv.getStop().getData().getBytes(StandardCharsets.UTF_8);
                start = Bytes.concat(subjBytes, DELIM_BYTES, predStartBytes);
                stop = Bytes.concat(subjBytes, DELIM_BYTES, predStopBytes, DELIM_BYTES, LAST_BYTES);
            } else {
                // sp
                // range = sp
                start = Bytes.concat(subject.getData().getBytes(StandardCharsets.UTF_8), DELIM_BYTES, predicate.getData().getBytes(StandardCharsets.UTF_8), DELIM_BYTES);
                stop = Bytes.concat(start, LAST_BYTES);
            }
        } else if (subject instanceof RyaRange) {
            // r(s)
            // range = r(s.s) -> r(s.e)
            RyaRange ru = (RyaRange) subject;
            ru = ryaContext.transformRange(ru);
            start = ru.getStart().getData().getBytes(StandardCharsets.UTF_8);
            stop = Bytes.concat(ru.getStop().getData().getBytes(StandardCharsets.UTF_8), DELIM_BYTES, LAST_BYTES);
        } else {
            // s
            // range = s
            start = Bytes.concat(subject.getData().getBytes(StandardCharsets.UTF_8), 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 7 with RyaContext

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

the class DocumentIndexIntersectingIteratorTest method testSerialization1.

@Test
public void testSerialization1() throws Exception {
    BatchWriter bw = null;
    AccumuloRdfConfiguration acc = new AccumuloRdfConfiguration();
    acc.set(AccumuloRdfConfiguration.CONF_ADDITIONAL_INDEXERS, EntityCentricIndex.class.getName());
    RyaTableMutationsFactory rtm = new RyaTableMutationsFactory(RyaTripleContext.getInstance(acc));
    bw = accCon.createBatchWriter(tablename, 500L * 1024L * 1024L, Long.MAX_VALUE, 30);
    for (int i = 0; i < 20; i++) {
        RyaStatement rs1 = new RyaStatement(new RyaURI("uri:" + i), new RyaURI("uri:cf1"), new RyaType(XMLSchema.STRING, "cq1"));
        RyaStatement rs2 = new RyaStatement(new RyaURI("uri:" + i), new RyaURI("uri:cf2"), new RyaType(XMLSchema.STRING, "cq2"));
        RyaStatement rs3 = null;
        RyaStatement rs4 = null;
        if (i == 5 || i == 15) {
            rs3 = new RyaStatement(new RyaURI("uri:" + i), new RyaURI("uri:cf3"), new RyaType(XMLSchema.INTEGER, Integer.toString(i)));
            rs4 = new RyaStatement(new RyaURI("uri:" + i), new RyaURI("uri:cf3"), new RyaType(XMLSchema.STRING, Integer.toString(i)));
        }
        Collection<Mutation> m1 = EntityCentricIndex.createMutations(rs1);
        for (Mutation m : m1) {
            bw.addMutation(m);
        }
        Collection<Mutation> m2 = EntityCentricIndex.createMutations(rs2);
        for (Mutation m : m2) {
            bw.addMutation(m);
        }
        if (rs3 != null) {
            Collection<Mutation> m3 = EntityCentricIndex.createMutations(rs3);
            for (Mutation m : m3) {
                bw.addMutation(m);
            }
        }
        if (rs4 != null) {
            Collection<Mutation> m4 = EntityCentricIndex.createMutations(rs4);
            for (Mutation m : m4) {
                bw.addMutation(m);
            }
        }
    }
    String q1 = // 
    "" + // 
    "SELECT ?X ?Y1 ?Y2 " + // 
    "{" + // 
    "?X <uri:cf1> ?Y1 ." + // 
    "?X <uri:cf2> ?Y2 ." + // 
    "?X <uri:cf3> 5 ." + "}";
    String q2 = // 
    "" + // 
    "SELECT ?X ?Y1 ?Y2 " + // 
    "{" + // 
    "?X <uri:cf1> ?Y1  ." + // 
    "?X <uri:cf2> ?Y2 ." + // 
    "?X <uri:cf3> \"15\" ." + "}";
    SPARQLParser parser = new SPARQLParser();
    ParsedQuery pq1 = parser.parseQuery(q1, null);
    ParsedQuery pq2 = parser.parseQuery(q2, null);
    TupleExpr te1 = pq1.getTupleExpr();
    TupleExpr te2 = pq2.getTupleExpr();
    List<StatementPattern> spList1 = StatementPatternCollector.process(te1);
    List<StatementPattern> spList2 = StatementPatternCollector.process(te2);
    System.out.println(spList1);
    System.out.println(spList2);
    RyaType rt1 = RdfToRyaConversions.convertValue(spList1.get(2).getObjectVar().getValue());
    RyaType rt2 = RdfToRyaConversions.convertValue(spList2.get(2).getObjectVar().getValue());
    RyaURI predURI1 = (RyaURI) RdfToRyaConversions.convertValue(spList1.get(0).getPredicateVar().getValue());
    RyaURI predURI2 = (RyaURI) RdfToRyaConversions.convertValue(spList1.get(1).getPredicateVar().getValue());
    RyaURI predURI3 = (RyaURI) RdfToRyaConversions.convertValue(spList1.get(2).getPredicateVar().getValue());
    // System.out.println("to string" + spList1.get(2).getObjectVar().getValue().stringValue());
    // System.out.println("converted obj" + rt1.getData());
    // System.out.println("equal: " + rt1.getData().equals(spList1.get(2).getObjectVar().getValue().stringValue()));
    System.out.println(rt1);
    System.out.println(rt2);
    RyaContext rc = RyaContext.getInstance();
    byte[][] b1 = rc.serializeType(rt1);
    byte[][] b2 = rc.serializeType(rt2);
    byte[] b3 = Bytes.concat("object".getBytes(), "\u0000".getBytes(), b1[0], b1[1]);
    byte[] b4 = Bytes.concat("object".getBytes(), "\u0000".getBytes(), b2[0], b2[1]);
    System.out.println(new String(b3));
    System.out.println(new String(b4));
    TextColumn tc1 = new TextColumn(new Text(predURI1.getData()), new Text("object"));
    TextColumn tc2 = new TextColumn(new Text(predURI2.getData()), new Text("object"));
    TextColumn tc3 = new TextColumn(new Text(predURI3.getData()), new Text(b3));
    tc1.setIsPrefix(true);
    tc2.setIsPrefix(true);
    TextColumn[] tc = new TextColumn[3];
    tc[0] = tc1;
    tc[1] = tc2;
    tc[2] = tc3;
    IteratorSetting is = new IteratorSetting(30, "fii", DocumentIndexIntersectingIterator.class);
    DocumentIndexIntersectingIterator.setColumnFamilies(is, tc);
    Scanner scan = accCon.createScanner(tablename, new Authorizations("auths"));
    scan.addScanIterator(is);
    int results = 0;
    System.out.println("************************Test 21****************************");
    Text t = null;
    for (Map.Entry<Key, Value> e : scan) {
        t = e.getKey().getColumnQualifier();
        System.out.println(e);
        results++;
    }
    Assert.assertEquals(1, results);
    String[] s = t.toString().split("\u001D" + "\u001E");
    String[] s1 = s[2].split("\u0000");
    RyaType rt = rc.deserialize(s1[2].getBytes());
    System.out.println("Rya type is " + rt);
    org.openrdf.model.Value v = RyaToRdfConversions.convertValue(rt);
    Assert.assertTrue(v.equals(spList1.get(2).getObjectVar().getValue()));
    tc1 = new TextColumn(new Text(predURI1.getData()), new Text("object"));
    tc2 = new TextColumn(new Text(predURI2.getData()), new Text("object"));
    tc3 = new TextColumn(new Text(predURI3.getData()), new Text(b4));
    tc1.setIsPrefix(true);
    tc2.setIsPrefix(true);
    tc = new TextColumn[3];
    tc[0] = tc1;
    tc[1] = tc2;
    tc[2] = tc3;
    is = new IteratorSetting(30, "fii", DocumentIndexIntersectingIterator.class);
    DocumentIndexIntersectingIterator.setColumnFamilies(is, tc);
    scan = accCon.createScanner(tablename, new Authorizations("auths"));
    scan.addScanIterator(is);
    results = 0;
    System.out.println("************************Test 21****************************");
    for (Map.Entry<Key, Value> e : scan) {
        t = e.getKey().getColumnQualifier();
        System.out.println(e);
        results++;
    }
    Assert.assertEquals(1, results);
    s = t.toString().split("\u001D" + "\u001E");
    s1 = s[2].split("\u0000");
    rt = rc.deserialize(s1[2].getBytes());
    System.out.println("Rya type is " + rt);
    v = RyaToRdfConversions.convertValue(rt);
    Assert.assertTrue(v.equals(spList2.get(2).getObjectVar().getValue()));
}
Also used : Scanner(org.apache.accumulo.core.client.Scanner) ParsedQuery(org.openrdf.query.parser.ParsedQuery) EntityCentricIndex(org.apache.rya.indexing.accumulo.entity.EntityCentricIndex) RyaStatement(org.apache.rya.api.domain.RyaStatement) RyaType(org.apache.rya.api.domain.RyaType) StatementPattern(org.openrdf.query.algebra.StatementPattern) RyaContext(org.apache.rya.api.resolver.RyaContext) SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) Authorizations(org.apache.accumulo.core.security.Authorizations) Text(org.apache.hadoop.io.Text) AccumuloRdfConfiguration(org.apache.rya.accumulo.AccumuloRdfConfiguration) TupleExpr(org.openrdf.query.algebra.TupleExpr) RyaURI(org.apache.rya.api.domain.RyaURI) IteratorSetting(org.apache.accumulo.core.client.IteratorSetting) RyaTableMutationsFactory(org.apache.rya.accumulo.RyaTableMutationsFactory) Value(org.apache.accumulo.core.data.Value) BatchWriter(org.apache.accumulo.core.client.BatchWriter) Mutation(org.apache.accumulo.core.data.Mutation) Map(java.util.Map) Key(org.apache.accumulo.core.data.Key) Test(org.junit.Test)

Aggregations

RyaContext (org.apache.rya.api.resolver.RyaContext)7 IOException (java.io.IOException)5 RyaRange (org.apache.rya.api.domain.RyaRange)5 ByteRange (org.apache.rya.api.query.strategy.ByteRange)5 RyaTypeResolverException (org.apache.rya.api.resolver.RyaTypeResolverException)5 TABLE_LAYOUT (org.apache.rya.api.RdfCloudTripleStoreConstants.TABLE_LAYOUT)3 MessageDigest (java.security.MessageDigest)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 Text (org.apache.hadoop.io.Text)2 RdfCloudTripleStoreConstants (org.apache.rya.api.RdfCloudTripleStoreConstants)2 RyaType (org.apache.rya.api.domain.RyaType)2 RyaURI (org.apache.rya.api.domain.RyaURI)2 Map (java.util.Map)1 BatchWriter (org.apache.accumulo.core.client.BatchWriter)1 IteratorSetting (org.apache.accumulo.core.client.IteratorSetting)1 Scanner (org.apache.accumulo.core.client.Scanner)1 Key (org.apache.accumulo.core.data.Key)1 Mutation (org.apache.accumulo.core.data.Mutation)1 Value (org.apache.accumulo.core.data.Value)1 Authorizations (org.apache.accumulo.core.security.Authorizations)1