Repository navigation
Write the live map files off the server thread #253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e6ca0e8
Write the live map files off the server thread
XxFran10xX 20436e1
Keep 0644 on map files written through a temporary file
XxFran10xX bb1e579
Create the map temp file like a plain new file
XxFran10xX f9ee6f6
Write the queued and full cycles' live map files with their upload too
XxFran10xX File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/java/net/tfminecraft/simplefactions/map/MapFile.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package net.tfminecraft.simplefactions.map; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.io.Writer; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.AtomicMoveNotSupportedException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.StandardCopyOption; | ||
| import java.nio.file.StandardOpenOption; | ||
| import java.util.UUID; | ||
|
|
||
| import com.google.gson.Gson; | ||
| import com.google.gson.GsonBuilder; | ||
| import com.google.gson.JsonElement; | ||
|
|
||
| /** | ||
| * A map payload already built on the server thread, to be written to disk later, possibly off it. The tree | ||
| * holds only copied values, so writing it never reads live faction state. It is written to a temporary file | ||
| * in the same folder and then moved over the target, so an upload never reads a half-written file and two | ||
| * overlapping writes of the same payload cannot interleave. | ||
| */ | ||
| public record MapFile(File file, JsonElement json, boolean pretty) { | ||
|
|
||
| public void write() throws IOException { | ||
| Path target = file.toPath().toAbsolutePath(); | ||
| Path folder = target.getParent(); | ||
| Files.createDirectories(folder); | ||
| // Created like a plain new file (umask and the folder's default ACL apply, as with the old in-place | ||
| // writer), not with Files.createTempFile, which would make it owner-only on Linux. | ||
| Path temp = folder.resolve(target.getFileName() + "." + UUID.randomUUID() + ".tmp"); | ||
| try { | ||
| try (Writer writer = Files.newBufferedWriter(temp, StandardCharsets.UTF_8, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE)) { | ||
| Gson gson = pretty ? new GsonBuilder().setPrettyPrinting().create() : new Gson(); | ||
| gson.toJson(json, writer); | ||
| } | ||
| try { | ||
| Files.move(temp, target, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE); | ||
| } catch (AtomicMoveNotSupportedException ex) { | ||
| Files.move(temp, target, StandardCopyOption.REPLACE_EXISTING); | ||
| } | ||
| } finally { | ||
| Files.deleteIfExists(temp); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.