Software /
code /
prosody
Annotate
plugins/mod_storage_memory.lua @ 9609:1dfcea523200 0.11
mod_storage_memory: Support the purge driver method
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Wed, 07 Nov 2018 20:30:21 +0100 |
parent | 9535:c1befd1c886d |
child | 9783:4c91afc43639 |
child | 9832:96d9c121547b |
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 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 local keyval_store = {}; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 keyval_store.__index = keyval_store; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 function keyval_store:get(username) |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 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
|
30 end |
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:set(username, data) |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 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
|
34 data = envload("return "..serialize(data), "=(data)", {}); |
9293
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 self.store[username or NULL] = data; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 return true; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 keyval_store.purge = _purge_store; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 local archive_store = {}; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 archive_store.__index = archive_store; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 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
|
46 if is_stanza(value) then |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 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
|
48 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
|
49 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
|
50 value = envload("return "..serialize(value), "=(data)", {}); |
9293
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 local a = self.store[username or NULL]; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 if not a then |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 a = {}; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 self.store[username or NULL] = a; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 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
|
58 if not key then |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 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
|
60 v.key = key; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 if a[key] then |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 table.remove(a, a[key]); |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 end |
9534
b301f7edf346
mod_storage_memory: Fix overwriting old keys
Kim Alvefur <zash@zash.se>
parents:
9533
diff
changeset
|
65 local i = #a+1; |
9293
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 a[i] = v; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 a[key] = i; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 return 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 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 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
|
72 local item, when, with; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 local count = 0; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 coroutine.yield(true); -- Ready |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 for i = start, stop, step do |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 item = a[i]; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 when, with = item.when, item.with; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 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
|
79 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
|
80 count = count + 1; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 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
|
82 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 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 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 function archive_store:find(username, query) |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 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
|
88 local start, stop, step = 1, #a, 1; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 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
|
90 local limit; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 if query then |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 module:log("debug", "query included") |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 if query.reverse then |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
94 start, stop, step = stop, start, -1; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
95 if query.before then |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
96 start = a[query.before]; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
97 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
98 elseif query.after then |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
99 start = a[query.after]; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
100 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
101 limit = query.limit; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
102 qstart = query.start or qstart; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
103 qend = query["end"] or qend; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
104 qwith = query.with; |
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 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
|
107 local iter = coroutine.wrap(archive_iter); |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
108 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
|
109 return iter; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
110 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
111 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
112 function archive_store:delete(username, query) |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
113 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
|
114 self.store[username or NULL] = nil; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
115 return true; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
116 end |
9535
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
117 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
|
118 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
|
119 -- Store is empty |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
120 return 0; |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
121 end |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
122 items = array(items); |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
123 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
|
124 if query then |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
125 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
|
126 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
|
127 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
|
128 end); |
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 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
|
131 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
|
132 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
|
133 end); |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
134 end |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
135 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
|
136 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
|
137 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
|
138 end); |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
139 end |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
140 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
|
141 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
|
142 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
|
143 end); |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
144 end |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
145 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
|
146 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
|
147 -- 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
|
148 -- 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
|
149 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
|
150 items[i] = nil; |
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 else |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
153 -- 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
|
154 -- 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
|
155 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
|
156 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
|
157 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
|
158 end |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
159 end |
9293
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
160 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
161 end |
9535
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
162 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
|
163 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
|
164 return 0; -- No changes, skip write |
9293
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
165 end |
9535
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
166 setmetatable(items, nil); |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
167 |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
168 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
|
169 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
|
170 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
|
171 items[k] = nil; |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
172 end |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
173 end |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
174 |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
175 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
|
176 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
|
177 end |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
178 end |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
179 |
c1befd1c886d
mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents:
9534
diff
changeset
|
180 return count; |
9293
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
181 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
182 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
183 archive_store.purge = _purge_store; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
184 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
185 local stores = { |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
186 keyval = keyval_store; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
187 archive = archive_store; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
188 } |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
189 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
190 local driver = {}; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
191 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
192 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
|
193 local store_mt = stores[typ or "keyval"]; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
194 if store_mt then |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
195 return setmetatable({ store = memory[store] }, store_mt); |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
196 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
197 return nil, "unsupported-store"; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
198 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
199 |
9609
1dfcea523200
mod_storage_memory: Support the purge driver method
Kim Alvefur <zash@zash.se>
parents:
9535
diff
changeset
|
200 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
|
201 for _, store in pairs(memory) do |
1dfcea523200
mod_storage_memory: Support the purge driver method
Kim Alvefur <zash@zash.se>
parents:
9535
diff
changeset
|
202 store[user] = nil; |
1dfcea523200
mod_storage_memory: Support the purge driver method
Kim Alvefur <zash@zash.se>
parents:
9535
diff
changeset
|
203 end |
1dfcea523200
mod_storage_memory: Support the purge driver method
Kim Alvefur <zash@zash.se>
parents:
9535
diff
changeset
|
204 end |
1dfcea523200
mod_storage_memory: Support the purge driver method
Kim Alvefur <zash@zash.se>
parents:
9535
diff
changeset
|
205 |
9293
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
206 if auto_purge_enabled then |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
207 module:hook("resource-unbind", function (event) |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
208 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
|
209 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
|
210 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
|
211 local f, s, v; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
212 if auto_purge_stores:empty() then |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
213 f, s, v = pairs(memory); |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
214 else |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
215 f, s, v = auto_purge_stores:items(); |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
216 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
217 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
218 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
|
219 if memory[store_name] then |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
220 memory[store_name][event.session.username] = nil; |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
221 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
222 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
223 end |
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 end |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
226 |
0a751835627d
mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
227 module:provides("storage", driver); |