Mercurial > prosody-modules
annotate mod_storage_memory/mod_storage_memory.lua @ 2637:39c39844cd4c
mod_list_active: Inverse of mod_list_inactive (closes #705)
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Wed, 22 Mar 2017 14:53:15 +0100 |
parents | 8b8cab2eb7fc |
children | 9e5015555fff |
rev | line source |
---|---|
2620
8b8cab2eb7fc
mod_storage_memory: Add support for clearing a user's data when they log out
Matthew Wild <mwild1@gmail.com>
parents:
2619
diff
changeset
|
1 local auto_purge_enabled = module:get_option_boolean("storage_memory_temporary", false); |
8b8cab2eb7fc
mod_storage_memory: Add support for clearing a user's data when they log out
Matthew Wild <mwild1@gmail.com>
parents:
2619
diff
changeset
|
2 local auto_purge_stores = module:get_option_set("storage_memory_temporary_stores", {}); |
1259
fa7e402fcdc1
mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 |
fa7e402fcdc1
mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 local memory = setmetatable({}, { |
fa7e402fcdc1
mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 __index = function(t, k) |
fa7e402fcdc1
mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 local store = module:shared(k) |
fa7e402fcdc1
mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 t[k] = store; |
fa7e402fcdc1
mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 return store; |
fa7e402fcdc1
mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 end |
fa7e402fcdc1
mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 }); |
fa7e402fcdc1
mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 |
2097
4454f124465a
mod_storage_memory: Support for empty username stores
Matthew Wild <mwild1@gmail.com>
parents:
1760
diff
changeset
|
12 local NULL = {}; |
4454f124465a
mod_storage_memory: Support for empty username stores
Matthew Wild <mwild1@gmail.com>
parents:
1760
diff
changeset
|
13 |
2619
1e4bbff0a6fd
mod_storage_memory: Add :purge() method to all store types
Matthew Wild <mwild1@gmail.com>
parents:
2172
diff
changeset
|
14 local function _purge_store(self, username) |
1e4bbff0a6fd
mod_storage_memory: Add :purge() method to all store types
Matthew Wild <mwild1@gmail.com>
parents:
2172
diff
changeset
|
15 self.store[username or NULL] = nil; |
1e4bbff0a6fd
mod_storage_memory: Add :purge() method to all store types
Matthew Wild <mwild1@gmail.com>
parents:
2172
diff
changeset
|
16 return true; |
1e4bbff0a6fd
mod_storage_memory: Add :purge() method to all store types
Matthew Wild <mwild1@gmail.com>
parents:
2172
diff
changeset
|
17 end |
1e4bbff0a6fd
mod_storage_memory: Add :purge() method to all store types
Matthew Wild <mwild1@gmail.com>
parents:
2172
diff
changeset
|
18 |
1259
fa7e402fcdc1
mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 local keyval_store = {}; |
fa7e402fcdc1
mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 keyval_store.__index = keyval_store; |
fa7e402fcdc1
mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 |
fa7e402fcdc1
mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 function keyval_store:get(username) |
2097
4454f124465a
mod_storage_memory: Support for empty username stores
Matthew Wild <mwild1@gmail.com>
parents:
1760
diff
changeset
|
23 return self.store[username or NULL]; |
1259
fa7e402fcdc1
mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 end |
fa7e402fcdc1
mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 |
fa7e402fcdc1
mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 function keyval_store:set(username, data) |
2097
4454f124465a
mod_storage_memory: Support for empty username stores
Matthew Wild <mwild1@gmail.com>
parents:
1760
diff
changeset
|
27 self.store[username or NULL] = data; |
1259
fa7e402fcdc1
mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 return true; |
fa7e402fcdc1
mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 end |
fa7e402fcdc1
mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 |
2619
1e4bbff0a6fd
mod_storage_memory: Add :purge() method to all store types
Matthew Wild <mwild1@gmail.com>
parents:
2172
diff
changeset
|
31 keyval_store.purge = _purge_store; |
1e4bbff0a6fd
mod_storage_memory: Add :purge() method to all store types
Matthew Wild <mwild1@gmail.com>
parents:
2172
diff
changeset
|
32 |
1607
8b997d9cf09e
mod_storage_memory: Add support for map stores
Kim Alvefur <zash@zash.se>
parents:
1259
diff
changeset
|
33 local map_store = {}; |
8b997d9cf09e
mod_storage_memory: Add support for map stores
Kim Alvefur <zash@zash.se>
parents:
1259
diff
changeset
|
34 map_store.__index = map_store; |
8b997d9cf09e
mod_storage_memory: Add support for map stores
Kim Alvefur <zash@zash.se>
parents:
1259
diff
changeset
|
35 |
8b997d9cf09e
mod_storage_memory: Add support for map stores
Kim Alvefur <zash@zash.se>
parents:
1259
diff
changeset
|
36 function map_store:get(username, key) |
2097
4454f124465a
mod_storage_memory: Support for empty username stores
Matthew Wild <mwild1@gmail.com>
parents:
1760
diff
changeset
|
37 local userstore = self.store[username or NULL]; |
1607
8b997d9cf09e
mod_storage_memory: Add support for map stores
Kim Alvefur <zash@zash.se>
parents:
1259
diff
changeset
|
38 if type(userstore) == "table" then |
8b997d9cf09e
mod_storage_memory: Add support for map stores
Kim Alvefur <zash@zash.se>
parents:
1259
diff
changeset
|
39 return userstore[key]; |
8b997d9cf09e
mod_storage_memory: Add support for map stores
Kim Alvefur <zash@zash.se>
parents:
1259
diff
changeset
|
40 end |
8b997d9cf09e
mod_storage_memory: Add support for map stores
Kim Alvefur <zash@zash.se>
parents:
1259
diff
changeset
|
41 end |
8b997d9cf09e
mod_storage_memory: Add support for map stores
Kim Alvefur <zash@zash.se>
parents:
1259
diff
changeset
|
42 |
8b997d9cf09e
mod_storage_memory: Add support for map stores
Kim Alvefur <zash@zash.se>
parents:
1259
diff
changeset
|
43 function map_store:set(username, key, data) |
2097
4454f124465a
mod_storage_memory: Support for empty username stores
Matthew Wild <mwild1@gmail.com>
parents:
1760
diff
changeset
|
44 local userstore = self.store[username or NULL]; |
1607
8b997d9cf09e
mod_storage_memory: Add support for map stores
Kim Alvefur <zash@zash.se>
parents:
1259
diff
changeset
|
45 if userstore == nil then |
8b997d9cf09e
mod_storage_memory: Add support for map stores
Kim Alvefur <zash@zash.se>
parents:
1259
diff
changeset
|
46 userstore = {}; |
2097
4454f124465a
mod_storage_memory: Support for empty username stores
Matthew Wild <mwild1@gmail.com>
parents:
1760
diff
changeset
|
47 self.store[username or NULL] = userstore; |
1607
8b997d9cf09e
mod_storage_memory: Add support for map stores
Kim Alvefur <zash@zash.se>
parents:
1259
diff
changeset
|
48 end |
8b997d9cf09e
mod_storage_memory: Add support for map stores
Kim Alvefur <zash@zash.se>
parents:
1259
diff
changeset
|
49 userstore[key] = data; |
8b997d9cf09e
mod_storage_memory: Add support for map stores
Kim Alvefur <zash@zash.se>
parents:
1259
diff
changeset
|
50 return true; |
8b997d9cf09e
mod_storage_memory: Add support for map stores
Kim Alvefur <zash@zash.se>
parents:
1259
diff
changeset
|
51 end |
8b997d9cf09e
mod_storage_memory: Add support for map stores
Kim Alvefur <zash@zash.se>
parents:
1259
diff
changeset
|
52 |
2171
85d88ed4f2a2
mod_storage_memory: Implement 'add_keys' method of map stores (required for rosters in 0.10+)
Kim Alvefur <zash@zash.se>
parents:
2097
diff
changeset
|
53 map_store.remove = {}; |
85d88ed4f2a2
mod_storage_memory: Implement 'add_keys' method of map stores (required for rosters in 0.10+)
Kim Alvefur <zash@zash.se>
parents:
2097
diff
changeset
|
54 function map_store:set_keys(username, keydatas) |
85d88ed4f2a2
mod_storage_memory: Implement 'add_keys' method of map stores (required for rosters in 0.10+)
Kim Alvefur <zash@zash.se>
parents:
2097
diff
changeset
|
55 local userstore = self.store[username or NULL]; |
85d88ed4f2a2
mod_storage_memory: Implement 'add_keys' method of map stores (required for rosters in 0.10+)
Kim Alvefur <zash@zash.se>
parents:
2097
diff
changeset
|
56 if userstore == nil then |
85d88ed4f2a2
mod_storage_memory: Implement 'add_keys' method of map stores (required for rosters in 0.10+)
Kim Alvefur <zash@zash.se>
parents:
2097
diff
changeset
|
57 userstore = {}; |
85d88ed4f2a2
mod_storage_memory: Implement 'add_keys' method of map stores (required for rosters in 0.10+)
Kim Alvefur <zash@zash.se>
parents:
2097
diff
changeset
|
58 self.store[username or NULL] = userstore; |
85d88ed4f2a2
mod_storage_memory: Implement 'add_keys' method of map stores (required for rosters in 0.10+)
Kim Alvefur <zash@zash.se>
parents:
2097
diff
changeset
|
59 end |
85d88ed4f2a2
mod_storage_memory: Implement 'add_keys' method of map stores (required for rosters in 0.10+)
Kim Alvefur <zash@zash.se>
parents:
2097
diff
changeset
|
60 for k,v in pairs(keydatas) do |
85d88ed4f2a2
mod_storage_memory: Implement 'add_keys' method of map stores (required for rosters in 0.10+)
Kim Alvefur <zash@zash.se>
parents:
2097
diff
changeset
|
61 if v == self.remove then v = nil; end |
2172
a347b71adb07
mod_storage_memory: Fix copypaste error
Kim Alvefur <zash@zash.se>
parents:
2171
diff
changeset
|
62 userstore[k] = v; |
2171
85d88ed4f2a2
mod_storage_memory: Implement 'add_keys' method of map stores (required for rosters in 0.10+)
Kim Alvefur <zash@zash.se>
parents:
2097
diff
changeset
|
63 end |
85d88ed4f2a2
mod_storage_memory: Implement 'add_keys' method of map stores (required for rosters in 0.10+)
Kim Alvefur <zash@zash.se>
parents:
2097
diff
changeset
|
64 return true; |
85d88ed4f2a2
mod_storage_memory: Implement 'add_keys' method of map stores (required for rosters in 0.10+)
Kim Alvefur <zash@zash.se>
parents:
2097
diff
changeset
|
65 end |
85d88ed4f2a2
mod_storage_memory: Implement 'add_keys' method of map stores (required for rosters in 0.10+)
Kim Alvefur <zash@zash.se>
parents:
2097
diff
changeset
|
66 |
2619
1e4bbff0a6fd
mod_storage_memory: Add :purge() method to all store types
Matthew Wild <mwild1@gmail.com>
parents:
2172
diff
changeset
|
67 map_store.purge = _purge_store; |
1e4bbff0a6fd
mod_storage_memory: Add :purge() method to all store types
Matthew Wild <mwild1@gmail.com>
parents:
2172
diff
changeset
|
68 |
1608
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
69 local archive_store = {}; |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
70 archive_store.__index = archive_store; |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
71 |
1753
54c8a0cb2996
mod_storage_(archive-capable): Change order of arguments to :append to be the same as return values from :find iterator (see prosody 41725f3df3cc)
Kim Alvefur <zash@zash.se>
parents:
1608
diff
changeset
|
72 function archive_store:append(username, key, value, when, with) |
54c8a0cb2996
mod_storage_(archive-capable): Change order of arguments to :append to be the same as return values from :find iterator (see prosody 41725f3df3cc)
Kim Alvefur <zash@zash.se>
parents:
1608
diff
changeset
|
73 if type(when) ~= "number" then |
1760
e72f9eac51c8
mod_storage_(various): Order swapping in 54c8a0cb2996 was backwards
Kim Alvefur <zash@zash.se>
parents:
1753
diff
changeset
|
74 when, with, value = value, when, with; |
1753
54c8a0cb2996
mod_storage_(archive-capable): Change order of arguments to :append to be the same as return values from :find iterator (see prosody 41725f3df3cc)
Kim Alvefur <zash@zash.se>
parents:
1608
diff
changeset
|
75 end |
2097
4454f124465a
mod_storage_memory: Support for empty username stores
Matthew Wild <mwild1@gmail.com>
parents:
1760
diff
changeset
|
76 local a = self.store[username or NULL]; |
1608
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
77 if not a then |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
78 a = {}; |
2097
4454f124465a
mod_storage_memory: Support for empty username stores
Matthew Wild <mwild1@gmail.com>
parents:
1760
diff
changeset
|
79 self.store[username or NULL] = a; |
1608
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
80 end |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
81 local i = #a+1; |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
82 local v = { key = key, when = when, with = with, value = value }; |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
83 if not key or a[key] then |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
84 key = tostring(a):match"%x+$"..tostring(v):match"%x+$"; |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
85 v.key = key; |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
86 end |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
87 a[i] = v; |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
88 a[key] = i; |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
89 return true; |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
90 end |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
91 |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
92 local function archive_iter (a, start, stop, step, limit, when_start, when_end, match_with) |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
93 local item, when, with; |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
94 local count = 0; |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
95 coroutine.yield(true); -- Ready |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
96 for i = start, stop, step do |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
97 item = a[i]; |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
98 when, with = item.when, item.with; |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
99 if when >= when_start and when_end >= when and (not match_with or match_with == with) then |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
100 coroutine.yield(item.key, item.value, when, with); |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
101 count = count + 1; |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
102 if limit and count >= limit then return end |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
103 end |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
104 end |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
105 end |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
106 |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
107 function archive_store:find(username, query) |
2097
4454f124465a
mod_storage_memory: Support for empty username stores
Matthew Wild <mwild1@gmail.com>
parents:
1760
diff
changeset
|
108 local a = self.store[username or NULL] or {}; |
1608
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
109 local start, stop, step = 1, #a, 1; |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
110 local qstart, qend, qwith = -math.huge, math.huge; |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
111 local limit; |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
112 if query then |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
113 module:log("debug", "query included") |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
114 if query.reverse then |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
115 start, stop, step = stop, start, -1; |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
116 if query.before then |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
117 start = a[query.before]; |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
118 end |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
119 elseif query.after then |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
120 start = a[query.after]; |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
121 end |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
122 limit = query.limit; |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
123 qstart = query.start or qstart; |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
124 qend = query["end"] or qend; |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
125 end |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
126 if not start then return nil, "invalid-key"; end |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
127 local iter = coroutine.wrap(archive_iter); |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
128 iter(a, start, stop, step, limit, qstart, qend, qwith); |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
129 return iter; |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
130 end |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
131 |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
132 function archive_store:delete(username, query) |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
133 if not query or next(query) == nil then |
2097
4454f124465a
mod_storage_memory: Support for empty username stores
Matthew Wild <mwild1@gmail.com>
parents:
1760
diff
changeset
|
134 self.store[username or NULL] = nil; |
1608
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
135 return true; |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
136 end |
2097
4454f124465a
mod_storage_memory: Support for empty username stores
Matthew Wild <mwild1@gmail.com>
parents:
1760
diff
changeset
|
137 local old = self.store[username or NULL]; |
1608
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
138 if not old then return true; end |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
139 local qstart = query.start or -math.huge; |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
140 local qend = query["end"] or math.huge; |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
141 local qwith = query.with; |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
142 local new = {}; |
2097
4454f124465a
mod_storage_memory: Support for empty username stores
Matthew Wild <mwild1@gmail.com>
parents:
1760
diff
changeset
|
143 self.store[username or NULL] = new; |
1608
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
144 local t; |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
145 for i = 1, #old do |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
146 i = old[i]; |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
147 t = i.when; |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
148 if not(qstart >= t and qend <= t and (not qwith or i.with == qwith)) then |
1753
54c8a0cb2996
mod_storage_(archive-capable): Change order of arguments to :append to be the same as return values from :find iterator (see prosody 41725f3df3cc)
Kim Alvefur <zash@zash.se>
parents:
1608
diff
changeset
|
149 self:append(username, i.key, i.value, t, i.with); |
1608
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
150 end |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
151 end |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
152 if #new == 0 then |
2097
4454f124465a
mod_storage_memory: Support for empty username stores
Matthew Wild <mwild1@gmail.com>
parents:
1760
diff
changeset
|
153 self.store[username or NULL] = nil; |
1608
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
154 end |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
155 return true; |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
156 end |
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
157 |
2619
1e4bbff0a6fd
mod_storage_memory: Add :purge() method to all store types
Matthew Wild <mwild1@gmail.com>
parents:
2172
diff
changeset
|
158 archive_store.purge = _purge_store; |
1e4bbff0a6fd
mod_storage_memory: Add :purge() method to all store types
Matthew Wild <mwild1@gmail.com>
parents:
2172
diff
changeset
|
159 |
1259
fa7e402fcdc1
mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
160 local stores = { |
fa7e402fcdc1
mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
161 keyval = keyval_store; |
1607
8b997d9cf09e
mod_storage_memory: Add support for map stores
Kim Alvefur <zash@zash.se>
parents:
1259
diff
changeset
|
162 map = map_store; |
1608
59fdf4f12343
mod_storage_memory: Add support for archive stores
Kim Alvefur <zash@zash.se>
parents:
1607
diff
changeset
|
163 archive = archive_store; |
1259
fa7e402fcdc1
mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
164 } |
fa7e402fcdc1
mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
165 |
fa7e402fcdc1
mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
166 local driver = {}; |
fa7e402fcdc1
mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
167 |
fa7e402fcdc1
mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
168 function driver:open(store, typ) |
fa7e402fcdc1
mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
169 local store_mt = stores[typ or "keyval"]; |
fa7e402fcdc1
mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
170 if store_mt then |
fa7e402fcdc1
mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
171 return setmetatable({ store = memory[store] }, store_mt); |
fa7e402fcdc1
mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
172 end |
fa7e402fcdc1
mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
173 return nil, "unsupported-store"; |
fa7e402fcdc1
mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
174 end |
fa7e402fcdc1
mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
175 |
2620
8b8cab2eb7fc
mod_storage_memory: Add support for clearing a user's data when they log out
Matthew Wild <mwild1@gmail.com>
parents:
2619
diff
changeset
|
176 if auto_purge_enabled then |
8b8cab2eb7fc
mod_storage_memory: Add support for clearing a user's data when they log out
Matthew Wild <mwild1@gmail.com>
parents:
2619
diff
changeset
|
177 module:hook("resource-unbind", function (event) |
8b8cab2eb7fc
mod_storage_memory: Add support for clearing a user's data when they log out
Matthew Wild <mwild1@gmail.com>
parents:
2619
diff
changeset
|
178 local user_bare_jid = event.session.username.."@"..event.session.host; |
8b8cab2eb7fc
mod_storage_memory: Add support for clearing a user's data when they log out
Matthew Wild <mwild1@gmail.com>
parents:
2619
diff
changeset
|
179 if not prosody.bare_sessions[user_bare_jid] then -- User went offline |
8b8cab2eb7fc
mod_storage_memory: Add support for clearing a user's data when they log out
Matthew Wild <mwild1@gmail.com>
parents:
2619
diff
changeset
|
180 module:log("debug", "Clearing store for offline user %s", user_bare_jid); |
8b8cab2eb7fc
mod_storage_memory: Add support for clearing a user's data when they log out
Matthew Wild <mwild1@gmail.com>
parents:
2619
diff
changeset
|
181 local f, s, v; |
8b8cab2eb7fc
mod_storage_memory: Add support for clearing a user's data when they log out
Matthew Wild <mwild1@gmail.com>
parents:
2619
diff
changeset
|
182 if auto_purge_stores:empty() then |
8b8cab2eb7fc
mod_storage_memory: Add support for clearing a user's data when they log out
Matthew Wild <mwild1@gmail.com>
parents:
2619
diff
changeset
|
183 f, s, v = pairs(memory); |
8b8cab2eb7fc
mod_storage_memory: Add support for clearing a user's data when they log out
Matthew Wild <mwild1@gmail.com>
parents:
2619
diff
changeset
|
184 else |
8b8cab2eb7fc
mod_storage_memory: Add support for clearing a user's data when they log out
Matthew Wild <mwild1@gmail.com>
parents:
2619
diff
changeset
|
185 f, s, v = auto_purge_stores:items(); |
8b8cab2eb7fc
mod_storage_memory: Add support for clearing a user's data when they log out
Matthew Wild <mwild1@gmail.com>
parents:
2619
diff
changeset
|
186 end |
8b8cab2eb7fc
mod_storage_memory: Add support for clearing a user's data when they log out
Matthew Wild <mwild1@gmail.com>
parents:
2619
diff
changeset
|
187 |
8b8cab2eb7fc
mod_storage_memory: Add support for clearing a user's data when they log out
Matthew Wild <mwild1@gmail.com>
parents:
2619
diff
changeset
|
188 for store_name in f, s, v do |
8b8cab2eb7fc
mod_storage_memory: Add support for clearing a user's data when they log out
Matthew Wild <mwild1@gmail.com>
parents:
2619
diff
changeset
|
189 if memory[store_name] then |
8b8cab2eb7fc
mod_storage_memory: Add support for clearing a user's data when they log out
Matthew Wild <mwild1@gmail.com>
parents:
2619
diff
changeset
|
190 memory[store_name][event.session.username] = nil; |
8b8cab2eb7fc
mod_storage_memory: Add support for clearing a user's data when they log out
Matthew Wild <mwild1@gmail.com>
parents:
2619
diff
changeset
|
191 end |
8b8cab2eb7fc
mod_storage_memory: Add support for clearing a user's data when they log out
Matthew Wild <mwild1@gmail.com>
parents:
2619
diff
changeset
|
192 end |
8b8cab2eb7fc
mod_storage_memory: Add support for clearing a user's data when they log out
Matthew Wild <mwild1@gmail.com>
parents:
2619
diff
changeset
|
193 end |
8b8cab2eb7fc
mod_storage_memory: Add support for clearing a user's data when they log out
Matthew Wild <mwild1@gmail.com>
parents:
2619
diff
changeset
|
194 end); |
8b8cab2eb7fc
mod_storage_memory: Add support for clearing a user's data when they log out
Matthew Wild <mwild1@gmail.com>
parents:
2619
diff
changeset
|
195 end |
8b8cab2eb7fc
mod_storage_memory: Add support for clearing a user's data when they log out
Matthew Wild <mwild1@gmail.com>
parents:
2619
diff
changeset
|
196 |
1259
fa7e402fcdc1
mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
197 module:provides("storage", driver); |