in src/main/java/com/google/firebase/database/core/WriteTree.java [173:235]
public boolean removeWrite(long writeId) {
// Note: disabling this check. It could be a transaction that preempted another
// transaction, and
// thus was applied out of order.
// var validClear = revert || this.allWrites_.length === 0 ||
// writeId <= this.allWrites_[0].writeId;
// fb.core.util.assert(validClear, "Either we don't have this write, or it's the first
// one in
// the queue");
// TODO: maybe use hashmap
UserWriteRecord writeToRemove = null;
int idx = 0;
for (UserWriteRecord record : this.allWrites) {
if (record.getWriteId() == writeId) {
writeToRemove = record;
break;
}
idx++;
}
assert writeToRemove != null : "removeWrite called with nonexistent writeId";
this.allWrites.remove(writeToRemove);
boolean removedWriteWasVisible = writeToRemove.isVisible();
boolean removedWriteOverlapsWithOtherWrites = false;
int i = this.allWrites.size() - 1;
while (removedWriteWasVisible && i >= 0) {
UserWriteRecord currentWrite = this.allWrites.get(i);
if (currentWrite.isVisible()) {
if (i >= idx && this.recordContainsPath(currentWrite, writeToRemove.getPath())) {
// The removed write was completely shadowed by a subsequent write.
removedWriteWasVisible = false;
} else if (writeToRemove.getPath().contains(currentWrite.getPath())) {
// Either we're covering some writes or they're covering part of us
// (depending on which
// came first).
removedWriteOverlapsWithOtherWrites = true;
}
}
i--;
}
if (!removedWriteWasVisible) {
return false;
} else if (removedWriteOverlapsWithOtherWrites) {
// There's some shadowing going on. Just rebuild the visible writes from scratch.
this.resetTree();
return true;
} else {
// There's no shadowing. We can safely just remove the write(s) from visibleWrites.
if (writeToRemove.isOverwrite()) {
this.visibleWrites = this.visibleWrites.removeWrite(writeToRemove.getPath());
} else {
for (Map.Entry<Path, Node> entry : writeToRemove.getMerge()) {
Path path = entry.getKey();
this.visibleWrites = this.visibleWrites.removeWrite(writeToRemove.getPath().child(path));
}
}
return true;
}
}