tiny.sql.FileReadOnlyDatabase
Defined in tiny.sql.
API (8)
Actions
Public operations.
Types and contracts
Public types and contracts.
Fields and members
Public fields and members.
Source
Source: lib/sql/src/file.zig:3041
zig
pub const ReadOnlyDatabase = struct { pub const Workspace = DatabaseWorkspace; storage: [@sizeOf(ReadOnlyState)]u8 align(@alignOf(ReadOnlyState)), pub fn openForTesting( allocator: Allocator, dir: std.Io.Dir, options: ReadOpenOptions, ) Error!ReadOpenResult { if (!builtin.is_test) @compileError("openForTesting is available only in tests"); const workspace = try allocator.create(Workspace); errdefer allocator.destroy(workspace); workspace.* = try Workspace.allocate(allocator, .{ .header = options.header, .max_wal_bytes = options.max_wal_bytes, .path_storage = .{ .database_bytes = 0, .wal_bytes = 0, }, .read_cache_pages = options.read_cache_capacity, }); errdefer workspace.deallocate(allocator); const opened = try openExisting(allocator, workspace, dir, options); return switch (opened) { .ready => |ready| ready: { var database = ready; database.impl().testing_workspace_owned = true; break :ready .{ .ready = database }; }, .repair_required => |reason| repair: { workspace.deallocate(allocator); allocator.destroy(workspace); break :repair .{ .repair_required = reason }; }, }; } pub fn ownWorkspaceForTesting(self: *ReadOnlyDatabase) void { if (!builtin.is_test) @compileError("ownWorkspaceForTesting is available only in tests"); const state = self.impl(); std.debug.assert(!state.testing_workspace_owned); state.testing_workspace_owned = true; } pub fn openExisting( allocator: Allocator, workspace: *Workspace, dir: std.Io.Dir, options: ReadOpenOptions, ) Error!ReadOpenResult { try options.control.check(); var read_cache = try workspace.acquireReadCache(.{ .pages = options.read_cache_capacity, }); errdefer workspace.releaseReadCache(&read_cache); read_cache.activate(); const recovered = try recoverReadOnly(allocator, workspace, dir, options); var recovered_pager = switch (recovered) { .repair_required => |reason| { workspace.releaseReadCache(&read_cache); return .{ .repair_required = reason }; }, .ready => |ready| ready, }; errdefer workspace.release(&recovered_pager); try options.control.check(); const base_file: ?std.Io.File = if (try filePresent(options.io, dir, options.paths.database)) try dir.openFile(options.io, options.paths.database, .{}) else null; errdefer if (base_file) |file| file.close(options.io); const view = (try recovered_pager.beginRead()).view; try options.control.check(); var database: ReadOnlyDatabase = undefined; database.impl().* = .{ .allocator = allocator, .workspace = workspace, .io = options.io, .base_file = base_file, .pager = recovered_pager, .read_cache = read_cache, .view = view, }; return .{ .ready = database }; } pub fn deinit(self: *ReadOnlyDatabase) void { const state = self.impl(); const allocator = state.allocator; const workspace = state.workspace; const testing_workspace_owned = if (builtin.is_test) state.testing_workspace_owned else false; if (state.base_file) |file| file.close(state.io); state.workspace.release(&state.pager); state.workspace.releaseReadCache(&state.read_cache); self.* = undefined; if (testing_workspace_owned) { workspace.deallocate(allocator); allocator.destroy(workspace); } } pub fn snapshot(self: *ReadOnlyDatabase) Snapshot { const state = self.impl(); return .{ .source = .{ .durable = .{ .io = state.io, .base_file = if (state.base_file) |*file| file else null, .pager = &state.pager, .read_cache = &state.read_cache, } }, .view = state.view, }; } pub fn readCacheCapacity(self: *const ReadOnlyDatabase) usize { return self.implConst().read_cache.capacity.pages; } fn impl(self: *ReadOnlyDatabase) *ReadOnlyState { return @ptrCast(&self.storage); } fn implConst(self: *const ReadOnlyDatabase) *const ReadOnlyState { return @ptrCast(&self.storage); }};Source: lib/sql/src/root.zig:180
zig
pub const FileReadOnlyDatabase = file.ReadOnlyDatabase;Audit
| Definitions | 8 |
|---|---|
| Public names | 8 |
| Members | 1 |
| Version | 26.7.0 |
| Revision | daab053ee433 |