Skip to content
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

indexer: Write index atomically using rename #6

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions source/vessel/indexer.d
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import moss.format.binary.payload;
import moss.format.binary.payload.meta;
import moss.format.binary.writer;
import std.algorithm : multiSort;
import std.file : exists, mkdirRecurse;
import std.file : exists, mkdirRecurse, rename;
import std.path : buildPath, dirName, relativePath;
import vessel.collectiondb;
import vibe.d;
Expand Down Expand Up @@ -64,7 +64,8 @@ public final class Indexer

auto records = collectionDB.volatileRecords();
records.multiSort!((a, b) => a.sourceID < b.sourceID, (a, b) => a.name < b.name);
auto fi = File(outputFilename, "wb");
immutable string tmpIndexFile = outputFilename ~ ".tmp";
auto fi = File(tmpIndexFile, "wb");
auto wr = new Writer(fi);
wr.fileType = MossFileType.Repository;
wr.compressionType = PayloadCompression.Zstd;
Expand Down Expand Up @@ -104,6 +105,8 @@ public final class Indexer
wr.addPayload(mp);
}
wr.close();
/* rename is guaranteed to be atomic */
tmpIndexFile.rename(outputFilename);
}

private:
Expand Down
Loading