Implement searching no tag

This commit is contained in:
Dendy 2022-11-05 04:38:33 +01:00
parent 4d9f3e32e2
commit b0f730d162
2 changed files with 15 additions and 12 deletions

View File

@ -60,7 +60,7 @@ pub fn main() !void {
// -------------------- QUERY ---------------------------
const jsonQuery: [:0]const u8 =
\\{ "query" : "fur made_with:krita date:2022", "limit" : 20 }
\\{ "query" : "" }
;
var jquery = json.Obj.newFromString(jsonQuery);

View File

@ -63,9 +63,6 @@ pub fn query(jobj: *json.Obj, db: *Db, allocator: std.mem.Allocator) !json.Obj {
const query_str = jobj.getString();
var jret = json.Obj.newArray();
// Go through each tag
var tag_iter = std.mem.split(u8, query_str, " ");
var ids = std.ArrayList([]u8).init(allocator);
defer {
for (ids.items) |item| {
@ -74,16 +71,22 @@ pub fn query(jobj: *json.Obj, db: *Db, allocator: std.mem.Allocator) !json.Obj {
ids.deinit();
}
while (tag_iter.next()) |tag| {
// Get the tag selector: "tag:<tag>"
const tag_sel = try std.mem.concat(allocator, u8, &[_][]const u8{ "tag:", tag });
defer allocator.free(tag_sel);
if (query_str.len > 0) {
// Get all the items under the individual tags
var tag_iter = std.mem.split(u8, query_str, " ");
while (tag_iter.next()) |tag| {
// Get the tag selector: "tag:<tag>"
const tag_sel = try std.mem.concat(allocator, u8, &[_][]const u8{ "tag:", tag });
defer allocator.free(tag_sel);
// Get the items that have that tag: "<item1> <item2> <item3>"
try ids.append(db.get(tag_sel) orelse return jret);
// Get the items that have that tag: "<item1> <item2> <item3>"
try ids.append(db.get(tag_sel) orelse return jret);
// This has been deferred before.
//defer Db.free(tag_str.ptr);
// This has been deferred before.
//defer Db.free(tag_str.ptr);
}
} else {
try ids.append(db.get("item") orelse return jret);
}
var id_iter = util.intersection(ids.items);