Annotate

core/storagemanager.lua @ 5092:a89f8f2f2943

mod_http: Don't produce paths with double / if a module is set to serve /
author Kim Alvefur <zash@zash.se>
date Tue, 21 Aug 2012 21:03:20 +0200
parent 5041:be204204cc5f
child 5110:72a7427368f8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3401
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
1
4085
7699cef04740 storagemanager, mod_storage_internal: Split out default driver to mod_storage_internal, and greatly simplify storagemanager's error handling and fallback code
Matthew Wild <mwild1@gmail.com>
parents: 4011
diff changeset
2 local error, type, pairs = error, type, pairs;
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";
3655
9a590b03a8d6 storagemanager: Import util.multitable again
Matthew Wild <mwild1@gmail.com>
parents: 3654
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
3728
b1b8fe846d68 storagemanager: Hook "host-activated", to make sure we are notified about data drivers.
Waqas Hussain <waqas20@gmail.com>
parents: 3727
diff changeset
12 local prosody = prosody;
3401
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
4085
7699cef04740 storagemanager, mod_storage_internal: Split out default driver to mod_storage_internal, and greatly simplify storagemanager's error handling and fallback code
Matthew Wild <mwild1@gmail.com>
parents: 4011
diff changeset
16 local olddm = {}; -- maintain old datamanager, for backwards compatibility
7699cef04740 storagemanager, mod_storage_internal: Split out default driver to mod_storage_internal, and greatly simplify storagemanager's error handling and fallback code
Matthew Wild <mwild1@gmail.com>
parents: 4011
diff changeset
17 for k,v in pairs(datamanager) do olddm[k] = v; end
7699cef04740 storagemanager, mod_storage_internal: Split out default driver to mod_storage_internal, and greatly simplify storagemanager's error handling and fallback code
Matthew Wild <mwild1@gmail.com>
parents: 4011
diff changeset
18 _M.olddm = olddm;
7699cef04740 storagemanager, mod_storage_internal: Split out default driver to mod_storage_internal, and greatly simplify storagemanager's error handling and fallback code
Matthew Wild <mwild1@gmail.com>
parents: 4011
diff changeset
19
4010
21311bd31f6b storagemanager: Add new "null" provider to get used when loading a driver or opening a store fails, instead of falling back to the default driver
Matthew Wild <mwild1@gmail.com>
parents: 4009
diff changeset
20 local null_storage_method = function () return false, "no data storage active"; end
21311bd31f6b storagemanager: Add new "null" provider to get used when loading a driver or opening a store fails, instead of falling back to the default driver
Matthew Wild <mwild1@gmail.com>
parents: 4009
diff changeset
21 local null_storage_driver = setmetatable(
21311bd31f6b storagemanager: Add new "null" provider to get used when loading a driver or opening a store fails, instead of falling back to the default driver
Matthew Wild <mwild1@gmail.com>
parents: 4009
diff changeset
22 {
21311bd31f6b storagemanager: Add new "null" provider to get used when loading a driver or opening a store fails, instead of falling back to the default driver
Matthew Wild <mwild1@gmail.com>
parents: 4009
diff changeset
23 name = "null",
21311bd31f6b storagemanager: Add new "null" provider to get used when loading a driver or opening a store fails, instead of falling back to the default driver
Matthew Wild <mwild1@gmail.com>
parents: 4009
diff changeset
24 open = function (self) return self; end
21311bd31f6b storagemanager: Add new "null" provider to get used when loading a driver or opening a store fails, instead of falling back to the default driver
Matthew Wild <mwild1@gmail.com>
parents: 4009
diff changeset
25 }, {
21311bd31f6b storagemanager: Add new "null" provider to get used when loading a driver or opening a store fails, instead of falling back to the default driver
Matthew Wild <mwild1@gmail.com>
parents: 4009
diff changeset
26 __index = function (self, method)
21311bd31f6b storagemanager: Add new "null" provider to get used when loading a driver or opening a store fails, instead of falling back to the default driver
Matthew Wild <mwild1@gmail.com>
parents: 4009
diff changeset
27 return null_storage_method;
21311bd31f6b storagemanager: Add new "null" provider to get used when loading a driver or opening a store fails, instead of falling back to the default driver
Matthew Wild <mwild1@gmail.com>
parents: 4009
diff changeset
28 end
21311bd31f6b storagemanager: Add new "null" provider to get used when loading a driver or opening a store fails, instead of falling back to the default driver
Matthew Wild <mwild1@gmail.com>
parents: 4009
diff changeset
29 }
21311bd31f6b storagemanager: Add new "null" provider to get used when loading a driver or opening a store fails, instead of falling back to the default driver
Matthew Wild <mwild1@gmail.com>
parents: 4009
diff changeset
30 );
21311bd31f6b storagemanager: Add new "null" provider to get used when loading a driver or opening a store fails, instead of falling back to the default driver
Matthew Wild <mwild1@gmail.com>
parents: 4009
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 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
33
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 function initialize_host(host)
3727
1bbd655975ca storagemanager: Fixed a nil global access.
Waqas Hussain <waqas20@gmail.com>
parents: 3662
diff changeset
35 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
36 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
37 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
38 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
39 end);
3401
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
40
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 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
42 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
43 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
44 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
45 end
3728
b1b8fe846d68 storagemanager: Hook "host-activated", to make sure we are notified about data drivers.
Waqas Hussain <waqas20@gmail.com>
parents: 3727
diff changeset
46 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
47
4115
801725a32c96 storagemanager: Export load_driver() function so it can be used from e.g. migrators
Matthew Wild <mwild1@gmail.com>
parents: 4085
diff changeset
48 function load_driver(host, driver_name)
4085
7699cef04740 storagemanager, mod_storage_internal: Split out default driver to mod_storage_internal, and greatly simplify storagemanager's error handling and fallback code
Matthew Wild <mwild1@gmail.com>
parents: 4011
diff changeset
49 if driver_name == "null" then
4758
b8b050e76ee1 storagemanager: Fix incorrect variable name
Matthew Wild <mwild1@gmail.com>
parents: 4115
diff changeset
50 return null_storage_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
51 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
52 local driver = stores_available:get(host, driver_name);
3734
ec59071e2a55 storagemanager: When we have a cached data driver, we are supposed to use it.
Waqas Hussain <waqas20@gmail.com>
parents: 3728
diff changeset
53 if driver then return driver; end
4085
7699cef04740 storagemanager, mod_storage_internal: Split out default driver to mod_storage_internal, and greatly simplify storagemanager's error handling and fallback code
Matthew Wild <mwild1@gmail.com>
parents: 4011
diff changeset
54 local ok, err = modulemanager.load(host, "storage_"..driver_name);
7699cef04740 storagemanager, mod_storage_internal: Split out default driver to mod_storage_internal, and greatly simplify storagemanager's error handling and fallback code
Matthew Wild <mwild1@gmail.com>
parents: 4011
diff changeset
55 if not ok then
7699cef04740 storagemanager, mod_storage_internal: Split out default driver to mod_storage_internal, and greatly simplify storagemanager's error handling and fallback code
Matthew Wild <mwild1@gmail.com>
parents: 4011
diff changeset
56 log("error", "Failed to load storage driver plugin %s on %s: %s", driver_name, host, err);
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
57 end
4085
7699cef04740 storagemanager, mod_storage_internal: Split out default driver to mod_storage_internal, and greatly simplify storagemanager's error handling and fallback code
Matthew Wild <mwild1@gmail.com>
parents: 4011
diff changeset
58 return stores_available:get(host, driver_name);
3401
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
5036
be33164aa97e storagemanager: Split out driver choosing from the open() method
Kim Alvefur <zash@zash.se>
parents: 4758
diff changeset
61 function get_driver(host, store)
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
62 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
63 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
64 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
65 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
66 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
67 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
68 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
69 end
4085
7699cef04740 storagemanager, mod_storage_internal: Split out default driver to mod_storage_internal, and greatly simplify storagemanager's error handling and fallback code
Matthew Wild <mwild1@gmail.com>
parents: 4011
diff changeset
70 if not driver_name then
7699cef04740 storagemanager, mod_storage_internal: Split out default driver to mod_storage_internal, and greatly simplify storagemanager's error handling and fallback code
Matthew Wild <mwild1@gmail.com>
parents: 4011
diff changeset
71 driver_name = config.get(host, "core", "default_storage") or "internal";
7699cef04740 storagemanager, mod_storage_internal: Split out default driver to mod_storage_internal, and greatly simplify storagemanager's error handling and fallback code
Matthew Wild <mwild1@gmail.com>
parents: 4011
diff changeset
72 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
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 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
75 if not driver then
4085
7699cef04740 storagemanager, mod_storage_internal: Split out default driver to mod_storage_internal, and greatly simplify storagemanager's error handling and fallback code
Matthew Wild <mwild1@gmail.com>
parents: 4011
diff changeset
76 log("warn", "Falling back to null driver for %s storage on %s", store, host);
7699cef04740 storagemanager, mod_storage_internal: Split out default driver to mod_storage_internal, and greatly simplify storagemanager's error handling and fallback code
Matthew Wild <mwild1@gmail.com>
parents: 4011
diff changeset
77 driver_name = "null";
7699cef04740 storagemanager, mod_storage_internal: Split out default driver to mod_storage_internal, and greatly simplify storagemanager's error handling and fallback code
Matthew Wild <mwild1@gmail.com>
parents: 4011
diff changeset
78 driver = null_storage_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
79 end
5036
be33164aa97e storagemanager: Split out driver choosing from the open() method
Kim Alvefur <zash@zash.se>
parents: 4758
diff changeset
80 return driver, driver_name;
be33164aa97e storagemanager: Split out driver choosing from the open() method
Kim Alvefur <zash@zash.se>
parents: 4758
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
5036
be33164aa97e storagemanager: Split out driver choosing from the open() method
Kim Alvefur <zash@zash.se>
parents: 4758
diff changeset
83 function open(host, store, typ)
be33164aa97e storagemanager: Split out driver choosing from the open() method
Kim Alvefur <zash@zash.se>
parents: 4758
diff changeset
84 local driver, driver_name = get_driver(host, store);
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 local ret, err = driver:open(store, typ);
3401
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
86 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
87 if err == "unsupported-store" then
4011
0df6b99cb74a storagemanager: Fix log message (s/internal/null/)
Matthew Wild <mwild1@gmail.com>
parents: 4010
diff changeset
88 log("debug", "Storage driver %s does not support store %s (%s), falling back to null 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 driver_name, store, typ);
4010
21311bd31f6b storagemanager: Add new "null" provider to get used when loading a driver or opening a store fails, instead of falling back to the default driver
Matthew Wild <mwild1@gmail.com>
parents: 4009
diff changeset
90 ret = null_storage_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
91 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
92 end
3401
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
93 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
94 return ret, err;
3401
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
95 end
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
96
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
97 function datamanager.load(username, host, datastore)
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
98 return open(host, datastore):get(username);
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
99 end
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
100 function datamanager.store(username, host, datastore, data)
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
101 return open(host, datastore):set(username, data);
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
102 end
5037
c34fdcae6d29 storagemanager: Add method for listing stores
Kim Alvefur <zash@zash.se>
parents: 5036
diff changeset
103 function datamanager.list_stores(username, host)
c34fdcae6d29 storagemanager: Add method for listing stores
Kim Alvefur <zash@zash.se>
parents: 5036
diff changeset
104 return get_driver(host):list_stores(username, host);
c34fdcae6d29 storagemanager: Add method for listing stores
Kim Alvefur <zash@zash.se>
parents: 5036
diff changeset
105 end
5041
be204204cc5f storagemanager: Add method for removing all data belonging to a user
Kim Alvefur <zash@zash.se>
parents: 5037
diff changeset
106 function datamanager.purge(username, host)
be204204cc5f storagemanager: Add method for removing all data belonging to a user
Kim Alvefur <zash@zash.se>
parents: 5037
diff changeset
107 return get_driver(host):purge(username, host);
be204204cc5f storagemanager: Add method for removing all data belonging to a user
Kim Alvefur <zash@zash.se>
parents: 5037
diff changeset
108 end
3401
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
109
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
110 return _M;