Comparison

core/storagemanager.lua @ 6779:6236668da30a

core.*: Remove use of module() function
author Kim Alvefur <zash@zash.se>
date Sat, 21 Feb 2015 10:42:19 +0100
parent 6663:d3023dd07cb6
child 6791:e813e8cf6046
child 6807:5e3242d349f2
comparison
equal deleted inserted replaced
6778:4009ae66e0f0 6779:6236668da30a
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 prosody = prosody; 12 local prosody = prosody;
13 13
14 module("storagemanager") 14 local _ENV = nil;
15 15
16 local olddm = {}; -- maintain old datamanager, for backwards compatibility 16 local olddm = {}; -- maintain old datamanager, for backwards compatibility
17 for k,v in pairs(datamanager) do olddm[k] = v; end 17 for k,v in pairs(datamanager) do olddm[k] = v; end
18 _M.olddm = olddm;
19 18
20 local null_storage_method = function () return false, "no data storage active"; end 19 local null_storage_method = function () return false, "no data storage active"; end
21 local null_storage_driver = setmetatable( 20 local null_storage_driver = setmetatable(
22 { 21 {
23 name = "null", 22 name = "null",
29 } 28 }
30 ); 29 );
31 30
32 local stores_available = multitable.new(); 31 local stores_available = multitable.new();
33 32
34 function initialize_host(host) 33 local function initialize_host(host)
35 local host_session = hosts[host]; 34 local host_session = hosts[host];
36 host_session.events.add_handler("item-added/storage-provider", function (event) 35 host_session.events.add_handler("item-added/storage-provider", function (event)
37 local item = event.item; 36 local item = event.item;
38 stores_available:set(host, item.name, item); 37 stores_available:set(host, item.name, item);
39 end); 38 end);
43 stores_available:set(host, item.name, nil); 42 stores_available:set(host, item.name, nil);
44 end); 43 end);
45 end 44 end
46 prosody.events.add_handler("host-activated", initialize_host, 101); 45 prosody.events.add_handler("host-activated", initialize_host, 101);
47 46
48 function load_driver(host, driver_name) 47 local function load_driver(host, driver_name)
49 if driver_name == "null" then 48 if driver_name == "null" then
50 return null_storage_driver; 49 return null_storage_driver;
51 end 50 end
52 local driver = stores_available:get(host, driver_name); 51 local driver = stores_available:get(host, driver_name);
53 if driver then return driver; end 52 if driver then return driver; end
56 log("error", "Failed to load storage driver plugin %s on %s: %s", driver_name, host, err); 55 log("error", "Failed to load storage driver plugin %s on %s: %s", driver_name, host, err);
57 end 56 end
58 return stores_available:get(host, driver_name); 57 return stores_available:get(host, driver_name);
59 end 58 end
60 59
61 function get_driver(host, store) 60 local function get_driver(host, store)
62 local storage = config.get(host, "storage"); 61 local storage = config.get(host, "storage");
63 local driver_name; 62 local driver_name;
64 local option_type = type(storage); 63 local option_type = type(storage);
65 if option_type == "string" then 64 if option_type == "string" then
66 driver_name = storage; 65 driver_name = storage;
78 driver = null_storage_driver; 77 driver = null_storage_driver;
79 end 78 end
80 return driver, driver_name; 79 return driver, driver_name;
81 end 80 end
82 81
83 function open(host, store, typ) 82 local function open(host, store, typ)
84 local driver, driver_name = get_driver(host, store); 83 local driver, driver_name = get_driver(host, store);
85 local ret, err = driver:open(store, typ); 84 local ret, err = driver:open(store, typ);
86 if not ret then 85 if not ret then
87 if err == "unsupported-store" then 86 if err == "unsupported-store" then
88 log("debug", "Storage driver %s does not support store %s (%s), falling back to null driver", 87 log("debug", "Storage driver %s does not support store %s (%s), falling back to null driver",
92 end 91 end
93 end 92 end
94 return ret, err; 93 return ret, err;
95 end 94 end
96 95
97 function purge(user, host) 96 local function purge(user, host)
98 local storage = config.get(host, "storage"); 97 local storage = config.get(host, "storage");
99 if type(storage) == "table" then 98 if type(storage) == "table" then
100 -- multiple storage backends in use that we need to purge 99 -- multiple storage backends in use that we need to purge
101 local purged = {}; 100 local purged = {};
102 for store, driver in pairs(storage) do 101 for store, driver in pairs(storage) do
130 end 129 end
131 function datamanager.purge(username, host) 130 function datamanager.purge(username, host)
132 return purge(username, host); 131 return purge(username, host);
133 end 132 end
134 133
135 return _M; 134 return {
135 initialize_host = initialize_host;
136 load_driver = load_driver;
137 get_driver = get_driver;
138 open = open;
139
140 olddm = olddm;
141 };