Software /
code /
prosody
Annotate
core/storagemanager.lua @ 3644:22fc2063b824
storagemanager: Much refactoring and renaming of options. Untested, needs storage plugin(s) to be brought into line.
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sat, 27 Nov 2010 19:46:07 +0000 |
parent | 3403:b89680015b7b |
child | 3654:8afd15a61743 |
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"; |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
8 local hosts = hosts; |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
9 local log = require "util.logger".init("storagemanager"); |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
10 |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
11 local olddm = {}; -- maintain old datamanager, for backwards compatibility |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
12 for k,v in pairs(datamanager) do olddm[k] = v; end |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
13 |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
14 module("storagemanager") |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
15 |
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
|
16 local default_driver_mt = { name = "internal" }; |
3401
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
17 default_driver_mt.__index = default_driver_mt; |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
18 function default_driver_mt:open(store) |
3403
b89680015b7b
storagemanager: Fixed a nil access.
Waqas Hussain <waqas20@gmail.com>
parents:
3401
diff
changeset
|
19 return setmetatable({ host = self.host, store = store }, default_driver_mt); |
3401
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
20 end |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
21 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
|
22 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
|
23 |
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
|
24 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
|
25 |
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 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
|
27 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
|
28 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
|
29 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
|
30 end); |
3401
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
31 |
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
|
32 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
|
33 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
|
34 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
|
35 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
|
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 |
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 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
|
39 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
|
40 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
|
41 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
|
42 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
|
43 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
|
44 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
|
45 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
|
46 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
|
47 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
|
48 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
|
49 end |
3401
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
50 end |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
51 |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
52 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
|
53 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
|
54 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
|
55 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
|
56 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
|
57 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
|
58 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
|
59 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
|
60 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
|
61 |
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 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
|
63 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
|
64 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
|
65 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
|
66 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
|
67 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
|
68 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
|
69 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 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
|
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 |
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 ret, err = driver:open(store, typ); |
3401
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
74 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
|
75 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
|
76 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
|
77 driver_name, store, typ); |
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 ret = setmetatable({ host = host, store = store }, default_driver_mt); end -- default to default 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
|
79 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
|
80 end |
3401
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
81 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
|
82 return ret, err; |
3401
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
83 end |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
84 |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
85 function datamanager.load(username, host, datastore) |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
86 return open(host, datastore):get(username); |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
87 end |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
88 function datamanager.store(username, host, datastore, data) |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
89 return open(host, datastore):set(username, data); |
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 return _M; |