Software /
code /
prosody
Changeset
3644:22fc2063b824
storagemanager: Much refactoring and renaming of options. Untested, needs storage plugin(s) to be brought into line.
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sat, 27 Nov 2010 19:46:07 +0000 |
parents | 3643:2dc342a13f35 |
children | 3645:951f4752ef31 |
files | core/storagemanager.lua |
diffstat | 1 files changed, 54 insertions(+), 30 deletions(-) [+] |
line wrap: on
line diff
--- a/core/storagemanager.lua Sat Nov 27 01:30:56 2010 +0000 +++ b/core/storagemanager.lua Sat Nov 27 19:46:07 2010 +0000 @@ -4,7 +4,6 @@ local config = require "core.configmanager"; local datamanager = require "util.datamanager"; -local multitable = require "util.multitable"; local modulemanager = require "core.modulemanager"; local hosts = hosts; local log = require "util.logger".init("storagemanager"); @@ -12,12 +11,9 @@ local olddm = {}; -- maintain old datamanager, for backwards compatibility for k,v in pairs(datamanager) do olddm[k] = v; end -local driver_cache = multitable.new(); -local store_cache = multitable.new(); - module("storagemanager") -local default_driver_mt = {}; +local default_driver_mt = { name = "internal" }; default_driver_mt.__index = default_driver_mt; function default_driver_mt:open(store) return setmetatable({ host = self.host, store = store }, default_driver_mt); @@ -25,37 +21,65 @@ function default_driver_mt:get(user) return olddm.load(user, self.host, self.store); end function default_driver_mt:set(user, data) return olddm.store(user, self.host, self.store, data); end -local function load_driver_for_host(host) - if driver_cache:get(host) then return driver_cache:get(host); end - - local host_session = hosts[host]; - if not host_session then error("No such host"); end - - local driver_plugin = config.get(host, "core", "datastore"); - if not driver_plugin then return setmetatable({ host = host }, default_driver_mt); end +local stores_available = multitable.new(); + +function initialize_host(host) + host_session.events.add_handler("item-added/data-driver", function (event) + local item = event.item; + stores_available:set(host, item.name, item); + end); - local provider; - local function handler(event) provider = event.item; end - host_session.events.add_handler("item-added/data-driver", handler); - local success, err = modulemanager.load(host, driver_plugin); - host_session.events.remove_handler("item-added/data-driver", handler); - if not success then error(err); end - if not provider then error("Module didn't add a provider"); end - - driver_cache:set(host, provider); - log("debug", "Data driver '%s' loaded for host '%s'", driver_plugin, host); - return provider; + host_session.events.add_handler("item-removed/data-driver", function (event) + local item = event.item; + stores_available:set(host, item.name, nil); + end); +end + +local function load_driver(host, driver_name) + if not driver_name then + return; + end + local driver = stores_available:get(host, driver_name); + if not driver then + if driver_name ~= "internal" then + modulemanager.load(host, "storage_"..driver_name); + else + return setmetatable({host = host}, default_driver_mt); + end + end end function open(host, store, typ) - local ret = store_cache:get(host, store); + local storage = config.get(host, "core", "storage"); + local driver_name; + local option_type = type(storage); + if option_type == "string" then + driver_name = storage; + elseif option_type == "table" then + driver_name = storage[store]; + end + + local driver = load_driver(host, driver_name); + if not driver then + driver_name = config.get(host, "core", "default_storage"); + driver = load_driver(host, driver_name); + if not driver then + driver_name = "internal"; + log("warn", "Falling back to default driver for %s storage on %s", store, host); + driver = load_driver(host, driver_name); + end + end + + local ret, err = driver:open(store, typ); if not ret then - local driver = load_driver_for_host(host); - ret = driver:open(store, typ); - if not ret then ret = setmetatable({ host = host, store = store }, default_driver_mt); end -- default to default driver - store_cache:set(host, store, ret); + if err == "unsupported-store" then + log("debug", "Storage driver %s does not support store %s (%s), falling back to internal driver", + driver_name, store, typ); + ret = setmetatable({ host = host, store = store }, default_driver_mt); end -- default to default driver + err = nil; + end end - return ret; + return ret, err; end function datamanager.load(username, host, datastore)