Software /
code /
prosody
Comparison
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 |
comparison
equal
deleted
inserted
replaced
8020:342ce07836de | 8021:83f18982bcfd |
---|---|
1 local datamanager = require "core.storagemanager".olddm; | 1 local datamanager = require "core.storagemanager".olddm; |
2 local array = require "util.array"; | |
3 local datetime = require "util.datetime"; | |
4 local st = require "util.stanza"; | |
5 local now = require "util.time".now; | |
6 local id = require "util.id".medium; | |
2 | 7 |
3 local host = module.host; | 8 local host = module.host; |
4 | 9 |
5 local driver = {}; | 10 local driver = {}; |
6 | 11 |
33 | 38 |
34 function keyval:users() | 39 function keyval:users() |
35 return datamanager.users(host, self.store, self.type); | 40 return datamanager.users(host, self.store, self.type); |
36 end | 41 end |
37 | 42 |
43 local archive = {}; | |
44 driver.archive = { __index = archive }; | |
45 | |
46 function archive:append(username, key, value, when, with) | |
47 key = key or id(); | |
48 when = when or now(); | |
49 if not st.is_stanza(value) then | |
50 return nil, "unsupported-datatype"; | |
51 end | |
52 value = st.preserialize(st.clone(value)); | |
53 value.key = key; | |
54 value.when = when; | |
55 value.with = with; | |
56 value.attr.stamp = datetime.datetime(when); | |
57 value.attr.stamp_legacy = datetime.legacy(when); | |
58 local ok, err = datamanager.list_append(username, host, self.store, value); | |
59 if not ok then return ok, err; end | |
60 return key; | |
61 end | |
62 | |
63 function archive:find(username, query) | |
64 local items, err = datamanager.list_load(username, host, self.store); | |
65 if not items then return items, err; end | |
66 local count = #items; | |
67 local i = 0; | |
68 if query then | |
69 items = array(items); | |
70 if query.with then | |
71 items:filter(function (item) | |
72 return item.with == query.with; | |
73 end); | |
74 end | |
75 if query.start then | |
76 items:filter(function (item) | |
77 return item.when >= query.start; | |
78 end); | |
79 end | |
80 if query["end"] then | |
81 items:filter(function (item) | |
82 return item.when <= query["end"]; | |
83 end); | |
84 end | |
85 count = #items; | |
86 if query.reverse then | |
87 items:reverse(); | |
88 if query.before then | |
89 for j = 1, count do | |
90 if (items[j].key or tostring(j)) == query.before then | |
91 i = j; | |
92 break; | |
93 end | |
94 end | |
95 end | |
96 elseif query.after then | |
97 for j = 1, count do | |
98 if (items[j].key or tostring(j)) == query.after then | |
99 i = j; | |
100 break; | |
101 end | |
102 end | |
103 end | |
104 if query.limit and #items - i > query.limit then | |
105 items[i+query.limit+1] = nil; | |
106 end | |
107 end | |
108 return function () | |
109 i = i + 1; | |
110 local item = items[i]; | |
111 if not item then return; end | |
112 local key = item.key or tostring(i); | |
113 local when = item.when or datetime.parse(item.attr.stamp); | |
114 local with = item.with; | |
115 item.key, item.when, item.with = nil, nil, nil; | |
116 item.attr.stamp = nil; | |
117 item.attr.stamp_legacy = nil; | |
118 item = st.deserialize(item); | |
119 return key, item, when, with; | |
120 end, count; | |
121 end | |
122 | |
38 module:provides("storage", driver); | 123 module:provides("storage", driver); |