tiny.pluck.query_context
Defined in tiny.pluck.
API (15)
Actions
Public operations.
ResolvedConfig.toLazyKCConfigcreateRunContextcreateWorkerContextdeinitQueryContextdeinitRunContextinitQueryContextinitRunContextqueryAllocatorresetQueryArena
Types and contracts
Public types and contracts.
Source
Source: lib/pluck/src/query.zig
zig
const std = @import("std");const alloc_arena = @import("alloc_arena");const Allocator = std.mem.Allocator;const pexpr = @import("pexpr.zig");const Definitions = pexpr.Definitions;const TypeRegistry = pexpr.TypeRegistry;const bdd = @import("bdd.zig");const Manager = bdd.Manager;const state_module = @import("state/root.zig");const LazyKCState = state_module.LazyKCState;const LazyKCConfig = state_module.LazyKCConfig;const LazyKCStats = state_module.LazyKCStats;const def_order = @import("order.zig");const DefinitionOrderMode = def_order.DefinitionOrderMode;const limits = @import("limits.zig");pub const SharedContext = struct { allocator: Allocator, arena: *alloc_arena.Arena, types: *const TypeRegistry, definitions: *const Definitions, config: SessionConfig,};pub const SessionConfig = struct { max_depth: ?u32 = null, ite_limit: ?u64 = limits.default_agent_ite_limit, time_limit: ?f64 = null, sample_after_max_depth: bool = false, use_strict_order: bool = true, use_reverse_order: bool = false, definition_order_mode: DefinitionOrderMode = .none, parallel_wmc: bool = false, verbose: bool = false, rng_seed: ?u64 = null, lpsmc_rng_seed: ?u64 = null,};pub const QueryConfig = struct { max_depth: ?u32 = null, ite_limit: ?u64 = null, time_limit: ?f64 = null, sample_after_max_depth: ?bool = null, use_strict_order: ?bool = null, use_reverse_order: ?bool = null, definition_order_mode: ?DefinitionOrderMode = null, parallel_wmc: ?bool = null, rng_seed: ?u64 = null, pub fn mergeWith(self: QueryConfig, session: SessionConfig) ResolvedConfig { return .{ .max_depth = self.max_depth orelse session.max_depth, .ite_limit = self.ite_limit orelse session.ite_limit, .time_limit = self.time_limit orelse session.time_limit, .sample_after_max_depth = self.sample_after_max_depth orelse session.sample_after_max_depth, .use_strict_order = self.use_strict_order orelse session.use_strict_order, .use_reverse_order = self.use_reverse_order orelse session.use_reverse_order, .definition_order_mode = self.definition_order_mode orelse session.definition_order_mode, .parallel_wmc = self.parallel_wmc orelse session.parallel_wmc, .rng_seed = self.rng_seed orelse session.rng_seed orelse session.lpsmc_rng_seed, }; }};pub const ResolvedConfig = struct { max_depth: ?u32, ite_limit: ?u64, time_limit: ?f64, sample_after_max_depth: bool, use_strict_order: bool, use_reverse_order: bool, definition_order_mode: DefinitionOrderMode, parallel_wmc: bool, rng_seed: ?u64, pub fn toLazyKCConfig(self: ResolvedConfig) LazyKCConfig { return .{ .max_depth = self.max_depth, .ite_limit = self.ite_limit, .time_limit = self.time_limit, .sample_after_max_depth = self.sample_after_max_depth, .use_strict_order = self.use_strict_order, .use_reverse_order = self.use_reverse_order, .definition_order = null, .parallel_wmc = self.parallel_wmc, .full_dist = true, }; }};pub const QueryContext = struct { allocator: Allocator, arena: std.heap.ArenaAllocator, manager: *Manager, shared: SharedContext, config: ResolvedConfig, stats: LazyKCStats,};pub const RunContext = struct { state: LazyKCState, worker_id: u32, query: *QueryContext,};pub fn initQueryContext(shared: SharedContext, query_config: QueryConfig) !QueryContext { const allocator = shared.allocator; const manager = try allocator.create(Manager); errdefer allocator.destroy(manager); manager.* = try Manager.init(allocator); errdefer manager.deinit(); return .{ .allocator = allocator, .arena = std.heap.ArenaAllocator.init(allocator), .manager = manager, .shared = shared, .config = query_config.mergeWith(shared.config), .stats = .{}, };}pub fn deinitQueryContext(query: *QueryContext) void { query.manager.deinit(); query.allocator.destroy(query.manager); query.arena.deinit();}pub fn queryAllocator(query: *QueryContext) Allocator { return query.arena.allocator();}pub fn resetQueryArena(query: *QueryContext) void { _ = query.arena.reset(.retain_capacity);}pub fn createRunContext(query: *QueryContext) !RunContext { return initRunContext(query, 0, query.config.rng_seed);}pub fn createWorkerContext(query: *QueryContext, worker_id: u32, seed: u64) !RunContext { const worker_seed = seed +% @as(u64, worker_id) *% 0x9e3779b97f4a7c15; return initRunContext(query, worker_id, worker_seed);}pub fn initRunContext(query: *QueryContext, worker_id: u32, seed: ?u64) !RunContext { var cfg = query.config.toLazyKCConfig(); const query_alloc = queryAllocator(query); if (query.config.use_strict_order) { cfg.definition_order = try def_order.buildDefinitionOrder( query_alloc, query.shared.definitions, query.config.definition_order_mode, ); } var state = try state_module.initChecked( query_alloc, query.manager, query.shared.definitions, cfg, ); if (seed) |s| { state.prng = std.Random.DefaultPrng.init(s); } return .{ .state = state, .worker_id = worker_id, .query = query, };}pub fn deinitRunContext(run: *RunContext) void { state_module.deinit(&run.state);}test "session config defaults to the agent BDD quota while LazyKC stays explicit" { const session = SessionConfig{}; try std.testing.expectEqual(@as(?u64, limits.default_agent_ite_limit), session.ite_limit); try std.testing.expectEqual(@as(?u64, limits.default_agent_ite_limit), (QueryConfig{}).mergeWith(session).ite_limit); try std.testing.expect((LazyKCConfig{}).ite_limit == null); const unlimited = SessionConfig{ .ite_limit = null }; try std.testing.expect((QueryConfig{}).mergeWith(unlimited).ite_limit == null);}test "QueryContext init and deinit" { const allocator = std.testing.allocator; var arena = alloc_arena.Arena.init(allocator); defer arena.deinit(); var types = try TypeRegistry.initWithDefaults(arena.allocator()); var definitions = Definitions.init(arena.allocator()); const shared = SharedContext{ .allocator = allocator, .arena = &arena, .types = &types, .definitions = &definitions, .config = .{}, }; var query_ctx = try initQueryContext(shared, .{}); defer deinitQueryContext(&query_ctx); try std.testing.expect(query_ctx.manager.nodes.items.len >= 1); try std.testing.expectEqual(@as(usize, 0), query_ctx.manager.var_order.items.len);}test "QueryContext with config overrides" { const allocator = std.testing.allocator; var arena = alloc_arena.Arena.init(allocator); defer arena.deinit(); var types = try TypeRegistry.initWithDefaults(arena.allocator()); var definitions = Definitions.init(arena.allocator()); const shared = SharedContext{ .allocator = allocator, .arena = &arena, .types = &types, .definitions = &definitions, .config = .{ .max_depth = 100, .time_limit = 10.0 }, }; var query_ctx = try initQueryContext(shared, .{ .max_depth = 50, .time_limit = null, }); defer deinitQueryContext(&query_ctx); try std.testing.expectEqual(@as(?u32, 50), query_ctx.config.max_depth); try std.testing.expectEqual(@as(?f64, 10.0), query_ctx.config.time_limit);}test "RunContext init and deinit" { const allocator = std.testing.allocator; var arena = alloc_arena.Arena.init(allocator); defer arena.deinit(); var types = try TypeRegistry.initWithDefaults(arena.allocator()); var definitions = Definitions.init(arena.allocator()); const shared = SharedContext{ .allocator = allocator, .arena = &arena, .types = &types, .definitions = &definitions, .config = .{}, }; var query_ctx = try initQueryContext(shared, .{ .rng_seed = 12345 }); defer deinitQueryContext(&query_ctx); var run_ctx = try createRunContext(&query_ctx); defer deinitRunContext(&run_ctx); try std.testing.expectEqual(@as(u32, 0), run_ctx.worker_id);}test "multiple worker contexts have independent state" { const allocator = std.testing.allocator; var arena = alloc_arena.Arena.init(allocator); defer arena.deinit(); var types = try TypeRegistry.initWithDefaults(arena.allocator()); var definitions = Definitions.init(arena.allocator()); const shared = SharedContext{ .allocator = allocator, .arena = &arena, .types = &types, .definitions = &definitions, .config = .{}, }; var query_ctx = try initQueryContext(shared, .{}); defer deinitQueryContext(&query_ctx); var worker1 = try createWorkerContext(&query_ctx, 1, 42); defer deinitRunContext(&worker1); var worker2 = try createWorkerContext(&query_ctx, 2, 42); defer deinitRunContext(&worker2); try std.testing.expectEqual(@as(u32, 1), worker1.worker_id); try std.testing.expectEqual(@as(u32, 2), worker2.worker_id); const rand1 = worker1.state.prng.random().int(u64); const rand2 = worker2.state.prng.random().int(u64); try std.testing.expect(rand1 != rand2);}test "QueryContext resetArena" { const allocator = std.testing.allocator; var arena = alloc_arena.Arena.init(allocator); defer arena.deinit(); var types = try TypeRegistry.initWithDefaults(arena.allocator()); var definitions = Definitions.init(arena.allocator()); const shared = SharedContext{ .allocator = allocator, .arena = &arena, .types = &types, .definitions = &definitions, .config = .{}, }; var query_ctx = try initQueryContext(shared, .{}); defer deinitQueryContext(&query_ctx); const query_alloc = queryAllocator(&query_ctx); _ = try query_alloc.alloc(u8, 1000); resetQueryArena(&query_ctx);}Source: lib/pluck/src/root.zig:19
zig
pub const query_context = @import("query.zig");Audit
| Definitions | 11 |
|---|---|
| Public names | 11 |
| Members | 9 |
| Version | 26.7.0 |
| Revision | daab053ee433 |