Search in sources :

Example 6 with EntityStorage

use of org.apache.rya.indexing.entity.storage.EntityStorage 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 7 with EntityStorage

use of org.apache.rya.indexing.entity.storage.EntityStorage 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 8 with EntityStorage

use of org.apache.rya.indexing.entity.storage.EntityStorage 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 9 with EntityStorage

use of org.apache.rya.indexing.entity.storage.EntityStorage 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 10 with EntityStorage

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

Aggregations

EntityStorage (org.apache.rya.indexing.entity.storage.EntityStorage)24 Entity (org.apache.rya.indexing.entity.model.Entity)23 RyaURI (org.apache.rya.api.domain.RyaURI)22 Test (org.junit.Test)22 Property (org.apache.rya.indexing.entity.model.Property)18 RyaType (org.apache.rya.api.domain.RyaType)16 MongoEntityStorage (org.apache.rya.indexing.entity.storage.mongo.MongoEntityStorage)11 TypeStorage (org.apache.rya.indexing.entity.storage.TypeStorage)10 TypedEntity (org.apache.rya.indexing.entity.model.TypedEntity)9 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)5 ValueFactory (org.openrdf.model.ValueFactory)3 BindingSet (org.openrdf.query.BindingSet)3 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)3 StatementPattern (org.openrdf.query.algebra.StatementPattern)3 MapBindingSet (org.openrdf.query.impl.MapBindingSet)3 ImmutableList (com.google.common.collect.ImmutableList)2 IOException (java.io.IOException)2 Date (java.util.Date)2