Search in sources :

Example 1 with TypeStorageException

use of org.apache.rya.indexing.entity.storage.TypeStorage.TypeStorageException in project incubator-rya by apache.

the class EntityIndexSetProvider method discoverEntities.

private void discoverEntities(final StatementPattern pattern, final List<StatementPattern> unmatched) {
    final Var subj = pattern.getSubjectVar();
    final String subjStr = subj.getName();
    final RyaURI predURI = getPredURI(pattern);
    // check to see if current node is type
    if (new URIImpl(predURI.getData()).equals(RDF.TYPE)) {
        final Var obj = pattern.getObjectVar();
        final RyaURI objURI = new RyaURI(obj.getValue().stringValue());
        try {
            final Optional<Type> optType = typeStorage.get(objURI);
            // if is type, fetch type add to subject -> type map
            if (optType.isPresent()) {
                final Type type = optType.get();
                typeMap.put(type, pattern);
                subjectTypeMap.put(subjStr, type);
                // check unmatched properties, add matches
                for (final StatementPattern propertyPattern : unmatched) {
                    // store sps into the type -> property map
                    final RyaURI property = getPredURI(propertyPattern);
                    final Var typeSubVar = getTypeSubject(type);
                    final Var patternSubVar = propertyPattern.getSubjectVar();
                    if (type.getPropertyNames().contains(property) && typeSubVar.equals(patternSubVar)) {
                        typeMap.put(type, propertyPattern);
                    }
                }
            }
        } catch (final TypeStorageException e) {
            e.printStackTrace();
        }
    } else {
        // if not type, check to see if subject is in type map
        if (subjectTypeMap.containsKey(subjStr)) {
            // if is, check to see if pred is a property of type
            final Type type = subjectTypeMap.get(subjStr);
            if (type.getPropertyNames().contains(predURI)) {
                // if is, add sp to type -> sp map
                if (!typeMap.containsKey(type)) {
                    // each variable can only contain 1 type for now @see:Rya-235?
                    typeMap.put(type, pattern);
                }
            } else {
                // if not, add to unmatched type
                unmatched.add(pattern);
            }
        } else {
            // if not, add to unmatched
            unmatched.add(pattern);
        }
    }
}
Also used : RyaURI(org.apache.rya.api.domain.RyaURI) StatementPattern(org.openrdf.query.algebra.StatementPattern) Type(org.apache.rya.indexing.entity.model.Type) Var(org.openrdf.query.algebra.Var) URIImpl(org.openrdf.model.impl.URIImpl) TypeStorageException(org.apache.rya.indexing.entity.storage.TypeStorage.TypeStorageException)

Example 2 with TypeStorageException

use of org.apache.rya.indexing.entity.storage.TypeStorage.TypeStorageException in project incubator-rya by apache.

the class MongoEntityStorage method searchHasAllExplicitTypes.

/**
 * Searches the Entity storage for all Entities that contain all the
 * specified explicit type IDs.
 * @param explicitTypeIds the {@link ImmutableList} of {@link RyaURI}s that
 * are being searched for.
 * @return the {@link List} of {@link Entity}s that have all the specified
 * explicit type IDs. If nothing was found an empty {@link List} is
 * returned.
 * @throws EntityStorageException
 */
private List<Entity> searchHasAllExplicitTypes(final ImmutableList<RyaURI> explicitTypeIds) throws EntityStorageException {
    final List<Entity> hasAllExplicitTypesEntities = new ArrayList<>();
    if (!explicitTypeIds.isEmpty()) {
        // Grab the first type from the explicit type IDs.
        final RyaURI firstType = explicitTypeIds.get(0);
        // Check if that type exists anywhere in storage.
        final List<RyaURI> subjects = new ArrayList<>();
        Optional<Type> type;
        try {
            if (mongoTypeStorage == null) {
                mongoTypeStorage = new MongoTypeStorage(mongo, ryaInstanceName);
            }
            type = mongoTypeStorage.get(firstType);
        } catch (final TypeStorageException e) {
            throw new EntityStorageException("Unable to get entity type: " + firstType, e);
        }
        if (type.isPresent()) {
            // Grab the subjects for all the types we found matching "firstType"
            final ConvertingCursor<TypedEntity> cursor = search(Optional.empty(), type.get(), Collections.emptySet());
            while (cursor.hasNext()) {
                final TypedEntity typedEntity = cursor.next();
                final RyaURI subject = typedEntity.getSubject();
                subjects.add(subject);
            }
        }
        // Now grab all the Entities that have the subjects we found.
        for (final RyaURI subject : subjects) {
            final Optional<Entity> entityFromSubject = get(subject);
            if (entityFromSubject.isPresent()) {
                final Entity candidateEntity = entityFromSubject.get();
                // types they have.
                if (candidateEntity.getExplicitTypeIds().containsAll(explicitTypeIds)) {
                    hasAllExplicitTypesEntities.add(candidateEntity);
                }
            }
        }
    }
    return hasAllExplicitTypesEntities;
}
Also used : Entity(org.apache.rya.indexing.entity.model.Entity) TypedEntity(org.apache.rya.indexing.entity.model.TypedEntity) ArrayList(java.util.ArrayList) TypeStorageException(org.apache.rya.indexing.entity.storage.TypeStorage.TypeStorageException) RyaURI(org.apache.rya.api.domain.RyaURI) Type(org.apache.rya.indexing.entity.model.Type) TypedEntity(org.apache.rya.indexing.entity.model.TypedEntity)

Example 3 with TypeStorageException

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

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

Aggregations

RyaURI (org.apache.rya.api.domain.RyaURI)4 Type (org.apache.rya.indexing.entity.model.Type)4 TypeStorageException (org.apache.rya.indexing.entity.storage.TypeStorage.TypeStorageException)4 Entity (org.apache.rya.indexing.entity.model.Entity)2 TypeStorage (org.apache.rya.indexing.entity.storage.TypeStorage)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 RyaStatement (org.apache.rya.api.domain.RyaStatement)1 RyaType (org.apache.rya.api.domain.RyaType)1 Property (org.apache.rya.indexing.entity.model.Property)1 TypedEntity (org.apache.rya.indexing.entity.model.TypedEntity)1 EntityStorage (org.apache.rya.indexing.entity.storage.EntityStorage)1 Test (org.junit.Test)1 URIImpl (org.openrdf.model.impl.URIImpl)1 StatementPattern (org.openrdf.query.algebra.StatementPattern)1 Var (org.openrdf.query.algebra.Var)1