Software /
code /
prosody
Annotate
core/storagemanager.lua @ 3850:39492ede0cf5
storagemanager: Hook "host-activated", to make sure we are notified about data drivers.
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Sun, 12 Dec 2010 05:14:35 +0500 |
parent | 3849:34981acbd5d5 |
child | 3734:ec59071e2a55 |
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); |
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
|
47 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
|
48 if driver_name ~= "internal" 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
|
49 modulemanager.load(host, "storage_"..driver_name); |
3789
3bd0b625f96e
storagemanager: Return driver from load_driver() if successful
Matthew Wild <mwild1@gmail.com>
parents:
3785
diff
changeset
|
50 return stores_available:get(host, driver_name); |
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
|
51 else |
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 return setmetatable({host = host}, default_driver_mt); |
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
|
53 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
|
54 end |
3401
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
55 end |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
56 |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
57 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
|
58 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
|
59 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
|
60 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
|
61 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
|
62 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
|
63 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
|
64 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
|
65 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
|
66 |
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 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
|
68 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
|
69 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
|
70 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
|
71 if not driver 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
|
72 if storage or driver_name then |
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
|
73 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
|
74 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
|
75 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
|
76 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
|
77 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
|
78 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
|
79 |
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 local ret, err = driver:open(store, typ); |
3401
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
81 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
|
82 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
|
83 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
|
84 driver_name, store, typ); |
3783
6ea353a7548a
storagemanager: Fix syntax error
Matthew Wild <mwild1@gmail.com>
parents:
3644
diff
changeset
|
85 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
|
86 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
|
87 end |
3401
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
88 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
|
89 return ret, err; |
3401
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
90 end |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
91 |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
92 function datamanager.load(username, host, datastore) |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
93 return open(host, datastore):get(username); |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
94 end |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
95 function datamanager.store(username, host, datastore, data) |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
96 return open(host, datastore):set(username, data); |
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 |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
99 return _M; |