mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-22 16:11:44 +01:00
Simplify path recursive removal
Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
parent
ece91ed4a5
commit
12543acf5e
@ -57,12 +57,25 @@ final class TagHandlerImpl implements TagHandler {
|
|||||||
TagHandlerImpl local = this;
|
TagHandlerImpl local = this;
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
if (paths != null) {
|
if (paths != null) {
|
||||||
if ((local = traversePathWrite(this, tagIndex, paths, present)) == null)
|
if ((local = traversePathWrite(this, paths, present)) == null)
|
||||||
return;
|
return; // Tried to remove an absent tag. Do nothing
|
||||||
}
|
}
|
||||||
SPMCMap entries = local.entries;
|
SPMCMap entries = local.entries;
|
||||||
if (present) entries.put(tagIndex, valueToEntry(local, tag, value));
|
if (present) {
|
||||||
else entries.remove(tagIndex);
|
entries.put(tagIndex, valueToEntry(local, tag, value));
|
||||||
|
} else {
|
||||||
|
// Remove recursively
|
||||||
|
if (entries.remove(tagIndex) == null) return;
|
||||||
|
if (paths != null) {
|
||||||
|
TagHandlerImpl tmp = local;
|
||||||
|
int i = paths.length;
|
||||||
|
do {
|
||||||
|
if (!tmp.entries.isEmpty()) break;
|
||||||
|
tmp = tmp.parent;
|
||||||
|
tmp.entries.remove(paths[--i].index());
|
||||||
|
} while (i > 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
entries.invalidate();
|
entries.invalidate();
|
||||||
assert !local.entries.rehashed;
|
assert !local.entries.rehashed;
|
||||||
}
|
}
|
||||||
@ -110,13 +123,10 @@ final class TagHandlerImpl implements TagHandler {
|
|||||||
return updatedCache().compound;
|
return updatedCache().compound;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static TagHandlerImpl traversePathWrite(TagHandlerImpl root, int tagIndex,
|
private static TagHandlerImpl traversePathWrite(TagHandlerImpl root, Tag.PathEntry[] paths,
|
||||||
Tag.PathEntry[] paths, boolean present) {
|
boolean present) {
|
||||||
final int length = paths.length;
|
|
||||||
TagHandlerImpl local = root;
|
TagHandlerImpl local = root;
|
||||||
TagHandlerImpl[] pathHandlers = new TagHandlerImpl[length];
|
for (Tag.PathEntry path : paths) {
|
||||||
for (int i = 0; i < length; i++) {
|
|
||||||
final Tag.PathEntry path = paths[i];
|
|
||||||
final int pathIndex = path.index();
|
final int pathIndex = path.index();
|
||||||
final Entry<?> entry = local.entries.get(pathIndex);
|
final Entry<?> entry = local.entries.get(pathIndex);
|
||||||
if (entry instanceof PathEntry pathEntry) {
|
if (entry instanceof PathEntry pathEntry) {
|
||||||
@ -134,28 +144,6 @@ final class TagHandlerImpl implements TagHandler {
|
|||||||
}
|
}
|
||||||
tmp.entries.put(pathIndex, new PathEntry(path.name(), local));
|
tmp.entries.put(pathIndex, new PathEntry(path.name(), local));
|
||||||
}
|
}
|
||||||
pathHandlers[i] = local;
|
|
||||||
}
|
|
||||||
// Handle removal if the tag was present (recursively)
|
|
||||||
if (!present) {
|
|
||||||
// Remove entry
|
|
||||||
TagHandlerImpl targetHandler = pathHandlers[length - 1];
|
|
||||||
if (targetHandler.entries.remove(tagIndex) == null) return null;
|
|
||||||
// Clear empty parents
|
|
||||||
for (int i = length - 1; i >= 0; i--) {
|
|
||||||
final TagHandlerImpl handler = pathHandlers[i];
|
|
||||||
if (!handler.entries.isEmpty()) break;
|
|
||||||
final int pathIndex = paths[i].index();
|
|
||||||
if (i == 0) {
|
|
||||||
// Remove the root handler
|
|
||||||
root.entries.remove(pathIndex);
|
|
||||||
} else {
|
|
||||||
TagHandlerImpl parent = pathHandlers[i - 1];
|
|
||||||
parent.entries.remove(pathIndex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
targetHandler.entries.invalidate();
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
return local;
|
return local;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user