use of org.apache.tinkerpop.gremlin.structure.Property in project timbuctoo by HuygensING.
the class EdgeMockBuilder method build.
public Edge build() {
Edge edge = mock(Edge.class);
when(edge.inVertex()).thenReturn(inVertex);
when(edge.outVertex()).thenReturn(outVertex);
List<Property> allProps = Lists.newArrayList();
given(edge.keys()).willReturn(properties.keySet());
properties.forEach((key, value) -> addEdgeProperty(edge, key, value, allProps));
doReturn(allProps.iterator()).when(edge).properties();
return edge;
}
use of org.apache.tinkerpop.gremlin.structure.Property in project timbuctoo by HuygensING.
the class TinkerPopOperations method retrieveProperty.
@Override
public Optional<RdfReadProperty> retrieveProperty(Vre vre, String entityRdfUri, String propertyUri) {
Optional<Vertex> entityOpt = indexHandler.findVertexInRdfIndex(vre, entityRdfUri);
if (entityOpt.isPresent()) {
Vertex entity = entityOpt.get();
Iterable<Vertex> collectionOfEntity = () -> collectionsFor(entity);
Optional<Collection> colOpt = StreamSupport.stream(collectionOfEntity.spliterator(), false).map(col -> getProp(col, COLLECTION_NAME_PROPERTY_NAME, String.class)).filter(colNameProp -> colNameProp.isPresent()).map(colNameProp -> colNameProp.get()).map(vre::getCollectionForCollectionName).filter(Optional::isPresent).map(Optional::get).findFirst();
if (colOpt.isPresent()) {
Collection collection = colOpt.get();
VertexProperty<String> property = entity.property(createPropName(collection.getEntityTypeName(), propertyUri));
if (property.isPresent()) {
return Optional.of(new RdfReadProperty(propertyUri, property.value()));
}
}
}
return Optional.empty();
}
use of org.apache.tinkerpop.gremlin.structure.Property in project timbuctoo by HuygensING.
the class TinkerPopToEntityMapper method mapEntity.
public ReadEntity mapEntity(GraphTraversal<Vertex, Vertex> entityT, boolean withRelations) {
final List<TimProperty<?>> properties = Lists.newArrayList();
TinkerPopPropertyConverter dbPropertyConverter = new TinkerPopPropertyConverter(collection);
String entityTypeName = collection.getEntityTypeName();
GraphTraversal[] propertyGetters = collection.getReadableProperties().entrySet().stream().map(prop -> prop.getValue().traversalRaw().sideEffect(x -> x.get().onSuccess(value -> {
try {
properties.add(dbPropertyConverter.from(prop.getKey(), value));
} catch (UnknownPropertyException e) {
LOG.error("Unknown property", e);
} catch (IOException e) {
LOG.error(databaseInvariant, "Property '" + prop.getKey() + "' is not encoded correctly", e.getCause());
}
}).onFailure(e -> {
if (e.getCause() instanceof IOException) {
LOG.error(databaseInvariant, "Property '" + prop.getKey() + "' is not encoded correctly", e.getCause());
} else {
LOG.error("Something went wrong while reading the property '" + prop.getKey() + "'.", e.getCause());
}
}))).toArray(GraphTraversal[]::new);
entityT.asAdmin().clone().union(propertyGetters).forEachRemaining(x -> {
// Force side effects to happen
});
ReadEntityImpl entity = new ReadEntityImpl();
entity.setProperties(properties);
Vertex entityVertex = entityT.asAdmin().clone().next();
// TODO make use conversion for the types
entity.setRev(getProp(entityVertex, "rev", Integer.class).orElse(-1));
entity.setDeleted(getProp(entityVertex, "deleted", Boolean.class).orElse(false));
entity.setPid(getProp(entityVertex, "pid", String.class).orElse(null));
URI rdfUri = getProp(entityVertex, RDF_URI_PROP, String.class).map(x -> {
try {
return new URI(x);
} catch (URISyntaxException e) {
return null;
}
}).orElse(null);
entity.setRdfUri(rdfUri);
Property<String[]> rdfAlternativesProp = entityVertex.property(RDF_SYNONYM_PROP);
if (rdfAlternativesProp.isPresent()) {
try {
entity.setRdfAlternatives(Lists.newArrayList(rdfAlternativesProp.value()));
} catch (Exception e) {
LOG.error(databaseInvariant, "Error while reading rdfAlternatives", e);
}
}
Optional<String> typesOptional = getProp(entityVertex, "types", String.class);
if (typesOptional.isPresent()) {
try {
List<String> types = new ObjectMapper().readValue(typesOptional.get(), new TypeReference<List<String>>() {
});
entity.setTypes(types);
} catch (Exception e) {
LOG.error(databaseInvariant, "Error while generating variation refs", e);
entity.setTypes(Lists.newArrayList(entityTypeName));
}
} else {
entity.setTypes(Lists.newArrayList(entityTypeName));
}
Optional<String> modifiedStringOptional = getProp(entityVertex, "modified", String.class);
if (modifiedStringOptional.isPresent()) {
try {
entity.setModified(new ObjectMapper().readValue(modifiedStringOptional.get(), Change.class));
} catch (IOException e) {
LOG.error(databaseInvariant, "Change cannot be converted", e);
entity.setModified(new Change());
}
} else {
entity.setModified(new Change());
}
Optional<String> createdStringOptional = getProp(entityVertex, "created", String.class);
if (createdStringOptional.isPresent()) {
try {
entity.setCreated(new ObjectMapper().readValue(createdStringOptional.get(), Change.class));
} catch (IOException e) {
LOG.error(databaseInvariant, "Change cannot be converted", e);
entity.setCreated(new Change());
}
} else {
entity.setCreated(new Change());
}
entity.setDisplayName(DisplayNameHelper.getDisplayname(traversalSource, entityVertex, collection).orElse(""));
entity.setId(getIdOrDefault(entityVertex));
if (withRelations) {
entity.setRelations(getRelations(entityVertex, traversalSource, collection));
}
customEntityProperties.execute(entity, entityVertex);
return entity;
}
use of org.apache.tinkerpop.gremlin.structure.Property in project timbuctoo by HuygensING.
the class DatabaseFixer method addMissingEdgeVersions.
private void addMissingEdgeVersions(Edge edge) {
int rev = edge.value("rev");
if (rev > 1) {
Optional<Edge> previousVersion = getPreviousVersion(edge);
if (previousVersion.isPresent()) {
addMissingEdgeVersions(previousVersion.get());
} else {
// FIXME stop using EdgeManipulator
Edge duplicate = EdgeManipulator.duplicateEdge(edge);
for (Iterator<Property<Object>> properties = edge.properties(); properties.hasNext(); ) {
Property<Object> property = properties.next();
if (Objects.equals(property.key(), "isLatest")) {
duplicate.property("isLatest", false);
} else if (Objects.equals(property.key(), "rev")) {
duplicate.property("rev", rev - 1);
} else if (Objects.equals(property.key(), "modified")) {
duplicate.property("modified", edge.value("created"));
} else if (property.key().endsWith("_accepted")) {
duplicate.property(property.key(), true);
}
}
addMissingEdgeVersions(duplicate);
}
}
}
use of org.apache.tinkerpop.gremlin.structure.Property in project janusgraph by JanusGraph.
the class AbstractVertex method property.
@Override
public <V> JanusGraphVertexProperty<V> property(@Nullable final VertexProperty.Cardinality cardinality, final String key, final V value, final Object... keyValues) {
PropertyKey propertyKey = tx().getOrCreatePropertyKey(key, value, cardinality);
if (propertyKey == null) {
return JanusGraphVertexProperty.empty();
}
VertexProperty.Cardinality vCardinality = cardinality == null ? propertyKey.cardinality().convert() : cardinality;
if (value == null) {
if (vCardinality.equals(VertexProperty.Cardinality.single)) {
// putting null value with SINGLE cardinality is equivalent to removing existing value
properties(key).forEachRemaining(Property::remove);
} else {
// simply ignore this mutation
assert vCardinality.equals(VertexProperty.Cardinality.list) || vCardinality.equals(VertexProperty.Cardinality.set);
}
return JanusGraphVertexProperty.empty();
}
JanusGraphVertexProperty<V> p = tx().addProperty(vCardinality, it(), propertyKey, value);
ElementHelper.attachProperties(p, keyValues);
return p;
}
Aggregations