@Override public AbstractElement<?, ?> getElement(CanonicalPath path) { try { E element = getBackend().find(path); Class<?> type = getBackend().extractType(element); return (AbstractElement<?, ?>) getBackend().convert(element, type); } catch (ElementNotFoundException e) { throw new EntityNotFoundException("No element found on path: " + path.toString()); } }
@SuppressWarnings("unchecked") public void visit(GraphTraversal<?, ?> query, With.CanonicalPaths filter, QueryTranslationState state) { String prop = chooseBasedOnDirection(__cp, __targetCp, __sourceCp, state.getComingFrom()).name(); String typeName = null; String[] typeNames = null; if (filter.getPaths().length == 1) { typeName = Constants.Type.of(filter.getPaths()[0].getSegment().getElementType()).name(); query.has(prop, filter.getPaths()[0].toString()); } else { typeNames = Stream.of(filter.getPaths()).map(p -> p.getSegment().getElementType()) .toArray(String[]::new); String[] paths = Stream.of(filter.getPaths()).map(Object::toString).toArray(String[]::new); query.has(prop, P.within(paths)); } goBackFromEdges(query, state); if (typeNames == null) { query.hasLabel(typeName); } else { query.hasLabel(P.within(typeNames)); } }
private Vertex common(org.hawkular.inventory.paths.CanonicalPath path, String name, Map<String, Object> properties, Class<? extends Entity<?, ?>> cls) { try { checkProperties(properties, Constants.Type.of(cls).getMappedProperties()); Map<String, Object> props = properties == null ? Collections.emptyMap() : properties; Object[] keyVals = asKeyValues(props, T.label, Constants.Type.of(cls).name(), __type.name(), Constants.Type.of(cls).name(), __eid.name(), path.getSegment().getElementId(), __cp.name(), path.toString(), Constants.Property.name.name(), name); return context.getGraph().addVertex(keyVals); } catch (RuntimeException e) { throw context.translateException(e, path); } } }, null);
@Override public Element find(CanonicalPath path) throws ElementNotFoundException { Iterator<? extends Element> it; if (SegmentType.rl.equals(path.getSegment().getElementType())) { //__eid is globally unique for relationships it = context.getGraph().traversal().E().has(__eid.name(), path.getSegment().getElementId()); } else { it = context.getGraph().traversal().V() .hasLabel(Constants.Type.of(path.getSegment().getElementType()).name()) .has(__cp.name(), path.toString()); } if (!it.hasNext()) { throw new ElementNotFoundException(); } return it.next(); }
@Override public Element relate(Element sourceEntity, Element targetEntity, String name, Map<String, Object> properties) { if (name == null) { throw new IllegalArgumentException("name == null"); } if (!(sourceEntity instanceof Vertex)) { throw new IllegalArgumentException("Source not a vertex."); } if (!(targetEntity instanceof Vertex)) { throw new IllegalArgumentException("Target not a vertex."); } Map<String, Object> props = properties == null ? Collections.emptyMap() : properties; Object[] keyValues = asKeyValues(props, __sourceType.name(), sourceEntity.property(__type.name()).value(), __targetType.name(), targetEntity.property(__type.name()).orElse(null), __sourceCp.name(), sourceEntity.property(__cp.name()).orElse(null), __targetCp.name(), targetEntity.property(__cp.name()).orElse(null), __sourceEid.name(), sourceEntity.property(__eid.name()).orElse(null), __targetEid.name(), targetEntity.property(__eid.name()).orElse(null)); Edge e = ((Vertex) sourceEntity).addEdge(name, (Vertex) targetEntity, keyValues); //these need the ID of the edge, so we need to set the props after the edge has been created e.property(__eid.name(), e.id().toString()); e.property(__cp.name(), CanonicalPath.of().relationship(e.id().toString()).get().toString()); return e; }
.has(__cp.name(), CanonicalPath.of().tenant(tenantId).get().toString()) .hasLabel(Constants.Type.tenant.name()) .next();
String prop = chooseBasedOnDirection(Constants.Property.__cp, Constants.Property.__targetCp, Constants .Property.__sourceCp, TinkerpopBackend.asDirection(related.getEntityRole())).name(); query.has(prop, related.getEntityPath().toString());