Mercurial > prosody-modules
comparison mod_storage_memory/mod_storage_memory.lua @ 2656:83fb61fa476e
mod_storage_memory: Serialize data functions that return the data (prevents mutation of stored data)
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Thu, 30 Mar 2017 23:40:29 +0200 |
parents | f4353f959460 |
children | 6c22cb7b0e66 |
comparison
equal
deleted
inserted
replaced
2655:f4353f959460 | 2656:83fb61fa476e |
---|---|
1 local serialize = require "util.serialization".serialize; | |
2 local envload = require "util.envload".envload; | |
3 local st = require "util.stanza"; | |
4 local is_stanza = st.is_stanza or function (s) return getmetatable(s) == st.stanza_mt end | |
5 | |
1 local auto_purge_enabled = module:get_option_boolean("storage_memory_temporary", false); | 6 local auto_purge_enabled = module:get_option_boolean("storage_memory_temporary", false); |
2 local auto_purge_stores = module:get_option_set("storage_memory_temporary_stores", {}); | 7 local auto_purge_stores = module:get_option_set("storage_memory_temporary_stores", {}); |
3 | 8 |
4 local memory = setmetatable({}, { | 9 local memory = setmetatable({}, { |
5 __index = function(t, k) | 10 __index = function(t, k) |
7 t[k] = store; | 12 t[k] = store; |
8 return store; | 13 return store; |
9 end | 14 end |
10 }); | 15 }); |
11 | 16 |
12 local NULL = {}; | 17 local function NULL() return nil end |
13 | 18 |
14 local function _purge_store(self, username) | 19 local function _purge_store(self, username) |
15 self.store[username or NULL] = nil; | 20 self.store[username or NULL] = nil; |
16 return true; | 21 return true; |
17 end | 22 end |
18 | 23 |
19 local keyval_store = {}; | 24 local keyval_store = {}; |
20 keyval_store.__index = keyval_store; | 25 keyval_store.__index = keyval_store; |
21 | 26 |
22 function keyval_store:get(username) | 27 function keyval_store:get(username) |
23 return self.store[username or NULL]; | 28 return (self.store[username or NULL] or NULL)(); |
24 end | 29 end |
25 | 30 |
26 function keyval_store:set(username, data) | 31 function keyval_store:set(username, data) |
27 self.store[username or NULL] = data; | 32 self.store[username or NULL] = envload(serialize(data), "@data", {}); |
28 return true; | 33 return true; |
29 end | 34 end |
30 | 35 |
31 keyval_store.purge = _purge_store; | 36 keyval_store.purge = _purge_store; |
32 | 37 |
34 archive_store.__index = archive_store; | 39 archive_store.__index = archive_store; |
35 | 40 |
36 function archive_store:append(username, key, value, when, with) | 41 function archive_store:append(username, key, value, when, with) |
37 if type(when) ~= "number" then | 42 if type(when) ~= "number" then |
38 when, with, value = value, when, with; | 43 when, with, value = value, when, with; |
44 end | |
45 if is_stanza(value) then | |
46 value = st.preserialize(value); | |
47 value = function () | |
48 return st.deserialize(envload(serialize(data), "@stanza", {})); | |
49 end | |
50 else | |
51 value = envload(serialize(data), "@data", {}); | |
39 end | 52 end |
40 local a = self.store[username or NULL]; | 53 local a = self.store[username or NULL]; |
41 if not a then | 54 if not a then |
42 a = {}; | 55 a = {}; |
43 self.store[username or NULL] = a; | 56 self.store[username or NULL] = a; |
59 coroutine.yield(true); -- Ready | 72 coroutine.yield(true); -- Ready |
60 for i = start, stop, step do | 73 for i = start, stop, step do |
61 item = a[i]; | 74 item = a[i]; |
62 when, with = item.when, item.with; | 75 when, with = item.when, item.with; |
63 if when >= when_start and when_end >= when and (not match_with or match_with == with) then | 76 if when >= when_start and when_end >= when and (not match_with or match_with == with) then |
64 coroutine.yield(item.key, item.value, when, with); | 77 coroutine.yield(item.key, item.value(), when, with); |
65 count = count + 1; | 78 count = count + 1; |
66 if limit and count >= limit then return end | 79 if limit and count >= limit then return end |
67 end | 80 end |
68 end | 81 end |
69 end | 82 end |