Software /
code /
prosody
Annotate
plugins/mod_storage_memory.lua @ 9340:368b092bf4bf
mod_storage_memory: Fix saving of remaining items during deletion
All data is compiled into functions that return new copies in order to
prevent mutation of stored data.
This has downsides.
Also an ugly hack.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 22 Sep 2018 16:03:50 +0200 |
parent | 9293:0a751835627d |
child | 9468:bd5e4485a245 |
rev | line source |
---|---|
9293
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 local serialize = require "util.serialization".serialize; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 local envload = require "util.envload".envload; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 local st = require "util.stanza"; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 local is_stanza = st.is_stanza or function (s) return getmetatable(s) == st.stanza_mt end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 local auto_purge_enabled = module:get_option_boolean("storage_memory_temporary", false); |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 local auto_purge_stores = module:get_option_set("storage_memory_temporary_stores", {}); |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 local memory = setmetatable({}, { |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 __index = function(t, k) |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 local store = module:shared(k) |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 t[k] = store; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 return store; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 }); |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 local function NULL() return nil end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 local function _purge_store(self, username) |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 self.store[username or NULL] = nil; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 return true; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 local keyval_store = {}; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 keyval_store.__index = keyval_store; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 function keyval_store:get(username) |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 return (self.store[username or NULL] or NULL)(); |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 function keyval_store:set(username, data) |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 if data ~= nil then |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 data = envload("return "..serialize(data), "@data", {}); |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 self.store[username or NULL] = data; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 return true; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 keyval_store.purge = _purge_store; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 local archive_store = {}; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 archive_store.__index = archive_store; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 function archive_store:append(username, key, value, when, with) |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 if type(when) ~= "number" then |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 when, with, value = value, when, with; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 if is_stanza(value) then |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 value = st.preserialize(value); |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 value = envload("return xml"..serialize(value), "@stanza", { xml = st.deserialize }) |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 else |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 value = envload("return "..serialize(value), "@data", {}); |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 local a = self.store[username or NULL]; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 if not a then |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 a = {}; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 self.store[username or NULL] = a; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 local i = #a+1; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 local v = { key = key, when = when, with = with, value = value }; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 if not key then |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 key = tostring(a):match"%x+$"..tostring(v):match"%x+$"; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 v.key = key; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 if a[key] then |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 table.remove(a, a[key]); |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 a[i] = v; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 a[key] = i; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 return key; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 local function archive_iter (a, start, stop, step, limit, when_start, when_end, match_with) |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 local item, when, with; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 local count = 0; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 coroutine.yield(true); -- Ready |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 for i = start, stop, step do |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 item = a[i]; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 when, with = item.when, item.with; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 if when >= when_start and when_end >= when and (not match_with or match_with == with) then |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 coroutine.yield(item.key, item.value(), when, with); |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 count = count + 1; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 if limit and count >= limit then return end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 function archive_store:find(username, query) |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 local a = self.store[username or NULL] or {}; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 local start, stop, step = 1, #a, 1; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 local qstart, qend, qwith = -math.huge, math.huge; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 local limit; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 if query then |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
94 module:log("debug", "query included") |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
95 if query.reverse then |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
96 start, stop, step = stop, start, -1; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
97 if query.before then |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
98 start = a[query.before]; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
99 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
100 elseif query.after then |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
101 start = a[query.after]; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
102 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
103 limit = query.limit; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
104 qstart = query.start or qstart; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
105 qend = query["end"] or qend; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
106 qwith = query.with; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
107 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
108 if not start then return nil, "invalid-key"; end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
109 local iter = coroutine.wrap(archive_iter); |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
110 iter(a, start, stop, step, limit, qstart, qend, qwith); |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
111 return iter; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
112 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
113 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
114 function archive_store:delete(username, query) |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
115 if not query or next(query) == nil then |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
116 self.store[username or NULL] = nil; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
117 return true; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
118 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
119 local old = self.store[username or NULL]; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
120 if not old then return true; end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
121 local qstart = query.start or -math.huge; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
122 local qend = query["end"] or math.huge; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
123 local qwith = query.with; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
124 local new = {}; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
125 self.store[username or NULL] = new; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
126 local t; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
127 for i = 1, #old do |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
128 i = old[i]; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
129 t = i.when; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
130 if not(qstart >= t and qend <= t and (not qwith or i.with == qwith)) then |
9340
368b092bf4bf
mod_storage_memory: Fix saving of remaining items during deletion
Kim Alvefur <zash@zash.se>
parents:
9293
diff
changeset
|
131 self:append(username, i.key, i.value(), t, i.with); |
9293
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
132 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
133 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
134 if #new == 0 then |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
135 self.store[username or NULL] = nil; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
136 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
137 return true; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
138 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
139 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
140 archive_store.purge = _purge_store; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
141 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
142 local stores = { |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
143 keyval = keyval_store; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
144 archive = archive_store; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
145 } |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
146 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
147 local driver = {}; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
148 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
149 function driver:open(store, typ) -- luacheck: ignore 212/self |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
150 local store_mt = stores[typ or "keyval"]; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
151 if store_mt then |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
152 return setmetatable({ store = memory[store] }, store_mt); |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
153 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
154 return nil, "unsupported-store"; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
155 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
156 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
157 if auto_purge_enabled then |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
158 module:hook("resource-unbind", function (event) |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
159 local user_bare_jid = event.session.username.."@"..event.session.host; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
160 if not prosody.bare_sessions[user_bare_jid] then -- User went offline |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
161 module:log("debug", "Clearing store for offline user %s", user_bare_jid); |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
162 local f, s, v; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
163 if auto_purge_stores:empty() then |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
164 f, s, v = pairs(memory); |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
165 else |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
166 f, s, v = auto_purge_stores:items(); |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
167 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
168 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
169 for store_name in f, s, v do |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
170 if memory[store_name] then |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
171 memory[store_name][event.session.username] = nil; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
172 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
173 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
174 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
175 end); |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
176 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
177 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
178 module:provides("storage", driver); |