Software /
code /
prosody
Comparison
core/storagemanager.lua @ 12956:52fcdfe710ca
storagemanager: Add keyval+ (combined keyval + map) store type
This combines the two most common store types, which modules often end up
opening with both interfaces separately anyway.
As well as combining them, I've taken the opportunity to improve some of the
method names to make them clearer.
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Tue, 27 Sep 2022 17:46:27 +0100 |
parent | 10680:19692fc5c106 |
child | 12972:ead41e25ebc0 |
comparison
equal
deleted
inserted
replaced
12955:d32926897ca4 | 12956:52fcdfe710ca |
---|---|
201 return true; | 201 return true; |
202 end; | 202 end; |
203 }; | 203 }; |
204 } | 204 } |
205 | 205 |
206 local combined_store_mt = { | |
207 __index = { | |
208 -- keyval | |
209 get = function (self, name) | |
210 return self.keyval_store:get(name); | |
211 end; | |
212 set = function (self, name, data) | |
213 return self.keyval_store:set(name, data); | |
214 end; | |
215 items = function (self) | |
216 return self.keyval_store:users(); | |
217 end; | |
218 -- map | |
219 get_key = function (self, name, key) | |
220 return self.map_store:get(name, key); | |
221 end; | |
222 set_key = function (self, name, key, value) | |
223 return self.map_store:set(name, key, value); | |
224 end; | |
225 set_keys = function (self, name, map) | |
226 return self.map_store:set_keys(name, map); | |
227 end; | |
228 get_key_from_all = function (self, key) | |
229 return self.map_store:get_all(key); | |
230 end; | |
231 delete_key_from_all = function (self, key) | |
232 return self.map_store:delete_all(key); | |
233 end; | |
234 }; | |
235 }; | |
236 | |
206 local open; -- forward declaration | 237 local open; -- forward declaration |
207 | 238 |
208 local function create_map_shim(host, store) | 239 local function create_map_shim(host, store) |
209 local keyval_store, err = open(host, store, "keyval"); | 240 local keyval_store, err = open(host, store, "keyval"); |
210 if keyval_store == nil then return nil, err end | 241 if keyval_store == nil then return nil, err end |
211 return setmetatable({ | 242 return setmetatable({ |
212 keyval_store = keyval_store; | 243 keyval_store = keyval_store; |
213 }, map_shim_mt); | 244 }, map_shim_mt); |
214 end | 245 end |
215 | 246 |
247 local function open_combined(host, store) | |
248 local driver, driver_name = get_driver(host, store); | |
249 | |
250 -- Open keyval | |
251 local keyval_store, err = driver:open(store, "keyval"); | |
252 if not keyval_store then | |
253 if err == "unsupported-store" then | |
254 log("debug", "Storage driver %s does not support store %s (keyval), falling back to null driver", | |
255 driver_name, store); | |
256 keyval_store, err = null_storage_driver, nil; | |
257 end | |
258 end | |
259 | |
260 local map_store; | |
261 if keyval_store then | |
262 -- Open map | |
263 map_store, err = driver:open(store, "map"); | |
264 if not map_store then | |
265 if err == "unsupported-store" then | |
266 log("debug", "Storage driver %s does not support store %s (map), falling back to shim", | |
267 driver_name, store); | |
268 map_store, err = setmetatable({ keyval_store = keyval_store }, map_shim_mt), nil; | |
269 end | |
270 end | |
271 end | |
272 | |
273 if not(keyval_store and map_store) then | |
274 return nil, err; | |
275 end | |
276 local combined_store = setmetatable({ | |
277 keyval_store = keyval_store; | |
278 map_store = map_store; | |
279 remove = map_store.remove; | |
280 }, combined_store_mt); | |
281 local event_data = { host = host, store_name = store, store_type = "keyval+", store = combined_store }; | |
282 hosts[host].events.fire_event("store-opened", event_data); | |
283 return event_data.store, event_data.store_err; | |
284 end | |
285 | |
216 function open(host, store, typ) | 286 function open(host, store, typ) |
287 if typ == "keyval+" then -- TODO: default in some release? | |
288 return open_combined(host, store); | |
289 end | |
217 local driver, driver_name = get_driver(host, store); | 290 local driver, driver_name = get_driver(host, store); |
218 local ret, err = driver:open(store, typ); | 291 local ret, err = driver:open(store, typ); |
219 if not ret then | 292 if not ret then |
220 if err == "unsupported-store" then | 293 if err == "unsupported-store" then |
221 if typ == "map" then -- Use shim on top of keyval store | 294 if typ == "map" then -- Use shim on top of keyval store |