tiny.sql.PreparedRelation
Defined in plan.
API (23)
Actions
Public operations.
cacheKeycurrentCacheKeydeinitexecutegetindexRangeindexScanlookupmatchesRootrefreshrelationStatsscanvalidatewriteSession
Fields and members
Public fields and members.
Source
Source: lib/sql/src/plan.zig:286
zig
pub const PreparedRelation = struct { allocator: Allocator, catalog: catalog_mod.Catalog, name: []u8, schema: catalog_mod.Schema, handle: catalog_mod.RelationHandle, stats: ?catalog_mod.RelationStats, root: ?version.RelationRoot, key: PlanKey, validation: Validation = .content, pub fn deinit(self: *PreparedRelation) void { if (self.root) |*root| root.deinit(); if (self.stats) |*stats| stats.deinit(); self.handle.deinit(); self.allocator.free(self.name); self.* = undefined; } pub fn validate(self: *PreparedRelation) Error!void { const phase = trace.scope("plan.validate_relation"); defer phase.end(); const current = try self.currentCacheKey(); if (!PlanKey.same(current, self.key)) return error.PlanChanged; } pub fn execute(self: *PreparedRelation) Error!RelationRead { const phase = trace.scope("plan.execute_relation"); defer phase.end(); try self.validate(); var lease = try self.handle.relation.space.database.beginRead(); errdefer lease.deinit(); return .{ .lease = lease, .execution = .{ .reader = try self.handle.relation.reader(lease.snapshot()), }, }; } pub fn relationStats(self: *const PreparedRelation) ?*const catalog_mod.RelationStats { if (self.stats) |*stats| return stats; return null; } pub fn cacheKey(self: *const PreparedRelation) PlanKey { return self.key; } pub fn currentCacheKey(self: *PreparedRelation) Error!PlanKey { var state = try self.catalog.readRelation(self.allocator, self.name); defer state.deinit(); return switch (self.validation) { .content => content_key: { const relation_key = try version.relationKey( self.name, &state.handle, state.relationStats(), ); break :content_key PlanKey.fromRelationKey(relation_key, self.key.parameters); }, .shape => shapeKey( self.name, &state.handle, state.relationStats(), self.key.parameters, ), }; } pub fn matchesRoot(self: *const PreparedRelation, root: *const version.RelationRoot) bool { if (self.root == null) return false; return PlanKey.same(self.key, PlanKey.fromRoot(root, self.key.parameters)); } pub fn writeSession(self: *PreparedRelation) Error!session_mod.RelationSession { const phase = trace.scope("plan.write_session"); defer phase.end(); var relation_session = try session_mod.RelationSession.open(self.allocator, &self.catalog, self.name); errdefer relation_session.deinit(); if (!self.matchesRoot(&relation_session.root)) return error.PlanChanged; return relation_session; } pub fn refresh(self: *PreparedRelation) Error!void { var state = try self.catalog.readRelation(self.allocator, self.name); errdefer state.deinit(); var root: ?version.RelationRoot = null; errdefer if (root) |*relation_root| relation_root.deinit(); const key = switch (self.validation) { .content => key: { root = try version.relationRootMaintained( self.allocator, self.name, state.schema, &state.handle, state.relationStats(), ); break :key PlanKey.fromRoot(&root.?, self.key.parameters); }, .shape => shapeKey( self.name, &state.handle, state.relationStats(), self.key.parameters, ), }; if (self.root) |*old_root| old_root.deinit(); if (self.stats) |*old_stats| old_stats.deinit(); self.handle.deinit(); self.schema = state.schema; self.handle = state.handle; self.stats = state.stats; self.root = root; self.key = key; } pub fn get(self: *PreparedRelation, allocator: Allocator, rowid: i64) Error!?[]u8 { var execution = try self.execute(); defer execution.deinit(); return try execution.get(allocator, rowid); } pub fn scan( self: *PreparedRelation, target: *relation_mod.Scan, allocator: Allocator, start: ?i64, end: ?i64, ) Error!void { var execution = try self.execute(); defer execution.deinit(); try execution.scan(target, allocator, start, end); } pub fn lookup( self: *PreparedRelation, target: *index_mod.Scan, allocator: Allocator, index_slot: usize, prefix: []const row.Value, ) Error!void { var execution = try self.execute(); defer execution.deinit(); try execution.lookup(target, allocator, index_slot, prefix); } pub fn indexScan( self: *PreparedRelation, target: *index_mod.Scan, allocator: Allocator, index_slot: usize, start: ?[]const row.Value, end: ?[]const row.Value, ) Error!void { var execution = try self.execute(); defer execution.deinit(); try execution.indexScan(target, allocator, index_slot, start, end); } pub fn indexRange( self: *PreparedRelation, target: *index_mod.Scan, allocator: Allocator, index_slot: usize, start: ?index_mod.Bound, end: ?index_mod.Bound, ) Error!void { var execution = try self.execute(); defer execution.deinit(); try execution.indexRange(target, allocator, index_slot, start, end); }};Source: lib/sql/src/root.zig:160
zig
pub const PreparedRelation = plan.PreparedRelation;Audit
| Definitions | 15 |
|---|---|
| Public names | 30 |
| Members | 9 |
| Version | 26.7.0 |
| Revision | daab053ee433 |