Search in sources :

Example 66 with QueryBindingSet

use of org.openrdf.query.algebra.evaluation.QueryBindingSet 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 67 with QueryBindingSet

use of org.openrdf.query.algebra.evaluation.QueryBindingSet in project incubator-rya by apache.

the class ConstructProjectionTest method testConstructProjectionProjectSubj.

@Test
public void testConstructProjectionProjectSubj() throws MalformedQueryException, UnsupportedEncodingException {
    String query = "select ?x where { ?x <uri:talksTo> <uri:Bob> }";
    SPARQLParser parser = new SPARQLParser();
    ParsedQuery pq = parser.parseQuery(query, null);
    List<StatementPattern> patterns = StatementPatternCollector.process(pq.getTupleExpr());
    ConstructProjection projection = new ConstructProjection(patterns.get(0));
    QueryBindingSet bs = new QueryBindingSet();
    bs.addBinding("x", vf.createURI("uri:Joe"));
    VisibilityBindingSet vBs = new VisibilityBindingSet(bs, "FOUO");
    RyaStatement statement = projection.projectBindingSet(vBs, new HashMap<>());
    RyaStatement expected = new RyaStatement(new RyaURI("uri:Joe"), new RyaURI("uri:talksTo"), new RyaURI("uri:Bob"));
    expected.setColumnVisibility("FOUO".getBytes("UTF-8"));
    expected.setTimestamp(statement.getTimestamp());
    assertEquals(expected, statement);
}
Also used : StatementPattern(org.openrdf.query.algebra.StatementPattern) RyaURI(org.apache.rya.api.domain.RyaURI) SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) ParsedQuery(org.openrdf.query.parser.ParsedQuery) RyaStatement(org.apache.rya.api.domain.RyaStatement) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) Test(org.junit.Test)

Example 68 with QueryBindingSet

use of org.openrdf.query.algebra.evaluation.QueryBindingSet in project incubator-rya by apache.

the class ConstructProjectionTest method testConstructProjectionBNodes.

@Test
public void testConstructProjectionBNodes() throws MalformedQueryException {
    String query = "select ?o where { _:b <uri:talksTo> ?o }";
    SPARQLParser parser = new SPARQLParser();
    ParsedQuery pq = parser.parseQuery(query, null);
    List<StatementPattern> patterns = StatementPatternCollector.process(pq.getTupleExpr());
    ConstructProjection projection = new ConstructProjection(patterns.get(0));
    QueryBindingSet bs = new QueryBindingSet();
    bs.addBinding("o", vf.createURI("uri:Bob"));
    VisibilityBindingSet vBs = new VisibilityBindingSet(bs);
    BNode bNode = vf.createBNode();
    Map<String, BNode> bNodeMap = new HashMap<>();
    bNodeMap.put("-anon-1", bNode);
    RyaStatement statement = projection.projectBindingSet(vBs, bNodeMap);
    RyaStatement expected = new RyaStatement(RdfToRyaConversions.convertResource(bNode), new RyaURI("uri:talksTo"), new RyaURI("uri:Bob"));
    expected.setTimestamp(statement.getTimestamp());
    expected.setColumnVisibility(new byte[0]);
    assertEquals(expected, statement);
}
Also used : SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) BNode(org.openrdf.model.BNode) ParsedQuery(org.openrdf.query.parser.ParsedQuery) HashMap(java.util.HashMap) RyaStatement(org.apache.rya.api.domain.RyaStatement) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) StatementPattern(org.openrdf.query.algebra.StatementPattern) RyaURI(org.apache.rya.api.domain.RyaURI) Test(org.junit.Test)

Example 69 with QueryBindingSet

use of org.openrdf.query.algebra.evaluation.QueryBindingSet in project incubator-rya by apache.

the class BatchInformationSerializerTest method testJoinBatchInformationSerialization.

@Test
public void testJoinBatchInformationSerialization() {
    QueryBindingSet bs = new QueryBindingSet();
    bs.addBinding("a", new URIImpl("urn:123"));
    bs.addBinding("b", new URIImpl("urn:456"));
    VisibilityBindingSet vBis = new VisibilityBindingSet(bs, "FOUO");
    JoinBatchInformation batch = JoinBatchInformation.builder().setBatchSize(1000).setTask(Task.Update).setColumn(FluoQueryColumns.PERIODIC_QUERY_BINDING_SET).setSpan(Span.prefix(Bytes.of("prefix346"))).setJoinType(JoinType.LEFT_OUTER_JOIN).setSide(Side.RIGHT).setBs(vBis).build();
    byte[] batchBytes = BatchInformationSerializer.toBytes(batch);
    Optional<BatchInformation> decodedBatch = BatchInformationSerializer.fromBytes(batchBytes);
    assertEquals(batch, decodedBatch.get());
}
Also used : VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) JoinBatchInformation(org.apache.rya.indexing.pcj.fluo.app.batch.JoinBatchInformation) BatchInformation(org.apache.rya.indexing.pcj.fluo.app.batch.BatchInformation) URIImpl(org.openrdf.model.impl.URIImpl) JoinBatchInformation(org.apache.rya.indexing.pcj.fluo.app.batch.JoinBatchInformation) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) Test(org.junit.Test)

Example 70 with QueryBindingSet

use of org.openrdf.query.algebra.evaluation.QueryBindingSet in project incubator-rya by apache.

the class BindingHashShardingFunctionTest method bindingSetRowTest.

@Test
public void bindingSetRowTest() {
    String nodeId = NodeType.generateNewFluoIdForType(NodeType.STATEMENT_PATTERN);
    QueryBindingSet bs = new QueryBindingSet();
    bs.addBinding("entity", vf.createURI("urn:entity"));
    bs.addBinding("location", vf.createLiteral("location_1"));
    VisibilityBindingSet vBs = new VisibilityBindingSet(bs);
    VariableOrder varOrder = new VariableOrder("entity", "location");
    Bytes row = RowKeyUtil.makeRowKey(nodeId, varOrder, vBs);
    Bytes shardedRow = BindingHashShardingFunction.addShard(nodeId, varOrder, vBs);
    BindingSetRow expected = BindingSetRow.make(row);
    BindingSetRow actual = BindingSetRow.makeFromShardedRow(Bytes.of(SP_PREFIX), shardedRow);
    Assert.assertEquals(expected, actual);
}
Also used : Bytes(org.apache.fluo.api.data.Bytes) VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) VariableOrder(org.apache.rya.indexing.pcj.storage.accumulo.VariableOrder) BindingSetRow(org.apache.rya.indexing.pcj.fluo.app.BindingSetRow) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) Test(org.junit.Test)

Aggregations

QueryBindingSet (org.openrdf.query.algebra.evaluation.QueryBindingSet)107 Test (org.junit.Test)84 BindingSet (org.openrdf.query.BindingSet)79 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)52 RyaURI (org.apache.rya.api.domain.RyaURI)46 RyaStatement (org.apache.rya.api.domain.RyaStatement)45 SPARQLParser (org.openrdf.query.parser.sparql.SPARQLParser)42 StatementPattern (org.openrdf.query.algebra.StatementPattern)41 ParsedQuery (org.openrdf.query.parser.ParsedQuery)41 RyaType (org.apache.rya.api.domain.RyaType)39 LiteralImpl (org.openrdf.model.impl.LiteralImpl)32 ArrayList (java.util.ArrayList)30 HashSet (java.util.HashSet)28 URIImpl (org.openrdf.model.impl.URIImpl)27 StatementMetadata (org.apache.rya.api.domain.StatementMetadata)26 VisibilityBindingSet (org.apache.rya.api.model.VisibilityBindingSet)20 StatementMetadataNode (org.apache.rya.indexing.statement.metadata.matching.StatementMetadataNode)15 Statement (org.openrdf.model.Statement)15 TupleExpr (org.openrdf.query.algebra.TupleExpr)14 Collection (java.util.Collection)13