Search in sources :

Example 16 with Property

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

the class MongoEntityStorageIT method update_stale.

@Test(expected = StaleUpdateException.class)
public void update_stale() throws Exception {
    final EntityStorage storage = new MongoEntityStorage(super.getMongoClient(), RYA_INSTANCE_NAME);
    // Store Alice in the repository.
    final Entity alice = Entity.builder().setSubject(new RyaURI("urn:SSN/111-11-1111")).setExplicitType(new RyaURI("urn:person")).setProperty(new RyaURI("urn:person"), new Property(new RyaURI("urn:name"), new RyaType(XMLSchema.STRING, "Alice"))).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(XMLSchema.STRING, "blue"))).build();
    storage.create(alice);
    // Show Alice was stored.
    final Optional<Entity> latest = storage.get(new RyaURI("urn:SSN/111-11-1111"));
    assertEquals(alice, latest.get());
    // Create the wrong old state and try to change Alice's eye color to brown.
    final Entity wrongOld = Entity.builder(alice).setVersion(500).build();
    final Entity updated = Entity.builder(alice).setVersion(501).setProperty(new RyaURI("urn:person"), new Property(new RyaURI("urn:eye"), new RyaType(XMLSchema.STRING, "brown"))).build();
    storage.update(wrongOld, 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) RyaType(org.apache.rya.api.domain.RyaType) Property(org.apache.rya.indexing.entity.model.Property) Test(org.junit.Test)

Example 17 with Property

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

the class MongoEntityStorageIT method delete.

@Test
public void delete() throws Exception {
    // An Entity that will be stored.
    final Entity entity = Entity.builder().setSubject(new RyaURI("urn:GTIN-14/00012345600012")).setExplicitType(new RyaURI("urn:icecream")).setProperty(new RyaURI("urn:icecream"), new Property(new RyaURI("urn:brand"), new RyaType(XMLSchema.STRING, "Awesome Icecream"))).setProperty(new RyaURI("urn:icecream"), new Property(new RyaURI("urn:flavor"), new RyaType(XMLSchema.STRING, "Chocolate"))).build();
    // Create it.
    final EntityStorage storage = new MongoEntityStorage(super.getMongoClient(), RYA_INSTANCE_NAME);
    storage.create(entity);
    // Delete it.
    final boolean deleted = storage.delete(new RyaURI("urn:GTIN-14/00012345600012"));
    // Verify a document was deleted.
    assertTrue(deleted);
}
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) RyaType(org.apache.rya.api.domain.RyaType) Property(org.apache.rya.indexing.entity.model.Property) Test(org.junit.Test)

Example 18 with Property

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

the class MongoEntityStorageIT method update.

@Test
public void update() throws Exception {
    final EntityStorage storage = new MongoEntityStorage(super.getMongoClient(), RYA_INSTANCE_NAME);
    // Store Alice in the repository.
    final Entity alice = Entity.builder().setSubject(new RyaURI("urn:SSN/111-11-1111")).setExplicitType(new RyaURI("urn:person")).setProperty(new RyaURI("urn:person"), new Property(new RyaURI("urn:name"), new RyaType(XMLSchema.STRING, "Alice"))).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(XMLSchema.STRING, "blue"))).build();
    storage.create(alice);
    // Show Alice was stored.
    Optional<Entity> latest = storage.get(new RyaURI("urn:SSN/111-11-1111"));
    assertEquals(alice, latest.get());
    // Change Alice's eye color to brown.
    final Entity updated = Entity.builder(alice).setVersion(latest.get().getVersion() + 1).setProperty(new RyaURI("urn:person"), new Property(new RyaURI("urn:eye"), new RyaType(XMLSchema.STRING, "brown"))).build();
    storage.update(alice, updated);
    // Fetch the Alice object and ensure it has the new value.
    latest = storage.get(new RyaURI("urn:SSN/111-11-1111"));
    assertEquals(updated, latest.get());
}
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) RyaType(org.apache.rya.api.domain.RyaType) Property(org.apache.rya.indexing.entity.model.Property) Test(org.junit.Test)

Example 19 with Property

use of org.apache.rya.indexing.entity.model.Property 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 20 with Property

use of org.apache.rya.indexing.entity.model.Property 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)

Aggregations

Property (org.apache.rya.indexing.entity.model.Property)35 RyaURI (org.apache.rya.api.domain.RyaURI)30 RyaType (org.apache.rya.api.domain.RyaType)25 Entity (org.apache.rya.indexing.entity.model.Entity)24 Test (org.junit.Test)21 EntityStorage (org.apache.rya.indexing.entity.storage.EntityStorage)18 TypedEntity (org.apache.rya.indexing.entity.model.TypedEntity)10 MongoEntityStorage (org.apache.rya.indexing.entity.storage.mongo.MongoEntityStorage)10 TypeStorage (org.apache.rya.indexing.entity.storage.TypeStorage)9 URI (org.openrdf.model.URI)8 URIImpl (org.openrdf.model.impl.URIImpl)8 MongoTypeStorage (org.apache.rya.indexing.entity.storage.mongo.MongoTypeStorage)7 Value (org.openrdf.model.Value)7 RyaStatement (org.apache.rya.api.domain.RyaStatement)6 ImmutableMap (com.google.common.collect.ImmutableMap)5 Type (org.apache.rya.indexing.entity.model.Type)5 BindingSet (org.openrdf.query.BindingSet)5 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)5 ArrayList (java.util.ArrayList)4 LinkedHashMap (java.util.LinkedHashMap)4