Skip to content

Commit

Permalink
Merge pull request #6 from serpent-os/atomic-index-write
Browse files Browse the repository at this point in the history
indexer: Write index atomically using `rename`
  • Loading branch information
ikeycode authored Sep 13, 2024
2 parents 7d0973a + 7ca36be commit c71e92a
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 c71e92a

Please sign in to comment.