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);
}
}
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);
}
}
}
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;
}
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();
}
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);
}
Aggregations