Annotate

core/storagemanager.lua @ 8706:e2919978673e

net.http: Fix parameter order to http request callbacks Commit e3b9dc9dd940 changed the parameter order in 2013, but did not update the names of the parameters in the callback function. Due to this inconsistency, 12df41a5a4b1 accidentally reversed the order when fixing the variable names without fixing where they are used. Additionally the documentation was incorrect (since 2013), and this has also now been fixed.
author Matthew Wild <mwild1@gmail.com>
date Wed, 04 Apr 2018 18:27:44 +0100
parent 7994:3325ac397f17
child 7999:980606856882
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
6552
bcb834728ee5 storagemanager: Remove unused import of error()
Matthew Wild <mwild1@gmail.com>
parents: 5776
diff changeset
2 local type, pairs = 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
6779
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6663
diff changeset
14 local _ENV = nil;
3401
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
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
19 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
20 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
21 {
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 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
23 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
24 }, {
6663
d3023dd07cb6 portmanager, s2smanager, sessionmanager, stanza_router, storagemanager, usermanager, util.xml: Add luacheck annotations
Matthew Wild <mwild1@gmail.com>
parents: 6552
diff changeset
25 __index = function (self, method) --luacheck: ignore 212
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
26 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
27 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
28 }
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
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
31 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
32
6779
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6663
diff changeset
33 local function initialize_host(host)
3727
1bbd655975ca storagemanager: Fixed a nil global access.
Waqas Hussain <waqas20@gmail.com>
parents: 3662
diff changeset
34 local host_session = hosts[host];
5121
b5a5643f8572 core.storagemanager, mod_storage_*: "data-driver" -> "storage-provider", to allow using module:provides().
Waqas Hussain <waqas20@gmail.com>
parents: 5111
diff changeset
35 host_session.events.add_handler("item-added/storage-provider", function (event)
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 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, 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 end);
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5502
diff changeset
39
5121
b5a5643f8572 core.storagemanager, mod_storage_*: "data-driver" -> "storage-provider", to allow using module:provides().
Waqas Hussain <waqas20@gmail.com>
parents: 5111
diff changeset
40 host_session.events.add_handler("item-removed/storage-provider", function (event)
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 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
42 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
43 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
44 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
45 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
46
6779
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6663
diff changeset
47 local 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
48 if driver_name == "null" then
4758
b8b050e76ee1 storagemanager: Fix incorrect variable name
Matthew Wild <mwild1@gmail.com>
parents: 4115
diff changeset
49 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
50 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
51 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
52 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
53 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
54 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
55 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
56 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
57 return stores_available:get(host, driver_name);
3401
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
58 end
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
59
6949
1c2c3d913172 storagemanager: Split config retrieval into its own function
Matthew Wild <mwild1@gmail.com>
parents: 6809
diff changeset
60 local function get_storage_config(host)
6951
99de8f30d99e storagemanager, mod_storage_sql, mod_storage_sql2: Rename mod_storage_sql2 -> mod_storage_sql, mod_storage_sql -> mod_storage_sql1 (temporarily), and emit warning for configs using sql2
Matthew Wild <mwild1@gmail.com>
parents: 6949
diff changeset
61 -- COMPAT w/ unreleased Prosody 0.10 and the once-experimental mod_storage_sql2 in peoples' config files
99de8f30d99e storagemanager, mod_storage_sql, mod_storage_sql2: Rename mod_storage_sql2 -> mod_storage_sql, mod_storage_sql -> mod_storage_sql1 (temporarily), and emit warning for configs using sql2
Matthew Wild <mwild1@gmail.com>
parents: 6949
diff changeset
62 local storage_config = config.get(host, "storage");
99de8f30d99e storagemanager, mod_storage_sql, mod_storage_sql2: Rename mod_storage_sql2 -> mod_storage_sql, mod_storage_sql -> mod_storage_sql1 (temporarily), and emit warning for configs using sql2
Matthew Wild <mwild1@gmail.com>
parents: 6949
diff changeset
63 local found_sql2;
99de8f30d99e storagemanager, mod_storage_sql, mod_storage_sql2: Rename mod_storage_sql2 -> mod_storage_sql, mod_storage_sql -> mod_storage_sql1 (temporarily), and emit warning for configs using sql2
Matthew Wild <mwild1@gmail.com>
parents: 6949
diff changeset
64 if storage_config == "sql2" then
99de8f30d99e storagemanager, mod_storage_sql, mod_storage_sql2: Rename mod_storage_sql2 -> mod_storage_sql, mod_storage_sql -> mod_storage_sql1 (temporarily), and emit warning for configs using sql2
Matthew Wild <mwild1@gmail.com>
parents: 6949
diff changeset
65 storage_config, found_sql2 = "sql", true;
99de8f30d99e storagemanager, mod_storage_sql, mod_storage_sql2: Rename mod_storage_sql2 -> mod_storage_sql, mod_storage_sql -> mod_storage_sql1 (temporarily), and emit warning for configs using sql2
Matthew Wild <mwild1@gmail.com>
parents: 6949
diff changeset
66 elseif type(storage_config) == "table" then
99de8f30d99e storagemanager, mod_storage_sql, mod_storage_sql2: Rename mod_storage_sql2 -> mod_storage_sql, mod_storage_sql -> mod_storage_sql1 (temporarily), and emit warning for configs using sql2
Matthew Wild <mwild1@gmail.com>
parents: 6949
diff changeset
67 for store_name, driver_name in pairs(storage_config) do
99de8f30d99e storagemanager, mod_storage_sql, mod_storage_sql2: Rename mod_storage_sql2 -> mod_storage_sql, mod_storage_sql -> mod_storage_sql1 (temporarily), and emit warning for configs using sql2
Matthew Wild <mwild1@gmail.com>
parents: 6949
diff changeset
68 if driver_name == "sql2" then
99de8f30d99e storagemanager, mod_storage_sql, mod_storage_sql2: Rename mod_storage_sql2 -> mod_storage_sql, mod_storage_sql -> mod_storage_sql1 (temporarily), and emit warning for configs using sql2
Matthew Wild <mwild1@gmail.com>
parents: 6949
diff changeset
69 storage_config[store_name] = "sql";
99de8f30d99e storagemanager, mod_storage_sql, mod_storage_sql2: Rename mod_storage_sql2 -> mod_storage_sql, mod_storage_sql -> mod_storage_sql1 (temporarily), and emit warning for configs using sql2
Matthew Wild <mwild1@gmail.com>
parents: 6949
diff changeset
70 found_sql2 = true;
99de8f30d99e storagemanager, mod_storage_sql, mod_storage_sql2: Rename mod_storage_sql2 -> mod_storage_sql, mod_storage_sql -> mod_storage_sql1 (temporarily), and emit warning for configs using sql2
Matthew Wild <mwild1@gmail.com>
parents: 6949
diff changeset
71 end
99de8f30d99e storagemanager, mod_storage_sql, mod_storage_sql2: Rename mod_storage_sql2 -> mod_storage_sql, mod_storage_sql -> mod_storage_sql1 (temporarily), and emit warning for configs using sql2
Matthew Wild <mwild1@gmail.com>
parents: 6949
diff changeset
72 end
99de8f30d99e storagemanager, mod_storage_sql, mod_storage_sql2: Rename mod_storage_sql2 -> mod_storage_sql, mod_storage_sql -> mod_storage_sql1 (temporarily), and emit warning for configs using sql2
Matthew Wild <mwild1@gmail.com>
parents: 6949
diff changeset
73 end
99de8f30d99e storagemanager, mod_storage_sql, mod_storage_sql2: Rename mod_storage_sql2 -> mod_storage_sql, mod_storage_sql -> mod_storage_sql1 (temporarily), and emit warning for configs using sql2
Matthew Wild <mwild1@gmail.com>
parents: 6949
diff changeset
74 if found_sql2 then
7947
24170d74b00b core: Split some very long lines [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7645
diff changeset
75 log("error", "The temporary 'sql2' storage module has now been renamed to 'sql', "
24170d74b00b core: Split some very long lines [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7645
diff changeset
76 .."please update your config file: https://prosody.im/doc/modules/mod_storage_sql2");
6951
99de8f30d99e storagemanager, mod_storage_sql, mod_storage_sql2: Rename mod_storage_sql2 -> mod_storage_sql, mod_storage_sql -> mod_storage_sql1 (temporarily), and emit warning for configs using sql2
Matthew Wild <mwild1@gmail.com>
parents: 6949
diff changeset
77 end
99de8f30d99e storagemanager, mod_storage_sql, mod_storage_sql2: Rename mod_storage_sql2 -> mod_storage_sql, mod_storage_sql -> mod_storage_sql1 (temporarily), and emit warning for configs using sql2
Matthew Wild <mwild1@gmail.com>
parents: 6949
diff changeset
78 return storage_config;
6949
1c2c3d913172 storagemanager: Split config retrieval into its own function
Matthew Wild <mwild1@gmail.com>
parents: 6809
diff changeset
79 end
1c2c3d913172 storagemanager: Split config retrieval into its own function
Matthew Wild <mwild1@gmail.com>
parents: 6809
diff changeset
80
6779
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6663
diff changeset
81 local function get_driver(host, store)
6949
1c2c3d913172 storagemanager: Split config retrieval into its own function
Matthew Wild <mwild1@gmail.com>
parents: 6809
diff changeset
82 local storage = get_storage_config(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
83 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
84 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
85 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
86 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
87 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
88 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
89 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
90 if not driver_name then
5131
0cd962661fa2 storagemanager: Remove usage of 'core' when calling configmanager.get()
Matthew Wild <mwild1@gmail.com>
parents: 5130
diff changeset
91 driver_name = config.get(host, "default_storage") or "internal";
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
92 end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5502
diff changeset
93
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 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
95 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
96 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
97 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
98 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
99 end
5036
be33164aa97e storagemanager: Split out driver choosing from the open() method
Kim Alvefur <zash@zash.se>
parents: 4758
diff changeset
100 return driver, driver_name;
5110
72a7427368f8 storagemanager: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 5041
diff changeset
101 end
72a7427368f8 storagemanager: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 5041
diff changeset
102
7150
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
103 local map_shim_mt = {
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
104 __index = {
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
105 get = function(self, username, key)
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
106 local ret, err = self.keyval_store:get(username);
7151
584d5229cb91 storagemanager: Fix map store shim if store is empty
Kim Alvefur <zash@zash.se>
parents: 7150
diff changeset
107 if ret == nil then return nil, err end
7150
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
108 return ret[key];
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
109 end;
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
110 set = function(self, username, key, data)
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
111 local current, err = self.keyval_store:get(username);
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
112 if current == nil then
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
113 if err then
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
114 return nil, err;
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
115 else
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
116 current = {};
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
117 end
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
118 end
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
119 current[key] = data;
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
120 return self.keyval_store:set(username, current);
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
121 end;
7152
ca64255bf7cd storagemanager: Add set_keys method to map store shim
Kim Alvefur <zash@zash.se>
parents: 7151
diff changeset
122 set_keys = function (self, username, keydatas)
ca64255bf7cd storagemanager: Add set_keys method to map store shim
Kim Alvefur <zash@zash.se>
parents: 7151
diff changeset
123 local current, err = self.keyval_store:get(username);
ca64255bf7cd storagemanager: Add set_keys method to map store shim
Kim Alvefur <zash@zash.se>
parents: 7151
diff changeset
124 if current == nil then
ca64255bf7cd storagemanager: Add set_keys method to map store shim
Kim Alvefur <zash@zash.se>
parents: 7151
diff changeset
125 if err then
ca64255bf7cd storagemanager: Add set_keys method to map store shim
Kim Alvefur <zash@zash.se>
parents: 7151
diff changeset
126 return nil, err;
ca64255bf7cd storagemanager: Add set_keys method to map store shim
Kim Alvefur <zash@zash.se>
parents: 7151
diff changeset
127 end
7244
8c6943918279 storagemanager: Fix saving data in map shim when no prior data exists
Kim Alvefur <zash@zash.se>
parents: 7152
diff changeset
128 current = {};
8c6943918279 storagemanager: Fix saving data in map shim when no prior data exists
Kim Alvefur <zash@zash.se>
parents: 7152
diff changeset
129 end
8c6943918279 storagemanager: Fix saving data in map shim when no prior data exists
Kim Alvefur <zash@zash.se>
parents: 7152
diff changeset
130 for k,v in pairs(keydatas) do
8c6943918279 storagemanager: Fix saving data in map shim when no prior data exists
Kim Alvefur <zash@zash.se>
parents: 7152
diff changeset
131 if v == self.remove then v = nil; end
8c6943918279 storagemanager: Fix saving data in map shim when no prior data exists
Kim Alvefur <zash@zash.se>
parents: 7152
diff changeset
132 current[k] = v;
7152
ca64255bf7cd storagemanager: Add set_keys method to map store shim
Kim Alvefur <zash@zash.se>
parents: 7151
diff changeset
133 end
ca64255bf7cd storagemanager: Add set_keys method to map store shim
Kim Alvefur <zash@zash.se>
parents: 7151
diff changeset
134 return self.keyval_store:set(username, current);
ca64255bf7cd storagemanager: Add set_keys method to map store shim
Kim Alvefur <zash@zash.se>
parents: 7151
diff changeset
135 end;
ca64255bf7cd storagemanager: Add set_keys method to map store shim
Kim Alvefur <zash@zash.se>
parents: 7151
diff changeset
136 remove = {};
7150
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
137 };
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
138 }
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
139
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
140 local open;
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
141
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
142 local function create_map_shim(host, store)
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
143 local keyval_store, err = open(host, store, "keyval");
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
144 if keyval_store == nil then return nil, err end
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
145 return setmetatable({
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
146 keyval_store = keyval_store;
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
147 }, map_shim_mt);
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
148 end
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
149
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
150 function open(host, store, typ)
5036
be33164aa97e storagemanager: Split out driver choosing from the open() method
Kim Alvefur <zash@zash.se>
parents: 4758
diff changeset
151 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
152 local ret, err = driver:open(store, typ);
3401
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
153 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
154 if err == "unsupported-store" then
7150
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
155 if typ == "map" then -- Use shim on top of keyval store
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
156 log("debug", "map storage driver unavailable, using shim on top of keyval store.");
7643
44fe2aaf817e storagemanager: Simplify function flow
Matthew Wild <mwild1@gmail.com>
parents: 7325
diff changeset
157 ret, err = create_map_shim(host, store);
44fe2aaf817e storagemanager: Simplify function flow
Matthew Wild <mwild1@gmail.com>
parents: 7325
diff changeset
158 else
44fe2aaf817e storagemanager: Simplify function flow
Matthew Wild <mwild1@gmail.com>
parents: 7325
diff changeset
159 log("debug", "Storage driver %s does not support store %s (%s), falling back to null driver",
44fe2aaf817e storagemanager: Simplify function flow
Matthew Wild <mwild1@gmail.com>
parents: 7325
diff changeset
160 driver_name, store, typ or "<nil>");
44fe2aaf817e storagemanager: Simplify function flow
Matthew Wild <mwild1@gmail.com>
parents: 7325
diff changeset
161 ret, err = null_storage_driver, nil;
7150
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
162 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
163 end
3401
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
164 end
7644
90a4790c2329 storagemanager: Fire event when opening a store, and allow the returned store/err to be overridden
Matthew Wild <mwild1@gmail.com>
parents: 7643
diff changeset
165 if ret then
90a4790c2329 storagemanager: Fire event when opening a store, and allow the returned store/err to be overridden
Matthew Wild <mwild1@gmail.com>
parents: 7643
diff changeset
166 local event_data = { host = host, store_name = store, store_type = typ, store = ret };
7994
3325ac397f17 storagemanager: Use the existing local reference to 'hosts'
Kim Alvefur <zash@zash.se>
parents: 7947
diff changeset
167 hosts[host].events.fire_event("store-opened", event_data);
7644
90a4790c2329 storagemanager: Fire event when opening a store, and allow the returned store/err to be overridden
Matthew Wild <mwild1@gmail.com>
parents: 7643
diff changeset
168 ret, err = event_data.store, event_data.store_err;
90a4790c2329 storagemanager: Fire event when opening a store, and allow the returned store/err to be overridden
Matthew Wild <mwild1@gmail.com>
parents: 7643
diff changeset
169 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
170 return ret, err;
3401
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
171 end
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
172
6779
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6663
diff changeset
173 local function purge(user, host)
6949
1c2c3d913172 storagemanager: Split config retrieval into its own function
Matthew Wild <mwild1@gmail.com>
parents: 6809
diff changeset
174 local storage = get_storage_config(host);
5129
e8253c931166 storagemanager: Add purge() for purging user data from all backends in use
Kim Alvefur <zash@zash.se>
parents: 5111
diff changeset
175 if type(storage) == "table" then
e8253c931166 storagemanager: Add purge() for purging user data from all backends in use
Kim Alvefur <zash@zash.se>
parents: 5111
diff changeset
176 -- multiple storage backends in use that we need to purge
e8253c931166 storagemanager: Add purge() for purging user data from all backends in use
Kim Alvefur <zash@zash.se>
parents: 5111
diff changeset
177 local purged = {};
6809
dd6b21862e3b storagemanager: Check if drivers support purging, warn otherwise
Kim Alvefur <zash@zash.se>
parents: 6807
diff changeset
178 for store, driver_name in pairs(storage) do
dd6b21862e3b storagemanager: Check if drivers support purging, warn otherwise
Kim Alvefur <zash@zash.se>
parents: 6807
diff changeset
179 if not purged[driver_name] then
dd6b21862e3b storagemanager: Check if drivers support purging, warn otherwise
Kim Alvefur <zash@zash.se>
parents: 6807
diff changeset
180 local driver = get_driver(host, store);
dd6b21862e3b storagemanager: Check if drivers support purging, warn otherwise
Kim Alvefur <zash@zash.se>
parents: 6807
diff changeset
181 if driver.purge then
dd6b21862e3b storagemanager: Check if drivers support purging, warn otherwise
Kim Alvefur <zash@zash.se>
parents: 6807
diff changeset
182 purged[driver_name] = driver:purge(user);
dd6b21862e3b storagemanager: Check if drivers support purging, warn otherwise
Kim Alvefur <zash@zash.se>
parents: 6807
diff changeset
183 else
7947
24170d74b00b core: Split some very long lines [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7645
diff changeset
184 log("warn", "Storage driver %s does not support removing all user data, "
24170d74b00b core: Split some very long lines [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7645
diff changeset
185 .."you may need to delete it manually", driver_name);
6809
dd6b21862e3b storagemanager: Check if drivers support purging, warn otherwise
Kim Alvefur <zash@zash.se>
parents: 6807
diff changeset
186 end
5129
e8253c931166 storagemanager: Add purge() for purging user data from all backends in use
Kim Alvefur <zash@zash.se>
parents: 5111
diff changeset
187 end
e8253c931166 storagemanager: Add purge() for purging user data from all backends in use
Kim Alvefur <zash@zash.se>
parents: 5111
diff changeset
188 end
e8253c931166 storagemanager: Add purge() for purging user data from all backends in use
Kim Alvefur <zash@zash.se>
parents: 5111
diff changeset
189 end
e8253c931166 storagemanager: Add purge() for purging user data from all backends in use
Kim Alvefur <zash@zash.se>
parents: 5111
diff changeset
190 get_driver(host):purge(user); -- and the default driver
e8253c931166 storagemanager: Add purge() for purging user data from all backends in use
Kim Alvefur <zash@zash.se>
parents: 5111
diff changeset
191
e8253c931166 storagemanager: Add purge() for purging user data from all backends in use
Kim Alvefur <zash@zash.se>
parents: 5111
diff changeset
192 olddm.purge(user, host); -- COMPAT list stores, like offline messages end up in the old datamanager
e8253c931166 storagemanager: Add purge() for purging user data from all backends in use
Kim Alvefur <zash@zash.se>
parents: 5111
diff changeset
193
e8253c931166 storagemanager: Add purge() for purging user data from all backends in use
Kim Alvefur <zash@zash.se>
parents: 5111
diff changeset
194 return true;
e8253c931166 storagemanager: Add purge() for purging user data from all backends in use
Kim Alvefur <zash@zash.se>
parents: 5111
diff changeset
195 end
e8253c931166 storagemanager: Add purge() for purging user data from all backends in use
Kim Alvefur <zash@zash.se>
parents: 5111
diff changeset
196
3401
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
197 function datamanager.load(username, host, datastore)
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
198 return open(host, datastore):get(username);
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
199 end
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
200 function datamanager.store(username, host, datastore, data)
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
201 return open(host, datastore):set(username, data);
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
202 end
5155
a207d4bff5a4 storagemanager: Support for iterating over users
Kim Alvefur <zash@zash.se>
parents: 5137
diff changeset
203 function datamanager.users(host, datastore, typ)
a207d4bff5a4 storagemanager: Support for iterating over users
Kim Alvefur <zash@zash.se>
parents: 5137
diff changeset
204 local driver = open(host, datastore, typ);
a207d4bff5a4 storagemanager: Support for iterating over users
Kim Alvefur <zash@zash.se>
parents: 5137
diff changeset
205 if not driver.users then
7325
7baf1b14defb storagemanager: Capitalize log message
Kim Alvefur <zash@zash.se>
parents: 7244
diff changeset
206 return function() log("warn", "Storage driver %s does not support listing users", driver.name) end
5155
a207d4bff5a4 storagemanager: Support for iterating over users
Kim Alvefur <zash@zash.se>
parents: 5137
diff changeset
207 end
a207d4bff5a4 storagemanager: Support for iterating over users
Kim Alvefur <zash@zash.se>
parents: 5137
diff changeset
208 return driver:users();
a207d4bff5a4 storagemanager: Support for iterating over users
Kim Alvefur <zash@zash.se>
parents: 5137
diff changeset
209 end
5130
051d352ed03c storagemanager, datamanager, mod_storage_{internal,sql}: Replace list_stores() with an iterator version
Kim Alvefur <zash@zash.se>
parents: 5129
diff changeset
210 function datamanager.stores(username, host, typ)
051d352ed03c storagemanager, datamanager, mod_storage_{internal,sql}: Replace list_stores() with an iterator version
Kim Alvefur <zash@zash.se>
parents: 5129
diff changeset
211 return get_driver(host):stores(username, typ);
5037
c34fdcae6d29 storagemanager: Add method for listing stores
Kim Alvefur <zash@zash.se>
parents: 5036
diff changeset
212 end
5041
be204204cc5f storagemanager: Add method for removing all data belonging to a user
Kim Alvefur <zash@zash.se>
parents: 5037
diff changeset
213 function datamanager.purge(username, host)
5136
77ea38607a89 storagemanager: Fix argument (Thanks Maranda)
Kim Alvefur <zash@zash.se>
parents: 5133
diff changeset
214 return purge(username, host);
5041
be204204cc5f storagemanager: Add method for removing all data belonging to a user
Kim Alvefur <zash@zash.se>
parents: 5037
diff changeset
215 end
3401
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
216
6779
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6663
diff changeset
217 return {
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6663
diff changeset
218 initialize_host = initialize_host;
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6663
diff changeset
219 load_driver = load_driver;
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6663
diff changeset
220 get_driver = get_driver;
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6663
diff changeset
221 open = open;
6807
5e3242d349f2 storagemanager: Export purge (fixes deleting users from usermanager) (thanks mt)
Kim Alvefur <zash@zash.se>
parents: 6779
diff changeset
222 purge = purge;
6779
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6663
diff changeset
223
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6663
diff changeset
224 olddm = olddm;
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6663
diff changeset
225 };