Comparison

core/storagemanager.lua @ 6791:e813e8cf6046

Merge 0.10->trunk
author Kim Alvefur <zash@zash.se>
date Thu, 20 Aug 2015 13:05:22 +0200
parent 6666:42fdc8dddf04
parent 6779:6236668da30a
child 6792:8b284787fe26
comparison
equal deleted inserted replaced
6776:4412a2307c89 6791:e813e8cf6046
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;
107 return setmetatable({ 106 return setmetatable({
108 keyval_store = keyval_store; 107 keyval_store = keyval_store;
109 }, map_shim_mt); 108 }, map_shim_mt);
110 end 109 end
111 110
112 function open(host, store, typ) 111 local function open(host, store, typ)
113 local driver, driver_name = get_driver(host, store); 112 local driver, driver_name = get_driver(host, store);
114 local ret, err = driver:open(store, typ); 113 local ret, err = driver:open(store, typ);
115 if not ret then 114 if not ret then
116 if err == "unsupported-store" then 115 if err == "unsupported-store" then
117 if typ == "map" then -- Use shim on top of keyval store 116 if typ == "map" then -- Use shim on top of keyval store
125 end 124 end
126 end 125 end
127 return ret, err; 126 return ret, err;
128 end 127 end
129 128
130 function purge(user, host) 129 local function purge(user, host)
131 local storage = config.get(host, "storage"); 130 local storage = config.get(host, "storage");
132 if type(storage) == "table" then 131 if type(storage) == "table" then
133 -- multiple storage backends in use that we need to purge 132 -- multiple storage backends in use that we need to purge
134 local purged = {}; 133 local purged = {};
135 for store, driver in pairs(storage) do 134 for store, driver in pairs(storage) do
163 end 162 end
164 function datamanager.purge(username, host) 163 function datamanager.purge(username, host)
165 return purge(username, host); 164 return purge(username, host);
166 end 165 end
167 166
168 return _M; 167 return {
168 initialize_host = initialize_host;
169 load_driver = load_driver;
170 get_driver = get_driver;
171 open = open;
172
173 olddm = olddm;
174 };