tiny.sql.FileReadCache
Defined in tiny.sql.
API (17)
Actions
Public operations.
Types and contracts
Public types and contracts.
Fields and members
Public fields and members.
Source
Source: lib/sql/src/file.zig:1262
zig
pub const ReadCache = struct { pub const storage_alignment: usize = @alignOf(ReadCacheKey); pub const Storage = []align(storage_alignment) u8; pub const Limits = struct { pages: usize, }; pub const Capacity = struct { pages: usize, storage_bytes: usize, pub fn derive(limits: Limits) error{CapacityOverflow}!Capacity { const key_bytes = std.math.mul(usize, limits.pages, @sizeOf(ReadCacheKey)) catch { return error.CapacityOverflow; }; const image_bytes = std.math.mul(usize, limits.pages, @sizeOf(ReadCacheImage)) catch { return error.CapacityOverflow; }; const storage_bytes = std.math.add(usize, key_bytes, image_bytes) catch { return error.CapacityOverflow; }; return .{ .pages = limits.pages, .storage_bytes = storage_bytes, }; } }; pub const InitError = error{ CapacityOverflow, StorageTooShort }; pub const Exhaustion = error{CacheDisabled}; pub const work_limits: alloc_phase.capacity.WorkLimits = .{ .transition_steps_max = std.math.maxInt(usize), .cleanup_steps_per_call_max = 0, .cleanup_calls_at_capacity_max = 0, }; pub const claim: alloc_phase.capacity.Declaration = .{ .source = .{ .id = "sql.file_read_cache", .kind = .phase_static, .limit_source = .caller, .storage = .{ .covered = &.{ .{ .id = "configured_direct_mapped_base_page_cache_slots_for_94cfd2eab286", .lifetime = .steady, .detail = "configured direct-mapped base-page cache slots for lazy and durable-checkpoint reads", }, }, .excluded = &.{ "operating-system page cache, database and WAL file contents, and file handles", "pager-owned WAL images, indexes, prepared checkpoint descriptors, and reader-bearing in-memory base history", "caller-owned copyPage destination images and trace instrumentation", }, }, .capacity = .{ .inputs = &.{ alloc_phase.capacity.bindInput(Limits, "pages", "pages"), }, .type_selectors = &.{ alloc_phase.capacity.bindType(ReadCacheKey, "readcachekey"), alloc_phase.capacity.bindType(ReadCacheImage, "readcacheimage"), }, .nodes = &.{ .{ .input = 0 }, .{ .scale = .{ .node = 0, .coefficient = .{ .size_of_concrete_type = 0 } } }, .{ .scale = .{ .node = 0, .coefficient = .{ .size_of_concrete_type = 1 } } }, .{ .add = .{ .left = 1, .right = 2 } }, }, .assertions = &.{.{ .scope = .closure_total, .measure = .retained, .relation = .exact, .expression = 3, }}, }, .overload = .{ .kind = .reject_before_mutation, .detail = "short caller storage rejects initialization and zero cache capacity returns CacheDisabled before mutation; admitted positive capacity replaces exactly page_id modulo slots", }, .risks = .{ .transitive = .{ .status = .open, .detail = "cache methods are allocation-free value copies, but no machine call-graph certificate excludes future transitive growth", }, .foreign = .{ .status = .excluded, .detail = "file reads durable checkpoint writes and the operating-system page cache are outside the owner-local heap-storage claim", }, }, .work = .{ .equation = "initialization and clear visit only the contiguous key bound, occupancy counting visits the admitted key bound, and get and put take constant steps", }, .obligations = &.{ .{ .key = "sql_read_cache_capacity", .role = .capacity_model }, .{ .key = "sql_read_cache_storage_rejection", .role = .initialization_failure }, .{ .key = "sql_read_cache_sealed_overload", .role = .overload }, .{ .key = "sql_read_cache_work_bound", .role = .work_bound }, .{ .key = "sql_read_cache_sealed_transitive_risk", .role = .transitive_risk }, .{ .key = "sql_read_cache_semantics_overload", .role = .overload }, .{ .key = "sql_read_cache_semantics_foreign_risk", .role = .foreign_risk }, .{ .key = "sql_read_cache_checkpoint_semantics", .role = .foreign_risk }, }, }, .bindings = .{ .owner = @This(), .seal = .{ .family = alloc_phase.capacity.selector(@This().activate), .premise = .{ .class = .checked_semantic_fact, .authority = .checker, }, }, .teardown = .{ .family = alloc_phase.capacity.selector(@This().deinit), .premise = .{ .class = .checked_semantic_fact, .authority = .checker, }, }, }, }; phase: alloc_phase.capacity.Phase, capacity: Capacity, storage: Storage, keys: []ReadCacheKey, pub fn init(storage: Storage, limits: Limits) InitError!ReadCache { const capacity = try Capacity.derive(limits); if (storage.len < capacity.storage_bytes) return error.StorageTooShort; const borrowed = storage[0..capacity.storage_bytes]; const keys = keysFromStorage(borrowed, capacity); for (keys) |*key| key.* = .{ .generation = 0, .page_id = 0 }; return .{ .phase = .initialization, .capacity = capacity, .storage = borrowed, .keys = keys, }; } pub fn activate(self: *ReadCache) void { std.debug.assert(self.phase == .initialization); std.debug.assert(self.keys.len == self.capacity.pages); std.debug.assert(imagesFromStorage(self.storage, self.capacity).len == self.capacity.pages); self.phase = .steady; } pub fn deinit(self: *ReadCache) Storage { std.debug.assert(self.phase != .teardown); std.debug.assert(self.storage.len == self.capacity.storage_bytes); std.debug.assert(self.keys.len == self.capacity.pages); std.debug.assert(self.keys.ptr == keysFromStorage(self.storage, self.capacity).ptr); self.phase = .teardown; const storage = self.storage; self.* = undefined; return storage; } fn clearRetainingCapacity(self: *ReadCache) void { std.debug.assert(self.phase == .steady); for (self.keys) |*key| key.* = .{ .generation = 0, .page_id = 0 }; } fn get(self: *ReadCache, key: ReadCacheKey, image: *[page.size]u8) bool { std.debug.assert(self.phase == .steady); std.debug.assert(key.page_id != 0); if (self.keys.len == 0) return false; const slot = cacheSlot(key, self.keys.len); const cached = self.keys[slot]; if (cached.generation != key.generation or cached.page_id != key.page_id) return false; image.* = imagesFromStorage(self.storage, self.capacity)[slot]; return true; } pub fn put(self: *ReadCache, key: ReadCacheKey, image: *const [page.size]u8) Exhaustion!void { std.debug.assert(self.phase == .steady); std.debug.assert(key.page_id != 0); if (self.keys.len == 0) return error.CacheDisabled; const slot = cacheSlot(key, self.keys.len); imagesFromStorage(self.storage, self.capacity)[slot] = image.*; self.keys[slot] = .{ .generation = key.generation, .page_id = key.page_id }; } /// Returns the check mark of the cached image for `key`, or null when the /// cache does not hold that image. fn checkMark(self: *ReadCache, key: ReadCacheKey) ?*bool { std.debug.assert(self.phase == .steady); if (self.keys.len == 0) return null; const cached = &self.keys[cacheSlot(key, self.keys.len)]; if (cached.generation != key.generation or cached.page_id != key.page_id) return null; return &cached.checked; } fn count(self: *const ReadCache) usize { std.debug.assert(self.phase == .steady); var total: usize = 0; for (self.keys) |key| { if (key.page_id != 0) total += 1; } return total; } fn keysFromStorage(storage: Storage, capacity: Capacity) []ReadCacheKey { return std.mem.bytesAsSlice(ReadCacheKey, storage[0..keyBytes(capacity)]); } fn imagesFromStorage(storage: Storage, capacity: Capacity) []ReadCacheImage { return std.mem.bytesAsSlice( ReadCacheImage, storage[keyBytes(capacity)..capacity.storage_bytes], ); } fn keyBytes(capacity: Capacity) usize { return std.math.mul(usize, capacity.pages, @sizeOf(ReadCacheKey)) catch unreachable; }};Source: lib/sql/src/root.zig:306
zig
pub const FileReadCache = file.ReadCache;Audit
| Definitions | 14 |
|---|---|
| Public names | 14 |
| Members | 10 |
| Version | 26.7.0 |
| Revision | daab053ee433 |