public static String of(Entity<? extends Entity.Blueprint, ?> entity) { //the only entity that requires the inventory to convert to its blueprint is the metadatapack. //but metadatapacks do not have a content hash, so we're good passing in null here. Entity.Blueprint bl = Inventory.asBlueprint(entity); return of(bl, entity.getPath()); }
private Map.Entry<InventoryStructure<B>, SyncHash.Tree> treeHashAndStructure(Transaction<BE> tx) { BE root = tx.querySingle(context.select().get()); E entity = tx.convert(root, context.entityClass); SyncHash.Tree.Builder bld = SyncHash.Tree.builder(); InventoryStructure.Builder<B> structBld = InventoryStructure.of(Inventory.asBlueprint(entity)); bld.withPath(RelativePath.empty().get()).withHash(entity.getSyncHash()); //the closure is returned in a breadth-first manner Iterator<BE> closure = tx.getTransitiveClosureOver(root, outgoing, contains.name()); if (closure.hasNext()) { Function<BE, Entity<? extends Entity.Blueprint, ?>> convert = e -> (Entity<Entity.Blueprint, ?>) tx.convert(e, tx.extractType(e)); Stream<BE> st = StreamSupport.stream(Spliterators.spliteratorUnknownSize(closure, 0), false); Iterator<Entity<? extends Entity.Blueprint, ?>> entities = st.map(convert).iterator(); buildChildTree(tx, entity.getPath(), singletonList(bld), singletonList(structBld), new ArrayList<>(), new ArrayList<>(), entities.next(), entities); } return new SimpleImmutableEntry<>(structBld.build(), bld.build()); }
structParent.startChild(Inventory.asBlueprint(e)); currentStructRow.add(childStructBuilder);
@SuppressWarnings("unchecked") private FullNode doGet(RelativePath path) { CanonicalPath pathToElement = rootEntity.getPath().modified().extend(path.getPath()).get(); try { FullNode cached = cache.get(pathToElement); if (cached != null) { return cached == FullNode.EMPTY ? null : cached; } Entity<Entity.Blueprint, ?> entity = (Entity<Entity.Blueprint, ?>) inventory.inspect(pathToElement, ResolvableToSingle.class).entity(); Entity.Blueprint b = Inventory.asBlueprint(entity); FullNode ret = new FullNode(b, entity); cache.put(entity.getPath(), ret); return ret; } catch (EntityNotFoundException e) { cache.put(pathToElement, FullNode.EMPTY); return null; } } };
@Override public Stream<FullNode> getAllChildNodes(RelativePath parent) { CanonicalPath absoluteParent = rootEntity.getPath().modified().extend(parent.getPath()).get(); Query q = Query.path().with(With.path(absoluteParent), Related.by(contains)).get(); @SuppressWarnings("rawtypes") Page<Entity> results = inventory.execute(q, Entity.class, Pager.none()); return StreamSupport.stream(results.spliterator(), false) .map(e -> { @SuppressWarnings("unchecked") FullNode n = new FullNode((Entity.Blueprint) Inventory.asBlueprint(e), e); cache.put(e.getPath(), n); return n; }) .sorted(ENTITY_ORDER) .onClose(results::close); }
InventoryStructure<B> of(E rootEntity, Inventory inventory) { return new InventoryStructure<B>() { FullNode root = new FullNode(Inventory.asBlueprint(rootEntity), rootEntity);