const std = @import("std"); const sqlite = @import("sqlite"); const DataLock = @import("data_lock.zig").DataLock; pub const db_file_name = "zyncbase.db"; pub const CreateInfo = struct { bytes: u64, duration_ms: i64, }; pub const RestoreInfo = struct { /// Previous database kept for rollback when `--no-safety-copy` was not set. /// Owned by the caller. safety_copy: ?[]u8, pub fn deinit(self: *RestoreInfo, allocator: std.mem.Allocator) void { if (self.safety_copy) |path| allocator.free(path); self.safety_copy = null; } }; const SafetyCopy = struct { db: []const u8, wal: []const u8, shm: []const u8, }; const restore_journal_header = "zyncbase-restore-v1\n"; pub fn dbPath(allocator: std.mem.Allocator, data_dir: []const u8) ![]u8 { return std.fs.path.join(allocator, &.{ data_dir, db_file_name }); } /// Write a consistent snapshot of a live (or stopped) database. /// /// `VACUUM INTO` runs in a read transaction, so it sees a snapshot that /// includes committed WAL frames while writers continue. The output is a /// compact, single-file SQLite database. pub fn create( io: std.Io, allocator: std.mem.Allocator, db_path: []const u8, out_path: []const u8, ) !CreateInfo { if (!try fileExists(io, db_path)) return error.DatabaseNotFound; if (try fileExists(io, out_path)) return error.OutputExists; const data_dir = std.fs.path.dirname(db_path) orelse "."; var lock = try DataLock.acquireShared(io, allocator, data_dir); defer lock.release(io); if (try restoreJournalExists(io, allocator, db_path)) return error.RestoreInProgress; const partial_path = try std.fmt.allocPrint( allocator, "{s}.partial-{d}", .{ out_path, std.Io.Clock.real.now(io).toNanoseconds() }, ); defer allocator.free(partial_path); // zwanzig-disable-next-line: sentinel-alloc const db_path_z = try allocator.dupeZ(u8, db_path); defer allocator.free(db_path_z); var db = try sqlite.Db.init(.{ .mode = .{ .File = db_path_z }, // Read-write so SQLite can recover a hot WAL after a crash. .open_flags = .{ .write = true, .create = false }, }); defer db.deinit(); _ = try db.pragma(void, .{}, "busy_timeout", "5000"); return publishSnapshot(io, allocator, &db, partial_path, out_path); } /// `VACUUM INTO` a partial file, verify, fsync, then publish with a /// non-replacing rename. A failed snapshot never appears at `out_path`. fn publishSnapshot( io: std.Io, allocator: std.mem.Allocator, db: *sqlite.Db, partial_path: []const u8, out_path: []const u8, ) !CreateInfo { errdefer deleteQuietly(io, partial_path); const started_at = std.Io.Clock.real.now(io).toMilliseconds(); try db.execDynamic("VACUUM INTO ?", .{}, .{partial_path}); try verifyFile(io, allocator, partial_path, false); try syncFile(io, partial_path); if (try fileExists(io, out_path)) return error.OutputExists; const cwd = std.Io.Dir.cwd(); cwd.renamePreserve(partial_path, cwd, out_path, io) catch |err| switch (err) { error.PathAlreadyExists => return error.OutputExists, else => return err, }; try syncParentDir(io, out_path); const stat = try cwd.statFile(io, out_path, .{}); return .{ .bytes = stat.size, .duration_ms = std.Io.Clock.real.now(io).toMilliseconds() - started_at, }; } /// Writes the pre-migration snapshot of `db` (the database file at `db_path`) /// to `.pre-migration-