Annotate

core/storagemanager.lua @ 4081:ce02a56b268f

ejabberdstore.lib.lua: Remove now needless library
author Matthew Wild <mwild1@gmail.com>
date Fri, 07 Jan 2011 03:17:36 +0000
parent 4077:ba3ab4ecce34
child 4085:7699cef04740
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3401
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
1
3785
374a1a680065 storagemanager: Import type()
Matthew Wild <mwild1@gmail.com>
parents: 3784
diff changeset
2 local error, type = error, type;
3401
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
3 local setmetatable = setmetatable;
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
4
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
5 local config = require "core.configmanager";
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
6 local datamanager = require "util.datamanager";
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
7 local modulemanager = require "core.modulemanager";
3784
7383ac93fb27 storagemanager: Import util.multitable again
Matthew Wild <mwild1@gmail.com>
parents: 3783
diff changeset
8 local multitable = require "util.multitable";
3401
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
9 local hosts = hosts;
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
10 local log = require "util.logger".init("storagemanager");
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
11
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
12 local olddm = {}; -- maintain old datamanager, for backwards compatibility
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
13 for k,v in pairs(datamanager) do olddm[k] = v; end
3850
39492ede0cf5 storagemanager: Hook "host-activated", to make sure we are notified about data drivers.
Waqas Hussain <waqas20@gmail.com>
parents: 3849
diff changeset
14 local prosody = prosody;
3401
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
15
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
16 module("storagemanager")
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
17
4076
00ce99071368 storagemanager: Add new "null" provider to get used when loading a driver or opening a store fails, instead of falling back to the default driver
Matthew Wild <mwild1@gmail.com>
parents: 4075
diff changeset
18 local null_storage_method = function () return false, "no data storage active"; end
00ce99071368 storagemanager: Add new "null" provider to get used when loading a driver or opening a store fails, instead of falling back to the default driver
Matthew Wild <mwild1@gmail.com>
parents: 4075
diff changeset
19 local null_storage_driver = setmetatable(
00ce99071368 storagemanager: Add new "null" provider to get used when loading a driver or opening a store fails, instead of falling back to the default driver
Matthew Wild <mwild1@gmail.com>
parents: 4075
diff changeset
20 {
00ce99071368 storagemanager: Add new "null" provider to get used when loading a driver or opening a store fails, instead of falling back to the default driver
Matthew Wild <mwild1@gmail.com>
parents: 4075
diff changeset
21 name = "null",
00ce99071368 storagemanager: Add new "null" provider to get used when loading a driver or opening a store fails, instead of falling back to the default driver
Matthew Wild <mwild1@gmail.com>
parents: 4075
diff changeset
22 open = function (self) return self; end
00ce99071368 storagemanager: Add new "null" provider to get used when loading a driver or opening a store fails, instead of falling back to the default driver
Matthew Wild <mwild1@gmail.com>
parents: 4075
diff changeset
23 }, {
00ce99071368 storagemanager: Add new "null" provider to get used when loading a driver or opening a store fails, instead of falling back to the default driver
Matthew Wild <mwild1@gmail.com>
parents: 4075
diff changeset
24 __index = function (self, method)
00ce99071368 storagemanager: Add new "null" provider to get used when loading a driver or opening a store fails, instead of falling back to the default driver
Matthew Wild <mwild1@gmail.com>
parents: 4075
diff changeset
25 return null_storage_method;
00ce99071368 storagemanager: Add new "null" provider to get used when loading a driver or opening a store fails, instead of falling back to the default driver
Matthew Wild <mwild1@gmail.com>
parents: 4075
diff changeset
26 end
00ce99071368 storagemanager: Add new "null" provider to get used when loading a driver or opening a store fails, instead of falling back to the default driver
Matthew Wild <mwild1@gmail.com>
parents: 4075
diff changeset
27 }
00ce99071368 storagemanager: Add new "null" provider to get used when loading a driver or opening a store fails, instead of falling back to the default driver
Matthew Wild <mwild1@gmail.com>
parents: 4075
diff changeset
28 );
00ce99071368 storagemanager: Add new "null" provider to get used when loading a driver or opening a store fails, instead of falling back to the default driver
Matthew Wild <mwild1@gmail.com>
parents: 4075
diff changeset
29
00ce99071368 storagemanager: Add new "null" provider to get used when loading a driver or opening a store fails, instead of falling back to the default driver
Matthew Wild <mwild1@gmail.com>
parents: 4075
diff changeset
30 --TODO: Move default driver to mod_auth_internal
3644
22fc2063b824 storagemanager: Much refactoring and renaming of options. Untested, needs storage plugin(s) to be brought into line.
Matthew Wild <mwild1@gmail.com>
parents: 3403
diff changeset
31 local default_driver_mt = { name = "internal" };
3401
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
32 default_driver_mt.__index = default_driver_mt;
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
33 function default_driver_mt:open(store)
3403
b89680015b7b storagemanager: Fixed a nil access.
Waqas Hussain <waqas20@gmail.com>
parents: 3401
diff changeset
34 return setmetatable({ host = self.host, store = store }, default_driver_mt);
3401
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
35 end
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
36 function default_driver_mt:get(user) return olddm.load(user, self.host, self.store); end
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
37 function default_driver_mt:set(user, data) return olddm.store(user, self.host, self.store, data); end
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
38
3644
22fc2063b824 storagemanager: Much refactoring and renaming of options. Untested, needs storage plugin(s) to be brought into line.
Matthew Wild <mwild1@gmail.com>
parents: 3403
diff changeset
39 local stores_available = multitable.new();
22fc2063b824 storagemanager: Much refactoring and renaming of options. Untested, needs storage plugin(s) to be brought into line.
Matthew Wild <mwild1@gmail.com>
parents: 3403
diff changeset
40
22fc2063b824 storagemanager: Much refactoring and renaming of options. Untested, needs storage plugin(s) to be brought into line.
Matthew Wild <mwild1@gmail.com>
parents: 3403
diff changeset
41 function initialize_host(host)
3849
34981acbd5d5 storagemanager: Fixed a nil global access.
Waqas Hussain <waqas20@gmail.com>
parents: 3790
diff changeset
42 local host_session = hosts[host];
3644
22fc2063b824 storagemanager: Much refactoring and renaming of options. Untested, needs storage plugin(s) to be brought into line.
Matthew Wild <mwild1@gmail.com>
parents: 3403
diff changeset
43 host_session.events.add_handler("item-added/data-driver", function (event)
22fc2063b824 storagemanager: Much refactoring and renaming of options. Untested, needs storage plugin(s) to be brought into line.
Matthew Wild <mwild1@gmail.com>
parents: 3403
diff changeset
44 local item = event.item;
22fc2063b824 storagemanager: Much refactoring and renaming of options. Untested, needs storage plugin(s) to be brought into line.
Matthew Wild <mwild1@gmail.com>
parents: 3403
diff changeset
45 stores_available:set(host, item.name, item);
22fc2063b824 storagemanager: Much refactoring and renaming of options. Untested, needs storage plugin(s) to be brought into line.
Matthew Wild <mwild1@gmail.com>
parents: 3403
diff changeset
46 end);
3401
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
47
3644
22fc2063b824 storagemanager: Much refactoring and renaming of options. Untested, needs storage plugin(s) to be brought into line.
Matthew Wild <mwild1@gmail.com>
parents: 3403
diff changeset
48 host_session.events.add_handler("item-removed/data-driver", function (event)
22fc2063b824 storagemanager: Much refactoring and renaming of options. Untested, needs storage plugin(s) to be brought into line.
Matthew Wild <mwild1@gmail.com>
parents: 3403
diff changeset
49 local item = event.item;
22fc2063b824 storagemanager: Much refactoring and renaming of options. Untested, needs storage plugin(s) to be brought into line.
Matthew Wild <mwild1@gmail.com>
parents: 3403
diff changeset
50 stores_available:set(host, item.name, nil);
22fc2063b824 storagemanager: Much refactoring and renaming of options. Untested, needs storage plugin(s) to be brought into line.
Matthew Wild <mwild1@gmail.com>
parents: 3403
diff changeset
51 end);
22fc2063b824 storagemanager: Much refactoring and renaming of options. Untested, needs storage plugin(s) to be brought into line.
Matthew Wild <mwild1@gmail.com>
parents: 3403
diff changeset
52 end
3850
39492ede0cf5 storagemanager: Hook "host-activated", to make sure we are notified about data drivers.
Waqas Hussain <waqas20@gmail.com>
parents: 3849
diff changeset
53 prosody.events.add_handler("host-activated", initialize_host, 101);
3644
22fc2063b824 storagemanager: Much refactoring and renaming of options. Untested, needs storage plugin(s) to be brought into line.
Matthew Wild <mwild1@gmail.com>
parents: 3403
diff changeset
54
22fc2063b824 storagemanager: Much refactoring and renaming of options. Untested, needs storage plugin(s) to be brought into line.
Matthew Wild <mwild1@gmail.com>
parents: 3403
diff changeset
55 local function load_driver(host, driver_name)
22fc2063b824 storagemanager: Much refactoring and renaming of options. Untested, needs storage plugin(s) to be brought into line.
Matthew Wild <mwild1@gmail.com>
parents: 3403
diff changeset
56 if not driver_name then
22fc2063b824 storagemanager: Much refactoring and renaming of options. Untested, needs storage plugin(s) to be brought into line.
Matthew Wild <mwild1@gmail.com>
parents: 3403
diff changeset
57 return;
22fc2063b824 storagemanager: Much refactoring and renaming of options. Untested, needs storage plugin(s) to be brought into line.
Matthew Wild <mwild1@gmail.com>
parents: 3403
diff changeset
58 end
22fc2063b824 storagemanager: Much refactoring and renaming of options. Untested, needs storage plugin(s) to be brought into line.
Matthew Wild <mwild1@gmail.com>
parents: 3403
diff changeset
59 local driver = stores_available:get(host, driver_name);
3855
b77bb597bcb0 storagemanager: When we have a cached data driver, we are supposed to use it.
Waqas Hussain <waqas20@gmail.com>
parents: 3850
diff changeset
60 if driver then return driver; end
b77bb597bcb0 storagemanager: When we have a cached data driver, we are supposed to use it.
Waqas Hussain <waqas20@gmail.com>
parents: 3850
diff changeset
61 if driver_name ~= "internal" then
3889
2ef3d10e107b storagemanager: Log warning when loading the storage provider plugin fails
Matthew Wild <mwild1@gmail.com>
parents: 3855
diff changeset
62 local ok, err = modulemanager.load(host, "storage_"..driver_name);
2ef3d10e107b storagemanager: Log warning when loading the storage provider plugin fails
Matthew Wild <mwild1@gmail.com>
parents: 3855
diff changeset
63 if not ok then
4075
c082ded3b5f1 storagemanager: Log host in message when loading storage provider fails
Matthew Wild <mwild1@gmail.com>
parents: 3893
diff changeset
64 log("error", "Failed to load storage driver plugin %s on %s: %s", driver_name, host, err);
3889
2ef3d10e107b storagemanager: Log warning when loading the storage provider plugin fails
Matthew Wild <mwild1@gmail.com>
parents: 3855
diff changeset
65 end
3855
b77bb597bcb0 storagemanager: When we have a cached data driver, we are supposed to use it.
Waqas Hussain <waqas20@gmail.com>
parents: 3850
diff changeset
66 return stores_available:get(host, driver_name);
b77bb597bcb0 storagemanager: When we have a cached data driver, we are supposed to use it.
Waqas Hussain <waqas20@gmail.com>
parents: 3850
diff changeset
67 else
b77bb597bcb0 storagemanager: When we have a cached data driver, we are supposed to use it.
Waqas Hussain <waqas20@gmail.com>
parents: 3850
diff changeset
68 return setmetatable({host = host}, default_driver_mt);
3644
22fc2063b824 storagemanager: Much refactoring and renaming of options. Untested, needs storage plugin(s) to be brought into line.
Matthew Wild <mwild1@gmail.com>
parents: 3403
diff changeset
69 end
3401
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
70 end
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
71
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
72 function open(host, store, typ)
3644
22fc2063b824 storagemanager: Much refactoring and renaming of options. Untested, needs storage plugin(s) to be brought into line.
Matthew Wild <mwild1@gmail.com>
parents: 3403
diff changeset
73 local storage = config.get(host, "core", "storage");
22fc2063b824 storagemanager: Much refactoring and renaming of options. Untested, needs storage plugin(s) to be brought into line.
Matthew Wild <mwild1@gmail.com>
parents: 3403
diff changeset
74 local driver_name;
22fc2063b824 storagemanager: Much refactoring and renaming of options. Untested, needs storage plugin(s) to be brought into line.
Matthew Wild <mwild1@gmail.com>
parents: 3403
diff changeset
75 local option_type = type(storage);
22fc2063b824 storagemanager: Much refactoring and renaming of options. Untested, needs storage plugin(s) to be brought into line.
Matthew Wild <mwild1@gmail.com>
parents: 3403
diff changeset
76 if option_type == "string" then
22fc2063b824 storagemanager: Much refactoring and renaming of options. Untested, needs storage plugin(s) to be brought into line.
Matthew Wild <mwild1@gmail.com>
parents: 3403
diff changeset
77 driver_name = storage;
22fc2063b824 storagemanager: Much refactoring and renaming of options. Untested, needs storage plugin(s) to be brought into line.
Matthew Wild <mwild1@gmail.com>
parents: 3403
diff changeset
78 elseif option_type == "table" then
22fc2063b824 storagemanager: Much refactoring and renaming of options. Untested, needs storage plugin(s) to be brought into line.
Matthew Wild <mwild1@gmail.com>
parents: 3403
diff changeset
79 driver_name = storage[store];
22fc2063b824 storagemanager: Much refactoring and renaming of options. Untested, needs storage plugin(s) to be brought into line.
Matthew Wild <mwild1@gmail.com>
parents: 3403
diff changeset
80 end
22fc2063b824 storagemanager: Much refactoring and renaming of options. Untested, needs storage plugin(s) to be brought into line.
Matthew Wild <mwild1@gmail.com>
parents: 3403
diff changeset
81
22fc2063b824 storagemanager: Much refactoring and renaming of options. Untested, needs storage plugin(s) to be brought into line.
Matthew Wild <mwild1@gmail.com>
parents: 3403
diff changeset
82 local driver = load_driver(host, driver_name);
22fc2063b824 storagemanager: Much refactoring and renaming of options. Untested, needs storage plugin(s) to be brought into line.
Matthew Wild <mwild1@gmail.com>
parents: 3403
diff changeset
83 if not driver then
22fc2063b824 storagemanager: Much refactoring and renaming of options. Untested, needs storage plugin(s) to be brought into line.
Matthew Wild <mwild1@gmail.com>
parents: 3403
diff changeset
84 driver_name = config.get(host, "core", "default_storage");
22fc2063b824 storagemanager: Much refactoring and renaming of options. Untested, needs storage plugin(s) to be brought into line.
Matthew Wild <mwild1@gmail.com>
parents: 3403
diff changeset
85 driver = load_driver(host, driver_name);
22fc2063b824 storagemanager: Much refactoring and renaming of options. Untested, needs storage plugin(s) to be brought into line.
Matthew Wild <mwild1@gmail.com>
parents: 3403
diff changeset
86 if not driver then
3893
475fc383527d storagemanager: Don't always show fallback warning when using per-store config
Matthew Wild <mwild1@gmail.com>
parents: 3889
diff changeset
87 if driver_name or (type(storage) == "string"
475fc383527d storagemanager: Don't always show fallback warning when using per-store config
Matthew Wild <mwild1@gmail.com>
parents: 3889
diff changeset
88 or type(storage) == "table" and storage[store]) then
4076
00ce99071368 storagemanager: Add new "null" provider to get used when loading a driver or opening a store fails, instead of falling back to the default driver
Matthew Wild <mwild1@gmail.com>
parents: 4075
diff changeset
89 log("warn", "Falling back to null driver for %s storage on %s", store, host);
00ce99071368 storagemanager: Add new "null" provider to get used when loading a driver or opening a store fails, instead of falling back to the default driver
Matthew Wild <mwild1@gmail.com>
parents: 4075
diff changeset
90 driver_name = "null";
00ce99071368 storagemanager: Add new "null" provider to get used when loading a driver or opening a store fails, instead of falling back to the default driver
Matthew Wild <mwild1@gmail.com>
parents: 4075
diff changeset
91 driver = null_storage_driver;
00ce99071368 storagemanager: Add new "null" provider to get used when loading a driver or opening a store fails, instead of falling back to the default driver
Matthew Wild <mwild1@gmail.com>
parents: 4075
diff changeset
92 else
00ce99071368 storagemanager: Add new "null" provider to get used when loading a driver or opening a store fails, instead of falling back to the default driver
Matthew Wild <mwild1@gmail.com>
parents: 4075
diff changeset
93 driver_name = "internal";
00ce99071368 storagemanager: Add new "null" provider to get used when loading a driver or opening a store fails, instead of falling back to the default driver
Matthew Wild <mwild1@gmail.com>
parents: 4075
diff changeset
94 driver = load_driver(host, driver_name);
3790
23cd998c765b storagemanager: Only show fallback warning if storage was configured to use another backend and it failed
Matthew Wild <mwild1@gmail.com>
parents: 3789
diff changeset
95 end
3644
22fc2063b824 storagemanager: Much refactoring and renaming of options. Untested, needs storage plugin(s) to be brought into line.
Matthew Wild <mwild1@gmail.com>
parents: 3403
diff changeset
96 end
22fc2063b824 storagemanager: Much refactoring and renaming of options. Untested, needs storage plugin(s) to be brought into line.
Matthew Wild <mwild1@gmail.com>
parents: 3403
diff changeset
97 end
22fc2063b824 storagemanager: Much refactoring and renaming of options. Untested, needs storage plugin(s) to be brought into line.
Matthew Wild <mwild1@gmail.com>
parents: 3403
diff changeset
98
22fc2063b824 storagemanager: Much refactoring and renaming of options. Untested, needs storage plugin(s) to be brought into line.
Matthew Wild <mwild1@gmail.com>
parents: 3403
diff changeset
99 local ret, err = driver:open(store, typ);
3401
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
100 if not ret then
3644
22fc2063b824 storagemanager: Much refactoring and renaming of options. Untested, needs storage plugin(s) to be brought into line.
Matthew Wild <mwild1@gmail.com>
parents: 3403
diff changeset
101 if err == "unsupported-store" then
4077
ba3ab4ecce34 storagemanager: Fix log message (s/internal/null/)
Matthew Wild <mwild1@gmail.com>
parents: 4076
diff changeset
102 log("debug", "Storage driver %s does not support store %s (%s), falling back to null driver",
3644
22fc2063b824 storagemanager: Much refactoring and renaming of options. Untested, needs storage plugin(s) to be brought into line.
Matthew Wild <mwild1@gmail.com>
parents: 3403
diff changeset
103 driver_name, store, typ);
4076
00ce99071368 storagemanager: Add new "null" provider to get used when loading a driver or opening a store fails, instead of falling back to the default driver
Matthew Wild <mwild1@gmail.com>
parents: 4075
diff changeset
104 ret = null_storage_driver;
3644
22fc2063b824 storagemanager: Much refactoring and renaming of options. Untested, needs storage plugin(s) to be brought into line.
Matthew Wild <mwild1@gmail.com>
parents: 3403
diff changeset
105 err = nil;
22fc2063b824 storagemanager: Much refactoring and renaming of options. Untested, needs storage plugin(s) to be brought into line.
Matthew Wild <mwild1@gmail.com>
parents: 3403
diff changeset
106 end
3401
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
107 end
3644
22fc2063b824 storagemanager: Much refactoring and renaming of options. Untested, needs storage plugin(s) to be brought into line.
Matthew Wild <mwild1@gmail.com>
parents: 3403
diff changeset
108 return ret, err;
3401
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
109 end
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
110
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
111 function datamanager.load(username, host, datastore)
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
112 return open(host, datastore):get(username);
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
113 end
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
114 function datamanager.store(username, host, datastore, data)
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
115 return open(host, datastore):set(username, data);
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
116 end
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
117
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
118 return _M;