Annotate

plugins/mod_storage_memory.lua @ 12032:3db09eb4c43b

util.format: Ensure sanitation of strings passed to wrong format Ie. log("debug", "%d", "\1\2\3") should not result in garbage. Also optimizing for the common case of ASCII string passed to %s and early returns everywhere. Returning nil from a gsub callback keeps the original substring.
author Kim Alvefur <zash@zash.se>
date Sat, 11 Dec 2021 13:30:34 +0100
parent 11277:1256f32f21b6
child 12977:74b9e05af71e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
9838
40ed04014b97 mod_storage_memory: Generate ID using standard util (fixes #1326)
Kim Alvefur <zash@zash.se>
parents: 9832
diff changeset
6 local new_id = require "util.id".medium;
11277
1256f32f21b6 mod_storage_memory: Support query for set of IDs
Kim Alvefur <zash@zash.se>
parents: 11274
diff changeset
7 local set = require "util.set";
9293
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 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
10 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
11
9887
69f810014853 mod_storage_memory: Add support for archive item limits
Kim Alvefur <zash@zash.se>
parents: 9882
diff changeset
12 local archive_item_limit = module:get_option_number("storage_archive_item_limit", 1000);
69f810014853 mod_storage_memory: Add support for archive item limits
Kim Alvefur <zash@zash.se>
parents: 9882
diff changeset
13
9293
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local memory = setmetatable({}, {
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 __index = function(t, k)
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 local store = module:shared(k)
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 t[k] = store;
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 return store;
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 end
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 });
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 local function NULL() return nil 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 function _purge_store(self, username)
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 self.store[username or NULL] = nil;
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 return true;
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 end
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28
9881
68faa0c1a99c mod_storage_memory: Implement :user iteration API
Kim Alvefur <zash@zash.se>
parents: 9838
diff changeset
29 local function _users(self)
68faa0c1a99c mod_storage_memory: Implement :user iteration API
Kim Alvefur <zash@zash.se>
parents: 9838
diff changeset
30 return next, self.store, nil;
68faa0c1a99c mod_storage_memory: Implement :user iteration API
Kim Alvefur <zash@zash.se>
parents: 9838
diff changeset
31 end
68faa0c1a99c mod_storage_memory: Implement :user iteration API
Kim Alvefur <zash@zash.se>
parents: 9838
diff changeset
32
9293
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 local keyval_store = {};
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 keyval_store.__index = keyval_store;
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:get(username)
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 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
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 function keyval_store:set(username, data)
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 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
42 data = envload("return "..serialize(data), "=(data)", {});
9293
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 end
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 self.store[username or NULL] = data;
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 return true;
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 end
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 keyval_store.purge = _purge_store;
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49
9881
68faa0c1a99c mod_storage_memory: Implement :user iteration API
Kim Alvefur <zash@zash.se>
parents: 9838
diff changeset
50 keyval_store.users = _users;
68faa0c1a99c mod_storage_memory: Implement :user iteration API
Kim Alvefur <zash@zash.se>
parents: 9838
diff changeset
51
9293
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 local archive_store = {};
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 archive_store.__index = archive_store;
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54
9881
68faa0c1a99c mod_storage_memory: Implement :user iteration API
Kim Alvefur <zash@zash.se>
parents: 9838
diff changeset
55 archive_store.users = _users;
68faa0c1a99c mod_storage_memory: Implement :user iteration API
Kim Alvefur <zash@zash.se>
parents: 9838
diff changeset
56
9887
69f810014853 mod_storage_memory: Add support for archive item limits
Kim Alvefur <zash@zash.se>
parents: 9882
diff changeset
57 archive_store.caps = {
69f810014853 mod_storage_memory: Add support for archive item limits
Kim Alvefur <zash@zash.se>
parents: 9882
diff changeset
58 total = true;
69f810014853 mod_storage_memory: Add support for archive item limits
Kim Alvefur <zash@zash.se>
parents: 9882
diff changeset
59 quota = archive_item_limit;
69f810014853 mod_storage_memory: Add support for archive item limits
Kim Alvefur <zash@zash.se>
parents: 9882
diff changeset
60 truncate = true;
11274
ecbfde402364 mod_storage_memory: Support full ID range queries
Kim Alvefur <zash@zash.se>
parents: 10926
diff changeset
61 full_id_range = true;
11277
1256f32f21b6 mod_storage_memory: Support query for set of IDs
Kim Alvefur <zash@zash.se>
parents: 11274
diff changeset
62 ids = true;
9887
69f810014853 mod_storage_memory: Add support for archive item limits
Kim Alvefur <zash@zash.se>
parents: 9882
diff changeset
63 };
69f810014853 mod_storage_memory: Add support for archive item limits
Kim Alvefur <zash@zash.se>
parents: 9882
diff changeset
64
9293
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 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
66 if is_stanza(value) then
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 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
68 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
69 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
70 value = envload("return "..serialize(value), "=(data)", {});
9293
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 local a = self.store[username or NULL];
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 if not a then
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 a = {};
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 self.store[username or NULL] = a;
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 end
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 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
78 if not key then
9838
40ed04014b97 mod_storage_memory: Generate ID using standard util (fixes #1326)
Kim Alvefur <zash@zash.se>
parents: 9832
diff changeset
79 key = new_id();
9293
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 v.key = key;
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 end
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 if a[key] then
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 table.remove(a, a[key]);
9887
69f810014853 mod_storage_memory: Add support for archive item limits
Kim Alvefur <zash@zash.se>
parents: 9882
diff changeset
84 elseif #a >= archive_item_limit then
69f810014853 mod_storage_memory: Add support for archive item limits
Kim Alvefur <zash@zash.se>
parents: 9882
diff changeset
85 return nil, "quota-limit";
9293
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 end
9534
b301f7edf346 mod_storage_memory: Fix overwriting old keys
Kim Alvefur <zash@zash.se>
parents: 9533
diff changeset
87 local i = #a+1;
9293
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 a[i] = v;
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 a[key] = i;
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 return key;
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
9832
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
93 function archive_store:find(username, query)
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
94 local items = self.store[username or NULL];
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
95 if not items then
9997
7c4631d7b6fb mod_storage_internal,memory: Only return total count if requested
Kim Alvefur <zash@zash.se>
parents: 9906
diff changeset
96 if query then
10023
f6959fe81a1d mod_storage_memory: Return correct error even if no archive data available
Kim Alvefur <zash@zash.se>
parents: 10019
diff changeset
97 if query.before or query.after then
f6959fe81a1d mod_storage_memory: Return correct error even if no archive data available
Kim Alvefur <zash@zash.se>
parents: 10019
diff changeset
98 return nil, "item-not-found";
f6959fe81a1d mod_storage_memory: Return correct error even if no archive data available
Kim Alvefur <zash@zash.se>
parents: 10019
diff changeset
99 end
9997
7c4631d7b6fb mod_storage_internal,memory: Only return total count if requested
Kim Alvefur <zash@zash.se>
parents: 9906
diff changeset
100 if query.total then
7c4631d7b6fb mod_storage_internal,memory: Only return total count if requested
Kim Alvefur <zash@zash.se>
parents: 9906
diff changeset
101 return function () end, 0;
7c4631d7b6fb mod_storage_internal,memory: Only return total count if requested
Kim Alvefur <zash@zash.se>
parents: 9906
diff changeset
102 end
7c4631d7b6fb mod_storage_internal,memory: Only return total count if requested
Kim Alvefur <zash@zash.se>
parents: 9906
diff changeset
103 end
7c4631d7b6fb mod_storage_internal,memory: Only return total count if requested
Kim Alvefur <zash@zash.se>
parents: 9906
diff changeset
104 return function () end;
9832
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
105 end
9997
7c4631d7b6fb mod_storage_internal,memory: Only return total count if requested
Kim Alvefur <zash@zash.se>
parents: 9906
diff changeset
106 local count = nil;
10926
c55bd98a54f8 mod_storage_internal, mod_storage_memory: Add support for query.before
Matthew Wild <mwild1@gmail.com>
parents: 10839
diff changeset
107 local i, last_key = 0;
9832
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
108 if query then
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
109 items = array():append(items);
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
110 if query.key then
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
111 items:filter(function (item)
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
112 return item.key == query.key;
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
113 end);
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
114 end
11277
1256f32f21b6 mod_storage_memory: Support query for set of IDs
Kim Alvefur <zash@zash.se>
parents: 11274
diff changeset
115 if query.ids then
1256f32f21b6 mod_storage_memory: Support query for set of IDs
Kim Alvefur <zash@zash.se>
parents: 11274
diff changeset
116 local ids = set.new(query.ids);
1256f32f21b6 mod_storage_memory: Support query for set of IDs
Kim Alvefur <zash@zash.se>
parents: 11274
diff changeset
117 items:filter(function (item)
1256f32f21b6 mod_storage_memory: Support query for set of IDs
Kim Alvefur <zash@zash.se>
parents: 11274
diff changeset
118 return ids:contains(item.key);
1256f32f21b6 mod_storage_memory: Support query for set of IDs
Kim Alvefur <zash@zash.se>
parents: 11274
diff changeset
119 end);
1256f32f21b6 mod_storage_memory: Support query for set of IDs
Kim Alvefur <zash@zash.se>
parents: 11274
diff changeset
120 end
9832
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
121 if query.with then
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
122 items:filter(function (item)
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
123 return item.with == query.with;
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
124 end);
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
125 end
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
126 if query.start then
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
127 items:filter(function (item)
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
128 return item.when >= query.start;
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
129 end);
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
130 end
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
131 if query["end"] then
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
132 items:filter(function (item)
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
133 return item.when <= query["end"];
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
134 end);
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
135 end
9997
7c4631d7b6fb mod_storage_internal,memory: Only return total count if requested
Kim Alvefur <zash@zash.se>
parents: 9906
diff changeset
136 if query.total then
7c4631d7b6fb mod_storage_internal,memory: Only return total count if requested
Kim Alvefur <zash@zash.se>
parents: 9906
diff changeset
137 count = #items;
7c4631d7b6fb mod_storage_internal,memory: Only return total count if requested
Kim Alvefur <zash@zash.se>
parents: 9906
diff changeset
138 end
9832
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
139 if query.reverse then
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
140 items:reverse();
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
141 if query.before then
10019
c30c81176752 mod_storage_memory: Return error if 'before' or 'after' are not found (partial fix for #1325)
Kim Alvefur <zash@zash.se>
parents: 9997
diff changeset
142 local found = false;
9997
7c4631d7b6fb mod_storage_internal,memory: Only return total count if requested
Kim Alvefur <zash@zash.se>
parents: 9906
diff changeset
143 for j = 1, #items do
9832
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
144 if (items[j].key or tostring(j)) == query.before then
10019
c30c81176752 mod_storage_memory: Return error if 'before' or 'after' are not found (partial fix for #1325)
Kim Alvefur <zash@zash.se>
parents: 9997
diff changeset
145 found = true;
9832
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
146 i = j;
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
147 break;
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
148 end
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
149 end
10019
c30c81176752 mod_storage_memory: Return error if 'before' or 'after' are not found (partial fix for #1325)
Kim Alvefur <zash@zash.se>
parents: 9997
diff changeset
150 if not found then
c30c81176752 mod_storage_memory: Return error if 'before' or 'after' are not found (partial fix for #1325)
Kim Alvefur <zash@zash.se>
parents: 9997
diff changeset
151 return nil, "item-not-found";
c30c81176752 mod_storage_memory: Return error if 'before' or 'after' are not found (partial fix for #1325)
Kim Alvefur <zash@zash.se>
parents: 9997
diff changeset
152 end
9832
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
153 end
11274
ecbfde402364 mod_storage_memory: Support full ID range queries
Kim Alvefur <zash@zash.se>
parents: 10926
diff changeset
154 last_key = query.after;
9832
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
155 elseif query.after then
10019
c30c81176752 mod_storage_memory: Return error if 'before' or 'after' are not found (partial fix for #1325)
Kim Alvefur <zash@zash.se>
parents: 9997
diff changeset
156 local found = false;
9997
7c4631d7b6fb mod_storage_internal,memory: Only return total count if requested
Kim Alvefur <zash@zash.se>
parents: 9906
diff changeset
157 for j = 1, #items do
9832
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
158 if (items[j].key or tostring(j)) == query.after then
10019
c30c81176752 mod_storage_memory: Return error if 'before' or 'after' are not found (partial fix for #1325)
Kim Alvefur <zash@zash.se>
parents: 9997
diff changeset
159 found = true;
9832
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
160 i = j;
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
161 break;
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
162 end
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
163 end
10019
c30c81176752 mod_storage_memory: Return error if 'before' or 'after' are not found (partial fix for #1325)
Kim Alvefur <zash@zash.se>
parents: 9997
diff changeset
164 if not found then
c30c81176752 mod_storage_memory: Return error if 'before' or 'after' are not found (partial fix for #1325)
Kim Alvefur <zash@zash.se>
parents: 9997
diff changeset
165 return nil, "item-not-found";
c30c81176752 mod_storage_memory: Return error if 'before' or 'after' are not found (partial fix for #1325)
Kim Alvefur <zash@zash.se>
parents: 9997
diff changeset
166 end
11274
ecbfde402364 mod_storage_memory: Support full ID range queries
Kim Alvefur <zash@zash.se>
parents: 10926
diff changeset
167 last_key = query.before;
ecbfde402364 mod_storage_memory: Support full ID range queries
Kim Alvefur <zash@zash.se>
parents: 10926
diff changeset
168 elseif query.before then
ecbfde402364 mod_storage_memory: Support full ID range queries
Kim Alvefur <zash@zash.se>
parents: 10926
diff changeset
169 last_key = query.before;
9832
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
170 end
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
171 if query.limit and #items - i > query.limit then
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
172 items[i+query.limit+1] = nil;
9293
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
9832
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
175 return function ()
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
176 i = i + 1;
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
177 local item = items[i];
10926
c55bd98a54f8 mod_storage_internal, mod_storage_memory: Add support for query.before
Matthew Wild <mwild1@gmail.com>
parents: 10839
diff changeset
178 if not item or (last_key and item.key == last_key) then return; end
9832
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
179 return item.key, item.value(), item.when, item.with;
96d9c121547b mod_storage_memory: Replace query function with one based on storage_internal (fixes #1322)
Kim Alvefur <zash@zash.se>
parents: 9609
diff changeset
180 end, 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
10839
018acdaf374f mod_storage_memory: Add map store methods to archive store
Kim Alvefur <zash@zash.se>
parents: 10222
diff changeset
183 function archive_store:get(username, wanted_key)
018acdaf374f mod_storage_memory: Add map store methods to archive store
Kim Alvefur <zash@zash.se>
parents: 10222
diff changeset
184 local items = self.store[username or NULL];
018acdaf374f mod_storage_memory: Add map store methods to archive store
Kim Alvefur <zash@zash.se>
parents: 10222
diff changeset
185 if not items then return nil, "item-not-found"; end
018acdaf374f mod_storage_memory: Add map store methods to archive store
Kim Alvefur <zash@zash.se>
parents: 10222
diff changeset
186 local i = items[wanted_key];
018acdaf374f mod_storage_memory: Add map store methods to archive store
Kim Alvefur <zash@zash.se>
parents: 10222
diff changeset
187 if not i then return nil, "item-not-found"; end
018acdaf374f mod_storage_memory: Add map store methods to archive store
Kim Alvefur <zash@zash.se>
parents: 10222
diff changeset
188 local item = items[i];
018acdaf374f mod_storage_memory: Add map store methods to archive store
Kim Alvefur <zash@zash.se>
parents: 10222
diff changeset
189 return item.value(), item.when, item.with;
018acdaf374f mod_storage_memory: Add map store methods to archive store
Kim Alvefur <zash@zash.se>
parents: 10222
diff changeset
190 end
018acdaf374f mod_storage_memory: Add map store methods to archive store
Kim Alvefur <zash@zash.se>
parents: 10222
diff changeset
191
018acdaf374f mod_storage_memory: Add map store methods to archive store
Kim Alvefur <zash@zash.se>
parents: 10222
diff changeset
192 function archive_store:set(username, wanted_key, new_value, new_when, new_with)
018acdaf374f mod_storage_memory: Add map store methods to archive store
Kim Alvefur <zash@zash.se>
parents: 10222
diff changeset
193 local items = self.store[username or NULL];
018acdaf374f mod_storage_memory: Add map store methods to archive store
Kim Alvefur <zash@zash.se>
parents: 10222
diff changeset
194 if not items then return nil, "item-not-found"; end
018acdaf374f mod_storage_memory: Add map store methods to archive store
Kim Alvefur <zash@zash.se>
parents: 10222
diff changeset
195 local i = items[wanted_key];
018acdaf374f mod_storage_memory: Add map store methods to archive store
Kim Alvefur <zash@zash.se>
parents: 10222
diff changeset
196 if not i then return nil, "item-not-found"; end
018acdaf374f mod_storage_memory: Add map store methods to archive store
Kim Alvefur <zash@zash.se>
parents: 10222
diff changeset
197 local item = items[i];
018acdaf374f mod_storage_memory: Add map store methods to archive store
Kim Alvefur <zash@zash.se>
parents: 10222
diff changeset
198
018acdaf374f mod_storage_memory: Add map store methods to archive store
Kim Alvefur <zash@zash.se>
parents: 10222
diff changeset
199 if is_stanza(new_value) then
018acdaf374f mod_storage_memory: Add map store methods to archive store
Kim Alvefur <zash@zash.se>
parents: 10222
diff changeset
200 new_value = st.preserialize(new_value);
018acdaf374f mod_storage_memory: Add map store methods to archive store
Kim Alvefur <zash@zash.se>
parents: 10222
diff changeset
201 item.value = envload("return xml"..serialize(new_value), "=(stanza)", { xml = st.deserialize })
018acdaf374f mod_storage_memory: Add map store methods to archive store
Kim Alvefur <zash@zash.se>
parents: 10222
diff changeset
202 else
018acdaf374f mod_storage_memory: Add map store methods to archive store
Kim Alvefur <zash@zash.se>
parents: 10222
diff changeset
203 item.value = envload("return "..serialize(new_value), "=(data)", {});
018acdaf374f mod_storage_memory: Add map store methods to archive store
Kim Alvefur <zash@zash.se>
parents: 10222
diff changeset
204 end
018acdaf374f mod_storage_memory: Add map store methods to archive store
Kim Alvefur <zash@zash.se>
parents: 10222
diff changeset
205 if new_when then
018acdaf374f mod_storage_memory: Add map store methods to archive store
Kim Alvefur <zash@zash.se>
parents: 10222
diff changeset
206 item.when = new_when;
018acdaf374f mod_storage_memory: Add map store methods to archive store
Kim Alvefur <zash@zash.se>
parents: 10222
diff changeset
207 end
018acdaf374f mod_storage_memory: Add map store methods to archive store
Kim Alvefur <zash@zash.se>
parents: 10222
diff changeset
208 if new_with then
018acdaf374f mod_storage_memory: Add map store methods to archive store
Kim Alvefur <zash@zash.se>
parents: 10222
diff changeset
209 item.with = new_when;
018acdaf374f mod_storage_memory: Add map store methods to archive store
Kim Alvefur <zash@zash.se>
parents: 10222
diff changeset
210 end
018acdaf374f mod_storage_memory: Add map store methods to archive store
Kim Alvefur <zash@zash.se>
parents: 10222
diff changeset
211 return true;
018acdaf374f mod_storage_memory: Add map store methods to archive store
Kim Alvefur <zash@zash.se>
parents: 10222
diff changeset
212 end
018acdaf374f mod_storage_memory: Add map store methods to archive store
Kim Alvefur <zash@zash.se>
parents: 10222
diff changeset
213
9906
d0b58bdd6c86 mod_storage_memory: Fix copypaste mistake
Kim Alvefur <zash@zash.se>
parents: 9905
diff changeset
214 function archive_store:summary(username, query)
9905
6d50efaee148 mod_storage_memory: Implement archive summary API
Kim Alvefur <zash@zash.se>
parents: 9887
diff changeset
215 local iter, err = self:find(username, query)
6d50efaee148 mod_storage_memory: Implement archive summary API
Kim Alvefur <zash@zash.se>
parents: 9887
diff changeset
216 if not iter then return iter, err; end
10220
1e2b444acb72 mod_storage_*: Tweak :summary API to allow future expansion with more fields
Kim Alvefur <zash@zash.se>
parents: 10023
diff changeset
217 local counts = {};
10222
51f145094648 mod_storage_*: Also include timestmap of first message in :summary API
Kim Alvefur <zash@zash.se>
parents: 10221
diff changeset
218 local earliest = {};
10221
068692cb9e78 mod_storage_*: Include timestamp of latest message in :summary API
Kim Alvefur <zash@zash.se>
parents: 10220
diff changeset
219 local latest = {};
068692cb9e78 mod_storage_*: Include timestamp of latest message in :summary API
Kim Alvefur <zash@zash.se>
parents: 10220
diff changeset
220 for _, _, when, with in iter do
10220
1e2b444acb72 mod_storage_*: Tweak :summary API to allow future expansion with more fields
Kim Alvefur <zash@zash.se>
parents: 10023
diff changeset
221 counts[with] = (counts[with] or 0) + 1;
10222
51f145094648 mod_storage_*: Also include timestmap of first message in :summary API
Kim Alvefur <zash@zash.se>
parents: 10221
diff changeset
222 if earliest[with] == nil then
51f145094648 mod_storage_*: Also include timestmap of first message in :summary API
Kim Alvefur <zash@zash.se>
parents: 10221
diff changeset
223 earliest[with] = when;
51f145094648 mod_storage_*: Also include timestmap of first message in :summary API
Kim Alvefur <zash@zash.se>
parents: 10221
diff changeset
224 end
10221
068692cb9e78 mod_storage_*: Include timestamp of latest message in :summary API
Kim Alvefur <zash@zash.se>
parents: 10220
diff changeset
225 latest[with] = when;
9905
6d50efaee148 mod_storage_memory: Implement archive summary API
Kim Alvefur <zash@zash.se>
parents: 9887
diff changeset
226 end
10220
1e2b444acb72 mod_storage_*: Tweak :summary API to allow future expansion with more fields
Kim Alvefur <zash@zash.se>
parents: 10023
diff changeset
227 return {
1e2b444acb72 mod_storage_*: Tweak :summary API to allow future expansion with more fields
Kim Alvefur <zash@zash.se>
parents: 10023
diff changeset
228 counts = counts;
10222
51f145094648 mod_storage_*: Also include timestmap of first message in :summary API
Kim Alvefur <zash@zash.se>
parents: 10221
diff changeset
229 earliest = earliest;
10221
068692cb9e78 mod_storage_*: Include timestamp of latest message in :summary API
Kim Alvefur <zash@zash.se>
parents: 10220
diff changeset
230 latest = latest;
10220
1e2b444acb72 mod_storage_*: Tweak :summary API to allow future expansion with more fields
Kim Alvefur <zash@zash.se>
parents: 10023
diff changeset
231 };
9905
6d50efaee148 mod_storage_memory: Implement archive summary API
Kim Alvefur <zash@zash.se>
parents: 9887
diff changeset
232 end
6d50efaee148 mod_storage_memory: Implement archive summary API
Kim Alvefur <zash@zash.se>
parents: 9887
diff changeset
233
9293
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 function archive_store:delete(username, query)
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
236 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
237 self.store[username or NULL] = nil;
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
238 return true;
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
239 end
9535
c1befd1c886d mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents: 9534
diff changeset
240 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
241 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
242 -- Store is empty
c1befd1c886d mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents: 9534
diff changeset
243 return 0;
c1befd1c886d mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents: 9534
diff changeset
244 end
c1befd1c886d mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents: 9534
diff changeset
245 items = array(items);
c1befd1c886d mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents: 9534
diff changeset
246 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
247 if query then
c1befd1c886d mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents: 9534
diff changeset
248 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
249 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
250 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
251 end);
c1befd1c886d mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents: 9534
diff changeset
252 end
c1befd1c886d mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents: 9534
diff changeset
253 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
254 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
255 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
256 end);
c1befd1c886d mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents: 9534
diff changeset
257 end
c1befd1c886d mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents: 9534
diff changeset
258 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
259 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
260 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
261 end);
c1befd1c886d mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents: 9534
diff changeset
262 end
c1befd1c886d mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents: 9534
diff changeset
263 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
264 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
265 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
266 end);
c1befd1c886d mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents: 9534
diff changeset
267 end
c1befd1c886d mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents: 9534
diff changeset
268 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
269 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
270 -- 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
271 -- 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
272 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
273 items[i] = nil;
c1befd1c886d mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents: 9534
diff changeset
274 end
c1befd1c886d mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents: 9534
diff changeset
275 else
c1befd1c886d mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents: 9534
diff changeset
276 -- 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
277 -- 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
278 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
279 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
280 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
281 end
c1befd1c886d mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents: 9534
diff changeset
282 end
9293
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
283 end
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
284 end
9535
c1befd1c886d mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents: 9534
diff changeset
285 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
286 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
287 return 0; -- No changes, skip write
9293
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
288 end
9535
c1befd1c886d mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents: 9534
diff changeset
289 setmetatable(items, nil);
c1befd1c886d mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents: 9534
diff changeset
290
c1befd1c886d mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents: 9534
diff changeset
291 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
292 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
293 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
294 items[k] = nil;
c1befd1c886d mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents: 9534
diff changeset
295 end
c1befd1c886d mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents: 9534
diff changeset
296 end
c1befd1c886d mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents: 9534
diff changeset
297
c1befd1c886d mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents: 9534
diff changeset
298 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
299 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
300 end
c1befd1c886d mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents: 9534
diff changeset
301 end
c1befd1c886d mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents: 9534
diff changeset
302
c1befd1c886d mod_storage_memory: Adapt archive deletion code from mod_storage_internal
Kim Alvefur <zash@zash.se>
parents: 9534
diff changeset
303 return count;
9293
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
304 end
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
305
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
306 archive_store.purge = _purge_store;
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
307
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
308 local stores = {
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
309 keyval = keyval_store;
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
310 archive = archive_store;
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
311 }
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
312
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
313 local driver = {};
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
314
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
315 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
316 local store_mt = stores[typ or "keyval"];
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
317 if store_mt then
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
318 return setmetatable({ store = memory[store] }, store_mt);
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
319 end
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
320 return nil, "unsupported-store";
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
321 end
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
322
9609
1dfcea523200 mod_storage_memory: Support the purge driver method
Kim Alvefur <zash@zash.se>
parents: 9535
diff changeset
323 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
324 for _, store in pairs(memory) do
1dfcea523200 mod_storage_memory: Support the purge driver method
Kim Alvefur <zash@zash.se>
parents: 9535
diff changeset
325 store[user] = nil;
1dfcea523200 mod_storage_memory: Support the purge driver method
Kim Alvefur <zash@zash.se>
parents: 9535
diff changeset
326 end
1dfcea523200 mod_storage_memory: Support the purge driver method
Kim Alvefur <zash@zash.se>
parents: 9535
diff changeset
327 end
1dfcea523200 mod_storage_memory: Support the purge driver method
Kim Alvefur <zash@zash.se>
parents: 9535
diff changeset
328
9293
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
329 if auto_purge_enabled then
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
330 module:hook("resource-unbind", function (event)
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
331 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
332 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
333 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
334 local f, s, v;
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
335 if auto_purge_stores:empty() then
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
336 f, s, v = pairs(memory);
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
337 else
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
338 f, s, v = auto_purge_stores:items();
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
339 end
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
340
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
341 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
342 if memory[store_name] then
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
343 memory[store_name][event.session.username] = nil;
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
344 end
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
345 end
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
346 end
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
347 end);
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
348 end
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
349
0a751835627d mod_storage_memory: Import from prosody-modules 4c3230c22c18
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
350 module:provides("storage", driver);