index/db/commands: fold duplicated index logic into single definitions

Cleanup pass over the secondary-index feature.

add_doc is now the one entry-commit path. create_index and
build_all_indexes each hand-rolled build -> check_unique -> reserve ->
insert, and had already drifted on whether multikey is set before or
after the unique check; add_doc gained an enforce_unique flag so the
rebuild path keeps its tolerate-and-warn behavior. reserve_for and
insert_entries are now the only way the engine touches Index.entries.

One definition each for: prefix comparison and the prefix binary
searches (prefix_order + std.sort), the cartesian-product odometer
(advance_choice), the spec pair list (write_spec builds on spec_pairs,
so the log format and the listIndexes reply share one schema), the _id
clause parser (plan_id reuses analyze_clause), key-pattern direction
(index.descending, which desc_dir already disagreed with on non-numeric
values), option truthiness (query.truthy), the E11000 message, and
index-removal-by-name (Collection.find_index/remove_index). Key-pattern
matching moved out of the dispatcher into index.find_by_key_pattern.

Dead or redundant: ParallelArraysError, the unread `dropped` counter,
insert_entries' discarded gpa, a third pass computing multikey, the
has_id/is_id_index flag pair, Plan.key_len (always lookup_keys[0].len,
now a method), first_match_consumed (now stages = stages[1..]).

Cheaper hot paths: remove_id compacts in one pass instead of an
orderedRemove per hit; Plan.search skips the sort/dedupe when neither
multikey nor multiple lookup keys can produce a repeat; the _id fast
path reuses one scratch key buffer (bson.write_serialized_value); the
plan loop uses the bound collection instead of re-resolving it through
two hash lookups per candidate.

Behavior is unchanged except that dropping plan_id's fixed 16-clause
buffer enables the _id fast path on filters that previously exceeded it.
This commit is contained in:
2026-08-02 13:40:42 +03:00
parent 0d264c6c57
commit 7482042f34
5 changed files with 260 additions and 334 deletions

View File

@@ -37,8 +37,6 @@ pub const max_index_keys: usize = 32;
/// planner falls back to a scan.
const max_combos: u64 = 100;
pub const ParallelArraysError = error{ ParallelArrays };
/// One key in an index spec. `path` is owned by the Index.
pub const IndexKey = struct {
path: []const u8,
@@ -129,16 +127,11 @@ pub const Index = struct {
var i: usize = 0;
while (i < direct) : (i += 1) {
if (values.items[i] == .array) {
multikey = true;
for (values.items[i].array) |elem| try values.append(gpa, elem);
}
}
if (direct > 1) multikey = true;
for (values.items[0..direct]) |v| {
if (v == .array) {
multikey = true;
break;
}
}
if (values.items.len > 1) multi_paths += 1;
if (values.items.len == 0) {
if (self.sparse) return .{ .entries = .empty, .multikey = false };
@@ -155,6 +148,8 @@ pub const Index = struct {
out.deinit(gpa);
}
const nkeys = self.keys.len;
var limits: [max_index_keys]usize = undefined;
for (0..nkeys) |ci| limits[ci] = per_path.items[ci].items.len;
var choice: [max_index_keys]usize = undefined;
@memset(choice[0..nkeys], 0);
while (true) {
@@ -162,14 +157,7 @@ pub const Index = struct {
errdefer gpa.free(key);
for (0..nkeys) |ci| key[ci] = per_path.items[ci].items[choice[ci]];
try out.append(gpa, .{ .key = key, .id = id });
var ci: usize = nkeys;
var carry = true;
while (carry and ci > 0) {
ci -= 1;
choice[ci] += 1;
if (choice[ci] < per_path.items[ci].items.len) carry = false else choice[ci] = 0;
}
if (carry) break;
if (!advance_choice(choice[0..nkeys], limits[0..nkeys])) break;
}
if (out.items.len > 1) {
std.mem.sort(Entry, out.items, {}, entry_less);
@@ -199,8 +187,7 @@ pub const Index = struct {
/// batch: ownership of each entry's key slice moves into the index, so
/// the batch's deinit must not free them. Infallible: capacity must
/// already be reserved.
pub fn insert_entries(self: *Index, gpa: std.mem.Allocator, built: *BuiltEntries) void {
_ = gpa;
pub fn insert_entries(self: *Index, built: *BuiltEntries) void {
for (built.entries.items) |e| {
const pos = self.insert_pos(e);
self.entries.insertAssumeCapacity(pos, e);
@@ -210,26 +197,46 @@ pub const Index = struct {
built.entries.items.len = 0;
}
/// Build, check, and insert entries for one document; the one-shot form
/// used when rebuilding an index on open.
pub fn add_doc(self: *Index, gpa: std.mem.Allocator, doc: *const bson.Document, id: []const u8) !void {
/// Build, check, and insert entries for one document the whole
/// entry-commit protocol in one call, used everywhere a single document
/// joins an index (create, rebuild on open). Writes that must reserve
/// capacity before a log append use the split build/reserve/insert form
/// directly.
///
/// With `enforce_unique` false a duplicate is tolerated rather than
/// rejected (the rebuild path keeps the index and warns); the return
/// value reports whether that happened.
pub fn add_doc(self: *Index, gpa: std.mem.Allocator, doc: *const bson.Document, id: []const u8, enforce_unique: bool) !bool {
var built = try self.build_entries(gpa, doc, id);
// Runs on success too: insert_entries drains the keys, leaving only
// the (now empty) ArrayList buffer to free.
defer built.deinit(gpa);
if (self.unique) try self.check_unique(built.entries.items, id);
var duplicate = false;
if (self.unique) {
self.check_unique(built.entries.items, id) catch |err| {
if (enforce_unique) return err;
duplicate = true;
};
}
if (built.multikey) self.multikey = true;
try self.entries.ensureUnusedCapacity(gpa, built.entries.items.len);
self.insert_entries(gpa, &built);
try self.reserve_for(gpa, built.entries.items.len);
self.insert_entries(&built);
return duplicate;
}
/// Remove every entry for `id` and free its key slices. Infallible.
/// One compaction pass: removing in place would memmove the tail per hit.
pub fn remove_id(self: *Index, gpa: std.mem.Allocator, id: []const u8) void {
var i: usize = 0;
while (i < self.entries.items.len) {
if (std.mem.eql(u8, self.entries.items[i].id, id)) {
gpa.free(self.entries.items[i].key);
_ = self.entries.orderedRemove(i);
} else i += 1;
var w: usize = 0;
for (self.entries.items) |e| {
if (std.mem.eql(u8, e.id, id)) {
gpa.free(e.key);
} else {
self.entries.items[w] = e;
w += 1;
}
}
self.entries.items.len = w;
}
/// Reject when any of `new_entries` has a key already present under a
@@ -246,13 +253,7 @@ pub const Index = struct {
}
fn insert_pos(self: *const Index, e: Entry) usize {
var lo: usize = 0;
var hi: usize = self.entries.items.len;
while (lo < hi) {
const mid = lo + (hi - lo) / 2;
if (compare_entries(self.entries.items[mid], e) == .lt) lo = mid + 1 else hi = mid;
}
return lo;
return std.sort.lowerBound(Entry, self.entries.items, e, compare_entries);
}
// -- search -------------------------------------------------------------
@@ -299,25 +300,13 @@ pub const Index = struct {
/// First entry whose first `prefix.len` components are not less than
/// `prefix`.
fn lower_bound_prefix(self: *const Index, prefix: []const bson.Value) usize {
var lo: usize = 0;
var hi: usize = self.entries.items.len;
while (lo < hi) {
const mid = lo + (hi - lo) / 2;
if (prefix_lt(self.entries.items[mid].key, prefix)) lo = mid + 1 else hi = mid;
}
return lo;
return std.sort.lowerBound(Entry, self.entries.items, prefix, prefix_order);
}
/// First entry whose first `prefix.len` components are greater than
/// `prefix`.
fn upper_bound_prefix(self: *const Index, prefix: []const bson.Value) usize {
var lo: usize = 0;
var hi: usize = self.entries.items.len;
while (lo < hi) {
const mid = lo + (hi - lo) / 2;
if (prefix_le(self.entries.items[mid].key, prefix)) lo = mid + 1 else hi = mid;
}
return lo;
return std.sort.upperBound(Entry, self.entries.items, prefix, prefix_order);
}
// -- serialization ------------------------------------------------------
@@ -325,18 +314,10 @@ pub const Index = struct {
/// The canonical spec document bytes ({v, key, name, unique?, sparse?})
/// stored in the log and used to rebuild the index on replay.
pub fn write_spec(self: *const Index, gpa: std.mem.Allocator, out: *std.ArrayListUnmanaged(u8)) !void {
var arena = std.heap.ArenaAllocator.init(gpa);
defer arena.deinit();
var pairs: std.ArrayListUnmanaged(bson.Pair) = .empty;
defer pairs.deinit(gpa);
var key_pairs: std.ArrayListUnmanaged(bson.Pair) = .empty;
defer key_pairs.deinit(gpa);
try pairs.append(gpa, .{ .key = "v", .value = .{ .int32 = 2 } });
for (self.keys) |k| {
try key_pairs.append(gpa, .{ .key = k.path, .value = if (k.descending) .{ .int32 = -1 } else .{ .int32 = 1 } });
}
try pairs.append(gpa, .{ .key = "key", .value = .{ .doc = key_pairs.items } });
try pairs.append(gpa, .{ .key = "name", .value = .{ .string = self.name } });
if (self.unique) try pairs.append(gpa, .{ .key = "unique", .value = .{ .bool = true } });
if (self.sparse) try pairs.append(gpa, .{ .key = "sparse", .value = .{ .bool = true } });
try self.spec_pairs(arena.allocator(), &pairs);
try bson.write_doc(pairs.items, gpa, out);
}
@@ -366,6 +347,24 @@ pub const Index = struct {
}
};
/// The index whose key pattern is exactly `key_pairs` (same paths, same
/// order, same directions), or null. Keeps the IndexKey layout — and what
/// counts as a match — inside this module.
pub fn find_by_key_pattern(indexes: []const Index, key_pairs: []const bson.Pair) ?*const Index {
for (indexes) |*ix| {
if (ix.keys.len != key_pairs.len) continue;
var match = true;
for (ix.keys, key_pairs) |k, kp| {
if (!std.mem.eql(u8, k.path, kp.key) or k.descending != descending(kp.value)) {
match = false;
break;
}
}
if (match) return ix;
}
return null;
}
pub const SpecError = error{ InvalidIndexSpec, OutOfMemory };
/// Parse {key: {...}, name?, unique?, sparse?} from a spec document — the
@@ -390,8 +389,8 @@ pub fn parse_spec(gpa: std.mem.Allocator, spec: *const bson.Document) SpecError!
}
var unique = false;
var sparse = false;
if (bson.get_pair(spec.pairs, "unique")) |v| unique = truthy(v);
if (bson.get_pair(spec.pairs, "sparse")) |v| sparse = truthy(v);
if (bson.get_pair(spec.pairs, "unique")) |v| unique = query.truthy(v);
if (bson.get_pair(spec.pairs, "sparse")) |v| sparse = query.truthy(v);
const name_value = bson.get_pair(spec.pairs, "name") orelse {
const nm = try default_name(gpa, key_pairs);
defer gpa.free(nm);
@@ -404,7 +403,10 @@ pub fn parse_spec(gpa: std.mem.Allocator, spec: *const bson.Document) SpecError!
return Index.init(gpa, name, keys[0..key_pairs.len], unique, sparse);
}
fn descending(v: bson.Value) bool {
/// Whether a key-pattern direction value means descending. The single
/// definition of what -1 means in a key pattern, shared with dropIndexes'
/// key-pattern matching.
pub fn descending(v: bson.Value) bool {
return switch (v) {
.int32 => |n| n < 0,
.int64 => |n| n < 0,
@@ -413,16 +415,6 @@ fn descending(v: bson.Value) bool {
};
}
fn truthy(v: bson.Value) bool {
return switch (v) {
.bool => |b| b,
.int32 => |i| i != 0,
.int64 => |i| i != 0,
.double => |d| d != 0,
else => false,
};
}
/// MongoDB's default index name: a_1_b_-1.
fn default_name(gpa: std.mem.Allocator, key_pairs: []const bson.Pair) ![]u8 {
var out: std.ArrayListUnmanaged(u8) = .empty;
@@ -460,22 +452,29 @@ fn entry_less(_: void, a: Entry, b: Entry) bool {
return compare_entries(a, b) == .lt;
}
/// Whether entry key's first `prefix.len` components are less than `prefix`.
fn prefix_lt(key: []const bson.Value, prefix: []const bson.Value) bool {
for (key[0..prefix.len], prefix) |a, b| {
const o = bson.compare(a, b);
if (o != .eq) return o == .lt;
/// Order of `prefix` against an entry key's leading components — `.eq` when
/// every prefix component matches (the key may be longer). The search-side
/// counterpart of compare_entries: same component-wise bson.compare, no
/// length or id tie-break, so a partial key matches a whole range.
fn prefix_order(prefix: []const bson.Value, e: Entry) std.math.Order {
for (prefix, e.key[0..prefix.len]) |p, k| {
const o = bson.compare(p, k);
if (o != .eq) return o;
}
return false;
return .eq;
}
/// Whether entry key's first `prefix.len` components are <= `prefix`.
fn prefix_le(key: []const bson.Value, prefix: []const bson.Value) bool {
for (key[0..prefix.len], prefix) |a, b| {
const o = bson.compare(a, b);
if (o != .eq) return o == .lt;
/// Advance an odometer of positions, each bounded by the matching `limits`
/// entry. Returns false once it wraps, i.e. the product is exhausted.
fn advance_choice(choice: []usize, limits: []const usize) bool {
var i = choice.len;
while (i > 0) {
i -= 1;
choice[i] += 1;
if (choice[i] < limits[i]) return true;
choice[i] = 0;
}
return true;
return false;
}
fn less_ids(_: void, a: []const u8, b: []const u8) bool {
@@ -579,7 +578,6 @@ fn analyze_clause(value: bson.Value, info: *CompInfo) void {
pub const Plan = struct {
index: *const Index,
lookup_keys: std.ArrayListUnmanaged([]const bson.Value),
key_len: usize,
lo: ?bson.Value,
lo_incl: bool,
hi: ?bson.Value,
@@ -590,10 +588,22 @@ pub const Plan = struct {
self.lookup_keys.deinit(gpa);
}
/// How many leading index components the lookup keys pin down. Every key
/// is built with the same component count, and there is always at least
/// one (a pure range plan appends a single empty key).
pub fn key_len(self: *const Plan) usize {
return self.lookup_keys.items[0].len;
}
/// Collect the candidate ids, sorted and deduplicated. Range scans can
/// return the same id non-adjacently (a doc with {tags: ["a","b"]}
/// contributes two entries inside one range), so adjacent-dup skipping
/// would be wrong.
///
/// Duplicates are only possible from a multikey index (one document,
/// several entries) or from several lookup keys (whose ranges can be the
/// same key repeated, as in {$in: [1, 1]}); the common single-key lookup
/// on a non-multikey index skips the pass entirely.
pub fn search(self: *const Plan, gpa: std.mem.Allocator, out: *std.ArrayListUnmanaged([]const u8)) !void {
for (self.lookup_keys.items) |key| {
if (self.lo == null and self.hi == null) {
@@ -602,7 +612,8 @@ pub const Plan = struct {
try self.index.lookup_range(gpa, key, self.lo, self.lo_incl, self.hi, self.hi_incl, out);
}
}
if (out.items.len > 1) {
const may_repeat = self.index.multikey or self.lookup_keys.items.len > 1;
if (may_repeat and out.items.len > 1) {
std.mem.sort([]const u8, out.items, {}, less_ids);
var w: usize = 1;
for (out.items[1..]) |id| {
@@ -644,7 +655,7 @@ pub fn plan(gpa: std.mem.Allocator, indexes: []const Index, filter: []const bson
}
fn plan_better(a: *const Plan, b: *const Plan) bool {
if (a.key_len != b.key_len) return a.key_len > b.key_len;
if (a.key_len() != b.key_len()) return a.key_len() > b.key_len();
const a_range = a.lo != null or a.hi != null;
const b_range = b.lo != null or b.hi != null;
if (a_range != b_range) return a_range;
@@ -716,7 +727,6 @@ fn evaluate_index(gpa: std.mem.Allocator, ix: *const Index, clauses: []const Cla
var pl = Plan{
.index = ix,
.lookup_keys = .empty,
.key_len = run,
.lo = lo,
.lo_incl = lo_incl,
.hi = hi,
@@ -740,14 +750,7 @@ fn evaluate_index(gpa: std.mem.Allocator, ix: *const Index, clauses: []const Cla
key[i] = if (infos[i].eq) |v| v else infos[i].in_values.?[choice[i]];
}
try pl.lookup_keys.append(gpa, key);
var i: usize = run;
var carry = true;
while (carry and i > 0) {
i -= 1;
choice[i] += 1;
if (choice[i] < counts[i]) carry = false else choice[i] = 0;
}
if (carry) break;
if (!advance_choice(choice[0..run], counts[0..run])) break;
}
}
return pl;
@@ -768,67 +771,42 @@ pub const IdPlan = struct {
/// and nested variants), but serialize_value produces different map keys —
/// a hash lookup would then miss documents a scan would match.
pub fn plan_id(filter: []const bson.Pair) ?IdPlan {
var clauses: [16]Clause = undefined;
var n: usize = 0;
if (!flatten_id(filter, &clauses, &n)) return null;
for (clauses[0..n]) |cl| {
if (!std.mem.eql(u8, cl.path, "_id")) continue;
if (id_lookup_values(cl.value)) |values| return .{ .values = values };
// The first usable _id clause wins; no flattening buffer is needed
// because nothing is compared across clauses. $and members are searched
// like top-level pairs, every other operator skipped — same rule as
// flatten_clauses, and safe for the same reason (the full filter is
// re-applied to every candidate).
for (filter) |p| {
if (p.key.len > 0 and p.key[0] == '$') {
if (!std.mem.eql(u8, p.key, "$and")) continue;
const members = switch (p.value) {
.array => |a| a,
else => continue,
};
for (members) |m| {
const mp = switch (m) {
.doc => |d| d,
else => continue,
};
if (plan_id(mp)) |found| return found;
}
continue;
}
if (!std.mem.eql(u8, p.key, "_id")) continue;
if (id_lookup_values(p.value)) |values| return .{ .values = values };
}
return null;
}
fn flatten_id(pairs: []const bson.Pair, out: *[16]Clause, n: *usize) bool {
for (pairs) |p| {
if (p.key.len > 0 and p.key[0] == '$') {
if (std.mem.eql(u8, p.key, "$and")) {
const members = switch (p.value) {
.array => |a| a,
else => continue,
};
for (members) |m| {
const mp = switch (m) {
.doc => |d| d,
else => continue,
};
if (!flatten_id(mp, out, n)) return false;
}
}
continue;
}
if (n.* >= out.len) return false;
out[n.*] = .{ .path = p.key, .value = p.value };
n.* += 1;
}
return true;
}
/// The map-lookup values for one _id clause, or null when it is not a pure
/// equality/$in of fast-path-safe values. A range is unusable here: the docs
/// map is a hash, not an ordered structure.
fn id_lookup_values(v: bson.Value) ?[]const bson.Value {
if (v == .regex) return null;
if (v != .doc) {
return if (value_fast_path_safe(v)) &.{v} else null;
}
const pairs = v.doc;
if (pairs.len > 0 and !query.all_operator_keys(pairs)) {
// Bare document equality (compare the whole doc).
return if (value_fast_path_safe(v)) &.{v} else null;
}
var eq: ?bson.Value = null;
var in_list: ?[]const bson.Value = null;
for (pairs) |p| {
if (std.mem.eql(u8, p.key, "$eq")) {
eq = p.value;
} else if (std.mem.eql(u8, p.key, "$in")) {
in_list = switch (p.value) {
.array => |a| a,
else => return null,
};
} else return null;
}
if (eq) |e| {
return if (value_fast_path_safe(e)) &.{e} else null;
}
if (in_list) |list| {
var info = CompInfo{};
analyze_clause(v, &info);
if (info.lo != null or info.hi != null) return null;
if (info.eq) |e| return if (value_fast_path_safe(e)) &.{e} else null;
if (info.in_values) |list| {
for (list) |m| {
if (!value_fast_path_safe(m)) return null;
}
@@ -897,11 +875,11 @@ test "entries sort across numeric types and string/null/objectid" {
const d_str = doc_of(&.{ .{ .key = "_id", .value = .{ .int32 = 3 } }, .{ .key = "a", .value = .{ .string = "b" } } });
const d_nul = doc_of(&.{ .{ .key = "_id", .value = .{ .int32 = 4 } }, .{ .key = "a", .value = .null } });
const d_oid = doc_of(&.{ .{ .key = "_id", .value = .{ .int32 = 5 } }, .{ .key = "a", .value = .{ .object_id = [_]u8{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 } } } });
try ix.add_doc(gpa, &d_int, "i1");
try ix.add_doc(gpa, &d_dbl, "i2");
try ix.add_doc(gpa, &d_str, "i3");
try ix.add_doc(gpa, &d_nul, "i4");
try ix.add_doc(gpa, &d_oid, "i5");
_ = try ix.add_doc(gpa, &d_int, "i1", true);
_ = try ix.add_doc(gpa, &d_dbl, "i2", true);
_ = try ix.add_doc(gpa, &d_str, "i3", true);
_ = try ix.add_doc(gpa, &d_nul, "i4", true);
_ = try ix.add_doc(gpa, &d_oid, "i5", true);
// An int64 query finds both the int32 and double entries: compare-equal.
try expect_ids(gpa, &ix, &.{.{ .int64 = 5 }}, &.{ "i1", "i2" });
@@ -920,13 +898,13 @@ test "missing field is indexed as null; sparse skips the document" {
var ix = try simple_index(gpa, &.{"a"}, false, false);
defer ix.deinit(gpa);
const d = doc_of(&.{.{ .key = "_id", .value = .{ .int32 = 1 } }});
try ix.add_doc(gpa, &d, "m1");
_ = try ix.add_doc(gpa, &d, "m1", true);
try testing.expectEqual(@as(usize, 1), ix.entries.items.len);
try expect_ids(gpa, &ix, &.{.null}, &.{"m1"});
var sp = try simple_index(gpa, &.{"a"}, false, true);
defer sp.deinit(gpa);
try sp.add_doc(gpa, &d, "m2");
_ = try sp.add_doc(gpa, &d, "m2", true);
try testing.expectEqual(@as(usize, 0), sp.entries.items.len);
}
@@ -936,7 +914,7 @@ test "multikey expansion indexes the array and its elements" {
defer ix.deinit(gpa);
const d = doc_of(&.{.{ .key = "_id", .value = .{ .int32 = 1 } }, .{ .key = "tags", .value = .{ .array = &.{ .{ .string = "a" }, .{ .string = "b" } } } }});
try ix.add_doc(gpa, &d, "mk1");
_ = try ix.add_doc(gpa, &d, "mk1", true);
// 3 entries: the array itself, "a", "b".
try testing.expectEqual(@as(usize, 3), ix.entries.items.len);
@@ -953,7 +931,7 @@ test "per-document dedup keeps {a: [1,1]} under a unique index" {
var ix = try simple_index(gpa, &.{"a"}, true, false);
defer ix.deinit(gpa);
const d = doc_of(&.{.{ .key = "_id", .value = .{ .int32 = 1 } }, .{ .key = "a", .value = .{ .array = &.{ .{ .int32 = 1 }, .{ .int32 = 1 } } } }});
try ix.add_doc(gpa, &d, "d1");
_ = try ix.add_doc(gpa, &d, "d1", true);
// Entries after dedup: the array itself and one element.
try testing.expectEqual(@as(usize, 2), ix.entries.items.len);
}
@@ -967,7 +945,7 @@ test "parallel arrays are rejected" {
.{ .key = "a", .value = .{ .array = &.{ .{ .int32 = 1 }, .{ .int32 = 2 } } } },
.{ .key = "b", .value = .{ .array = &.{ .{ .int32 = 3 }, .{ .int32 = 4 } } } },
});
try testing.expectError(error.ParallelArrays, ix.add_doc(gpa, &d, "p1"));
try testing.expectError(error.ParallelArrays, ix.add_doc(gpa, &d, "p1", true));
// One array path is fine.
const ok = doc_of(&.{
@@ -975,7 +953,7 @@ test "parallel arrays are rejected" {
.{ .key = "a", .value = .{ .array = &.{ .{ .int32 = 1 }, .{ .int32 = 2 } } } },
.{ .key = "b", .value = .{ .int32 = 3 } },
});
try ix.add_doc(gpa, &ok, "p2");
_ = try ix.add_doc(gpa, &ok, "p2", true);
try testing.expectEqual(@as(usize, 3), ix.entries.items.len);
}
@@ -985,16 +963,16 @@ test "unique conflict across documents, replace of own entries allowed" {
defer ix.deinit(gpa);
const d1 = doc_of(&.{ .{ .key = "_id", .value = .{ .int32 = 1 } }, .{ .key = "a", .value = .{ .int32 = 10 } } });
try ix.add_doc(gpa, &d1, "u1");
_ = try ix.add_doc(gpa, &d1, "u1", true);
const d2 = doc_of(&.{ .{ .key = "_id", .value = .{ .int32 = 2 } }, .{ .key = "a", .value = .{ .int32 = 10 } } });
try testing.expectError(error.DuplicateKeyIndex, ix.add_doc(gpa, &d2, "u2"));
try testing.expectError(error.DuplicateKeyIndex, ix.add_doc(gpa, &d2, "u2", true));
// A replace keeps its own key: remove old entries first (the engine's
// evict_doc does this), then add the new ones.
const d1b = doc_of(&.{ .{ .key = "_id", .value = .{ .int32 = 1 } }, .{ .key = "a", .value = .{ .int32 = 20 } } });
ix.remove_id(gpa, "u1");
try ix.add_doc(gpa, &d1b, "u1");
_ = try ix.add_doc(gpa, &d1b, "u1", true);
try expect_ids(gpa, &ix, &.{.{ .int32 = 20 }}, &.{"u1"});
try expect_ids(gpa, &ix, &.{.{ .int32 = 10 }}, &.{});
}
@@ -1012,7 +990,7 @@ test "range bounds inclusive and exclusive" {
};
for (docs) |s| {
const d = doc_of(&.{ .{ .key = "_id", .value = .{ .int32 = s.a } }, .{ .key = "a", .value = .{ .int32 = s.a } } });
try ix.add_doc(gpa, &d, s.id);
_ = try ix.add_doc(gpa, &d, s.id, true);
}
var out: std.ArrayListUnmanaged([]const u8) = .empty;
@@ -1043,7 +1021,7 @@ test "empty index and remove_id" {
try testing.expectEqual(@as(usize, 0), out.items.len);
const d = doc_of(&.{ .{ .key = "_id", .value = .{ .int32 = 1 } }, .{ .key = "a", .value = .{ .int32 = 1 } } });
try ix.add_doc(gpa, &d, "e1");
_ = try ix.add_doc(gpa, &d, "e1", true);
ix.remove_id(gpa, "e1");
try testing.expectEqual(@as(usize, 0), ix.entries.items.len);
try ix.lookup_eq(gpa, &.{.{ .int32 = 1 }}, &out);
@@ -1069,7 +1047,7 @@ test "compound index prefix search and range on the next key" {
.{ .key = "a", .value = .{ .int32 = s.a } },
.{ .key = "b", .value = .{ .int32 = s.b } },
});
try ix.add_doc(gpa, &d, s.id);
_ = try ix.add_doc(gpa, &d, s.id, true);
}
// Prefix on a only.
@@ -1140,7 +1118,7 @@ test "planner picks eq run, ranges, and bails on sparse null" {
};
var p = (try plan(gpa, &.{ix}, &f)).?;
defer p.deinit(gpa);
try testing.expectEqual(@as(usize, 2), p.key_len);
try testing.expectEqual(@as(usize, 2), p.key_len());
try testing.expect(p.lo == null and p.hi == null);
}
// {a: 1, b: {$gt: 2}} → equality run of 1 + range on the next key.
@@ -1151,7 +1129,7 @@ test "planner picks eq run, ranges, and bails on sparse null" {
};
var p = (try plan(gpa, &.{ix}, &f)).?;
defer p.deinit(gpa);
try testing.expectEqual(@as(usize, 1), p.key_len);
try testing.expectEqual(@as(usize, 1), p.key_len());
try testing.expect(p.hi == null and p.lo != null and !p.lo_incl);
}
// {a: 1} only → prefix run of 1.
@@ -1159,14 +1137,14 @@ test "planner picks eq run, ranges, and bails on sparse null" {
const f = [_]bson.Pair{.{ .key = "a", .value = .{ .int32 = 1 } }};
var p = (try plan(gpa, &.{ix}, &f)).?;
defer p.deinit(gpa);
try testing.expectEqual(@as(usize, 1), p.key_len);
try testing.expectEqual(@as(usize, 1), p.key_len());
}
// Pure range on the first key → key_len 0 with a bound.
{
const f = [_]bson.Pair{.{ .key = "a", .value = .{ .doc = &.{.{ .key = "$gte", .value = .{ .int32 = 1 } }} } }};
var p = (try plan(gpa, &.{ix}, &f)).?;
defer p.deinit(gpa);
try testing.expectEqual(@as(usize, 0), p.key_len);
try testing.expectEqual(@as(usize, 0), p.key_len());
try testing.expect(p.lo != null and p.lo_incl);
}
// Unusable filter → no plan.
@@ -1185,7 +1163,7 @@ test "planner picks eq run, ranges, and bails on sparse null" {
// Non-sparse is fine with null.
var p = (try plan(gpa, &.{ix}, &f)).?;
defer p.deinit(gpa);
try testing.expect(p.key_len == 1);
try testing.expect(p.key_len() == 1);
// A null inside $in bails too.
const fin = [_]bson.Pair{.{ .key = "a", .value = .{ .doc = &.{.{ .key = "$in", .value = .{ .array = &.{ .{ .int32 = 1 }, .null } } }} } }};
try testing.expect((try plan(gpa, &.{sp}, &fin)) == null);