Software /
code /
prosody
Annotate
plugins/mod_storage_internal.lua @ 8021:83f18982bcfd
mod_storage_internal: Add basic archive store implementation
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Fri, 31 Mar 2017 17:47:06 +0200 |
parent | 8020:342ce07836de |
child | 8022:05e201468f27 |
rev | line source |
---|---|
4085
7699cef04740
storagemanager, mod_storage_internal: Split out default driver to mod_storage_internal, and greatly simplify storagemanager's error handling and fallback code
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 local datamanager = require "core.storagemanager".olddm; |
8021
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
2 local array = require "util.array"; |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
3 local datetime = require "util.datetime"; |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
4 local st = require "util.stanza"; |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
5 local now = require "util.time".now; |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
6 local id = require "util.id".medium; |
4085
7699cef04740
storagemanager, mod_storage_internal: Split out default driver to mod_storage_internal, and greatly simplify storagemanager's error handling and fallback code
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 |
7699cef04740
storagemanager, mod_storage_internal: Split out default driver to mod_storage_internal, and greatly simplify storagemanager's error handling and fallback code
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 local host = module.host; |
7699cef04740
storagemanager, mod_storage_internal: Split out default driver to mod_storage_internal, and greatly simplify storagemanager's error handling and fallback code
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 |
5122
b41c33dc7c36
mod_storage_*: Don't explicitly set driver name, to ease copying/renaming modules.
Waqas Hussain <waqas20@gmail.com>
parents:
5121
diff
changeset
|
10 local driver = {}; |
4085
7699cef04740
storagemanager, mod_storage_internal: Split out default driver to mod_storage_internal, and greatly simplify storagemanager's error handling and fallback code
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 |
5153
688aeac0012a
mod_storage_internal, datamanager: Add support for iterating over users with data in a store
Kim Alvefur <zash@zash.se>
parents:
5133
diff
changeset
|
12 function driver:open(store, typ) |
8018
9545d0a9401f
mod_storage_internal: Separate driver from keyval implementation
Kim Alvefur <zash@zash.se>
parents:
6283
diff
changeset
|
13 local mt = self[typ or "keyval"] |
9545d0a9401f
mod_storage_internal: Separate driver from keyval implementation
Kim Alvefur <zash@zash.se>
parents:
6283
diff
changeset
|
14 if not mt then |
6283
7cf6d3a2c855
mod_storage_{none,internal,sql}: Return error for unsupported (everything but keyval) store types
Kim Alvefur <zash@zash.se>
parents:
5153
diff
changeset
|
15 return nil, "unsupported-store"; |
7cf6d3a2c855
mod_storage_{none,internal,sql}: Return error for unsupported (everything but keyval) store types
Kim Alvefur <zash@zash.se>
parents:
5153
diff
changeset
|
16 end |
8018
9545d0a9401f
mod_storage_internal: Separate driver from keyval implementation
Kim Alvefur <zash@zash.se>
parents:
6283
diff
changeset
|
17 return setmetatable({ store = store, type = typ }, mt); |
4085
7699cef04740
storagemanager, mod_storage_internal: Split out default driver to mod_storage_internal, and greatly simplify storagemanager's error handling and fallback code
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 end |
8018
9545d0a9401f
mod_storage_internal: Separate driver from keyval implementation
Kim Alvefur <zash@zash.se>
parents:
6283
diff
changeset
|
19 |
8020
342ce07836de
mod_storage_internal: Ignore unused 'self' argument [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8019
diff
changeset
|
20 function driver:stores(username) -- luacheck: ignore 212/self |
8019
925098aad268
mod_storage_internal: Reorder methods
Kim Alvefur <zash@zash.se>
parents:
8018
diff
changeset
|
21 return datamanager.stores(username, host); |
925098aad268
mod_storage_internal: Reorder methods
Kim Alvefur <zash@zash.se>
parents:
8018
diff
changeset
|
22 end |
925098aad268
mod_storage_internal: Reorder methods
Kim Alvefur <zash@zash.se>
parents:
8018
diff
changeset
|
23 |
8020
342ce07836de
mod_storage_internal: Ignore unused 'self' argument [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8019
diff
changeset
|
24 function driver:purge(user) -- luacheck: ignore 212/self |
8019
925098aad268
mod_storage_internal: Reorder methods
Kim Alvefur <zash@zash.se>
parents:
8018
diff
changeset
|
25 return datamanager.purge(user, host); |
925098aad268
mod_storage_internal: Reorder methods
Kim Alvefur <zash@zash.se>
parents:
8018
diff
changeset
|
26 end |
925098aad268
mod_storage_internal: Reorder methods
Kim Alvefur <zash@zash.se>
parents:
8018
diff
changeset
|
27 |
8018
9545d0a9401f
mod_storage_internal: Separate driver from keyval implementation
Kim Alvefur <zash@zash.se>
parents:
6283
diff
changeset
|
28 local keyval = { }; |
9545d0a9401f
mod_storage_internal: Separate driver from keyval implementation
Kim Alvefur <zash@zash.se>
parents:
6283
diff
changeset
|
29 driver.keyval = { __index = keyval }; |
9545d0a9401f
mod_storage_internal: Separate driver from keyval implementation
Kim Alvefur <zash@zash.se>
parents:
6283
diff
changeset
|
30 |
9545d0a9401f
mod_storage_internal: Separate driver from keyval implementation
Kim Alvefur <zash@zash.se>
parents:
6283
diff
changeset
|
31 function keyval:get(user) |
4085
7699cef04740
storagemanager, mod_storage_internal: Split out default driver to mod_storage_internal, and greatly simplify storagemanager's error handling and fallback code
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 return datamanager.load(user, host, self.store); |
7699cef04740
storagemanager, mod_storage_internal: Split out default driver to mod_storage_internal, and greatly simplify storagemanager's error handling and fallback code
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 end |
7699cef04740
storagemanager, mod_storage_internal: Split out default driver to mod_storage_internal, and greatly simplify storagemanager's error handling and fallback code
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 |
8018
9545d0a9401f
mod_storage_internal: Separate driver from keyval implementation
Kim Alvefur <zash@zash.se>
parents:
6283
diff
changeset
|
35 function keyval:set(user, data) |
4085
7699cef04740
storagemanager, mod_storage_internal: Split out default driver to mod_storage_internal, and greatly simplify storagemanager's error handling and fallback code
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 return datamanager.store(user, host, self.store, data); |
7699cef04740
storagemanager, mod_storage_internal: Split out default driver to mod_storage_internal, and greatly simplify storagemanager's error handling and fallback code
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 end |
7699cef04740
storagemanager, mod_storage_internal: Split out default driver to mod_storage_internal, and greatly simplify storagemanager's error handling and fallback code
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 |
8018
9545d0a9401f
mod_storage_internal: Separate driver from keyval implementation
Kim Alvefur <zash@zash.se>
parents:
6283
diff
changeset
|
39 function keyval:users() |
5153
688aeac0012a
mod_storage_internal, datamanager: Add support for iterating over users with data in a store
Kim Alvefur <zash@zash.se>
parents:
5133
diff
changeset
|
40 return datamanager.users(host, self.store, self.type); |
688aeac0012a
mod_storage_internal, datamanager: Add support for iterating over users with data in a store
Kim Alvefur <zash@zash.se>
parents:
5133
diff
changeset
|
41 end |
688aeac0012a
mod_storage_internal, datamanager: Add support for iterating over users with data in a store
Kim Alvefur <zash@zash.se>
parents:
5133
diff
changeset
|
42 |
8021
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
43 local archive = {}; |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
44 driver.archive = { __index = archive }; |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
45 |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
46 function archive:append(username, key, value, when, with) |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
47 key = key or id(); |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
48 when = when or now(); |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
49 if not st.is_stanza(value) then |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
50 return nil, "unsupported-datatype"; |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
51 end |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
52 value = st.preserialize(st.clone(value)); |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
53 value.key = key; |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
54 value.when = when; |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
55 value.with = with; |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
56 value.attr.stamp = datetime.datetime(when); |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
57 value.attr.stamp_legacy = datetime.legacy(when); |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
58 local ok, err = datamanager.list_append(username, host, self.store, value); |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
59 if not ok then return ok, err; end |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
60 return key; |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
61 end |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
62 |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
63 function archive:find(username, query) |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
64 local items, err = datamanager.list_load(username, host, self.store); |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
65 if not items then return items, err; end |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
66 local count = #items; |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
67 local i = 0; |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
68 if query then |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
69 items = array(items); |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
70 if query.with then |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
71 items:filter(function (item) |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
72 return item.with == query.with; |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
73 end); |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
74 end |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
75 if query.start then |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
76 items:filter(function (item) |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
77 return item.when >= query.start; |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
78 end); |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
79 end |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
80 if query["end"] then |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
81 items:filter(function (item) |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
82 return item.when <= query["end"]; |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
83 end); |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
84 end |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
85 count = #items; |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
86 if query.reverse then |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
87 items:reverse(); |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
88 if query.before then |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
89 for j = 1, count do |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
90 if (items[j].key or tostring(j)) == query.before then |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
91 i = j; |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
92 break; |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
93 end |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
94 end |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
95 end |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
96 elseif query.after then |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
97 for j = 1, count do |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
98 if (items[j].key or tostring(j)) == query.after then |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
99 i = j; |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
100 break; |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
101 end |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
102 end |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
103 end |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
104 if query.limit and #items - i > query.limit then |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
105 items[i+query.limit+1] = nil; |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
106 end |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
107 end |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
108 return function () |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
109 i = i + 1; |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
110 local item = items[i]; |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
111 if not item then return; end |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
112 local key = item.key or tostring(i); |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
113 local when = item.when or datetime.parse(item.attr.stamp); |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
114 local with = item.with; |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
115 item.key, item.when, item.with = nil, nil, nil; |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
116 item.attr.stamp = nil; |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
117 item.attr.stamp_legacy = nil; |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
118 item = st.deserialize(item); |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
119 return key, item, when, with; |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
120 end, count; |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
121 end |
83f18982bcfd
mod_storage_internal: Add basic archive store implementation
Kim Alvefur <zash@zash.se>
parents:
8020
diff
changeset
|
122 |
5121
b5a5643f8572
core.storagemanager, mod_storage_*: "data-driver" -> "storage-provider", to allow using module:provides().
Waqas Hussain <waqas20@gmail.com>
parents:
5039
diff
changeset
|
123 module:provides("storage", driver); |