a.getPath().getSegment().getElementId().compareTo(b.getPath().getSegment().getElementId())); a.path.getElementId().compareTo(b.path.getElementId())); Hashes.Tree h = treeChildren.get(j); int cmp = p.path.getElementId().compareTo(h.getPath().getSegment().getElementId()); if (cmp == 0) {
.map((s) -> AbstractElement.toElementClass(s.getSegment().getElementType())).collect(toSet()); Set<String> expectedIds = Arrays.asList(sources).stream().map((s) -> s.getSegment().getElementId()) .collect(toSet());
org.hawkular.inventory.paths.SegmentType[] types = typeFilter.getSegmentTypes(); for (int n = 0; n < typeFilter.getTypes().length; ++n) { Path.Segment seg = new Path.Segment(types[n], idFilter.getIds()[n]); if (!checker.canExtendTo(seg)) { if (cpFilter == null) {
CanonicalPath cp = extractCanonicalPath(v); String identityHash = extractIdentityHash(v); e = new DataEntity(cp.up(), DataRole.valueOf(cp.getSegment().getElementId()), loadStructuredData(v, hasData), identityHash, extractContentHash(v), extractSyncHash(v)); break;
Stream<FullNode> getChildNodes(RelativePath parent, Class<EE> childType) { CanonicalPath absoluteParent = rootEntity.getPath().modified().extend(parent.getPath()).get(); SegmentType parentType = absoluteParent.getSegment().getElementType();
EntityType structureType = EntityType.of(newStructurePath.getSegment().getElementType());
childCp.getSegment().accept(new ElementTypeVisitor.Simple<Void, Void>() { @Override public Void visitFeed(Void parameter) { ((Feeds.Container<Feeds.ReadWrite>) parentAccess).feeds().create((Feed.Blueprint) blueprint);
if (!SegmentType.r.equals(p.getSegment().getElementType())) { throw new IllegalArgumentException("Descend can only traverse child resources.");
private boolean __correctChangesPrologue(ProcessingTree<BE> changedEntity) { if (changedEntity.cp == null) { return false; } if (!changedEntity.isSignificant()) { //fast track - this is an entity that has not been CRUD'd but is present in the tree to complete the //hierarchy DBG.debug("Entity not hash-significant. Proceeding quickly."); return false; } if (!(Entity.Blueprint.class.isAssignableFrom( Inventory.types().bySegment(changedEntity.cp.getSegment().getElementType()).getBlueprintType()))) { //this is currently the case for metadata packs, which do not have their IDs assigned by the user. //we therefore mark them as processed. DBG.debug("Metadatapacks not processable. Bailing out quickly."); return true; } try { DBG.debug("Refreshing the entity in the current transaction."); changedEntity.loadFrom(tx); } catch (ElementNotFoundException e) { //ok, we're inside a delete and the root entity no longer exists... bail out quickly... if (processDeletion(changedEntity, changedEntity.notifications)) { return true; } else { throw new EntityNotFoundException(Query.filters(Query.to(changedEntity.cp))); } } return processDeletion(changedEntity, changedEntity.notifications); }
private GraphTraversal<?, ? extends Element> translate(Element startingPoint, Query query) { GraphTraversal<?, ? extends Element> q; if (startingPoint == null) { Filter first = query.getFragments()[0].getFilter(); if (first instanceof RelationFilter) { q = context.getGraph().traversal().E(); } else if (first instanceof With.CanonicalPaths) { //XXX this does NOT handle the situation where we mix relationships and entities in one filter SegmentType elementType = ((With.CanonicalPaths) first).getPaths()[0].getSegment().getElementType(); if (SegmentType.rl == elementType) { q = context.getGraph().traversal().E(); } else { q = context.getGraph().traversal().V(); } } else { q = context.getGraph().traversal().V(); } } else { q = startingPoint instanceof Vertex ? context.getGraph().traversal().V(startingPoint) : context.getGraph().traversal().E(startingPoint); } FilterApplicator.applyAll(query, q); return q; }
private ProcessingTree<BE> extendTreeTo(CanonicalPath entityPath) { Set<ProcessingTree<BE>> children = this.children; CanonicalPath.Extender cp = CanonicalPath.empty(); ProcessingTree<BE> found = null; for (Path.Segment seg : entityPath.getPath()) { cp.extend(seg); found = null; for (ProcessingTree<BE> child : children) { if (seg.equals(child.path)) { found = child; break; } } if (found == null) { found = new ProcessingTree<>(seg, cp.get()); children.add(found); } children = found.children; } if (found == null) { throw new IllegalStateException("Could not figure out the processing tree element for entity on path " + entityPath); } return found; }
@SuppressWarnings("unchecked") private InventoryStructure.Builder<B> mergeDeepTree(InventoryStructure<?> currentTree, RelativePath treePath, InventoryStructure.AbstractBuilder<?> newTree, Set<SegmentType> mergedTypes) { DBG.debugf("Starting to merge shallow tree. Loading children under [%s]", treePath); try (Stream<InventoryStructure.FullNode> currentChildren = currentTree.getAllChildNodes(treePath)) { DBG.debugf("Done loading the children under [%s]", treePath); Set<Path.Segment> newChildPaths = newTree.getChildrenPaths(); currentChildren.forEach(currentChild -> { SegmentType childType = Inventory.types().byBlueprint(currentChild.getEntity().getClass()).getSegmentType(); Path.Segment childPathSegment = new Path.Segment(childType, currentChild.getEntity().getId()); if (newChildPaths.contains(childPathSegment)) { mergeDeepTree(currentTree, treePath.modified().extend(childPathSegment).get(), newTree.getChild(childPathSegment), mergedTypes); } else if (!mergedTypes.contains(childType)) { newTree.addChild(currentChild.getEntity(), currentChild.getAttachment()); mergeDeepTree(currentTree, treePath.modified().extend(childPathSegment).get(), newTree.getChild(childPathSegment), mergedTypes); } }); DBG.debugf("Done merging shallow tree of [%s]", treePath); if (newTree instanceof InventoryStructure.Builder) { return (InventoryStructure.Builder<B>) newTree; } else { return null; } } }
@SuppressWarnings("unchecked") public void visit(GraphTraversal<?, ?> query, With.DataAt dataPos, QueryTranslationState state) { goBackFromEdges(query, state); query.out(hasData.name()); for (Path.Segment seg : dataPos.getDataPath().getPath()) { if (SegmentType.up.equals(seg.getElementType())) { query.in(contains.name()); } else { query.out(contains.name()); } query.has(__type.name(), Constants.Type.structuredData.name()); // map members have both index and key (so that the order of the elements is preserved) // list members have only the index Integer index = toInteger(seg.getElementId()); if (index == null) { query.has(Constants.Property.__structuredDataKey.name(), seg.getElementId()); } else { //well, the map could have a numeric key, so we cannot say it has to be a list index here. GraphTraversal<?, ?>[] indexOrKey = new GraphTraversal<?, ?>[2]; indexOrKey[0] = __.has(Constants.Property.__structuredDataIndex.name(), index) .hasNot(Constants.Property.__structuredDataKey.name()); indexOrKey[1] = __.has(Constants.Property.__structuredDataKey.name(), seg.getElementId()); query.or((Traversal[]) indexOrKey); } } }
@SuppressWarnings("unchecked") private InventoryStructure.Builder<B> mergeShallowTree(InventoryStructure<?> currentTree, RelativePath treePath, InventoryStructure.AbstractBuilder<?> newTree, Set<SegmentType> mergedTypes) { DBG.debugf("Starting to merge shallow tree. Loading children under [%s]", treePath); try (Stream<InventoryStructure.FullNode> currentChildren = currentTree.getAllChildNodes(treePath)) { DBG.debugf("Done loading the children under [%s]", treePath); Set<Path.Segment> newChildPaths = newTree.getChildrenPaths(); currentChildren.forEach(currentChild -> { SegmentType childType = Inventory.types().byBlueprint(currentChild.getEntity().getClass()).getSegmentType(); Path.Segment childPathSegment = new Path.Segment(childType, currentChild.getEntity().getId()); if (newChildPaths.contains(childPathSegment)) { mergeShallowTree(currentTree, treePath.modified().extend(childPathSegment).get(), newTree.getChild(childPathSegment), mergedTypes); } else if (!mergedTypes.contains(childType)) { newTree.addChild(currentChild.getEntity(), currentChild.getAttachment()); } }); DBG.debugf("Done merging shallow tree of [%s]", treePath); if (newTree instanceof InventoryStructure.Builder) { return (InventoryStructure.Builder<B>) newTree; } else { return null; } } }
private void deserializeChildren(JsonParser p, AbstractHashTree.Builder<?, ?, T, H> bld, RelativePath.Extender parentPath, DeserializationContext ctx) throws IOException { //make a copy so that we don't modify the parent RelativePath.Extender origParentPath = parentPath; parentPath = origParentPath.get().modified(); RelativePath childPath = null; while (p.nextToken() != null) { switch (p.getCurrentToken()) { case FIELD_NAME: childPath = parentPath.extend(Path.Segment.from(p.getCurrentName())).get(); break; case START_OBJECT: AbstractHashTree.ChildBuilder<?, ?, ?, T, H> childBld = bld.startChild(); deserializeChild(p, childBld, childPath.modified(), ctx); childBld.endChild(); parentPath = origParentPath.get().modified(); break; case END_OBJECT: return; } } }
ProcessingTree<BE> getChild(RelativePath childPath) { Iterator<Path.Segment> segments = childPath.getPath().iterator(); Set<ProcessingTree<BE>> children = this.children; ProcessingTree<BE> current = null; while (segments.hasNext()) { Path.Segment seg = segments.next(); current = null; for (ProcessingTree<BE> c : children) { //children are bound to each have a different ending segment, otherwise their CP would be the same //which is illegal in inventory. We can therefore just compare the current segment with the last //segment of the child path and if they match, move on to that child. if (seg.equals(c.cp.getSegment())) { current = c; children = c.children; break; } } //no child found matching this segment... if (current == null) { break; } } return segments.hasNext() ? null : current; }
@Override protected EntityAndPendingNotifications<BE, MetadataPack> wireUpNewEntity(BE entity, MetadataPack.Blueprint blueprint, CanonicalPath parentPath, BE parent, Transaction<BE> tx) { Set<Notification<?, ?>> newRels = new HashSet<>(); blueprint.getMembers().forEach((p) -> { try { BE member = tx.find(p); BE rel = tx.relate(entity, member, incorporates.name(), null); Relationship r = tx.convert(rel, Relationship.class); newRels.add(new Notification<>(r, r, created())); } catch (ElementNotFoundException e) { throw new EntityNotFoundException(p.getSegment().getElementType().getSimpleName(), Query.filters(Query.to(p))); } }); MetadataPack entityObject = tx.convert(entity, MetadataPack.class); return new EntityAndPendingNotifications<>(entity, entityObject, newRels); }
@Override public void preUpdate(BE dataEntity, DataEntity.Update update, Transaction<BE> tx) { if (update.getValue() == null) { return; } BE mp = tx.traverseToSingle(dataEntity, Query.path().with( asTargetBy(contains), //up to operation type asTargetBy(contains), //up to resource type asTargetBy(incorporates), With.type(MetadataPack.class) // up to the pack ).get()); if (mp != null) { CanonicalPath dataPath = tx.extractCanonicalPath(dataEntity); throw new IllegalArgumentException( "Data '" + dataPath.getSegment().getElementId() + "' cannot be updated" + " under operation type " + dataPath.up() + ", because the owning resource type is part of a meta data pack." + " Doing this would invalidate meta data pack's identity."); } }
@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)); } }
/** * Returns a child builder of a pre-existing child. * @param childPath the path to the child * @return the child builder or null */ public ChildBuilder<This> getChild(Path.Segment childPath) { Map<EntityType, Set<FullNode>> myChildren = children.get(myPath); if (myChildren == null) { return null; } EntityType childType = EntityType.of(childPath.getElementType()); Set<FullNode> childrenOfType = myChildren.get(childType); return childrenOfType.stream().filter(child -> child.getEntity().getId().equals(childPath.getElementId())) .findAny().map(child -> { RelativePath rp = myPath.modified().extend(childPath).get(); return new ChildBuilder<>(castThis(), rp, blueprints, children); }).orElse(null); }