use of org.apache.rya.api.domain.RyaType 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);
}
}
use of org.apache.rya.api.domain.RyaType 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;
}
use of org.apache.rya.api.domain.RyaType in project incubator-rya by apache.
the class MongoFreeTextIndexerIT method testRestrictPredicatesSearch.
@Test
public void testRestrictPredicatesSearch() throws Exception {
conf.setStrings(ConfigUtils.FREETEXT_PREDICATES_LIST, "pred:1,pred:2");
try (MongoFreeTextIndexer f = new MongoFreeTextIndexer()) {
f.setConf(conf);
f.init();
// These should not be stored because they are not in the predicate list
f.storeStatement(new RyaStatement(new RyaURI("foo:subj1"), new RyaURI(RDFS.LABEL.toString()), new RyaType("invalid")));
f.storeStatement(new RyaStatement(new RyaURI("foo:subj2"), new RyaURI(RDFS.COMMENT.toString()), new RyaType("invalid")));
final RyaURI pred1 = new RyaURI("pred:1");
final RyaURI pred2 = new RyaURI("pred:2");
// These should be stored because they are in the predicate list
final RyaStatement s3 = new RyaStatement(new RyaURI("foo:subj3"), pred1, new RyaType("valid"));
final RyaStatement s4 = new RyaStatement(new RyaURI("foo:subj4"), pred2, new RyaType("valid"));
f.storeStatement(s3);
f.storeStatement(s4);
// This should not be stored because the object is not a literal
f.storeStatement(new RyaStatement(new RyaURI("foo:subj5"), pred1, new RyaURI("in:validURI")));
f.flush();
assertEquals(Sets.newHashSet(), getSet(f.queryText("invalid", EMPTY_CONSTRAINTS)));
assertEquals(Sets.newHashSet(), getSet(f.queryText("in:validURI", EMPTY_CONSTRAINTS)));
final Set<Statement> actual = getSet(f.queryText("valid", EMPTY_CONSTRAINTS));
assertEquals(2, actual.size());
assertTrue(actual.contains(RyaToRdfConversions.convertStatement(s3)));
assertTrue(actual.contains(RyaToRdfConversions.convertStatement(s4)));
}
}
use of org.apache.rya.api.domain.RyaType in project incubator-rya by apache.
the class AccumuloStatementMetadataNodeTest method simpleQueryWithBindingSetJoinOnProperty.
/**
* Tests if the StatementMetadataNode joins BindingSet correctly for
* variables appearing in metadata statements. In this case, the metadata
* statements are (_:blankNode <http://createdOn 2017-01-04 ) and
* (_:blankNode <http://createdBy> ?y). The variable ?y appears as the
* object in the above metadata statement and its values are joined to the
* constraint BindingSets in the example below.
*
* @throws MalformedQueryException
* @throws QueryEvaluationException
* @throws RyaDAOException
*/
@Test
public void simpleQueryWithBindingSetJoinOnProperty() throws MalformedQueryException, QueryEvaluationException, RyaDAOException {
StatementMetadata metadata = new StatementMetadata();
metadata.addMetadata(new RyaURI("http://createdBy"), new RyaType("Joe"));
metadata.addMetadata(new RyaURI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-01-04"));
RyaStatement statement1 = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), new RyaType("CoffeeShop"), new RyaURI("http://context"), "", metadata);
dao.add(statement1);
SPARQLParser parser = new SPARQLParser();
ParsedQuery pq = parser.parseQuery(query, null);
List<StatementPattern> spList = StatementPatternCollector.process(pq.getTupleExpr());
StatementMetadataNode<AccumuloRdfConfiguration> node = new StatementMetadataNode<>(spList, conf);
QueryBindingSet bsConstraint = new QueryBindingSet();
bsConstraint.addBinding("x", new LiteralImpl("CoffeeShop"));
bsConstraint.addBinding("y", new LiteralImpl("Doug"));
CloseableIteration<BindingSet, QueryEvaluationException> iteration = node.evaluate(bsConstraint);
List<BindingSet> bsList = new ArrayList<>();
while (iteration.hasNext()) {
bsList.add(iteration.next());
}
Assert.assertEquals(0, bsList.size());
dao.delete(statement1, conf);
}
use of org.apache.rya.api.domain.RyaType in project incubator-rya by apache.
the class AccumuloStatementMetadataNodeTest method simpleQueryWithBindingSet.
@Test
public void simpleQueryWithBindingSet() throws MalformedQueryException, QueryEvaluationException, RyaDAOException {
StatementMetadata metadata = new StatementMetadata();
metadata.addMetadata(new RyaURI("http://createdBy"), new RyaType("Joe"));
metadata.addMetadata(new RyaURI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-01-04"));
RyaStatement statement1 = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), new RyaType("CoffeeShop"), new RyaURI("http://context"), "", metadata);
RyaStatement statement2 = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), new RyaType("HardwareStore"), new RyaURI("http://context"), "", metadata);
dao.add(statement1);
dao.add(statement2);
SPARQLParser parser = new SPARQLParser();
ParsedQuery pq = parser.parseQuery(query, null);
List<StatementPattern> spList = StatementPatternCollector.process(pq.getTupleExpr());
StatementMetadataNode<AccumuloRdfConfiguration> node = new StatementMetadataNode<>(spList, conf);
QueryBindingSet bsConstraint = new QueryBindingSet();
bsConstraint.addBinding("x", new LiteralImpl("CoffeeShop"));
bsConstraint.addBinding("z", new LiteralImpl("Virginia"));
CloseableIteration<BindingSet, QueryEvaluationException> iteration = node.evaluate(bsConstraint);
QueryBindingSet expected = new QueryBindingSet();
expected.addBinding("x", new LiteralImpl("CoffeeShop"));
expected.addBinding("y", new LiteralImpl("Joe"));
expected.addBinding("z", new LiteralImpl("Virginia"));
List<BindingSet> bsList = new ArrayList<>();
while (iteration.hasNext()) {
bsList.add(iteration.next());
}
Assert.assertEquals(1, bsList.size());
Assert.assertEquals(expected, bsList.get(0));
dao.delete(statement1, conf);
dao.delete(statement2, conf);
}
Aggregations