dogmaql.driver.redis
Version:
Redis driver for DogmaQL (Dogma Query Language).
97 lines (95 loc) • 5.71 kB
JavaScript
"use strict";
var _dogmalang = require("dogmalang");
const LuaTrans = _dogmalang.dogma.use(require("./LuaTrans"));const time = _dogmalang.dogma.use(require("./time"));
const luaTrans = LuaTrans();
const $SelectTrans = class SelectTrans {
constructor() {
{}
}
};
const SelectTrans = new Proxy($SelectTrans, { apply(receiver, self, args) {
return new $SelectTrans(...args);
} });module.exports = exports = SelectTrans;
SelectTrans.prototype.transListTables = function (stmt) {
_dogmalang.dogma.paramExpected("stmt", stmt, null);{
return [["eval", "local tabs = {}\n\nfor _, key in ipairs(redis.call(\"keys\", \"db.tables:*\")) do\n table.insert(tabs, redis.call(\"json.get\", key))\nend\n\nreturn tabs", 0]];
}
};
SelectTrans.prototype.transSelectItem = function (stmt) {
_dogmalang.dogma.paramExpected("stmt", stmt, null);{
return _dogmalang.dogma.like(stmt.table, "^ts\\.") ? transSelectTsItem(stmt) : _dogmalang.dogma.like(stmt.table, "^fts\\.") ? transSelectFtsItem(stmt) : transSelectKvItem(stmt);
}
};
function transSelectFtsItem(stmt) {
_dogmalang.dogma.paramExpected("stmt", stmt, null);{
return !stmt.where ? transSelectMultipleFtsItems(stmt) : transSelectOneFtsItem(stmt);
}
}
function transSelectOneFtsItem(stmt) {
_dogmalang.dogma.paramExpected("stmt", stmt, null);{
return [["ft.get", stmt.table, stmt.table + ":" + _dogmalang.dogma.getItem(stmt.where, 0).value]];
}
}
function transSelectMultipleFtsItems(stmt) {
_dogmalang.dogma.paramExpected("stmt", stmt, null);{
return [["eval", "local tab = ARGV[1]\nlocal rslt = {}\n\nfor _, key in ipairs(redis.call(\"keys\", tab .. \":\" .. \"*\")) do\n local item = {}\n local doc = redis.call(\"hgetall\", key)\n for i=1, #doc, 2 do item[doc[i]] = doc[i+1] end\n table.insert(rslt, cjson.encode(item))\nend\n\nreturn rslt", 0, stmt.table]];
}
}
function transSelectTsItem(stmt) {
let cmd = [];_dogmalang.dogma.paramExpected("stmt", stmt, null);{
if (stmt.where) {
if ((0, _dogmalang.len)(stmt.where) == 1) {
if (_dogmalang.dogma.getItem(stmt.where, 0).field == "time") {
cmd = transSelectTsItemByTime(stmt);
} else {
cmd = transSelectTsItemByKey(stmt);
}
} else {
cmd = transSelectTsItemByCompositeKey(stmt);
}
} else {
cmd = transSelectAllTsItems(stmt);
}
}return cmd;
}
function transSelectAllTsItems(stmt) {
_dogmalang.dogma.paramExpected("stmt", stmt, null);{
return [["eval", "local tab = ARGV[1]\nlocal rslt = {}\n\nfor _, key in ipairs(redis.call(\"keys\", tab .. \":*\")) do\n for _, item in ipairs(redis.call(\"zrangebyscore\", key, \"-inf\", \"+inf\")) do\n table.insert(rslt, item)\n end\nend\n\nreturn rslt", 0, stmt.table]];
}
}
function transSelectTsItemByKey(stmt) {
_dogmalang.dogma.paramExpected("stmt", stmt, null);{
return [["zrangebyscore", stmt.table + ":" + _dogmalang.dogma.getItem(stmt.where, 0).value, "-inf", "+inf"]];
}
}
function transSelectTsItemByTime(stmt) {
_dogmalang.dogma.paramExpected("stmt", stmt, null);{
return [["eval", "local tab, init, fin = ARGV[1], ARGV[2], ARGV[3]\nlocal rslt = {}\n\nfor _, key in ipairs(redis.call(\"keys\", tab .. \":*\")) do\n for _, item in ipairs(redis.call(\"zrangebyscore\", key, init, fin)) do\n table.insert(rslt, item)\n end\nend\n\nreturn rslt", 0, stmt.table, ...time.formatInterval(_dogmalang.dogma.getItem(stmt.where, 0).op, _dogmalang.dogma.getItem(stmt.where, 0).value)]];
}
}
function transSelectTsItemByCompositeKey(stmt) {
_dogmalang.dogma.paramExpected("stmt", stmt, null);{
return [["zrangebyscore", stmt.table + ":" + _dogmalang.dogma.getItem(stmt.where, 0).value, ...time.formatInterval(_dogmalang.dogma.getItem(stmt.where, 1).op, _dogmalang.dogma.getItem(stmt.where, 1).value)]];
}
}
function transSelectKvItem(stmt) {
let cmd = [];_dogmalang.dogma.paramExpected("stmt", stmt, null);{
if (stmt.inc) {
cmd = [["eval", "local key, counter = ARGV[1] .. \":\" .. KEYS[1], ARGV[2]\nlocal ok, res = pcall(function() redis.call(\"json.numincrby\", key, counter, 1) end)\nif ok then\n return redis.call(\"json.get\", key)\nelse\n if not res:find(\"could not perform this operation on a key that doesn't exist\") then\n error(res)\n end\nend", 1, _dogmalang.dogma.getItem(stmt.where, 0).value, stmt.table, stmt.inc]];
} else {
if (stmt.where) {
if (stmt.ifc) {
cmd = [["eval", (0, _dogmalang.fmt)("local tab = ARGV[1]\nlocal key = tab .. \":\" .. KEYS[1]\nlocal item = redis.call(\"json.get\", key)\nlocal function checkCond()\n local type = type\n setfenv(1, cjson.decode(item))\n return %s\nend\n\nif item and not checkCond() then\n item = nil\nend\n\nreturn item", luaTrans.transform(stmt.ifc)), 1, _dogmalang.dogma.getItem(stmt.where, 0).value, stmt.table]];
} else {
cmd = [["json.get", (0, _dogmalang.fmt)("%s:%s", stmt.table, _dogmalang.dogma.getItem(stmt.where, 0).value)]];
}
} else {
if (stmt.ifc) {
cmd = [["eval", (0, _dogmalang.fmt)("local items = {}\n\nfor _, key in ipairs(redis.call(\"keys\", ARGV[1] .. \":*\")) do\n local item = redis.call(\"json.get\", key)\n local function checkCond()\n local type = type\n setfenv(1, cjson.decode(item))\n return %s\n end\n\n if checkCond() then\n table.insert(items, item)\n end\nend\n\nreturn items", luaTrans.transform(stmt.ifc)), 0, stmt.table]];
} else {
cmd = [["eval", "local items = {}\n\nfor _, key in ipairs(redis.call(\"keys\", ARGV[1] .. \":*\")) do\n table.insert(items, redis.call(\"json.get\", key))\nend\n\nreturn items", 0, stmt.table]];
}
}
}
}return cmd;
}