Permalink
Browse files

couch tracks,playlists and schedules

  • Loading branch information...
1 parent 0b78d70 commit 378dad6682af7c734f7e8dcd2f56fa345de0982e @comotion committed Feb 7, 2013
Showing with 175 additions and 64 deletions.
  1. +54 −6 web/lib/couch.lua
  2. +1 −1 web/lib/lamt
  3. +2 −2 web/lib/metadata.lua
  4. +41 −6 web/lib/playlist.lua
  5. +34 −34 web/lib/{program.lua → schedule.lua}
  6. +6 −15 web/lib/tracks.lua
  7. +37 −0 web/tests/couch.lua
View
@@ -4,9 +4,6 @@
couchdb has views for queries.
want to look up some word for all fields
-get all keys:
-map: function(doc) { for (var thing in doc) { emit(thing,1); } }
-reduce: function(key,values) { return sum(values); }
get all values for a key: ( not really cool, uses loadsa disk)
@@ -27,6 +24,7 @@ http://sitr.us/2009/06/30/database-queries-the-couchdb-way.html
require "luchia"
module("couch", package.seeall)
+local u = require "util"
local default_server = { host = "localhost", port = 5984, protocol = "http" }
local db
local doc
@@ -47,7 +45,29 @@ function couch:init(database, server)
return doc
end
-function couch:genuid(doc)
+-- put up view scaffolding
+-- FIXME: add moar views.
+function couch:put_views(database)
+ local views = {
+ _id = "_design/search",
+ language = "javascript",
+ views = {
+ all = {
+ map= "function(doc) { emit(null, doc) }"
+ },
+ allkeys = {
+ map = "function(doc) { for (var thing in doc) { emit(thing,1); } }",
+ reduce = "function(key,values) { return sum(values); }"
+ },
+ md5 = {
+ map = "function(doc) { if(doc.md5) { emit(doc.md5, null) } }"
+ }
+ }
+ }
+ return database:create(views, views._id)
+end
+
+function couch:genuid()
local uuid = srv:uuids(1)[1]
-- check if uuid is in use, to be sure
while pcall(doc.info,doc,uuid) do
@@ -80,11 +100,39 @@ function couch:dump()
end
+function lookup(doc, view, k, v)
+ local path = "_design/"..view.."/_view/"..k
+ print("Path is "..path .. " v: " ..v)
+ return doc:retrieve(path, { key = '"'..v..'"' })
+end
-- needs a moar thorough rewrite..
-- lookup artist and album and title contains query
---- or tag = "" or tag contains query
-function couch:search(db, query, qop, order)
- doc:retrieve(query);
+function couch:search(doc, query, qop, order)
+ local rset = {}
+ local ret
+ for k,v in pairs(query) do
+ print("search: "..k .." -> " .. v)
+ ret = couch.lookup(doc, "search", k, v)
+ --ret = doc:retrieve("_design/search/_view/md5", {key=v})
+ if ret then
+ return ret
+ end
+ end
+ return rset
+end
+
+function couch:generate_view(doc, query)
+ local sel = ''
+ for k, v in pairs(query) do
+ sel = sel .. 'doc.'..k..'.indexOf("'..v..'") != -1 && '
+ end
+ sel = sel .. '1'
+ print("generating view: ".. sel)
+ local view = {
+ map = "function(doc) { if ( "..sel.." ) { emit(doc) } }"
+ }
+ return doc:create(view, "_temp_view")
end
return couch
Submodule lamt updated 2 files
+2 −1 id3v2.lua
+2 −0 misc.lua
View
@@ -10,9 +10,9 @@ function pathstuff(path)
local tag = {}
local spa = u.split(path, '/')
spa = spa[#spa] -- drop path stuff
+ tag.filename = spa
spa = u.split(spa, '.')
- tag.extension = spa[#spa]
- tag.filename = spa[1]
+ tag.extension = table.remove(spa)
-- initial title from path.. overwritten by tags
tag.title = tag.filename:gsub('_', ' ')
tag.path = path
View
@@ -1,14 +1,49 @@
-- playlist
--- stores a list of tracks in order
-local db = require "lib.tokyo"
-local u = require "lib.util"
+--
+--[[ track:
+{
+ id : "DMxCgbHM",
+ name : "For recreational use only",
+ author : "lafa",
+ schedules: ["oslobass/NsaDU"],
+ tracks : [
+ "1dcca23355272056f04fe8bf20edfce0",
+ "26ab0db90d72e28ad0ba1e22ee510510",
+ "31d30eea8d0968d6458e0ad0027c9f80"
+ ]
+}
+--]]
-module(..., package.seeall)
+
+local db = require "couch"
+local u = require "util"
+
+module("playlist", package.seeall)
+local database = "playlist"
function playlist:init()
- return tokyo:init("playlists")
+ return db:init(database)
end
-pls = playlist:init()
-function playlist:put(pkey,cols)
+local pls = playlist:init()
+function playlist:put(id,cols)
+ db:put(pls, id, cols)
+end
+
+function playlist:add(cols)
+ cols.added = os.time()
+ cols.station = cols.station or default_station
+ local id = cols.station..'/'..db:genuid()
+ db:put(pls, id, cols)
+end
+
+function playlist:get(id)
+ return db:get(pls, id)
+end
+
+function playlist:find(name)
+ -- bah
+ return db:generate_view(trk, { name = name })
+end
@@ -11,51 +11,51 @@
--
-- a program has
-- { id, name, [selector, playlist]}
+--[[
+ {
+ name : "Drinking is not a spectator sport",
+ selector : "",
+ selector : [ artist='mama', mood='evening' ],
+ selector : "zabu cronik",
+ start : "2013-02-05T22:00:00Z",
+ end : "2013-02-06T04:00:00Z",
+ time : "22:45",
+ duration : "60",
+ repeat : "weekly",
+ playlists: ["oslobass/DMxCgbHM", "oslobass/LTk1dGZ0"]
+}
+--]]
+
require "os"
require "math"
-local db = require "lib.tokyo"
+local db = require "couch"
--local tracks = require "lib.tracks"
-local u = require "lib.util"
-
-module("program", package.seeall)
-
-function program:init()
- return tokyo:init("programs")
-end
-prg = program:init()
-
-program = {
- name = "zonkas kronkas",
- start = 123556127,
- time = '12:45',
- day = "sunday",
- duration = '60 minutes',
- recur = 'weekly',
- stop = 133377777,
- selector = { artist='mama', mood='evening' },
- selector = "zabu cronik",
- playlist = { 1, 3, 4, 10, 42 },
-}
-
-
-function selector(search)
-end
+local u = require "util"
+module("schedule", package.seeall)
+local database = "schedule"
-function program:put(pkey,cols)
- return tokyo:put(prg,pkey,cols)
+function schedule:init()
+ return db:init(database)
end
+shd = schedule:init()
-- a program has a human-friendly name,
-- launches at some date, starts at a particular time of day
-- lasts for x minutes and optionally repeats
-- every X * {day, week, month, year}
-function program:add(name, startdate, time, duration, every, X)
- local p = {}
- p.name = name
- p.startday = startdate
- p.time = time
- p.duration = duration
+function schedule:add(name, startdate, time, duration, every, X)
+ local p = {}
+ p.name = name
+ p.startday = startdate
+ p.time = time
+ p.duration = duration
+ local id = p.station .. '/'..db:genuid()
+ return db:add(shd, id, p)
+end
+
+function schedule:get(id)
+ return db:get(shd, id)
end
-- when asked to compile a plan,
View
@@ -27,7 +27,9 @@ module("tracks", package.seeall)
local database = "tracks"
function tracks:init()
- return db:init(database)
+ local base = db:init(database)
+ db:put_views(base)
+ return base
end
-- local instance of the db connection saves us an init call
@@ -45,30 +47,19 @@ function tracks:add(cols)
cols.station = cols.station or default_station
print("adding ".. u.dump(cols))
if cols.md5 then
- id = db:ssearch(_t,{md5=md5})
+ local id = db:search(trk, {md5=cols.md5})
if #id ~= 0 then
print("found id "..u.dump(id))
return id
end
end
- return db:add(_t, cols)
+ return db:put(trk, id, cols)
end
-- search within the database
-- returns array [id]={result}
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, size
- if type(query) == "table" then
- result,size = db:ssearch(_t, query, qop, order)
- else
- result,size = db:gsearch(_t, query, qf, order)
- end
- return tracks.fill(result),size
end
function tracks.fields(result)
@@ -100,7 +91,7 @@ function tracks:dump()
end
function tracks:get(pkey)
- local track = db:get(_t,pkey)
+ local track = db:get(pkey)
track.id = pkey
return track
end
View
@@ -0,0 +1,37 @@
+-- test some c0uchdb stuff
+
+require "luchia"
+local cjson = require "cjson.safe" -- arf on error instead of barfing
+local json = cjson.new() -- is thread safer
+
+local server = { host = "localhost", port = 5984, protocol = "http" }
+local db = luchia.database:new(server)
+local info = db:info("example")
+if not info then
+ resp = db:create("example")
+ print(json.encode(resp))
+end
+local doc = luchia.document:new("example", server)
+-- Simple document.
+local contents = { hello = "world" }
+-- Create new document.
+local resp = doc:create(contents)
+print(json.encode(resp))
+-- Check for successful creation.
+if doc:response_ok(resp) then
+ -- Update document contents.
+ print(resp.id)
+
+ contents = { hello = "world", foo = "bar" }
+ -- Update document.
+ doc:update(contents, resp.id, resp.rev)
+else
+ print "Fail"
+end
+
+local tr = luchia.document:new("tracks", server)
+resp = tr:retrieve("_design/search/_view/md5", { startkey="a", endkey="b"})
+print(json.encode(resp.rows[1]))
+utils = luchia.utilities:new(server)
+resp = utils:utilities_get_call("_design/v/_view/name")
+print(resp)

0 comments on commit 378dad6

Please sign in to comment.