Comparison

core/storagemanager.lua @ 4085:7699cef04740

storagemanager, mod_storage_internal: Split out default driver to mod_storage_internal, and greatly simplify storagemanager's error handling and fallback code
author Matthew Wild <mwild1@gmail.com>
date Fri, 07 Jan 2011 04:22:28 +0000
parent 4077:ba3ab4ecce34
child 4115:801725a32c96
comparison
equal deleted inserted replaced
4084:680df3c635c6 4085:7699cef04740
1 1
2 local error, type = error, type; 2 local error, type, pairs = error, type, pairs;
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 modulemanager = require "core.modulemanager"; 7 local modulemanager = require "core.modulemanager";
8 local multitable = require "util.multitable"; 8 local multitable = require "util.multitable";
9 local hosts = hosts; 9 local hosts = hosts;
10 local log = require "util.logger".init("storagemanager"); 10 local log = require "util.logger".init("storagemanager");
11 11
12 local olddm = {}; -- maintain old datamanager, for backwards compatibility
13 for k,v in pairs(datamanager) do olddm[k] = v; end
14 local prosody = prosody; 12 local prosody = prosody;
15 13
16 module("storagemanager") 14 module("storagemanager")
15
16 local olddm = {}; -- maintain old datamanager, for backwards compatibility
17 for k,v in pairs(datamanager) do olddm[k] = v; end
18 _M.olddm = olddm;
17 19
18 local null_storage_method = function () return false, "no data storage active"; end 20 local null_storage_method = function () return false, "no data storage active"; end
19 local null_storage_driver = setmetatable( 21 local null_storage_driver = setmetatable(
20 { 22 {
21 name = "null", 23 name = "null",
24 __index = function (self, method) 26 __index = function (self, method)
25 return null_storage_method; 27 return null_storage_method;
26 end 28 end
27 } 29 }
28 ); 30 );
29
30 --TODO: Move default driver to mod_auth_internal
31 local default_driver_mt = { name = "internal" };
32 default_driver_mt.__index = default_driver_mt;
33 function default_driver_mt:open(store)
34 return setmetatable({ host = self.host, store = store }, default_driver_mt);
35 end
36 function default_driver_mt:get(user) return olddm.load(user, self.host, self.store); end
37 function default_driver_mt:set(user, data) return olddm.store(user, self.host, self.store, data); end
38 31
39 local stores_available = multitable.new(); 32 local stores_available = multitable.new();
40 33
41 function initialize_host(host) 34 function initialize_host(host)
42 local host_session = hosts[host]; 35 local host_session = hosts[host];
51 end); 44 end);
52 end 45 end
53 prosody.events.add_handler("host-activated", initialize_host, 101); 46 prosody.events.add_handler("host-activated", initialize_host, 101);
54 47
55 local function load_driver(host, driver_name) 48 local function load_driver(host, driver_name)
56 if not driver_name then 49 if driver_name == "null" then
57 return; 50 return null_storage_provider;
58 end 51 end
59 local driver = stores_available:get(host, driver_name); 52 local driver = stores_available:get(host, driver_name);
60 if driver then return driver; end 53 if driver then return driver; end
61 if driver_name ~= "internal" then 54 local ok, err = modulemanager.load(host, "storage_"..driver_name);
62 local ok, err = modulemanager.load(host, "storage_"..driver_name); 55 if not ok then
63 if not ok then 56 log("error", "Failed to load storage driver plugin %s on %s: %s", driver_name, host, err);
64 log("error", "Failed to load storage driver plugin %s on %s: %s", driver_name, host, err);
65 end
66 return stores_available:get(host, driver_name);
67 else
68 return setmetatable({host = host}, default_driver_mt);
69 end 57 end
58 return stores_available:get(host, driver_name);
70 end 59 end
71 60
72 function open(host, store, typ) 61 function open(host, store, typ)
73 local storage = config.get(host, "core", "storage"); 62 local storage = config.get(host, "core", "storage");
74 local driver_name; 63 local driver_name;
76 if option_type == "string" then 65 if option_type == "string" then
77 driver_name = storage; 66 driver_name = storage;
78 elseif option_type == "table" then 67 elseif option_type == "table" then
79 driver_name = storage[store]; 68 driver_name = storage[store];
80 end 69 end
70 if not driver_name then
71 driver_name = config.get(host, "core", "default_storage") or "internal";
72 end
81 73
82 local driver = load_driver(host, driver_name); 74 local driver = load_driver(host, driver_name);
83 if not driver then 75 if not driver then
84 driver_name = config.get(host, "core", "default_storage"); 76 log("warn", "Falling back to null driver for %s storage on %s", store, host);
85 driver = load_driver(host, driver_name); 77 driver_name = "null";
86 if not driver then 78 driver = null_storage_driver;
87 if driver_name or (type(storage) == "string"
88 or type(storage) == "table" and storage[store]) then
89 log("warn", "Falling back to null driver for %s storage on %s", store, host);
90 driver_name = "null";
91 driver = null_storage_driver;
92 else
93 driver_name = "internal";
94 driver = load_driver(host, driver_name);
95 end
96 end
97 end 79 end
98 80
99 local ret, err = driver:open(store, typ); 81 local ret, err = driver:open(store, typ);
100 if not ret then 82 if not ret then
101 if err == "unsupported-store" then 83 if err == "unsupported-store" then