1
Fork 0
turbonss/src/File.zig

94 lines
2.4 KiB
Zig
Raw Normal View History

2022-04-20 07:19:34 +03:00
const std = @import("std");
const os = std.os;
2022-04-21 09:30:39 +03:00
const fs = std.fs;
const Allocator = std.mem.Allocator;
2022-04-20 07:19:34 +03:00
2022-04-21 09:30:39 +03:00
const Corpus = @import("Corpus.zig");
2022-04-20 07:19:34 +03:00
const DB = @import("DB.zig");
2022-07-04 06:09:03 +03:00
const ErrCtx = @import("ErrCtx.zig");
2022-04-20 07:19:34 +03:00
const InvalidHeader = @import("header.zig").Invalid;
const File = @This();
db: DB,
ptr: []align(4096) const u8,
pub const Error = os.OpenError || os.FStatError || os.MMapError || InvalidHeader;
pub fn open(fname: []const u8) Error!File {
const fd = try os.open(fname, os.O.RDONLY, 0);
var fd_open = true;
2022-04-21 09:30:39 +03:00
errdefer if (fd_open) os.close(fd);
2022-04-20 07:19:34 +03:00
const st = try os.fstat(fd);
2022-04-21 09:30:39 +03:00
const size = @intCast(usize, st.size);
2022-04-20 07:19:34 +03:00
const ptr = try os.mmap(null, size, os.PROT.READ, os.MAP.SHARED, fd, 0);
errdefer os.munmap(ptr);
os.close(fd);
fd_open = false;
const db = try DB.fromBytes(ptr);
return File{ .db = db, .ptr = ptr };
}
pub fn close(self: *File) void {
os.munmap(self.ptr);
self.* = undefined;
}
2022-04-21 09:30:39 +03:00
const testing = std.testing;
2022-06-07 06:30:18 +03:00
pub const TestDB = struct {
2022-07-09 16:50:49 +03:00
file: File,
2022-04-21 09:30:39 +03:00
dir: testing.TmpDir,
path: [:0]const u8,
2022-07-09 16:50:49 +03:00
allocator: Allocator,
2022-04-21 09:30:39 +03:00
2022-07-06 16:54:43 +03:00
pub fn init(allocator: Allocator) !TestDB {
var errc = ErrCtx{};
2022-04-21 09:30:39 +03:00
var corpus = try Corpus.testCorpus(allocator);
defer corpus.deinit();
2022-07-06 16:54:43 +03:00
var db = try DB.fromCorpus(allocator, &corpus, &errc);
2022-04-21 09:30:39 +03:00
defer db.deinit(allocator);
var tmp = testing.tmpDir(.{});
errdefer tmp.cleanup();
const base_path = blk: {
2022-06-07 06:30:18 +03:00
const relative_path = try fs.path.join(allocator, &[_][]const u8{
"zig-cache",
"tmp",
tmp.sub_path[0..],
});
const real_path = try fs.realpathAlloc(allocator, relative_path);
allocator.free(relative_path);
break :blk real_path;
};
2022-07-09 16:50:49 +03:00
const full_path = try fs.path.joinZ(allocator, &[_][]const u8{
2022-06-07 06:30:18 +03:00
base_path,
2022-07-09 16:50:49 +03:00
"db.turbo",
2022-06-07 06:30:18 +03:00
});
allocator.free(base_path);
2022-04-21 09:30:39 +03:00
const mode = os.O.RDWR | os.O.CREAT | os.O.EXCL;
const fd = try os.openat(tmp.dir.fd, "db.turbo", mode, 0o666);
defer os.close(fd);
_ = try os.writev(fd, db.iov().constSlice());
2022-06-07 06:30:18 +03:00
return TestDB{
2022-07-09 16:50:49 +03:00
.file = try open(full_path),
2022-06-07 06:30:18 +03:00
.dir = tmp,
2022-07-09 16:50:49 +03:00
.path = full_path,
2022-06-07 06:30:18 +03:00
.allocator = allocator,
};
2022-04-21 09:30:39 +03:00
}
2022-06-07 06:30:18 +03:00
pub fn deinit(self: *TestDB) void {
2022-04-21 09:30:39 +03:00
self.dir.cleanup();
2022-06-07 06:30:18 +03:00
self.allocator.free(self.path);
2022-04-21 09:30:39 +03:00
}
};