Permalink
Browse files

metadata and skip/limit rewrites

  • Loading branch information...
1 parent 6f14449 commit 4812b1d0133f9c340aac8124925da93e184abc2a @comotion committed with why Oct 5, 2012
Showing with 41 additions and 39 deletions.
  1. +13 −12 web/hangover.lua
  2. +1 −1 web/lib/metadata.lua
  3. +27 −26 web/lib/tracks.lua
View
@@ -4,15 +4,16 @@ local ocash = require "orbit.cache"
local cjson = require "cjson.safe" -- arf on error instead of barfing
local json = cjson.new() -- is thread safer
local convert, ratio, safe = json.encode_sparse_array(true)
-json.encode_invalid_numbers = true
+json.encode_invalid_numbers = true -- avoid b00gs
module("hangover", package.seeall, orbit.new)
+package.path = package.path..";lib/?.lua;lib/?/?.lua"
local cache = ocash.new(hangover, cache_path)
-local tracks = require "lib.tracks"
-local u = require "lib.util"
+local tracks = require "tracks"
+local u = require "util"
local io = require "io"
local md5 = require "md5"
-local meta = require "lib.metadata"
+local meta = require "metadata"
require "config"
local user = "badface" -- XXX: basic auth/user db
@@ -41,7 +42,8 @@ function get_db(web,...)
end
local query = web.GET.q or {}
local limit = web.GET.maxresults or 25
- local page = web.GET.page or 0
+ local page = web.GET.page or 1
+ local skip = (page-1)*limit
local fields = web.GET.fields
local qf = web.GET.qf or "artist,title"
if type(limit) == "table" then limit=limit[1] end
@@ -51,15 +53,16 @@ function get_db(web,...)
if type(query) == "table" then query = u.join(query) end
if type(qf) == "table" then qf = u.join(qf) end
- local result, pages = tracks:search(query, qf, limit, page)
+ local result, size = tracks:search(query, qf)
+ local pages = math.floor(size/limit)+1
if fields then
fields = u.split(fields)
- result = tracks.filter(result, fields)
else
fields = tracks.fields(result);
end
- return json.encode{{fields=fields,pages=pages,result=result}}
+ result = tracks.filter(result, fields, limit, skip)
+ return json.encode({{fields=fields,pages=pages,result=result}}).."\n"
end
function getfile(file)
@@ -73,12 +76,12 @@ function getfile(file)
if not dest then
return nil,json.encode{{status="fail",reason="bad tempfile, baad"}}
end
- -- for progress we need chunking and status
+ -- XXX: for progress we need chunking and status
dest:write(bytes)
dest:close()
print("["..os.date("%c", t.submitted).. "] '"..t.filename.."' -> "..tname)
print("'"..t.filename .. "'".." " .. os.difftime(os.time(), t.submitted).."s")
-t.md5 = u.bintohex(md5.sum(bytes))
+ t.md5 = u.bintohex(md5.sum(bytes))
local destname = t.md5..t.contenttype -- krav's pathless filename
t.path = tracks_path .. "/" .. destname
local rc, err = os.rename(tname, t.path)
@@ -102,8 +105,6 @@ function post_db(web,...)
return failure
end
print("'"..destname.. "'".." " .. os.difftime(os.time(), t.submitted).."s")
- -- id3 extraction / file metadata
- t.tag = meta.gettags(t.path)
-- add to database, tags and all
id = tracks:add({unpack(t), unpack(tags)})
-- add to database, tags and all
View
@@ -1,5 +1,5 @@
module("metadata",package.seeall)
-local types,info,edit = unpack(require "lamt.mpeg")
+local types,info,edit = unpack(require ("lamt.mpeg"))
local u = require "util"
function gettags(path)
View
@@ -15,7 +15,7 @@
---------------------------------
-- Goals:
-- unique add(artist,track,{key=val})
--- search(track{key=val},sort,limit,paginate)
+-- search(track{key=val},sort) : paginate
-- filter to return only specific fields
-- count total results (or pages)
@@ -45,37 +45,33 @@ function tracks:add(cols)
return tracks:put(pkey, cols)
end
-function tracks:ssearch(query, limit, page, qop, order)
+function tracks:ssearch(query, qop, order)
local query = query or {station=default_station}
local qop = qop or db.op.equal
q = tokyocabinet.tdbqrynew(trk)
for k,v in pairs(query) do
q:addcond(k, qop, v)
end
- local size = #q:search() -- just to get size
q:setorder(unpack(order))
- q:setlimit(limit, skip)
- return q:search(),math.floor(size/limit)+1
+ local res = q:search()
+ -- q:setlimit(limit, skip) -- we need the size so there is no use
+ return res, #res
end
-- search within the database
--- you can specify limit and page number
-- returns array [id]={result}
-function tracks:search(query, qf, limit, page, qop, order)
- local limit = limit or 25
- local page = page or 1
- local skip = (page-1)*limit
+function tracks:search(query, qf, qop, order)
local order = order or {"added", db.sort.decreasing}
if type(order) ~= "table" then order = {order, db.sort.increasing} end
if not query or query == "" then query = {} end
- local result, pages
+ local result, size
if type(query) == "table" then
- result,pages = tracks:ssearch(query, limit, page, qop, order)
+ result,size = tracks:ssearch(query, qop, order)
else
- result,pages = tracks:gsearch(query, qf, limit, page, order)
+ result,size = tracks:gsearch(query, qf, order)
end
- return tracks.fill(result),pages
+ return tracks.fill(result),size
end
function tracks.fields(result)
@@ -93,6 +89,7 @@ function tracks.fields(result)
end
+-- take an array of id's and return a set of result tracks
function tracks.fill(result)
local rset = {}
for i,v in ipairs(result) do
@@ -104,7 +101,7 @@ 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, limit, page, order)
+function tracks:gsearch(q, qf, order)
local queries = {}
q,qf = q or '', qf or ''
local tokens = u.split(q,', ')
@@ -143,9 +140,8 @@ function tracks:gsearch(q, qf, limit, page, order)
-- pull out last query and execute on it
qry = table.remove(queries)
qry:setorder(unpack(order))
- local pages = math.floor(#qry:metasearch(queries,qry.MSUNION)/limit)+1
- qry:setlimit(limit, skip)
- return qry:metasearch(queries,qry.MSUNION),pages
+ local result = qry:metasearch(queries,qry.MSUNION)
+ return result, #result
end
function tracks:dump()
@@ -177,20 +173,25 @@ function tracks:update(pkey, cols)
end
-- return result with only fields
-function tracks.filter(result, fields)
+-- you can specify limit and page number
+function tracks.filter(result, fields, limit, skip)
local res = {}
+ local c = 0
for k,v in pairs(result) do
- local one = {}
- for i,f in pairs(fields) do
- one[f] = v[f]
+ c = c + 1
+ if(c > skip+limit) then
+ break
+ end
+ if(c > skip) then
+ local one = {}
+ for i,f in pairs(fields) do
+ one[f] = v[f]
+ end
+ res[k] = one
end
- res[k] = one
end
return res
end
-
-tracks:add({artist="yo",title="mama",foo="bar"})
-tracks:add( {artist="world",title="musack", foo="baz"})
u.out("fapfapfap", tracks:dump())
return tracks

0 comments on commit 4812b1d

Please sign in to comment.