Software /
code /
prosody
Annotate
core/storagemanager.lua @ 3784:7383ac93fb27
storagemanager: Import util.multitable again
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sat, 27 Nov 2010 21:16:32 +0000 |
parent | 3783:6ea353a7548a |
child | 3659:aa7bf12a5668 |
rev | line source |
---|---|
3401
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
1 |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
2 local error = error; |
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 |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
14 |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
15 module("storagemanager") |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
16 |
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
|
17 local default_driver_mt = { name = "internal" }; |
3401
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
18 default_driver_mt.__index = default_driver_mt; |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
19 function default_driver_mt:open(store) |
3403
b89680015b7b
storagemanager: Fixed a nil access.
Waqas Hussain <waqas20@gmail.com>
parents:
3401
diff
changeset
|
20 return setmetatable({ host = self.host, store = store }, default_driver_mt); |
3401
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
21 end |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
22 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
|
23 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
|
24 |
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
|
25 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
|
26 |
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 function initialize_host(host) |
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 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
|
29 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
|
30 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
|
31 end); |
3401
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
32 |
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
|
33 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
|
34 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
|
35 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
|
36 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
|
37 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
|
38 |
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 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
|
40 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
|
41 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
|
42 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
|
43 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
|
44 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
|
45 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
|
46 modulemanager.load(host, "storage_"..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 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
|
48 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
|
49 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
|
50 end |
3401
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
51 end |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
52 |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
53 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
|
54 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
|
55 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
|
56 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
|
57 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
|
58 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
|
59 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
|
60 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
|
61 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
|
62 |
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 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
|
64 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
|
65 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
|
66 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
|
67 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
|
68 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
|
69 log("warn", "Falling back to default driver for %s storage on %s", store, host); |
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 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
|
72 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
|
73 |
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 ret, err = driver:open(store, typ); |
3401
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
75 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
|
76 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
|
77 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
|
78 driver_name, store, typ); |
3783
6ea353a7548a
storagemanager: Fix syntax error
Matthew Wild <mwild1@gmail.com>
parents:
3644
diff
changeset
|
79 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
|
80 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
|
81 end |
3401
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
82 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
|
83 return ret, err; |
3401
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
84 end |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
85 |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
86 function datamanager.load(username, host, datastore) |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
87 return open(host, datastore):get(username); |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
88 end |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
89 function datamanager.store(username, host, datastore, data) |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
90 return open(host, datastore):set(username, data); |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
91 end |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
92 |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
93 return _M; |