Software /
code /
prosody
Annotate
core/storagemanager.lua @ 4047:1a89fd3cc954
util.json: Initial commit.
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Mon, 27 Dec 2010 19:37:04 +0500 |
parent | 3893:475fc383527d |
child | 4009:71b2c1dcf26d |
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 |
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
|
18 local default_driver_mt = { name = "internal" }; |
3401
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
19 default_driver_mt.__index = default_driver_mt; |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
20 function default_driver_mt:open(store) |
3403
b89680015b7b
storagemanager: Fixed a nil access.
Waqas Hussain <waqas20@gmail.com>
parents:
3401
diff
changeset
|
21 return setmetatable({ host = self.host, store = store }, default_driver_mt); |
3401
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
22 end |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
23 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
|
24 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
|
25 |
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
|
26 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
|
27 |
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
|
28 function initialize_host(host) |
3849
34981acbd5d5
storagemanager: Fixed a nil global access.
Waqas Hussain <waqas20@gmail.com>
parents:
3790
diff
changeset
|
29 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
|
30 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
|
31 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
|
32 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
|
33 end); |
3401
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
34 |
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
|
35 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
|
36 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
|
37 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
|
38 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
|
39 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
|
40 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
|
41 |
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
|
42 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
|
43 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
|
44 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
|
45 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
|
46 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
|
47 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
|
48 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
|
49 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
|
50 if not ok then |
2ef3d10e107b
storagemanager: Log warning when loading the storage provider plugin fails
Matthew Wild <mwild1@gmail.com>
parents:
3855
diff
changeset
|
51 log("error", "Failed to load storage driver plugin %s: %s", driver_name, err); |
2ef3d10e107b
storagemanager: Log warning when loading the storage provider plugin fails
Matthew Wild <mwild1@gmail.com>
parents:
3855
diff
changeset
|
52 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
|
53 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
|
54 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
|
55 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
|
56 end |
3401
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
57 end |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
58 |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
59 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
|
60 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
|
61 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
|
62 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
|
63 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
|
64 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
|
65 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
|
66 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
|
67 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
|
68 |
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 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
|
70 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
|
71 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
|
72 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
|
73 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
|
74 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
|
75 or type(storage) == "table" and storage[store]) then |
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
|
76 log("warn", "Falling back to default driver for %s storage on %s", store, host); |
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
|
77 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
|
78 driver_name = "internal"; |
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 = 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
|
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 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
|
82 |
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 local ret, err = driver:open(store, typ); |
3401
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
84 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
|
85 if err == "unsupported-store" 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
|
86 log("debug", "Storage driver %s does not support store %s (%s), falling back to internal driver", |
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
|
87 driver_name, store, typ); |
3783
6ea353a7548a
storagemanager: Fix syntax error
Matthew Wild <mwild1@gmail.com>
parents:
3644
diff
changeset
|
88 ret = setmetatable({ host = host, store = store }, default_driver_mt); -- default to default 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
|
89 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
|
90 end |
3401
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
91 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
|
92 return ret, err; |
3401
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
93 end |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
94 |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
95 function datamanager.load(username, host, datastore) |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
96 return open(host, datastore):get(username); |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
97 end |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
98 function datamanager.store(username, host, datastore, data) |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
99 return open(host, datastore):set(username, data); |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
100 end |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
101 |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
102 return _M; |