diff options
Diffstat (limited to 'core/storagemanager.lua')
-rw-r--r-- | core/storagemanager.lua | 132 |
1 files changed, 111 insertions, 21 deletions
diff --git a/core/storagemanager.lua b/core/storagemanager.lua index 1c82af6d..c93438af 100644 --- a/core/storagemanager.lua +++ b/core/storagemanager.lua @@ -1,5 +1,5 @@ -local error, type, pairs = error, type, pairs; +local type, pairs = type, pairs; local setmetatable = setmetatable; local config = require "core.configmanager"; @@ -11,11 +11,10 @@ local log = require "util.logger".init("storagemanager"); local prosody = prosody; -module("storagemanager") +local _ENV = nil; local olddm = {}; -- maintain old datamanager, for backwards compatibility for k,v in pairs(datamanager) do olddm[k] = v; end -_M.olddm = olddm; local null_storage_method = function () return false, "no data storage active"; end local null_storage_driver = setmetatable( @@ -23,7 +22,7 @@ local null_storage_driver = setmetatable( name = "null", open = function (self) return self; end }, { - __index = function (self, method) + __index = function (self, method) --luacheck: ignore 212 return null_storage_method; end } @@ -31,13 +30,13 @@ local null_storage_driver = setmetatable( local stores_available = multitable.new(); -function initialize_host(host) +local function initialize_host(host) local host_session = hosts[host]; host_session.events.add_handler("item-added/storage-provider", function (event) local item = event.item; stores_available:set(host, item.name, item); end); - + host_session.events.add_handler("item-removed/storage-provider", function (event) local item = event.item; stores_available:set(host, item.name, nil); @@ -45,7 +44,7 @@ function initialize_host(host) end prosody.events.add_handler("host-activated", initialize_host, 101); -function load_driver(host, driver_name) +local function load_driver(host, driver_name) if driver_name == "null" then return null_storage_driver; end @@ -58,8 +57,29 @@ function load_driver(host, driver_name) return stores_available:get(host, driver_name); end -function get_driver(host, store) - local storage = config.get(host, "storage"); +local function get_storage_config(host) + -- COMPAT w/ unreleased Prosody 0.10 and the once-experimental mod_storage_sql2 in peoples' config files + local storage_config = config.get(host, "storage"); + local found_sql2; + if storage_config == "sql2" then + storage_config, found_sql2 = "sql", true; + elseif type(storage_config) == "table" then + for store_name, driver_name in pairs(storage_config) do + if driver_name == "sql2" then + storage_config[store_name] = "sql"; + found_sql2 = true; + end + end + end + if found_sql2 then + log("error", "The temporary 'sql2' storage module has now been renamed to 'sql', " + .."please update your config file: https://prosody.im/doc/modules/mod_storage_sql2"); + end + return storage_config; +end + +local function get_driver(host, store) + local storage = get_storage_config(host); local driver_name; local option_type = type(storage); if option_type == "string" then @@ -70,7 +90,7 @@ function get_driver(host, store) if not driver_name then driver_name = config.get(host, "default_storage") or "internal"; end - + local driver = load_driver(host, driver_name); if not driver then log("warn", "Falling back to null driver for %s storage on %s", store, host); @@ -80,28 +100,90 @@ function get_driver(host, store) return driver, driver_name; end +local map_shim_mt = { + __index = { + get = function(self, username, key) + local ret, err = self.keyval_store:get(username); + if ret == nil then return nil, err end + return ret[key]; + end; + set = function(self, username, key, data) + local current, err = self.keyval_store:get(username); + if current == nil then + if err then + return nil, err; + else + current = {}; + end + end + current[key] = data; + return self.keyval_store:set(username, current); + end; + set_keys = function (self, username, keydatas) + local current, err = self.keyval_store:get(username); + if current == nil then + if err then + return nil, err; + end + current = {}; + end + for k,v in pairs(keydatas) do + if v == self.remove then v = nil; end + current[k] = v; + end + return self.keyval_store:set(username, current); + end; + remove = {}; + }; +} + +local open; + +local function create_map_shim(host, store) + local keyval_store, err = open(host, store, "keyval"); + if keyval_store == nil then return nil, err end + return setmetatable({ + keyval_store = keyval_store; + }, map_shim_mt); +end + function open(host, store, typ) local driver, driver_name = get_driver(host, store); local ret, err = driver:open(store, typ); if not ret then if err == "unsupported-store" then - log("debug", "Storage driver %s does not support store %s (%s), falling back to null driver", - driver_name, store, typ or "<nil>"); - ret = null_storage_driver; - err = nil; + if typ == "map" then -- Use shim on top of keyval store + log("debug", "map storage driver unavailable, using shim on top of keyval store."); + ret, err = create_map_shim(host, store); + else + log("debug", "Storage driver %s does not support store %s (%s), falling back to null driver", + driver_name, store, typ or "<nil>"); + ret, err = null_storage_driver, nil; + end end end + if ret then + local event_data = { host = host, store_name = store, store_type = typ, store = ret }; + hosts[host].events.fire_event("store-opened", event_data); + ret, err = event_data.store, event_data.store_err; + end return ret, err; end -function purge(user, host) - local storage = config.get(host, "storage"); +local function purge(user, host) + local storage = get_storage_config(host); if type(storage) == "table" then -- multiple storage backends in use that we need to purge local purged = {}; - for store, driver in pairs(storage) do - if not purged[driver] then - purged[driver] = get_driver(host, store):purge(user); + for store, driver_name in pairs(storage) do + if not purged[driver_name] then + local driver = get_driver(host, store); + if driver.purge then + purged[driver_name] = driver:purge(user); + else + log("warn", "Storage driver %s does not support removing all user data, " + .."you may need to delete it manually", driver_name); + end end end end @@ -121,7 +203,7 @@ end function datamanager.users(host, datastore, typ) local driver = open(host, datastore, typ); if not driver.users then - return function() log("warn", "storage driver %s does not support listing users", driver.name) end + return function() log("warn", "Storage driver %s does not support listing users", driver.name) end end return driver:users(); end @@ -132,4 +214,12 @@ function datamanager.purge(username, host) return purge(username, host); end -return _M; +return { + initialize_host = initialize_host; + load_driver = load_driver; + get_driver = get_driver; + open = open; + purge = purge; + + olddm = olddm; +}; |