From cad5d2169733adac9293fcb70945182332414780 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Fri, 30 Nov 2018 17:12:27 +0300 Subject: [PATCH] added tag querying.... (not checked to be optimal) Signed-off-by: Alex A. Naanou --- ui (gen4)/imagegrid/tags.js | 62 ++++++++++++++++++++++++++++--------- ui (gen4)/lib/util.js | 1 + 2 files changed, 48 insertions(+), 15 deletions(-) diff --git a/ui (gen4)/imagegrid/tags.js b/ui (gen4)/imagegrid/tags.js index d878ea44..46b73579 100755 --- a/ui (gen4)/imagegrid/tags.js +++ b/ui (gen4)/imagegrid/tags.js @@ -152,19 +152,6 @@ var TagsPrototype = { // } __index: null, - // XXX each of the arguments can be: - // - tag -> resolves to list of values - // - query -> resolves to list of values - // - list of values - __query_ns: { - and: function(...args){ - }, - or: function(...args){ - }, - not: function(...args){ - }, - }, - // Utils... // @@ -337,7 +324,8 @@ var TagsPrototype = { return [...new Set( Object.entries(this.__index || {}) .filter(function(e){ - return tag == '*' || that.match(tag, e[0]) }) + return tag == '*' + || that.match(tag, e[0]) }) .map(function(s){ return [...s[1]] }) .flat())] }, @@ -458,9 +446,53 @@ var TagsPrototype = { // 'x', // ['or', 'a', 'b'], // ['not', 'z']]) - query: function(){ + // XXX each of the arguments can be: + // - tag -> resolves to list of values + // - query -> resolves to list of values + // - list of values + __query_ns: { + and: function(...args){ + // NOTE: we are sorting the lists here to start with the + // largest and smallest lists (head/tail) to drop the + // majority of the values the earliest and speed things + // up... + args = args + .sort(function(a, b){ return a.length - b.length }) + return [...args + .reduce(function(res, l){ + return res.intersect(l) }, + new Set(args.pop()))] }, + or: function(...args){ + return [...new Set(args.flat())] }, + not: function(...args){ + return [...args + .reduce(function(res, l){ + return res.subtract(l) }, + new Set(args.shift()))] }, + }, + // XXX add support for string queries... + parseQuery: function(query){ // XXX }, + query: function(query){ + var that = this + var ns = this.__query_ns + + // Query Language Executor... + var QL = function(args){ + return args[0] in ns ? + ns[args[0]].call(ns, ...QL(args.slice(1))) + : args + .map(function(arg){ + return arg instanceof Array ? + QL(arg) + : that.values(arg) }) } + + return QL(query instanceof Array ? + query + : [query] ) + .flat() + }, // Object utility API... diff --git a/ui (gen4)/lib/util.js b/ui (gen4)/lib/util.js index a98a9452..7a97dd45 100755 --- a/ui (gen4)/lib/util.js +++ b/ui (gen4)/lib/util.js @@ -215,6 +215,7 @@ Array.prototype.sortAs = function(other){ // Set set operation shorthands... +// XXX should these accept lists of sets??? Set.prototype.unite = function(other){ return new Set([...this, ...other]) } Set.prototype.intersect = function(other){