Search in sources :

Example 26 with Entity

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

the class MongoDbSmartUri method storeEntity.

@Override
public void storeEntity(final RyaURI subject, final Map<URI, Value> map) throws SmartUriException {
    checkInit();
    final URI uri = SmartUriAdapter.serializeUri(subject, map);
    final Entity entity = SmartUriAdapter.deserializeUriEntity(uri);
    // Create it.
    try {
        entityStorage.create(entity);
    } catch (final ObjectStorageException e) {
        throw new SmartUriException("Failed to create entity storage", e);
    }
}
Also used : Entity(org.apache.rya.indexing.entity.model.Entity) TypedEntity(org.apache.rya.indexing.entity.model.TypedEntity) SmartUriException(org.apache.rya.indexing.smarturi.SmartUriException) ObjectStorageException(org.apache.rya.indexing.mongodb.update.RyaObjectStorage.ObjectStorageException) RyaURI(org.apache.rya.api.domain.RyaURI) URI(org.openrdf.model.URI)

Example 27 with Entity

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

the class DuplicateDataDetector method removeDuplicatesFromCollection.

/**
 * Removes any duplicate (nearly identical) entities from the collection
 * of entities.
 * @param entities the {@link List} of {@link Entity}s. (not {@code null})
 * @throws SmartUriException
 */
public void removeDuplicatesFromCollection(final List<Entity> entities) throws SmartUriException {
    requireNonNull(entities);
    // Use a Sorted Set in reverse order to hold the indices
    final Set<Integer> indicesToRemove = new TreeSet<>((a, b) -> Integer.compare(b, a));
    if (entities != null && entities.size() > 1) {
        // same comparisons again and not comparing an entity to itself.
        for (int i = 0; i < entities.size() - 1; i++) {
            final Entity entity1 = entities.get(i);
            for (int j = entities.size() - 1; j > i; j--) {
                final Entity entity2 = entities.get(j);
                final boolean areDuplicates = compareEntities(entity1, entity2);
                if (areDuplicates) {
                    indicesToRemove.add(j);
                }
            }
        }
    }
    if (!indicesToRemove.isEmpty()) {
        // order so just loop through them)
        for (final int index : indicesToRemove) {
            entities.remove(index);
        }
    }
}
Also used : Entity(org.apache.rya.indexing.entity.model.Entity) TreeSet(java.util.TreeSet)

Example 28 with Entity

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

the class SmartUriAdapter method deserializeUriEntity.

public static Entity deserializeUriEntity(final URI uri) throws SmartUriException {
    final String uriString = uri.stringValue();
    final int fragmentPosition = uriString.indexOf("#");
    String prefix = uriString.substring(0, fragmentPosition + 1);
    if (fragmentPosition == -1) {
        prefix = uriString.split("\\?", 2)[0];
    }
    final String fragment = uriString.substring(fragmentPosition + 1, uriString.length());
    java.net.URI queryUri;
    URIBuilder uriBuilder = null;
    try {
        if (fragmentPosition > -1) {
            queryUri = new java.net.URI("urn://" + fragment);
        } else {
            queryUri = new java.net.URI(uriString);
        }
        uriBuilder = new URIBuilder(queryUri);
    } catch (final URISyntaxException e) {
        throw new SmartUriException("Unable to deserialize Smart URI", e);
    }
    final RyaURI subject = findSubject(uri.stringValue());
    final List<NameValuePair> parameters = uriBuilder.getQueryParams();
    Map<RyaURI, String> entityTypeMap = new LinkedHashMap<>();
    Map<String, RyaURI> invertedEntityTypeMap = new LinkedHashMap<>();
    final Map<RyaURI, Map<URI, Value>> fullMap = new LinkedHashMap<>();
    for (final NameValuePair pair : parameters) {
        final String keyString = pair.getName();
        final String valueString = pair.getValue();
        final URI keyUri = new URIImpl(prefix + keyString);
        final String decoded;
        try {
            decoded = URLDecoder.decode(valueString, Charsets.UTF_8.name());
        } catch (final UnsupportedEncodingException e) {
            throw new SmartUriException("", e);
        }
        final URI type = TypeDeterminer.determineType(decoded);
        if (type == XMLSchema.ANYURI) {
            if (keyString.equals(RYA_TYPES_URI.getLocalName())) {
                entityTypeMap = convertUriToTypeMap(new URIImpl(decoded));
                invertedEntityTypeMap = HashBiMap.create(entityTypeMap).inverse();
            }
        } else {
            final int keyPrefixLocation = keyString.indexOf(".");
            final String keyPrefix = keyString.substring(0, keyPrefixLocation);
            final RyaURI keyCorrespondingType = invertedEntityTypeMap.get(keyPrefix);
            final String keyName = keyString.substring(keyPrefixLocation + 1, keyString.length());
            final RyaType ryaType = new RyaType(type, valueString);
            final Value value = RyaToRdfConversions.convertValue(ryaType);
            final String formattedKeyUriString = removeTypePrefixFromUri(keyUri.stringValue(), keyPrefix);
            final URI formattedKeyUri = new URIImpl(formattedKeyUriString);
            final Map<URI, Value> map = fullMap.get(keyCorrespondingType);
            if (map == null) {
                final Map<URI, Value> subMap = new HashMap<>();
                subMap.put(formattedKeyUri, value);
                fullMap.put(keyCorrespondingType, subMap);
            } else {
                map.put(formattedKeyUri, value);
                fullMap.put(keyCorrespondingType, map);
            }
        }
    }
    final Entity entity = convertMapToEntity(subject, fullMap);
    return entity;
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) Entity(org.apache.rya.indexing.entity.model.Entity) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) UnsupportedEncodingException(java.io.UnsupportedEncodingException) URIImpl(org.openrdf.model.impl.URIImpl) URISyntaxException(java.net.URISyntaxException) RyaType(org.apache.rya.api.domain.RyaType) URI(org.openrdf.model.URI) RyaURI(org.apache.rya.api.domain.RyaURI) URIBuilder(org.apache.http.client.utils.URIBuilder) LinkedHashMap(java.util.LinkedHashMap) RyaURI(org.apache.rya.api.domain.RyaURI) Value(org.openrdf.model.Value) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashBiMap(com.google.common.collect.HashBiMap)

Example 29 with Entity

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

the class EntityDocumentConverter method fromDocument.

@Override
public Entity fromDocument(final Document document) throws DocumentConverterException {
    requireNonNull(document);
    // Preconditions.
    if (!document.containsKey(SUBJECT)) {
        throw new DocumentConverterException("Could not convert document '" + document + "' because its '" + SUBJECT + "' field is missing.");
    }
    if (!document.containsKey(EXPLICIT_TYPE_IDS)) {
        throw new DocumentConverterException("Could not convert document '" + document + "' because its '" + EXPLICIT_TYPE_IDS + "' field is missing.");
    }
    if (!document.containsKey(PROPERTIES)) {
        throw new DocumentConverterException("Could not convert document '" + document + "' because its '" + PROPERTIES + "' field is missing.");
    }
    if (!document.containsKey(VERSION)) {
        throw new DocumentConverterException("Could not convert document '" + document + "' because its '" + VERSION + "' field is missing.");
    }
    if (!document.containsKey(SMART_URI)) {
        throw new DocumentConverterException("Could not convert document '" + document + "' because its '" + SMART_URI + "' field is missing.");
    }
    // Perform the conversion.
    final Entity.Builder builder = Entity.builder().setSubject(new RyaURI(document.getString(SUBJECT)));
    ((List<String>) document.get(EXPLICIT_TYPE_IDS)).stream().forEach(explicitTypeId -> builder.setExplicitType(new RyaURI(explicitTypeId)));
    final Document propertiesDoc = (Document) document.get(PROPERTIES);
    for (final String typeId : propertiesDoc.keySet()) {
        final Document typePropertiesDoc = (Document) propertiesDoc.get(typeId);
        for (final String propertyName : typePropertiesDoc.keySet()) {
            final String decodedPropertyName = MongoDbSafeKey.decodeKey(propertyName);
            final Document value = (Document) typePropertiesDoc.get(propertyName);
            final RyaType propertyValue = ryaTypeConverter.fromDocument(value);
            builder.setProperty(new RyaURI(typeId), new Property(new RyaURI(decodedPropertyName), propertyValue));
        }
    }
    builder.setVersion(document.getInteger(VERSION));
    builder.setSmartUri(new URIImpl(document.getString(SMART_URI)));
    return builder.build();
}
Also used : Entity(org.apache.rya.indexing.entity.model.Entity) RyaURI(org.apache.rya.api.domain.RyaURI) URIImpl(org.openrdf.model.impl.URIImpl) Document(org.bson.Document) RyaType(org.apache.rya.api.domain.RyaType) Property(org.apache.rya.indexing.entity.model.Property)

Example 30 with Entity

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

the class MongoEntityIndex2IT method beforeClass.

@Before
public void beforeClass() throws Exception {
    optimizer = new EntityIndexOptimizer();
    optimizer.setConf(conf);
    final TypeStorage typeStorage = optimizer.getTypeStorage();
    typeStorage.create(PERSON_TYPE);
    final Entity entity = Entity.builder().setSubject(new RyaURI("urn:SSN:111-11-1111")).setExplicitType(RYA_PERSON_TYPE).setProperty(RYA_PERSON_TYPE, new Property(new RyaURI("urn:age"), new RyaType("25"))).setProperty(RYA_PERSON_TYPE, new Property(new RyaURI("urn:eye"), new RyaType("blue"))).setProperty(RYA_PERSON_TYPE, new Property(new RyaURI("urn:name"), new RyaType("bob"))).build();
    entityStorage = optimizer.getEntityStorage();
    entityStorage.create(entity);
}
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) EntityIndexOptimizer(org.apache.rya.indexing.entity.EntityIndexOptimizer) RyaType(org.apache.rya.api.domain.RyaType) Property(org.apache.rya.indexing.entity.model.Property) Before(org.junit.Before)

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