Software /
code /
prosody
Comparison
core/storagemanager.lua @ 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 |
parent | 3403:b89680015b7b |
child | 3654:8afd15a61743 |
comparison
equal
deleted
inserted
replaced
3643:2dc342a13f35 | 3644:22fc2063b824 |
---|---|
2 local error = error; | 2 local error = error; |
3 local setmetatable = setmetatable; | 3 local setmetatable = setmetatable; |
4 | 4 |
5 local config = require "core.configmanager"; | 5 local config = require "core.configmanager"; |
6 local datamanager = require "util.datamanager"; | 6 local datamanager = require "util.datamanager"; |
7 local multitable = require "util.multitable"; | |
8 local modulemanager = require "core.modulemanager"; | 7 local modulemanager = require "core.modulemanager"; |
9 local hosts = hosts; | 8 local hosts = hosts; |
10 local log = require "util.logger".init("storagemanager"); | 9 local log = require "util.logger".init("storagemanager"); |
11 | 10 |
12 local olddm = {}; -- maintain old datamanager, for backwards compatibility | 11 local olddm = {}; -- maintain old datamanager, for backwards compatibility |
13 for k,v in pairs(datamanager) do olddm[k] = v; end | 12 for k,v in pairs(datamanager) do olddm[k] = v; end |
14 | 13 |
15 local driver_cache = multitable.new(); | |
16 local store_cache = multitable.new(); | |
17 | |
18 module("storagemanager") | 14 module("storagemanager") |
19 | 15 |
20 local default_driver_mt = {}; | 16 local default_driver_mt = { name = "internal" }; |
21 default_driver_mt.__index = default_driver_mt; | 17 default_driver_mt.__index = default_driver_mt; |
22 function default_driver_mt:open(store) | 18 function default_driver_mt:open(store) |
23 return setmetatable({ host = self.host, store = store }, default_driver_mt); | 19 return setmetatable({ host = self.host, store = store }, default_driver_mt); |
24 end | 20 end |
25 function default_driver_mt:get(user) return olddm.load(user, self.host, self.store); end | 21 function default_driver_mt:get(user) return olddm.load(user, self.host, self.store); end |
26 function default_driver_mt:set(user, data) return olddm.store(user, self.host, self.store, data); end | 22 function default_driver_mt:set(user, data) return olddm.store(user, self.host, self.store, data); end |
27 | 23 |
28 local function load_driver_for_host(host) | 24 local stores_available = multitable.new(); |
29 if driver_cache:get(host) then return driver_cache:get(host); end | 25 |
26 function initialize_host(host) | |
27 host_session.events.add_handler("item-added/data-driver", function (event) | |
28 local item = event.item; | |
29 stores_available:set(host, item.name, item); | |
30 end); | |
30 | 31 |
31 local host_session = hosts[host]; | 32 host_session.events.add_handler("item-removed/data-driver", function (event) |
32 if not host_session then error("No such host"); end | 33 local item = event.item; |
33 | 34 stores_available:set(host, item.name, nil); |
34 local driver_plugin = config.get(host, "core", "datastore"); | 35 end); |
35 if not driver_plugin then return setmetatable({ host = host }, default_driver_mt); end | 36 end |
36 | 37 |
37 local provider; | 38 local function load_driver(host, driver_name) |
38 local function handler(event) provider = event.item; end | 39 if not driver_name then |
39 host_session.events.add_handler("item-added/data-driver", handler); | 40 return; |
40 local success, err = modulemanager.load(host, driver_plugin); | 41 end |
41 host_session.events.remove_handler("item-added/data-driver", handler); | 42 local driver = stores_available:get(host, driver_name); |
42 if not success then error(err); end | 43 if not driver then |
43 if not provider then error("Module didn't add a provider"); end | 44 if driver_name ~= "internal" then |
44 | 45 modulemanager.load(host, "storage_"..driver_name); |
45 driver_cache:set(host, provider); | 46 else |
46 log("debug", "Data driver '%s' loaded for host '%s'", driver_plugin, host); | 47 return setmetatable({host = host}, default_driver_mt); |
47 return provider; | 48 end |
49 end | |
48 end | 50 end |
49 | 51 |
50 function open(host, store, typ) | 52 function open(host, store, typ) |
51 local ret = store_cache:get(host, store); | 53 local storage = config.get(host, "core", "storage"); |
54 local driver_name; | |
55 local option_type = type(storage); | |
56 if option_type == "string" then | |
57 driver_name = storage; | |
58 elseif option_type == "table" then | |
59 driver_name = storage[store]; | |
60 end | |
61 | |
62 local driver = load_driver(host, driver_name); | |
63 if not driver then | |
64 driver_name = config.get(host, "core", "default_storage"); | |
65 driver = load_driver(host, driver_name); | |
66 if not driver then | |
67 driver_name = "internal"; | |
68 log("warn", "Falling back to default driver for %s storage on %s", store, host); | |
69 driver = load_driver(host, driver_name); | |
70 end | |
71 end | |
72 | |
73 local ret, err = driver:open(store, typ); | |
52 if not ret then | 74 if not ret then |
53 local driver = load_driver_for_host(host); | 75 if err == "unsupported-store" then |
54 ret = driver:open(store, typ); | 76 log("debug", "Storage driver %s does not support store %s (%s), falling back to internal driver", |
55 if not ret then ret = setmetatable({ host = host, store = store }, default_driver_mt); end -- default to default driver | 77 driver_name, store, typ); |
56 store_cache:set(host, store, ret); | 78 ret = setmetatable({ host = host, store = store }, default_driver_mt); end -- default to default driver |
79 err = nil; | |
80 end | |
57 end | 81 end |
58 return ret; | 82 return ret, err; |
59 end | 83 end |
60 | 84 |
61 function datamanager.load(username, host, datastore) | 85 function datamanager.load(username, host, datastore) |
62 return open(host, datastore):get(username); | 86 return open(host, datastore):get(username); |
63 end | 87 end |