Search in sources :

Example 16 with Entity

use of org.apache.rya.indexing.entity.model.Entity in project incubator-rya by apache.

the class MongoEntityStorageIT method update_differentSubjects.

@Test(expected = EntityStorageException.class)
public void update_differentSubjects() throws Exception {
    // Two objects that do not have the same Subjects.
    final Entity old = Entity.builder().setSubject(new RyaURI("urn:SSN/111-11-1111")).setExplicitType(new RyaURI("urn:person")).build();
    final Entity updated = Entity.builder().setSubject(new RyaURI("urn:SSN/222-22-2222")).setExplicitType(new RyaURI("urn:person")).build();
    // The update will fail.
    final EntityStorage storage = new MongoEntityStorage(super.getMongoClient(), RYA_INSTANCE_NAME);
    storage.update(old, updated);
}
Also used : Entity(org.apache.rya.indexing.entity.model.Entity) TypedEntity(org.apache.rya.indexing.entity.model.TypedEntity) RyaURI(org.apache.rya.api.domain.RyaURI) EntityStorage(org.apache.rya.indexing.entity.storage.EntityStorage) Test(org.junit.Test)

Example 17 with Entity

use of org.apache.rya.indexing.entity.model.Entity 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);
    }
}
Also used : TypeStorage(org.apache.rya.indexing.entity.storage.TypeStorage) Entity(org.apache.rya.indexing.entity.model.Entity) RyaURI(org.apache.rya.api.domain.RyaURI) IndexingException(org.apache.rya.indexing.mongodb.IndexingException) EntityStorage(org.apache.rya.indexing.entity.storage.EntityStorage) IOException(java.io.IOException)

Example 18 with Entity

use of org.apache.rya.indexing.entity.model.Entity 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());
    });
}
Also used : Entity(org.apache.rya.indexing.entity.model.Entity) EntityStorage(org.apache.rya.indexing.entity.storage.EntityStorage) RyaStatement(org.apache.rya.api.domain.RyaStatement) IOException(java.io.IOException) RyaType(org.apache.rya.api.domain.RyaType) TypeStorageException(org.apache.rya.indexing.entity.storage.TypeStorage.TypeStorageException) TypeStorage(org.apache.rya.indexing.entity.storage.TypeStorage) RyaURI(org.apache.rya.api.domain.RyaURI) RyaType(org.apache.rya.api.domain.RyaType) Type(org.apache.rya.indexing.entity.model.Type) Property(org.apache.rya.indexing.entity.model.Property)

Example 19 with Entity

use of org.apache.rya.indexing.entity.model.Entity in project incubator-rya by apache.

the class EntityDocumentConverterTest method to_and_from_document.

@Test
public void to_and_from_document() throws DocumentConverterException {
    // Convert an Entity into a Document.
    final Entity entity = Entity.builder().setSubject(new RyaURI("urn:alice")).setExplicitType(new RyaURI("urn:person")).setProperty(new RyaURI("urn:person"), new Property(new RyaURI("urn:age"), new RyaType(XMLSchema.INT, "30"))).setProperty(new RyaURI("urn:person"), new Property(new RyaURI("urn:eye"), new RyaType("blue"))).setProperty(new RyaURI("urn:employee"), new Property(new RyaURI("urn:hours"), new RyaType(XMLSchema.INT, "40"))).setProperty(new RyaURI("urn:employee"), new Property(new RyaURI("urn:employer"), new RyaType("Burger Joint"))).build();
    final Document document = new EntityDocumentConverter().toDocument(entity);
    // Convert the Document back into an Entity.
    final Entity converted = new EntityDocumentConverter().fromDocument(document);
    // Ensure the original matches the round trip converted Entity.
    assertEquals(entity, converted);
}
Also used : Entity(org.apache.rya.indexing.entity.model.Entity) RyaURI(org.apache.rya.api.domain.RyaURI) RyaType(org.apache.rya.api.domain.RyaType) Document(org.bson.Document) Property(org.apache.rya.indexing.entity.model.Property) Test(org.junit.Test)

Example 20 with Entity

use of org.apache.rya.indexing.entity.model.Entity in project incubator-rya by apache.

the class MongoDbSmartUriIT method testSerializeDeserialize.

@Test
public void testSerializeDeserialize() throws SmartUriException, URISyntaxException {
    final URI smartUri = SmartUriAdapter.serializeUriEntity(BOB_ENTITY);
    final Entity resultEntity = SmartUriAdapter.deserializeUriEntity(smartUri);
    assertEquals(BOB_ENTITY.getSubject(), resultEntity.getSubject());
}
Also used : Entity(org.apache.rya.indexing.entity.model.Entity) TypedEntity(org.apache.rya.indexing.entity.model.TypedEntity) URI(org.openrdf.model.URI) RyaURI(org.apache.rya.api.domain.RyaURI) Test(org.junit.Test)

Aggregations

Entity (org.apache.rya.indexing.entity.model.Entity)40 RyaURI (org.apache.rya.api.domain.RyaURI)31 Test (org.junit.Test)29 Property (org.apache.rya.indexing.entity.model.Property)24 EntityStorage (org.apache.rya.indexing.entity.storage.EntityStorage)23 RyaType (org.apache.rya.api.domain.RyaType)22 TypedEntity (org.apache.rya.indexing.entity.model.TypedEntity)13 TypeStorage (org.apache.rya.indexing.entity.storage.TypeStorage)11 MongoEntityStorage (org.apache.rya.indexing.entity.storage.mongo.MongoEntityStorage)11 ReflectionToStringBuilder (org.apache.commons.lang.builder.ReflectionToStringBuilder)8 Builder (org.apache.rya.indexing.entity.model.Entity.Builder)8 MongoTypeStorage (org.apache.rya.indexing.entity.storage.mongo.MongoTypeStorage)8 RyaStatement (org.apache.rya.api.domain.RyaStatement)7 Type (org.apache.rya.indexing.entity.model.Type)6 URI (org.openrdf.model.URI)4 URIImpl (org.openrdf.model.impl.URIImpl)4 RyaTypeUtils.booleanRyaType (org.apache.rya.api.domain.RyaTypeUtils.booleanRyaType)3 RyaTypeUtils.byteRyaType (org.apache.rya.api.domain.RyaTypeUtils.byteRyaType)3 RyaTypeUtils.dateRyaType (org.apache.rya.api.domain.RyaTypeUtils.dateRyaType)3 RyaTypeUtils.doubleRyaType (org.apache.rya.api.domain.RyaTypeUtils.doubleRyaType)3