use of org.apache.rya.indexing.entity.storage.TypeStorage in project incubator-rya by apache.
the class MongoEntityIndexIT method setupTypes.
private void setupTypes(MongoEntityIndexer indexer) throws Exception {
final TypeStorage typeStore = indexer.getTypeStorage();
// Add some Types to the storage.
final Type cat = new Type(new RyaURI("urn:cat"), ImmutableSet.<RyaURI>builder().add(new RyaURI("urn:numLegs")).add(new RyaURI("urn:eye")).add(new RyaURI("urn:species")).build());
final Type dog = new Type(new RyaURI("urn:dog"), ImmutableSet.<RyaURI>builder().add(new RyaURI("urn:numLegs")).add(new RyaURI("urn:eye")).add(new RyaURI("urn:species")).build());
final Type icecream = new Type(new RyaURI("urn:icecream"), ImmutableSet.<RyaURI>builder().add(new RyaURI("urn:brand")).add(new RyaURI("urn:flavor")).add(new RyaURI("urn:cost")).build());
final Type person = new Type(new RyaURI("urn:person"), ImmutableSet.<RyaURI>builder().add(new RyaURI("urn:name")).add(new RyaURI("urn:age")).add(new RyaURI("urn:eye")).build());
typeStore.create(cat);
typeStore.create(dog);
typeStore.create(icecream);
typeStore.create(person);
}
use of org.apache.rya.indexing.entity.storage.TypeStorage in project incubator-rya by apache.
the class MongoTypeStorageIT method get_nonexisting.
@Test
public void get_nonexisting() throws TypeStorageException {
// Get a Type that hasn't been created.
final TypeStorage storage = new MongoTypeStorage(super.getMongoClient(), RYA_INSTANCE_NAME);
final Optional<Type> storedType = storage.get(new RyaURI("urn:icecream"));
// Verify nothing was returned.
assertFalse(storedType.isPresent());
}
use of org.apache.rya.indexing.entity.storage.TypeStorage in project incubator-rya by apache.
the class BaseEntityIndexer method deleteStatement.
@Override
public void deleteStatement(final RyaStatement statement) throws IOException {
requireNonNull(statement);
final EntityStorage entities = this.entities.get();
final TypeStorage types = this.types.get();
checkState(entities != null, "Must set this indexers configuration before storing statements.");
checkState(types != null, "Must set this indexers configuration before storing statements.");
try {
new EntityUpdater(entities).update(statement.getSubject(), old -> {
// If there is no Entity for the subject of the statement, then do nothing.
if (!old.isPresent()) {
return Optional.empty();
}
final Entity oldEntity = old.get();
// Increment the version of the Entity.
final Entity.Builder updated = Entity.builder(oldEntity);
updated.setVersion(oldEntity.getVersion() + 1);
if (TYPE_URI.equals(statement.getPredicate())) {
// If the Type ID already isn't in the list of explicit types, then do nothing.
final RyaURI typeId = new RyaURI(statement.getObject().getData());
if (!oldEntity.getExplicitTypeIds().contains(typeId)) {
return Optional.empty();
}
// Otherwise remove it from the list.
updated.unsetExplicitType(typeId);
} else {
// If the deleted property appears within the old entity's properties, then remove it.
final RyaURI deletedPropertyName = statement.getPredicate();
boolean propertyWasPresent = false;
for (final RyaURI typeId : oldEntity.getProperties().keySet()) {
for (final RyaURI propertyName : oldEntity.getProperties().get(typeId).keySet()) {
if (deletedPropertyName.equals(propertyName)) {
propertyWasPresent = true;
updated.unsetProperty(typeId, deletedPropertyName);
}
}
}
// If no properties were removed, then do nothing.
if (!propertyWasPresent) {
return Optional.empty();
}
}
return Optional.of(updated.build());
});
} catch (final IndexingException e) {
throw new IOException("Failed to update the Entity index.", e);
}
}
use of org.apache.rya.indexing.entity.storage.TypeStorage in project incubator-rya by apache.
the class BaseEntityIndexer method updateEntity.
/**
* Updates a {@link Entity} to reflect new {@link RyaStatement}s.
*
* @param subject - The Subject of the {@link Entity} the statements are for. (not null)
* @param statements - Statements that the {@link Entity} will be updated with. (not null)
* @throws IndexingException
*/
private void updateEntity(final RyaURI subject, final Collection<RyaStatement> statements) throws IndexingException {
requireNonNull(subject);
requireNonNull(statements);
final EntityStorage entities = this.entities.get();
final TypeStorage types = this.types.get();
checkState(entities != null, "Must set this indexers configuration before storing statements.");
checkState(types != null, "Must set this indexers configuration before storing statements.");
new EntityUpdater(entities).update(subject, old -> {
// Create a builder with the updated Version.
final Entity.Builder updated;
if (!old.isPresent()) {
updated = Entity.builder().setSubject(subject).setVersion(0);
} else {
final int updatedVersion = old.get().getVersion() + 1;
updated = Entity.builder(old.get()).setVersion(updatedVersion);
}
// Update the entity based on the Statements.
for (final RyaStatement statement : statements) {
// The Statement is setting an Explicit Type ID for the Entity.
if (Objects.equal(TYPE_URI, statement.getPredicate())) {
final RyaURI typeId = new RyaURI(statement.getObject().getData());
updated.setExplicitType(typeId);
} else // The Statement is adding a Property to the Entity.
{
final RyaURI propertyName = statement.getPredicate();
final RyaType propertyValue = statement.getObject();
try (final ConvertingCursor<Type> typesIt = types.search(propertyName)) {
// Set the Property for each type that includes the Statement's predicate.
while (typesIt.hasNext()) {
final RyaURI typeId = typesIt.next().getId();
updated.setProperty(typeId, new Property(propertyName, propertyValue));
}
} catch (final TypeStorageException | IOException e) {
throw new RuntimeException("Failed to fetch Types that include the property name '" + statement.getPredicate().getData() + "'.", e);
}
}
}
return Optional.of(updated.build());
});
}
use of org.apache.rya.indexing.entity.storage.TypeStorage in project incubator-rya by apache.
the class MongoTypeStorageIT method can_not_create_with_same_id.
@Test
public void can_not_create_with_same_id() throws TypeStorageException {
// A Type that will be stored.
final Type type = new Type(new RyaURI("urn:icecream"), ImmutableSet.<RyaURI>builder().add(new RyaURI("urn:brand")).add(new RyaURI("urn:flavor")).add(new RyaURI("urn:cost")).build());
// Create it.
final TypeStorage storage = new MongoTypeStorage(super.getMongoClient(), RYA_INSTANCE_NAME);
storage.create(type);
// Try to create it again. This will fail.
boolean failed = false;
try {
storage.create(type);
} catch (final TypeStorageException e) {
failed = true;
}
assertTrue(failed);
}
Aggregations