tiny.sql.history.identity
Defined in history.
API (8)
Actions
Public operations.
Location.encodedLeneach: Emits each typed object key from one checked envelope's payload.
Types and contracts
Public types and contracts.
EntryErrorKeyKind: Object kinds have separate key spaces even when their key bytes agree.Location: Node position is zero except for one entry inside a tree-node batch.Scratch: Scratch contains only borrowed slices into the current payload.
Source
Source: lib/sql/src/history/identity.zig
zig
const std = @import("std");const sql = @import("../root.zig");const conflict_mod = @import("conflict.zig");const record_mod = @import("record.zig");const version = sql.version;/// Object kinds have separate key spaces even when their key bytes agree.pub const Kind = enum(u8) { commit, conflict, database_root, relation_root, relation_rows, conflict_root, row_chunk, chunk_index_page, tree_node, relation_spans,};pub const Key = struct { kind: Kind, bytes: version.Hash,};/// Node position is zero except for one entry inside a tree-node batch.pub const Location = struct { record_offset: u64, payload_len: u32, envelope_hash: version.Hash, node_position: u32 = 0, pub fn encodedLen(self: Location) u64 { return record_mod.record_header_size + @as(u64, self.payload_len); }};pub const Entry = struct { key: Key, location: Location,};/// Scratch contains only borrowed slices into the current payload.pub const Scratch = struct { relations: []version.RelationEntry, conflicts: []version.ConflictEntry,};pub const Error = error{ InvalidHistory, KeyScratchExhausted };/// Emits each typed object key from one checked envelope's payload.pub fn each( kind: record_mod.RecordKind, payload: []const u8, location: Location, scratch: Scratch, context: anytype, comptime accept: fn (@TypeOf(context), Entry) anyerror!void,) anyerror!void { var reader = record_mod.PayloadReader.init(payload); switch (kind) { .commit => { const root = try reader.hash(); const count = try reader.readU32(); const bytes = std.math.mul(usize, count, version.hash_bytes) catch return error.InvalidHistory; if (bytes != reader.remaining()) return error.InvalidHistory; const parents = std.mem.bytesAsSlice(version.Hash, payload[reader.cursor..]); try accept(context, .{ .key = .{ .kind = .commit, .bytes = version.Commit.init(root, parents).hash }, .location = location, }); }, .database_root => { const conflicts = try reader.hash(); const count = try reader.readU32(); if (count > scratch.relations.len) return error.KeyScratchExhausted; const entries = scratch.relations[0..count]; for (entries) |*entry| { entry.* = .{ .name = try reader.readBytes(), .hash = try reader.hash() }; } try reader.finish(); std.mem.sort(version.RelationEntry, entries, {}, relationLessThan); const root = version.DatabaseRoot.init(entries, .{ .hash = conflicts }); try accept(context, .{ .key = .{ .kind = .database_root, .bytes = root.hash }, .location = location, }); }, .relation_root => { if (payload.len < version.hash_bytes) return error.InvalidHistory; try accept(context, .{ .key = .{ .kind = .relation_root, .bytes = payload[payload.len - 32 ..][0..32].* }, .location = location, }); }, .relation_rows, .relation_spans, .row_chunk, .chunk_index_page => { const key = try reader.hash(); const object_kind: Kind = switch (kind) { .relation_rows => .relation_rows, .relation_spans => .relation_spans, .row_chunk => .row_chunk, .chunk_index_page => .chunk_index_page, else => unreachable, }; try accept(context, .{ .key = .{ .kind = object_kind, .bytes = key }, .location = location, }); }, .conflict => { const artifact = try conflict_mod.decodeConflictArtifactPayload(&reader); try reader.finish(); try accept(context, .{ .key = .{ .kind = .conflict, .bytes = artifact.hash }, .location = location, }); }, .conflict_root => { const count = try reader.readU32(); if (count > scratch.conflicts.len) return error.KeyScratchExhausted; const entries = scratch.conflicts[0..count]; for (entries) |*entry| { const entry_kind = try conflict_mod.conflictKind(try reader.readU8()); const relation = try reader.readBytes(); const rowid = if (entry_kind == .row) try reader.readI64() else 0; entry.* = .{ .kind = entry_kind, .relation = relation, .rowid = rowid, .hash = try reader.hash(), }; } try reader.finish(); try accept(context, .{ .key = .{ .kind = .conflict_root, .bytes = version.ConflictRoot.init(entries).hash, }, .location = location, }); }, .tree_nodes => { const count = try reader.readU32(); if (count > record_mod.node_batch_max) return error.InvalidHistory; for (0..count) |position| { const key = try reader.hash(); _ = try record_mod.nodeKind(try reader.readU8()); _ = try reader.readBytes(); _ = try reader.optionalBytes(); _ = try record_mod.readUsize(&reader); _ = try record_mod.readSummary(&reader); _ = try reader.hash(); const children = try reader.readU32(); const child_bytes = std.math.mul(usize, children, 32) catch return error.InvalidHistory; if (child_bytes > reader.remaining()) return error.InvalidHistory; reader.cursor += child_bytes; var node_location = location; node_location.node_position = @intCast(position); try accept(context, .{ .key = .{ .kind = .tree_node, .bytes = key }, .location = node_location, }); } try reader.finish(); }, .ref, .ref_delete, .fast_forward_prepare, .fast_forward_commit, .fast_forward_abort, .fast_forward_complete, => {}, }}fn relationLessThan(_: void, left: version.RelationEntry, right: version.RelationEntry) bool { return std.mem.lessThan(u8, left.name, right.name);}Source: lib/sql/src/history/root.zig:5
zig
pub const identity = @import("identity.zig");Audit
| Definitions | 9 |
|---|---|
| Public names | 9 |
| Members | 22 |
| Version | 26.7.0 |
| Revision | daab053ee433 |