protected ChildReference childReferenceFor(Name name) { NodeKey key = childKeysByName.get(stringValueFactory.create(name)); return key == null ? null : new ChildReference(key, name, 1); }
/** * Get the name for this node, without any same-name-sibiling (SNS) index. * * @param cache the workspace cache to which this node belongs, required in case this node needs to use the cache; may not be * null * @return the name; never null, but the root node will have a zero-length name * @throws NodeNotFoundInParentException if this node no longer exists * @see #getSegment(NodeCache) * @see #getPath(NodeCache) */ protected Segment getSegment( WorkspaceCache cache ) { return parentReferenceToSelf(cache).getSegment(); }
public void append( NodeKey key, Name name ) { ChildReference reference = new ChildReference(key, name, 1); Lock lock = this.lock.writeLock(); try { lock.lock(); ChildReference old = this.childReferencesByKey.put(key, reference); if (old != null && old.getName().equals(name)) { // We already have this key/name pair, so we don't need to add it again ... return; } // We've not seen this node key yet, so it is okay. In fact, we should not see any // node key more than once, since that is clearly an unexpected condition (as a child // may not appear more than once in its parent's list of child nodes). See MODE-2120. List<NodeKey> nodeKeysWithSameName = this.childReferenceKeysByName.get(name); if (nodeKeysWithSameName == null) { nodeKeysWithSameName = new ArrayList<>(); this.childReferenceKeysByName.put(name, nodeKeysWithSameName); } nodeKeysWithSameName.add(key); } finally { lock.unlock(); } }
while (iter.hasNext()) { ChildReference result = iter.next(); int index = context.consume(result.getName(), result.getKey()); if (index == snsIndex) return result.with(index); while (iter.hasNext()) { ChildReference ref = iter.next(); if (ref.getSnsIndex() == snsIndex) return ref; if (snsIndex == childWithSameName.getSnsIndex()) { return childWithSameName;
private Map<NodeKey, Map<Path, Path>> computePathChangesForSNS( PathCache workspacePaths, Path parentPath, ChildReferences childReferences, ChildReference reorderedChildRef, Path newChildPath, Path oldChildPath ) { Map<NodeKey, Map<Path, Path>> snsPathChangesByNodeKey = new HashMap<>(); if (oldChildPath.getLastSegment().hasIndex() || newChildPath.getLastSegment().hasIndex()) { // we're reordering a SNS so we have to look at all the other SNSs // and record for each their paths (see https://issues.jboss.org/browse/MODE-2510) for (ChildReference childReference : childReferences) { NodeKey childKey = childReference.getKey(); if (childKey.equals(reorderedChildRef.getKey())) { continue; } if (childReference.getName().equals(reorderedChildRef.getName())) { // childReference is a SNS so we must record it's old and new path CachedNode persistedSNS = workspaceCache().getNode(childKey); Path snsNewPath = pathFactory().create(parentPath, childReference.getSegment()); Path snsOldPath = workspacePaths.getPath(persistedSNS); if (!snsOldPath.equals(snsNewPath)) { snsPathChangesByNodeKey.put(childKey, Collections.singletonMap(snsOldPath, snsNewPath)); } } } } return snsPathChangesByNodeKey; }
ChildReference childB = refs.getChild(name("childB")); assertThat(system.getKey().toString(), is("source1system-jcrsystem")); assertThat(childA.getKey().toString(), is("source1works1-childA")); assertThat(childB.getKey().toString(), is("source1works1-childB")); assertThat(system.getName(), is(JcrLexicon.SYSTEM)); assertThat(childA.getName(), is(name("childA"))); assertThat(childB.getName(), is(name("childB"))); assertThat(childA.getSnsIndex(), is(1)); assertThat(childB.getSnsIndex(), is(1));
@Override public NodeKey extractFrom( ChildReference ref ) { return ref.getKey(); } });
public ChildReference remove( NodeKey key ) { Lock lock = this.lock.writeLock(); try { lock.lock(); ChildReference existing = this.childReferencesByKey.remove(key); if (existing != null) { List<NodeKey> nodeKeys = this.childReferenceKeysByName.get(existing.getName()); assert nodeKeys != null; nodeKeys.remove(key); } return existing; } finally { lock.unlock(); } }
protected void assertChildren( Document doc, Segment... children ) { List<?> childReferences = doc.getArray(DocumentTranslator.CHILDREN); Iterator<?> actualIter = childReferences.iterator(); Iterator<Segment> expectedIter = Arrays.asList(children).iterator(); while (actualIter.hasNext()) { ChildReference childRef = workspaceCache.translator().childReferenceFrom(actualIter.next()); if (!expectedIter.hasNext()) { fail("Found \"" + childRef + "\" but not expecting any children"); } Segment expectedName = expectedIter.next(); assertThat("Expecting child \"" + expectedName + "\" but found \"" + childRef.toString() + "\"", childRef.getSegment(), is(expectedName)); } if (expectedIter.hasNext()) { fail("Expected \"" + expectedIter.next() + "\" but found no such child"); } }
@Override public ChildReference getChild( NodeKey key, Context context ) { if (context == null) context = new SingleNameContext(); Lock lock = this.lock.readLock(); try { lock.lock(); ChildReference ref = childReferencesByKey.get(key); if (ref != null) { // It's in this list but there may be changes ... List<NodeKey> childrenKeysWithSameName = this.childReferenceKeysByName.get(ref.getName()); assert childrenKeysWithSameName != null; assert childrenKeysWithSameName.size() != 0; int index = childrenKeysWithSameName.indexOf(key); return ref.with(index + 1); } return null; } finally { lock.unlock(); } }
@Override public String toString() { return childReferenceInParent.toString() + " in " + parent.get(); } }
if (inserted.getSnsIndex() == snsIndex) { return inserted;
@Override public int hashCode() { return this.before.hashCode(); }
@Override public boolean equals( Object obj ) { if (obj == this) return true; if (obj instanceof Insertions) { Insertions that = (Insertions)obj; return this.before.equals(that.before) && this.inserted.equals(that.inserted); } return false; } }
@Override public ChildReference getChild( Name name, int snsIndex, Context context ) { if (context == null) context = new SingleNameContext(); Lock lock = this.lock.readLock(); try { lock.lock(); List<NodeKey> childrenKeysWithSameName = this.childReferenceKeysByName.get(name); if (childrenKeysWithSameName == null || childrenKeysWithSameName.isEmpty()) { // This segment contains no nodes with the supplied name ... return null; } // there are no changes, so we can optimize this lookup if (snsIndex > childrenKeysWithSameName.size()) { return null; } else { NodeKey nodeKey = childrenKeysWithSameName.get(snsIndex - 1); return this.childReferencesByKey.get(nodeKey).with(snsIndex); } } finally { lock.unlock(); } }
while (iter.hasNext()) { ChildReference result = iter.next(); int index = context.consume(result.getName(), result.getKey()); if (index == snsIndex) return result.with(index); while (iter.hasNext()) { ChildReference ref = iter.next(); if (ref.getSnsIndex() == snsIndex) return ref; if (snsIndex == childWithSameName.getSnsIndex()) { return childWithSameName;
private Map<NodeKey, Map<Path, Path>> computePathChangesForSNS( PathCache workspacePaths, Path parentPath, ChildReferences childReferences, ChildReference reorderedChildRef, Path newChildPath, Path oldChildPath ) { Map<NodeKey, Map<Path, Path>> snsPathChangesByNodeKey = new HashMap<>(); if (oldChildPath.getLastSegment().hasIndex() || newChildPath.getLastSegment().hasIndex()) { // we're reordering a SNS so we have to look at all the other SNSs // and record for each their paths (see https://issues.jboss.org/browse/MODE-2510) for (ChildReference childReference : childReferences) { NodeKey childKey = childReference.getKey(); if (childKey.equals(reorderedChildRef.getKey())) { continue; } if (childReference.getName().equals(reorderedChildRef.getName())) { // childReference is a SNS so we must record it's old and new path CachedNode persistedSNS = workspaceCache().getNode(childKey); Path snsNewPath = pathFactory().create(parentPath, childReference.getSegment()); Path snsOldPath = workspacePaths.getPath(persistedSNS); if (!snsOldPath.equals(snsNewPath)) { snsPathChangesByNodeKey.put(childKey, Collections.singletonMap(snsOldPath, snsNewPath)); } } } } return snsPathChangesByNodeKey; }