Mercurial > prosody-modules
comparison mod_storage_memory/mod_storage_memory.lua @ 2097:4454f124465a
mod_storage_memory: Support for empty username stores
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Wed, 16 Mar 2016 12:55:25 +0000 |
parents | e72f9eac51c8 |
children | 85d88ed4f2a2 |
comparison
equal
deleted
inserted
replaced
2096:b75d29a162cd | 2097:4454f124465a |
---|---|
5 t[k] = store; | 5 t[k] = store; |
6 return store; | 6 return store; |
7 end | 7 end |
8 }); | 8 }); |
9 | 9 |
10 local NULL = {}; | |
11 | |
10 local keyval_store = {}; | 12 local keyval_store = {}; |
11 keyval_store.__index = keyval_store; | 13 keyval_store.__index = keyval_store; |
12 | 14 |
13 function keyval_store:get(username) | 15 function keyval_store:get(username) |
14 return self.store[username]; | 16 return self.store[username or NULL]; |
15 end | 17 end |
16 | 18 |
17 function keyval_store:set(username, data) | 19 function keyval_store:set(username, data) |
18 self.store[username] = data; | 20 self.store[username or NULL] = data; |
19 return true; | 21 return true; |
20 end | 22 end |
21 | 23 |
22 local map_store = {}; | 24 local map_store = {}; |
23 map_store.__index = map_store; | 25 map_store.__index = map_store; |
24 | 26 |
25 function map_store:get(username, key) | 27 function map_store:get(username, key) |
26 local userstore = self.store[username]; | 28 local userstore = self.store[username or NULL]; |
27 if type(userstore) == "table" then | 29 if type(userstore) == "table" then |
28 return userstore[key]; | 30 return userstore[key]; |
29 end | 31 end |
30 end | 32 end |
31 | 33 |
32 function map_store:set(username, key, data) | 34 function map_store:set(username, key, data) |
33 local userstore = self.store[username]; | 35 local userstore = self.store[username or NULL]; |
34 if userstore == nil then | 36 if userstore == nil then |
35 userstore = {}; | 37 userstore = {}; |
36 self.store[username] = userstore; | 38 self.store[username or NULL] = userstore; |
37 end | 39 end |
38 userstore[key] = data; | 40 userstore[key] = data; |
39 return true; | 41 return true; |
40 end | 42 end |
41 | 43 |
44 | 46 |
45 function archive_store:append(username, key, value, when, with) | 47 function archive_store:append(username, key, value, when, with) |
46 if type(when) ~= "number" then | 48 if type(when) ~= "number" then |
47 when, with, value = value, when, with; | 49 when, with, value = value, when, with; |
48 end | 50 end |
49 local a = self.store[username]; | 51 local a = self.store[username or NULL]; |
50 if not a then | 52 if not a then |
51 a = {}; | 53 a = {}; |
52 self.store[username] = a; | 54 self.store[username or NULL] = a; |
53 end | 55 end |
54 local i = #a+1; | 56 local i = #a+1; |
55 local v = { key = key, when = when, with = with, value = value }; | 57 local v = { key = key, when = when, with = with, value = value }; |
56 if not key or a[key] then | 58 if not key or a[key] then |
57 key = tostring(a):match"%x+$"..tostring(v):match"%x+$"; | 59 key = tostring(a):match"%x+$"..tostring(v):match"%x+$"; |
76 end | 78 end |
77 end | 79 end |
78 end | 80 end |
79 | 81 |
80 function archive_store:find(username, query) | 82 function archive_store:find(username, query) |
81 local a = self.store[username] or {}; | 83 local a = self.store[username or NULL] or {}; |
82 local start, stop, step = 1, #a, 1; | 84 local start, stop, step = 1, #a, 1; |
83 local qstart, qend, qwith = -math.huge, math.huge; | 85 local qstart, qend, qwith = -math.huge, math.huge; |
84 local limit; | 86 local limit; |
85 if query then | 87 if query then |
86 module:log("debug", "query included") | 88 module:log("debug", "query included") |
102 return iter; | 104 return iter; |
103 end | 105 end |
104 | 106 |
105 function archive_store:delete(username, query) | 107 function archive_store:delete(username, query) |
106 if not query or next(query) == nil then | 108 if not query or next(query) == nil then |
107 self.store[username] = nil; | 109 self.store[username or NULL] = nil; |
108 return true; | 110 return true; |
109 end | 111 end |
110 local old = self.store[username]; | 112 local old = self.store[username or NULL]; |
111 if not old then return true; end | 113 if not old then return true; end |
112 local qstart = query.start or -math.huge; | 114 local qstart = query.start or -math.huge; |
113 local qend = query["end"] or math.huge; | 115 local qend = query["end"] or math.huge; |
114 local qwith = query.with; | 116 local qwith = query.with; |
115 local new = {}; | 117 local new = {}; |
116 self.store[username] = new; | 118 self.store[username or NULL] = new; |
117 local t; | 119 local t; |
118 for i = 1, #old do | 120 for i = 1, #old do |
119 i = old[i]; | 121 i = old[i]; |
120 t = i.when; | 122 t = i.when; |
121 if not(qstart >= t and qend <= t and (not qwith or i.with == qwith)) then | 123 if not(qstart >= t and qend <= t and (not qwith or i.with == qwith)) then |
122 self:append(username, i.key, i.value, t, i.with); | 124 self:append(username, i.key, i.value, t, i.with); |
123 end | 125 end |
124 end | 126 end |
125 if #new == 0 then | 127 if #new == 0 then |
126 self.store[username] = nil; | 128 self.store[username or NULL] = nil; |
127 end | 129 end |
128 return true; | 130 return true; |
129 end | 131 end |
130 | 132 |
131 local stores = { | 133 local stores = { |