Comparison

mod_storage_gdbm/mod_storage_gdbm.lua @ 1629:36eb0dbea7ba

mod_storage_gdbm: Minor reorganization, more locals
author Kim Alvefur <zash@zash.se>
date Wed, 25 Mar 2015 17:37:52 +0100
parent 1628:5e4b37b9cde1
child 1630:0fcd63818aa1
comparison
equal deleted inserted replaced
1628:5e4b37b9cde1 1629:36eb0dbea7ba
1 -- mod_storage_gdbm 1 -- mod_storage_gdbm
2 -- Copyright (C) 2014 Kim Alvefur 2 -- Copyright (C) 2014-2015 Kim Alvefur
3 -- 3 --
4 -- This file is MIT/X11 licensed. 4 -- This file is MIT/X11 licensed.
5 -- 5 --
6 -- Depends on lgdbm: 6 -- Depends on lgdbm:
7 -- http://webserver2.tecgraf.puc-rio.br/~lhf/ftp/lua/#lgdbm 7 -- http://webserver2.tecgraf.puc-rio.br/~lhf/ftp/lua/#lgdbm
8 8
9 local gdbm = require"gdbm"; 9 local gdbm = require"gdbm";
10 local path = require"util.paths"; 10 local path = require"util.paths";
11 local lfs = require"lfs"; 11 local lfs = require"lfs";
12 local st = require"util.stanza";
12 local uuid = require"util.uuid".generate; 13 local uuid = require"util.uuid".generate;
14
13 local serialization = require"util.serialization"; 15 local serialization = require"util.serialization";
14 local st = require"util.stanza";
15 local serialize = serialization.serialize; 16 local serialize = serialization.serialize;
16 local deserialize = serialization.deserialize; 17 local deserialize = serialization.deserialize;
18
19 local g_set, g_get, g_del = gdbm.replace, gdbm.fetch, gdbm.delete;
20 local g_first, g_next = gdbm.firstkey, gdbm.nextkey;
21
22 local t_remove = table.remove;
17 23
18 local empty = {}; 24 local empty = {};
19 25
20 local function id(v) return v; end 26 local function id(v) return v; end
21 27
22 local function is_stanza(s) 28 local function is_stanza(s)
23 return getmetatable(s) == st.stanza_mt; 29 return getmetatable(s) == st.stanza_mt;
24 end 30 end
25 31
26 local function ifelse(cond, iftrue, iffalse) 32 local function t(c, a, b)
27 if cond then return iftrue; end return iffalse; 33 if c then return a; end return b;
28 end 34 end
29 35
30 local base_path = path.resolve_relative_path(prosody.paths.data, module.host); 36 local base_path = path.resolve_relative_path(prosody.paths.data, module.host);
31 lfs.mkdir(base_path); 37 lfs.mkdir(base_path);
32 38
34 40
35 local keyval = {}; 41 local keyval = {};
36 local keyval_mt = { __index = keyval, suffix = ".db" }; 42 local keyval_mt = { __index = keyval, suffix = ".db" };
37 43
38 function keyval:set(user, value) 44 function keyval:set(user, value)
39 local ok, err = gdbm.replace(self._db, user or "@", serialize(value)); 45 local ok, err = g_set(self._db, user or "@", serialize(value));
40 if not ok then return nil, err; end 46 if not ok then return nil, err; end
41 return true; 47 return true;
42 end 48 end
43 49
44 function keyval:get(user) 50 function keyval:get(user)
45 local data, err = gdbm.fetch(self._db, user or "@"); 51 local data, err = g_get(self._db, user or "@");
46 if not data then return nil, err; end 52 if not data then return nil, err; end
47 return deserialize(data); 53 return deserialize(data);
48 end 54 end
49 55
50 local archive = {}; 56 local archive = {};
79 85
80 function archive:find(username, query) 86 function archive:find(username, query)
81 query = query or empty_query; 87 query = query or empty_query;
82 local meta = self:get(username) or empty; 88 local meta = self:get(username) or empty;
83 local r = query.reverse; 89 local r = query.reverse;
84 local d = r and -1 or 1; 90 local d = t(r, -1, 1);
85 local s = meta[ifelse(r, query.before, query.after)]; 91 local s = meta[t(r, query.before, query.after)];
86 local limit = query.limit; 92 local limit = query.limit;
87 if s then 93 if s then
88 s = s + d; 94 s = s + d;
89 else 95 else
90 s = ifelse(r, #meta, 1) 96 s = t(r, #meta, 1)
91 end 97 end
92 local e = ifelse(r, 1, #meta); 98 local e = t(r, 1, #meta);
93 local c = 0; 99 local c = 0;
94 return function () 100 return function ()
95 if limit and c >= limit then return end 101 if limit and c >= limit then return end
96 local item, value; 102 local item, value;
97 for i = s, e, d do 103 for i = s, e, d do