Permalink
Browse files

tokyotyrant

  • Loading branch information...
comotion committed Feb 6, 2013
1 parent 0b4020f commit 0c086c391d368657b09c34f05df68b50e6667e6c
Showing with 197 additions and 65 deletions.
  1. +9 −65 web/lib/tracks.lua
  2. +188 −0 web/lib/tyrant.lua
View
@@ -20,26 +20,24 @@
-- count total results (or pages)
require "os"
-local db = require "lib/tokyo"
+local db = require "lib/tyrant"
local u = require "lib/util"
-module("tracks", package.seeall)
+local _t = "tracks"
+module(_t, package.seeall)
+
function tracks:init()
return db:init("tracks")
end
-local trk = tracks:init()
-
-function tracks:put(pkey,cols)
- return db:put(trk, pkey, cols)
+function tracks:put(id, cols)
+ return db:put(_t, id, cols)
end
-
-- can have table of tags
-- returns: trackid,entry,error
function tracks:add(cols)
local cols = cols or {}
- local pkey = trk:genuid()
cols.added = os.time()
cols.station = cols.station or default_station
print("adding ".. u.dump(cols))
@@ -50,17 +48,9 @@ function tracks:add(cols)
return id
end
end
- return tracks:put(pkey, cols)
+ return db:add(_t, cols)
end
--- the simple search
-function tracks:ssearch(query, qop, order)
- local query = query or {station=default_station}
- local qop = qop or db.op.equal
- -- q:setlimit(limit, skip) -- we need the size so there is no use
- local res = db:search(trk, query, qop, order)
- return res, #res
-end
-- search within the database
-- returns array [id]={result}
@@ -71,7 +61,7 @@ function tracks:search(query, qf, qop, order)
local result, size
if type(query) == "table" then
- result,size = tracks:ssearch(query, qop, order)
+ result,size = db:ssearch(query, qop, order)
else
result,size = tracks:gsearch(query, qf, order)
end
@@ -100,53 +90,7 @@ function tracks.fill(result)
rawset(rset,tonumber(v),tracks:get(v))
end
return rset
-end
-
--- search for query in all queryfields
--- honour queries like "foo bar tag:value"
--- todo: filter out results that don't match whole query
-function tracks:gsearch(q, qf, order)
- local queries = {}
- q,qf = q or '', qf or ''
- local tokens = u.split(q,', ')
- local qf = u.split(qf)
- local qry
-
- -- collect terms
- local accu = {}
- local tags = {}
- for i,v in pairs(tokens) do
- t = u.split(v,':')
- if #t > 1 then
- tags[t[1]] = t[2]
- else
- table.insert(accu, v)
- end
- end
-
- -- create one search per field
- for j,f in pairs(qf) do
- local q = tokyocabinet.tdbqrynew(trk)
- for t,v in pairs(tags) do
- a = u.split(v,',')
- if #a > 1 then
- q:addcond(t,db.op.one,v)
- else
- q:addcond(t,db.op.inclusive,v)
- end
- end
- if #accu > 0 then
- print("search " .. f .. " for " .. u.join(accu))
- q:addcond(f,db.op.onetoken,u.join(accu))
- end
- table.insert(queries,q)
- end
- -- pull out last query and execute on it
- qry = table.remove(queries)
- qry:setorder(unpack(order))
- local result = qry:metasearch(queries,qry.MSUNION)
- return result, #result
-end
+end
function tracks:dump()
trk:iterinit()
View
@@ -0,0 +1,188 @@
+-- access model through tyrant
+--
+-- interface to the database
+-- get(table, id), set(table, id, cols), add(table, cols)
+-- search(table, query, fields, order)
+-- only one database, but many "models" or object types
+
+package.path = package.path..";lib/?.lua;lib/?/?.lua"
+require "os"
+local tyr = require "tokyotyrant"
+local u = require "util"
+
+module("tyrant", package.seeall)
+
+local db = nil
+local tbl = nil
+
+-- create table mapping to magic constants
+local op = {
+ -- string
+ equal = "STREQ",
+ inclusive = "STRINC",
+ begins = "STRBW",
+ ends = "STREW",
+ all = "STRAND",
+ one = "STROR", -- inclusive or
+ eqone = "STROREQ", -- equal or
+ regex = "STRRX",
+ -- numeric
+ eq = "NUMEQ",
+ gt = "NUMGT",
+ ge = "NUMGE",
+ lt = "NUMLT",
+ le = "NUMLE",
+ tween = "NUMBT",
+ numor = "NUMOREQ",
+ -- fulltext
+ phrase = "FTSPH",
+ alltokens = "FTSAND",
+ onetoken = "FTSOR",
+ compound = "FTSEX",
+ -- flags
+ negate = "NEGATE",
+ noindex = "NOIDX",
+}
+
+local sort = {
+ lexic = "STRASC",
+ reverse = "STRDESC",
+ increasing= "NUMASC",
+ decreasing= "NUMDESC",
+}
+
+function tyrant:init(name)
+ if db == nil then
+ tbl = tyr.tbldb.new()
+ db = tyr.rdb.new()
+ tbl:open()
+ db:open()
+ end
+ -- XXX pass "handle" back to client
+ return self
+end
+
+-- create an entry
+function tyrant:add(name, cols)
+ cols._type = name
+ cols.added = cols.added or os.time()
+ local id = tbl:genuid()
+ return tbl:put(id, cols)
+end
+
+-- find entries of given _type=name
+function tyrant:search(name, query, qop, order)
+ local q = tyr.query.new()
+ q:addcond('_type', op.equals, name)
+ for k,v in pairs(query) do
+ q:addcond(k, qop, v)
+ end
+ if order then q:setorder(unpack(order)) end
+ return tbl:search(q)
+end
+
+-- the simple search. pass us a table of queries
+function tyrant:ssearch(name, query, qop, order)
+ local query = query or {_type = name, station=default_station}
+ local qop = qop or op.equal
+ -- q:setlimit(limit, skip) -- we need the size so there is no use
+ local res = tyrant:search(name, query, qop, order)
+ return res, #res
+end
+
+
+function tyrant:get(name ,id)
+ local thing = db:get(id)
+ if id._type == name then
+ return thing
+ end
+ return nil
+end
+
+function tyrant:put(name, id, cols)
+ cols._type = name
+ db:put(id, cols)
+end
+
+
+db,tbl = tyrant:init()
+print(u.dump(tbl:size()))
+id = tbl:genuid()
+u.out(id)
+print(u.dump(tbl:put(id, {a="b",c="d"})))
+print('aha')
+print(u.dump(tbl:get(id)))
+print("aargh")
+u.out(tbl)
+q = tyr.query.new()
+keys = tbl:search(q)
+for k=1,#keys do
+ v = keys[k]
+ thing = tbl:get(v)
+ print(v .. ' -> ' .. u.dump(thing))
+end
+
+function tyrant:dump(name)
+--iterate over the whole db, or _type = name
+end
+function tyrant:get(pkey)
+-- get primary key
+end
+function tyrnat:put(pkey, cols)
+-- put stuff
+end
+function tyrant:update(pkey,cols)
+-- merge stuff in to the database
+end
+function tyrant:search(db, query, qop, order)
+-- search db for strings
+end
+
+-- search for query in all queryfields
+-- honour queries like "foo bar tag:value"
+-- todo: filter out results that don't match whole query
+function tyrant:gsearch(q, qf, order)
+ local queries = {}
+ q,qf = q or '', qf or ''
+ local tokens = u.split(q,', ')
+ local qf = u.split(qf)
+ local qry
+
+ -- collect terms
+ local accu = {}
+ local tags = {}
+ for i,v in pairs(tokens) do
+ t = u.split(v,':')
+ if #t > 1 then
+ tags[t[1]] = t[2]
+ else
+ table.insert(accu, v)
+ end
+ end
+
+ -- create one search per field
+ for j,f in pairs(qf) do
+ local q = tokyocabinet.tdbqrynew(trk)
+ for t,v in pairs(tags) do
+ a = u.split(v,',')
+ if #a > 1 then
+ q:addcond(t,db.op.one,v)
+ else
+ q:addcond(t,db.op.inclusive,v)
+ end
+ end
+ if #accu > 0 then
+ print("search " .. f .. " for " .. u.join(accu))
+ q:addcond(f,db.op.onetoken,u.join(accu))
+ end
+ table.insert(queries,q)
+ end
+ -- pull out last query and execute on it
+ qry = table.remove(queries)
+ qry:setorder(unpack(order))
+ local result = qry:metasearch(queries,qry.MSUNION)
+ return result, #result
+end
+
+
+return tyrant

0 comments on commit 0c086c3

Please sign in to comment.