Skip to content

Commit

Permalink
indexer: Write index atomically using rename
Browse files Browse the repository at this point in the history
This should help avoid build failures due to attempting to read the index
while it is being rebuilt by vessel.

Signed-off-by: Rune Morling <[email protected]>
  • Loading branch information
ermo committed Sep 13, 2024
1 parent 7d0973a commit 7ca36be
Showing 1 changed file with 5 additions and 2 deletions.
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

0 comments on commit 7ca36be

Please sign in to comment.