/** * Obtain a copy of this request (without any results) with the new supplied maximum depth. * * @param maxDepth the maximum depth for the new request * @return the copy of thist request, but with the desired maximum depth * @throws IllegalArgumentException if the maximum depth is not positive */ public ReadBranchRequest withMaximumDepth( int maxDepth ) { return new ReadBranchRequest(at, workspaceName, maxDepth); } }
/** * {@inheritDoc} * * @see java.lang.Object#equals(java.lang.Object) */ @Override public boolean equals( Object obj ) { if (obj == this) return true; if (this.getClass().isInstance(obj)) { ReadBranchRequest that = (ReadBranchRequest)obj; if (!this.at().isSame(that.at())) return false; if (this.maximumDepth() != that.maximumDepth()) return false; if (!this.inWorkspace().equals(that.inWorkspace())) return false; return true; } return false; }
public Iterator<SubgraphNode> iterator() { final Iterator<Location> iter = request.iterator(); return new Iterator<SubgraphNode>() { public boolean hasNext() { return iter.hasNext(); } public SubgraphNode next() { return getNode(iter.next()); } public void remove() { throw new UnsupportedOperationException(); } }; }
/** * {@inheritDoc} * * @see java.lang.Object#toString() */ @Override public String toString() { String workspaceName = this.workspaceName != null ? "'" + this.workspaceName + "'" : "default"; return "read " + printable(at()) + " (in " + workspaceName + " workspace) to depth " + maximumDepth(); }
@Test public void shouldIterateOverNodesInBranchOfDepthOne() { request = new ReadBranchRequest(location("/a"), workspace1, 1); request.setActualLocationOfNode(request.at()); request.setChildren(location("/a"), location("/a/b"), location("/a/c")); Iterator<Location> actual = request.iterator(); assertThat(actual.hasNext(), is(true)); assertThat(actual.next(), is(location("/a"))); assertThat(actual.hasNext(), is(false)); }
locationsToRead.add(new LocationWithDepth(request.at(), 1)); int maxDepthPerRead = Math.min(request.maximumDepth(), absoluteMaximumDepthForBranchReads()); if (request.isCancelled()) return; LocationWithDepth read = locationsToRead.poll(); ReadNodeRequest readNode = new ReadNodeRequest(read.location, request.inWorkspace()); process(readNode); if (readNode.hasError()) { request.setError(readNode.getError()); return; if (first) { request.setActualLocationOfNode(actualLocation); request.setChildren(actualLocation, readNode.getChildren()); request.setProperties(actualLocation, readNode.getProperties());
@Test public void shouldCreateValidRequestWithValidLocationAndMaximumDepth() { request = new ReadBranchRequest(validPathLocation1, workspace1, 10); assertThat(request.at(), is(sameInstance(validPathLocation1))); assertThat(request.hasError(), is(false)); assertThat(request.getError(), is(nullValue())); assertThat(request.maximumDepth(), is(10)); }
/** * {@inheritDoc} * * @see org.modeshape.graph.request.processor.RequestProcessor#process(org.modeshape.graph.request.ReadBranchRequest) */ @Override public void process( ReadBranchRequest request ) { CacheableRequest source = (CacheableRequest)federatedRequest.getFirstProjectedRequest().getRequest(); if (checkErrorOrCancel(request, source)) return; if (RequestType.READ_BRANCH == source.getType()) { ReadBranchRequest readSource = (ReadBranchRequest)source; request.setActualLocationOfNode(readSource.getActualLocationOfNode()); for (Location node : readSource) { List<Location> children = readSource.getChildren(node); if (children != null) request.setChildren(node, children); Map<Name, Property> props = readSource.getPropertiesFor(node); if (props != null) request.setProperties(node, props.values()); } } else if (RequestType.READ_NODE == source.getType()) { ReadNodeRequest readSource = (ReadNodeRequest)source; request.setActualLocationOfNode(readSource.getActualLocationOfNode()); Location parent = readSource.getActualLocationOfNode(); request.setChildren(parent, readSource.getChildren()); request.setProperties(parent, readSource.getPropertiesByName().values()); } request.setCachePolicy(getDefaultCachePolicy()); setCacheableInfo(request, source.getCachePolicy()); }
ProjectedRequest projectedRequest = federatedRequest.getFirstProjectedRequest(); request.setCachePolicy(getDefaultCachePolicy()); Location actualLocation = request.at(); int numMerged = 0; request.cancel(); return; ReadBranchRequest readFromSource = (ReadBranchRequest)fromSource; for (Location parent : readFromSource) { List<Location> children = readFromSource.getChildren(parent); Map<Name, Property> properties = readFromSource.getPropertiesFor(parent); projectToFederated(actualLocation, projection, request, parent, children, properties); Location locationOfProxy = readFromSource.getActualLocationOfNode(); setPathNotFound(request, request.at(), federatedRequest.getFirstProjectedRequest()); } else { request.setActualLocationOfNode(actualLocation);
ReadBranchRequest readSubgraph = new ReadBranchRequest(startingLocation, workspaceName, depthPerRead); try { channel.addAndAwait(readSubgraph); Iterator<Location> locationIter = readSubgraph.iterator(); if (!locationIter.hasNext()) return; Map<Name, Property> properties = readSubgraph.getPropertiesFor(topNode); if (properties == null) return; int startingDepth = 0; for (Location child : readSubgraph.getChildren(topNode)) { Path path = location.getPath(); Path parentPath = path.getParent(); Location parent = readSubgraph.getLocationFor(parentPath); if (parent == null) { parent = locationsByPath.get(parentPath); Collection<Property> nodePoperties = readSubgraph.getPropertiesFor(location).values(); CreateNodeRequest create = new CreateNodeRequest(parent, workspaceName, childName, nodePoperties); for (Location child : readSubgraph.getChildren(location)) { if (!readSubgraph.includes(child)) { if (!recordedParentLocation) { locationsByPath.put(location.getPath(), location); readSubgraph = new ReadBranchRequest(location, workspaceName, depthPerRead); try {
@Test public void shouldConsiderNotEqualTwoRequestsWithDifferentLocations() { request = new ReadBranchRequest(validPathLocation1, workspace1, 20); ReadBranchRequest request2 = new ReadBranchRequest(validPathLocation2, workspace1, 20); assertThat(request.equals(request2), is(false)); }
List<Location> existing = request.getChildren(parent); if (existing == null) existing = new ArrayList<Location>(children.size()); for (Location child : children) { existing.add(child); request.setChildren(parent, existing); Map<Name, Property> propsByName = request.getPropertiesFor(parent); if (propsByName == null) propsByName = new HashMap<Name, Property>(); for (Property property : propertiesByName.values()) { request.setProperties(parent, propsByName.values()); if (children != null) { List<Location> existing = request.getChildren(parentInFederation); if (existing == null) existing = new ArrayList<Location>(children.size()); for (Location child : children) { existing.add(child.with(childPath)); request.setChildren(parentInFederation, existing); Map<Name, Property> propsByName = request.getPropertiesFor(parentInFederation); if (propsByName == null) propsByName = new HashMap<Name, Property>(); for (Property property : propertiesByName.values()) { request.setProperties(parentInFederation, propsByName.values());
public int getMaximumDepth() { return request.maximumDepth(); }
/** * Sets the actual and complete location of the node being read. This method must be called when processing the request, and * the actual location must have a {@link Location#getPath() path}. * * @param actual the actual location of the node being read, or null if the {@link #at() current location} should be used * @throws IllegalArgumentException if the actual location is null or does not have a path */ public void setActualLocationOfNode( Location actual ) { checkNotFrozen(); CheckArg.isNotNull(actual, "actual"); if (!actual.hasPath()) { throw new IllegalArgumentException(GraphI18n.actualLocationMustHavePath.text(actual)); } this.actualLocation = actual; }
/** * {@inheritDoc} * * @see java.lang.Object#equals(java.lang.Object) */ @Override public boolean equals( Object obj ) { if (obj instanceof SubgraphResults) { SubgraphResults that = (SubgraphResults)obj; return getLocation().equals(that.getLocation()) && request.equals(that.request); } else if (obj instanceof Subgraph) { Subgraph that = (Subgraph)obj; if (!getLocation().equals(that.getLocation())) return false; Iterator<SubgraphNode> thisIter = this.iterator(); Iterator<SubgraphNode> thatIter = that.iterator(); while (thisIter.hasNext() && thatIter.hasNext()) { SubgraphNode thisNode = thisIter.next(); SubgraphNode thatNode = thatIter.next(); if (!thisNode.getLocation().equals(thatNode.getLocation())) return false; if (!thisNode.getProperties().equals(thatNode.getProperties())) return false; if (!thisNode.getChildren().equals(thatNode.getChildren())) return false; } if (thisIter.hasNext() || thatIter.hasNext()) return false; return true; } return false; }
@Test public void shouldIterateOverNodesInBranchOfDepthTwo() { request = new ReadBranchRequest(location("/a"), workspace1, 2); request.setActualLocationOfNode(request.at()); request.setChildren(location("/a"), location("/a/b"), location("/a/c"), location("/a/d")); request.setChildren(location("/a/b"), location("/a/b/j"), location("/a/b/k")); request.setChildren(location("/a/c"), location("/a/c/j"), location("/a/c/k")); request.setChildren(location("/a/d")); Iterator<Location> actual = request.iterator(); assertThat(actual.hasNext(), is(true)); assertThat(actual.next(), is(location("/a"))); assertThat(actual.hasNext(), is(true)); assertThat(actual.next(), is(location("/a/b"))); assertThat(actual.hasNext(), is(true)); assertThat(actual.next(), is(location("/a/c"))); assertThat(actual.hasNext(), is(true)); assertThat(actual.next(), is(location("/a/d"))); assertThat(actual.hasNext(), is(false)); }
@Test public void shouldCreateValidRequestWithValidLocation() { request = new ReadBranchRequest(validPathLocation1, workspace1); assertThat(request.at(), is(sameInstance(validPathLocation1))); assertThat(request.inWorkspace(), is(sameInstance(workspace1))); assertThat(request.hasError(), is(false)); assertThat(request.getError(), is(nullValue())); assertThat(request.maximumDepth(), is(ReadBranchRequest.DEFAULT_MAXIMUM_DEPTH)); }
@Test public void shouldConsiderNotEqualTwoRequestsWithDifferentWorkspaceNames() { request = new ReadBranchRequest(validPathLocation1, workspace1, 20); ReadBranchRequest request2 = new ReadBranchRequest(validPathLocation1, workspace2, 20); assertThat(request.equals(request2), is(false)); }
/** * Record the children for a parent node in the branch. * * @param parent the location of the parent; must {@link Location#hasPath() have a path} * @param children the location of each child, in the order they appear in the parent * @throws IllegalStateException if the request is frozen */ public void setChildren( Location parent, Location... children ) { checkNotFrozen(); CheckArg.isNotNull(parent, "parent"); CheckArg.isNotNull(children, "children"); assert parent.hasPath(); Node nodeObj = nodes.get(parent.getPath()); if (nodeObj == null) { nodeObj = new Node(parent); nodes.put(parent.getPath(), nodeObj); } nodeObj.setChildren(Arrays.asList(children)); }
@Test public void shouldIterateOverNodesInBranchOfDepthThree() { request = new ReadBranchRequest(location("/a"), workspace1, 3); request.setActualLocationOfNode(request.at()); request.setChildren(location("/a"), location("/a/b"), location("/a/c"), location("/a/d")); request.setChildren(location("/a/b"), location("/a/b/j"), location("/a/b/k")); request.setChildren(location("/a/c"), location("/a/c/j"), location("/a/c/k")); request.setChildren(location("/a/c/j"), location("/a/c/j/j1"), location("/a/c/j/j2")); request.setChildren(location("/a/c/k"), location("/a/c/k/k1"), location("/a/c/k/k2")); request.setChildren(location("/a/b/j"), location("/a/b/j/m"), location("/a/b/j/n")); request.setChildren(location("/a/b/k")); request.setChildren(location("/a/d")); Iterator<Location> actual = request.iterator(); assertThat(actual.hasNext(), is(true)); assertThat(actual.next(), is(location("/a")));