Software /
code /
prosody
Annotate
core/storagemanager.lua @ 3401:2387f35db5c8
storagemanager: Initial commit.
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Fri, 23 Jul 2010 23:09:58 +0500 |
child | 3403:b89680015b7b |
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 multitable = require "util.multitable"; |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
8 local modulemanager = require "core.modulemanager"; |
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 local driver_cache = multitable.new(); |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
16 local store_cache = multitable.new(); |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
17 |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
18 module("storagemanager") |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
19 |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
20 local default_driver_mt = {}; |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
21 default_driver_mt.__index = default_driver_mt; |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
22 function default_driver_mt:open(store) |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
23 return setmetatable({ host = self.host, store = self.store }, default_driver_mt); |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
24 end |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
25 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
|
26 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
|
27 |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
28 local function load_driver_for_host(host) |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
29 if driver_cache:get(host) then return driver_cache:get(host); end |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
30 |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
31 local host_session = hosts[host]; |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
32 if not host_session then error("No such host"); end |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
33 |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
34 local driver_plugin = config.get(host, "core", "datastore"); |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
35 if not driver_plugin then return setmetatable({ host = host }, default_driver_mt); end |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
36 |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
37 local provider; |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
38 local function handler(event) provider = event.item; end |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
39 host_session.events.add_handler("item-added/data-driver", handler); |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
40 local success, err = modulemanager.load(host, driver_plugin); |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
41 host_session.events.remove_handler("item-added/data-driver", handler); |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
42 if not success then error(err); end |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
43 if not provider then error("Module didn't add a provider"); end |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
44 |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
45 driver_cache:set(host, provider); |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
46 log("debug", "Data driver '%s' loaded for host '%s'", driver_plugin, host); |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
47 return provider; |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
48 end |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
49 |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
50 function open(host, store, typ) |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
51 local ret = store_cache:get(host, store); |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
52 if not ret then |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
53 local driver = load_driver_for_host(host); |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
54 ret = driver:open(store, typ); |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
55 if not ret then ret = setmetatable({ host = host, store = store }, default_driver_mt); end -- default to default driver |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
56 store_cache:set(host, store, ret); |
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 return ret; |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
59 end |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
60 |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
61 function datamanager.load(username, host, datastore) |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
62 return open(host, datastore):get(username); |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
63 end |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
64 function datamanager.store(username, host, datastore, data) |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
65 return open(host, datastore):set(username, data); |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
66 end |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
67 |
2387f35db5c8
storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
68 return _M; |