Skip to content

Commit

Permalink
No clue but try make stuff more thread safe
Browse files Browse the repository at this point in the history
  • Loading branch information
zbx1425 committed Jan 17, 2025
1 parent 4405e88 commit ef2daf9
Showing 1 changed file with 27 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,33 +59,41 @@ private Path getLevelRegionPath(ResourceLocation dimension, ChunkPos region) {
}

public void insert(CommentEntry newEntry) throws IOException {
try {
Files.createDirectory(getLevelPath(newEntry.level));
} catch (FileAlreadyExistsException ignored) { }
Path targetFile = getLevelRegionPath(newEntry.level, newEntry.region);
try (FileOutputStream oStream = new FileOutputStream(targetFile.toFile(), true)) {
newEntry.writeFileStream(oStream);
synchronized (this) {
try {
Files.createDirectory(getLevelPath(newEntry.level));
} catch (FileAlreadyExistsException ignored) {
}
Path targetFile = getLevelRegionPath(newEntry.level, newEntry.region);
try (FileOutputStream oStream = new FileOutputStream(targetFile.toFile(), true)) {
newEntry.writeFileStream(oStream);
}
}
}

public void update(CommentEntry existingEntry) throws IOException {
assert existingEntry.fileOffset > 0;
Path targetFile = getLevelRegionPath(existingEntry.level, existingEntry.region);
try (RandomAccessFile oStream = new RandomAccessFile(targetFile.toFile(), "rw")) {
existingEntry.updateInFile(oStream);
synchronized (this) {
assert existingEntry.fileOffset > 0;
Path targetFile = getLevelRegionPath(existingEntry.level, existingEntry.region);
try (RandomAccessFile oStream = new RandomAccessFile(targetFile.toFile(), "rw")) {
existingEntry.updateInFile(oStream);
}
}
}

public void updateRegion(List<CommentEntry> regionEntries) throws IOException {
if (regionEntries.isEmpty()) return;
CommentEntry pivot = regionEntries.getFirst();
try {
Files.createDirectory(getLevelPath(pivot.level));
} catch (FileAlreadyExistsException ignored) { }
Path targetFile = getLevelRegionPath(pivot.level, pivot.region);
try (FileOutputStream oStream = new FileOutputStream(targetFile.toFile(), false)) {
for (CommentEntry entry : regionEntries) {
entry.writeFileStream(oStream);
synchronized (this) {
if (regionEntries.isEmpty()) return;
CommentEntry pivot = regionEntries.getFirst();
try {
Files.createDirectory(getLevelPath(pivot.level));
} catch (FileAlreadyExistsException ignored) {
}
Path targetFile = getLevelRegionPath(pivot.level, pivot.region);
try (FileOutputStream oStream = new FileOutputStream(targetFile.toFile(), false)) {
for (CommentEntry entry : regionEntries) {
entry.writeFileStream(oStream);
}
}
}
}
Expand Down

0 comments on commit ef2daf9

Please sign in to comment.