Search in sources :

Example 6 with Type

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

the class TypeDocumentConverter method fromDocument.

@Override
public Type fromDocument(final Document document) throws DocumentConverterException {
    requireNonNull(document);
    if (!document.containsKey(ID)) {
        throw new DocumentConverterException("Could not convert document '" + document + "' because its '" + ID + "' field is missing.");
    }
    if (!document.containsKey(PROPERTY_NAMES)) {
        throw new DocumentConverterException("Could not convert document '" + document + "' because its '" + PROPERTY_NAMES + "' field is missing.");
    }
    final RyaURI typeId = new RyaURI(document.getString(ID));
    final ImmutableSet.Builder<RyaURI> propertyNames = ImmutableSet.builder();
    ((List<String>) document.get(PROPERTY_NAMES)).forEach(propertyName -> propertyNames.add(new RyaURI(propertyName)));
    return new Type(typeId, propertyNames.build());
}
Also used : RyaURI(org.apache.rya.api.domain.RyaURI) Type(org.apache.rya.indexing.entity.model.Type) ImmutableSet(com.google.common.collect.ImmutableSet) List(java.util.List) ArrayList(java.util.ArrayList)

Example 7 with Type

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

use of org.apache.rya.indexing.entity.model.Type 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);
}
Also used : TypeStorage(org.apache.rya.indexing.entity.storage.TypeStorage) RyaURI(org.apache.rya.api.domain.RyaURI) Type(org.apache.rya.indexing.entity.model.Type) TypeStorageException(org.apache.rya.indexing.entity.storage.TypeStorage.TypeStorageException) Test(org.junit.Test)

Example 9 with Type

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

the class MongoTypeStorageIT method delete.

@Test
public void delete() throws TypeStorageException {
    // An 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);
    // Delete it.
    final boolean deleted = storage.delete(new RyaURI("urn:icecream"));
    // Verify a document was deleted.
    assertTrue(deleted);
}
Also used : TypeStorage(org.apache.rya.indexing.entity.storage.TypeStorage) RyaURI(org.apache.rya.api.domain.RyaURI) Type(org.apache.rya.indexing.entity.model.Type) Test(org.junit.Test)

Example 10 with Type

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

the class MongoTypeStorageIT method search.

@Test
public void search() throws Exception {
    // 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 TypeStorage storage = new MongoTypeStorage(super.getMongoClient(), RYA_INSTANCE_NAME);
    storage.create(cat);
    storage.create(dog);
    storage.create(icecream);
    // Search for all Types that have the 'urn:eye' property.
    final ConvertingCursor<Type> typeIt = storage.search(new RyaURI("urn:eye"));
    final Set<Type> types = new HashSet<>();
    while (typeIt.hasNext()) {
        types.add(typeIt.next());
    }
    // Verify the correct types were returned.
    final Set<Type> expected = Sets.newHashSet(cat, dog);
    assertEquals(expected, types);
}
Also used : TypeStorage(org.apache.rya.indexing.entity.storage.TypeStorage) RyaURI(org.apache.rya.api.domain.RyaURI) Type(org.apache.rya.indexing.entity.model.Type) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

Type (org.apache.rya.indexing.entity.model.Type)17 RyaURI (org.apache.rya.api.domain.RyaURI)14 Test (org.junit.Test)11 TypeStorage (org.apache.rya.indexing.entity.storage.TypeStorage)9 Entity (org.apache.rya.indexing.entity.model.Entity)6 RyaType (org.apache.rya.api.domain.RyaType)5 Property (org.apache.rya.indexing.entity.model.Property)5 EntityStorage (org.apache.rya.indexing.entity.storage.EntityStorage)5 TypeStorageException (org.apache.rya.indexing.entity.storage.TypeStorage.TypeStorageException)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 List (java.util.List)3 TypedEntity (org.apache.rya.indexing.entity.model.TypedEntity)3 URIImpl (org.openrdf.model.impl.URIImpl)3 ImmutableList (com.google.common.collect.ImmutableList)2 Date (java.util.Date)2 ReflectionToStringBuilder (org.apache.commons.lang.builder.ReflectionToStringBuilder)2 RyaTypeUtils.booleanRyaType (org.apache.rya.api.domain.RyaTypeUtils.booleanRyaType)2 RyaTypeUtils.byteRyaType (org.apache.rya.api.domain.RyaTypeUtils.byteRyaType)2 RyaTypeUtils.dateRyaType (org.apache.rya.api.domain.RyaTypeUtils.dateRyaType)2