Software /
code /
prosody
Annotate
plugins/mod_storage_memory.lua @ 9783:4c91afc43639
mod_storage_memory: Implement :user iteration API
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Mon, 14 Jan 2019 00:17:02 +0100 |
parent | 9609:1dfcea523200 |
child | 9834:a657df70cc31 |
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; |
9535
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
2 local array = require "util.array"; |
9293
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 local envload = require "util.envload".envload; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 local st = require "util.stanza"; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 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
|
6 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 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
|
8 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
|
9 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 local memory = setmetatable({}, { |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 __index = function(t, k) |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 local store = module:shared(k) |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 t[k] = store; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 return store; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 end |
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 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 local function NULL() return nil end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 local function _purge_store(self, username) |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 self.store[username or NULL] = nil; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 return true; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 |
9783
4c91afc43639
mod_storage_memory: Implement :user iteration API
Kim Alvefur <zash@zash.se>
parents:
9609
diff
changeset
|
25 local function _users(self) |
4c91afc43639
mod_storage_memory: Implement :user iteration API
Kim Alvefur <zash@zash.se>
parents:
9609
diff
changeset
|
26 return next, self.store, nil; |
4c91afc43639
mod_storage_memory: Implement :user iteration API
Kim Alvefur <zash@zash.se>
parents:
9609
diff
changeset
|
27 end |
4c91afc43639
mod_storage_memory: Implement :user iteration API
Kim Alvefur <zash@zash.se>
parents:
9609
diff
changeset
|
28 |
9293
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 local keyval_store = {}; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 keyval_store.__index = keyval_store; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 function keyval_store:get(username) |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 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
|
34 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 function keyval_store:set(username, data) |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 if data ~= nil then |
9468
bd5e4485a245
mod_storage_memory: Switch from '@' prefix to '=' for chunks, '@' is used to indicate a source file name only
Matthew Wild <mwild1@gmail.com>
parents:
9340
diff
changeset
|
38 data = envload("return "..serialize(data), "=(data)", {}); |
9293
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 self.store[username or NULL] = data; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 return true; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 end |
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 keyval_store.purge = _purge_store; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 |
9783
4c91afc43639
mod_storage_memory: Implement :user iteration API
Kim Alvefur <zash@zash.se>
parents:
9609
diff
changeset
|
46 keyval_store.users = _users; |
4c91afc43639
mod_storage_memory: Implement :user iteration API
Kim Alvefur <zash@zash.se>
parents:
9609
diff
changeset
|
47 |
9293
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 local archive_store = {}; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 archive_store.__index = archive_store; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 |
9783
4c91afc43639
mod_storage_memory: Implement :user iteration API
Kim Alvefur <zash@zash.se>
parents:
9609
diff
changeset
|
51 archive_store.users = _users; |
4c91afc43639
mod_storage_memory: Implement :user iteration API
Kim Alvefur <zash@zash.se>
parents:
9609
diff
changeset
|
52 |
9293
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 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
|
54 if is_stanza(value) then |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 value = st.preserialize(value); |
9468
bd5e4485a245
mod_storage_memory: Switch from '@' prefix to '=' for chunks, '@' is used to indicate a source file name only
Matthew Wild <mwild1@gmail.com>
parents:
9340
diff
changeset
|
56 value = envload("return xml"..serialize(value), "=(stanza)", { xml = st.deserialize }) |
9293
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 else |
9468
bd5e4485a245
mod_storage_memory: Switch from '@' prefix to '=' for chunks, '@' is used to indicate a source file name only
Matthew Wild <mwild1@gmail.com>
parents:
9340
diff
changeset
|
58 value = envload("return "..serialize(value), "=(data)", {}); |
9293
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 local a = self.store[username or NULL]; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 if not a then |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 a = {}; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 self.store[username or NULL] = a; |
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 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
|
66 if not key then |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 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
|
68 v.key = key; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 if a[key] then |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 table.remove(a, a[key]); |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 end |
9534
b301f7edf346
mod_storage_memory: Fix overwriting old keys
Kim Alvefur <zash@zash.se>
parents:
9533
diff
changeset
|
73 local i = #a+1; |
9293
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 a[i] = v; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 a[key] = i; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 return key; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 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
|
80 local item, when, with; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 local count = 0; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 coroutine.yield(true); -- Ready |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 for i = start, stop, step do |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 item = a[i]; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 when, with = item.when, item.with; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 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
|
87 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
|
88 count = count + 1; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 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
|
90 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
94 function archive_store:find(username, query) |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
95 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
|
96 local start, stop, step = 1, #a, 1; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
97 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
|
98 local limit; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
99 if query then |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
100 module:log("debug", "query included") |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
101 if query.reverse then |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
102 start, stop, step = stop, start, -1; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
103 if query.before then |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
104 start = a[query.before]; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
105 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
106 elseif query.after then |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
107 start = a[query.after]; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
108 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
109 limit = query.limit; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
110 qstart = query.start or qstart; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
111 qend = query["end"] or qend; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
112 qwith = query.with; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
113 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
114 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
|
115 local iter = coroutine.wrap(archive_iter); |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
116 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
|
117 return iter; |
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 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
120 function archive_store:delete(username, query) |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
121 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
|
122 self.store[username or NULL] = nil; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
123 return true; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
124 end |
9535
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
125 local items = self.store[username or NULL]; |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
126 if not items then |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
127 -- Store is empty |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
128 return 0; |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
129 end |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
130 items = array(items); |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
131 local count_before = #items; |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
132 if query then |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
133 if query.key then |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
134 items:filter(function (item) |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
135 return item.key ~= query.key; |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
136 end); |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
137 end |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
138 if query.with then |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
139 items:filter(function (item) |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
140 return item.with ~= query.with; |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
141 end); |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
142 end |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
143 if query.start then |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
144 items:filter(function (item) |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
145 return item.when < query.start; |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
146 end); |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
147 end |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
148 if query["end"] then |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
149 items:filter(function (item) |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
150 return item.when > query["end"]; |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
151 end); |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
152 end |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
153 if query.truncate and #items > query.truncate then |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
154 if query.reverse then |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
155 -- Before: { 1, 2, 3, 4, 5, } |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
156 -- After: { 1, 2, 3 } |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
157 for i = #items, query.truncate + 1, -1 do |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
158 items[i] = nil; |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
159 end |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
160 else |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
161 -- Before: { 1, 2, 3, 4, 5, } |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
162 -- After: { 3, 4, 5 } |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
163 local offset = #items - query.truncate; |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
164 for i = 1, #items do |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
165 items[i] = items[i+offset]; |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
166 end |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
167 end |
9293
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
168 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
169 end |
9535
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
170 local count = count_before - #items; |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
171 if count == 0 then |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
172 return 0; -- No changes, skip write |
9293
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
173 end |
9535
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
174 setmetatable(items, nil); |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
175 |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
176 do -- re-index by key |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
177 for k in pairs(items) do |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
178 if type(k) == "string" then |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
179 items[k] = nil; |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
180 end |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
181 end |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
182 |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
183 for i = 1, #items do |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
184 items[ items[i].key ] = i; |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
185 end |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
186 end |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
187 |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
188 return count; |
9293
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
189 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
190 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
191 archive_store.purge = _purge_store; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
192 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
193 local stores = { |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
194 keyval = keyval_store; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
195 archive = archive_store; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
196 } |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
197 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
198 local driver = {}; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
199 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
200 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
|
201 local store_mt = stores[typ or "keyval"]; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
202 if store_mt then |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
203 return setmetatable({ store = memory[store] }, store_mt); |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
204 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
205 return nil, "unsupported-store"; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
206 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
207 |
9609
1dfcea523200
mod_storage_memory: Support the purge driver method
Kim Alvefur <zash@zash.se>
parents:
9535
diff
changeset
|
208 function driver:purge(user) -- luacheck: ignore 212/self |
1dfcea523200
mod_storage_memory: Support the purge driver method
Kim Alvefur <zash@zash.se>
parents:
9535
diff
changeset
|
209 for _, store in pairs(memory) do |
1dfcea523200
mod_storage_memory: Support the purge driver method
Kim Alvefur <zash@zash.se>
parents:
9535
diff
changeset
|
210 store[user] = nil; |
1dfcea523200
mod_storage_memory: Support the purge driver method
Kim Alvefur <zash@zash.se>
parents:
9535
diff
changeset
|
211 end |
1dfcea523200
mod_storage_memory: Support the purge driver method
Kim Alvefur <zash@zash.se>
parents:
9535
diff
changeset
|
212 end |
1dfcea523200
mod_storage_memory: Support the purge driver method
Kim Alvefur <zash@zash.se>
parents:
9535
diff
changeset
|
213 |
9293
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
214 if auto_purge_enabled then |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
215 module:hook("resource-unbind", function (event) |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
216 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
|
217 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
|
218 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
|
219 local f, s, v; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
220 if auto_purge_stores:empty() then |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
221 f, s, v = pairs(memory); |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
222 else |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
223 f, s, v = auto_purge_stores:items(); |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
224 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
225 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
226 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
|
227 if memory[store_name] then |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
228 memory[store_name][event.session.username] = nil; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
229 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
230 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
231 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
232 end); |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
233 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
234 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
235 module:provides("storage", driver); |