Making link_array working, now can do query with filter for array of ofther struct

This commit is contained in:
Adrien Bouvais 2024-11-28 19:20:44 +01:00
parent 12ae7f9a62
commit 3f53da5220
4 changed files with 86 additions and 26 deletions

View File

@ -27,7 +27,7 @@ pub fn build(b: *std.Build) void {
// All tests
const tests1 = b.addTest(.{
.root_source_file = b.path("src/stuffs/UUIDTree.zig"),
.root_source_file = b.path("src/stuffs/UUIDFileIndex.zig"),
.target = target,
.optimize = optimize,
.name = "CLI tokenizer",

View File

@ -361,7 +361,10 @@ pub const FileEngine = struct {
};
defer iter.deinit();
while (iter.next() catch return) |row| {
while (iter.next() catch |err| {
sync_context.logError("Error in iter next", err);
return;
}) |row| {
if (sync_context.checkStructLimit()) break;
if (filter == null or filter.?.evaluate(row)) {
list.*.append(UUID{ .bytes = row[0].UUID }) catch |err| {
@ -594,6 +597,8 @@ pub const FileEngine = struct {
const path = std.fmt.bufPrint(&path_buffer, "{s}/DATA/{s}/{d}.zid", .{ self.path_to_ZipponDB_dir, struct_name, file_index }) catch return FileEngineError.MemoryError;
const data = try self.orderedNewData(allocator, struct_name, map);
std.debug.print("{any}", .{data});
var data_writer = zid.DataWriter.init(path, null) catch return FileEngineError.ZipponDataError;
defer data_writer.deinit();
@ -914,18 +919,23 @@ pub const FileEngine = struct {
.str => |v| return zid.Data.initStr(v),
.link => |v| {
var iter = v.keyIterator();
if (v.count() == 1) {
if (v.count() > 0) {
return zid.Data.initUUID(iter.next().?.bytes);
} else {
var items = std.ArrayList([16]u8).init(allocator);
defer items.deinit();
while (iter.next()) |uuid| {
items.append(uuid.bytes) catch return ZipponError.MemoryError;
}
return zid.Data.initUUIDArray(zid.allocEncodArray.UUID(allocator, items.items) catch return FileEngineError.AllocEncodError);
const uuid = UUID.parse("00000000-0000-0000-0000-000000000000") catch return ZipponError.InvalidUUID;
return zid.Data.initUUID(uuid.bytes);
}
},
.link_array => |v| {
var iter = v.keyIterator();
var items = std.ArrayList([16]u8).init(allocator);
defer items.deinit();
while (iter.next()) |uuid| {
items.append(uuid.bytes) catch return ZipponError.MemoryError;
}
return zid.Data.initUUIDArray(zid.allocEncodArray.UUID(allocator, items.items) catch return FileEngineError.AllocEncodError);
},
.self => |v| return zid.Data.initUUID(v.bytes),
.int_array => |v| return zid.Data.initIntArray(zid.allocEncodArray.Int(allocator, v) catch return FileEngineError.AllocEncodError),
.float_array => |v| return zid.Data.initFloatArray(zid.allocEncodArray.Float(allocator, v) catch return FileEngineError.AllocEncodError),
@ -954,8 +964,6 @@ pub const FileEngine = struct {
datas[i] = try string2Data(allocator, map.get(member).?);
}
log.debug("New ordered data: {any}\n", .{datas});
return datas;
}

View File

@ -67,6 +67,7 @@ pub const ConditionValue = union(enum) {
bool_array: []const bool,
unix_array: []const u64,
link: *std.AutoHashMap(UUID, void),
link_array: *std.AutoHashMap(UUID, void),
pub fn initInt(value: []const u8) ConditionValue {
return ConditionValue{ .int = s2t.parseInt(value) };
@ -132,6 +133,10 @@ pub const ConditionValue = union(enum) {
pub fn initLink(value: *std.AutoHashMap(UUID, void)) ConditionValue {
return ConditionValue{ .link = value };
}
pub fn initArrayLink(value: *std.AutoHashMap(UUID, void)) ConditionValue {
return ConditionValue{ .link_array = value };
}
};
pub const Condition = struct {

View File

@ -373,7 +373,7 @@ pub const Parser = struct {
token = if (keep_next) token else self.toker.next();
keep_next = false;
if (PRINT_STATE) std.debug.print("parseFilter: {any}\n", .{state});
}) {
})
switch (state) {
.expect_condition => switch (token.tag) {
.r_brace => {
@ -469,9 +469,10 @@ pub const Parser = struct {
),
},
.end => {},
else => unreachable,
}
}
};
return filter;
}
@ -799,6 +800,7 @@ pub const Parser = struct {
.expect_new_value => {
const data_type = self.schema_engine.memberName2DataType(struct_name, member_name) catch return ZiQlParserError.StructNotFound;
std.debug.print("DATA TYPE: {any}\n", .{data_type});
map.put(member_name, try self.parseConditionValue(allocator, struct_name, data_type, &token)) catch return ZipponError.MemoryError;
if (data_type == .link or data_type == .link_array) {
token = self.toker.last_token;
@ -925,7 +927,7 @@ pub const Parser = struct {
.date_array => value = try ConditionValue.initArrayDate(allocator, self.toker.buffer[start_index..token.loc.end]),
.time_array => value = try ConditionValue.initArrayTime(allocator, self.toker.buffer[start_index..token.loc.end]),
.datetime_array => value = try ConditionValue.initArrayDateTime(allocator, self.toker.buffer[start_index..token.loc.end]),
.link_array, .link => switch (token.tag) {
.link => switch (token.tag) {
.keyword_none => {
const map = allocator.create(std.AutoHashMap(UUID, void)) catch return ZipponError.MemoryError;
map.* = std.AutoHashMap(UUID, void).init(allocator);
@ -960,7 +962,7 @@ pub const Parser = struct {
token.* = self.toker.next();
}
if (data_type == .link) additional_data.entity_count_to_find = 1;
additional_data.entity_count_to_find = 1;
if (token.tag == .l_brace) filter = try self.parseFilter(allocator, struct_name, false) else return printError(
"Error: Expected filter",
@ -979,9 +981,54 @@ pub const Parser = struct {
map,
&additional_data,
);
log.debug("Found {d} entity when parsing for populateVoidUUID\n", .{map.count()});
value = ConditionValue.initLink(map);
},
else => return printError(
"Error: Expected uuid or none",
ZiQlParserError.SynthaxError,
self.toker.buffer,
token.loc.start,
token.loc.end,
),
},
.link_array => switch (token.tag) {
.keyword_none => {
const map = allocator.create(std.AutoHashMap(UUID, void)) catch return ZipponError.MemoryError;
map.* = std.AutoHashMap(UUID, void).init(allocator);
value = ConditionValue.initArrayLink(map);
_ = self.toker.next();
},
.l_brace, .l_bracket => {
var filter: ?Filter = null;
defer if (filter != null) filter.?.deinit();
var additional_data = AdditionalData.init(allocator);
defer additional_data.deinit();
if (token.tag == .l_bracket) {
try self.parseAdditionalData(allocator, &additional_data, struct_name);
token.* = self.toker.next();
}
if (token.tag == .l_brace) filter = try self.parseFilter(allocator, struct_name, false) else return printError(
"Error: Expected filter",
ZiQlParserError.SynthaxError,
self.toker.buffer,
token.loc.start,
token.loc.end,
);
// Here I have the filter and additionalData
const map = allocator.create(std.AutoHashMap(UUID, void)) catch return ZipponError.MemoryError;
map.* = std.AutoHashMap(UUID, void).init(allocator);
try self.file_engine.populateVoidUUIDMap(
struct_name,
filter,
map,
&additional_data,
);
value = ConditionValue.initArrayLink(map);
},
else => return printError(
"Error: Expected uuid or none",
ZiQlParserError.SynthaxError,
@ -990,7 +1037,7 @@ pub const Parser = struct {
token.loc.end,
),
},
else => unreachable,
.self => unreachable,
}
return value;
@ -1013,16 +1060,16 @@ pub const Parser = struct {
};
test "ADD" {
try testParsing("ADD User (name = 'Bob', email='bob@email.com', age=55, scores=[ 1 ], best_friend=none, bday=2000/01/01, a_time=12:04, last_order=2000/01/01-12:45)");
try testParsing("ADD User (name = 'Bob', email='bob@email.com', age=55, scores=[ 666 123 331 ], best_friend=none, bday=2000/11/01, a_time=12:04:54, last_order=2000/01/01-12:45)");
try testParsing("ADD User (name = 'Bob', email='bob@email.com', age=-55, scores=[ 33 ], best_friend=none, bday=2000/01/04, a_time=12:04:54.8741, last_order=2000/01/01-12:45)");
try testParsing("ADD User (name = 'Boba', email='boba@email.com', age=20, scores=[ ], best_friend=none, bday=2000/06/06, a_time=04:04:54.8741, last_order=2000/01/01-12:45)");
try testParsing("ADD User (name = 'Bob', email='bob@email.com', age=55, scores=[ 1 ], best_friend=none, friends=none, bday=2000/01/01, a_time=12:04, last_order=2000/01/01-12:45)");
try testParsing("ADD User (name = 'Bob', email='bob@email.com', age=55, scores=[ 666 123 331 ], best_friend=none, friends=none, bday=2000/11/01, a_time=12:04:54, last_order=2000/01/01-12:45)");
try testParsing("ADD User (name = 'Bob', email='bob@email.com', age=-55, scores=[ 33 ], best_friend=none, friends=none, bday=2000/01/04, a_time=12:04:54.8741, last_order=2000/01/01-12:45)");
try testParsing("ADD User (name = 'Boba', email='boba@email.com', age=20, scores=[ ], best_friend=none, friends=none, bday=2000/06/06, a_time=04:04:54.8741, last_order=2000/01/01-12:45)");
// This need to take the first User named Bob as it is a unique link
try testParsing("ADD User (name = 'Bob', email='bob@email.com', age=-55, scores=[ 1 ], best_friend={name = 'Bob'}, bday=2000/01/01, a_time=12:04:54.8741, last_order=2000/01/01-12:45)");
try testParsing("ADD User (name = 'Bou', email='bob@email.com', age=66, scores=[ 1 ], best_friend={name = 'Boba'}, bday=2000/01/01, a_time=02:04:54.8741, last_order=2000/01/01-12:45)");
try testParsing("ADD User (name = 'Bob', email='bob@email.com', age=-55, scores=[ 1 ], best_friend=none, friends=none, bday=2000/01/01, a_time=12:04:54.8741, last_order=2000/01/01-12:45)");
try testParsing("ADD User (name = 'Bou', email='bob@email.com', age=66, scores=[ 1 ], best_friend={name = 'Boba'}, friends={name = 'Bob'}, bday=2000/01/01, a_time=02:04:54.8741, last_order=2000/01/01-12:45)");
try testParsing("GRAB User");
try testParsing("GRAB User {}");
}
test "GRAB filter with string" {