Annotate

core/storagemanager.lua @ 13027:012d6e7b723a

integration tests: Preserve unmocked time.monotonic() With monotonic() frozen, timers may fail to trigger. This caused problems after the new util.startup changes that moved the server-started event to a timer. The timer wouldn't trigger, the event didn't fire, and prosody would fail to daemonize. All the tests that depend on specific time behaviour are depending on wall clock time, so only mocking util.time.now() and os.time() fixes those.
author Matthew Wild <mwild1@gmail.com>
date Thu, 06 Apr 2023 14:00:54 +0100
parent 12972:ead41e25ebc0
child 13258:c8c0cfb7f5df
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;
8670
800c648827e3 storagemanager: Remove unused variable [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8654
diff changeset
4 local rawset = rawset;
3401
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
5
12972
ead41e25ebc0 core: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12956
diff changeset
6 local config = require "prosody.core.configmanager";
ead41e25ebc0 core: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12956
diff changeset
7 local datamanager = require "prosody.util.datamanager";
ead41e25ebc0 core: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12956
diff changeset
8 local modulemanager = require "prosody.core.modulemanager";
ead41e25ebc0 core: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12956
diff changeset
9 local multitable = require "prosody.util.multitable";
ead41e25ebc0 core: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12956
diff changeset
10 local log = require "prosody.util.logger".init("storagemanager");
ead41e25ebc0 core: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12956
diff changeset
11 local async = require "prosody.util.async";
8654
425de10efde4 storagemanager: Log warning on storage access outside of async contexts
Matthew Wild <mwild1@gmail.com>
parents: 8555
diff changeset
12 local debug = debug;
3401
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
13
3728
b1b8fe846d68 storagemanager: Hook "host-activated", to make sure we are notified about data drivers.
Waqas Hussain <waqas20@gmail.com>
parents: 3727
diff changeset
14 local prosody = prosody;
8717
9ddd0fbbe53a core: Use prosody.hosts instead of _G.hosts for consistency
Kim Alvefur <zash@zash.se>
parents: 8691
diff changeset
15 local hosts = prosody.hosts;
3401
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
16
6779
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6663
diff changeset
17 local _ENV = nil;
8555
4f0f5b49bb03 vairious: Add annotation when an empty environment is set [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7999
diff changeset
18 -- luacheck: std none
3401
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
19
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
20 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
21 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
22
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
23 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
24 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
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 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
27 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
28 }, {
6663
d3023dd07cb6 portmanager, s2smanager, sessionmanager, stanza_router, storagemanager, usermanager, util.xml: Add luacheck annotations
Matthew Wild <mwild1@gmail.com>
parents: 6552
diff changeset
29 __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
30 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
31 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
32 }
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
33 );
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
34
8691
564e2c63e0d4 storagemanager: Default storage_async_check to false for a while
Matthew Wild <mwild1@gmail.com>
parents: 8670
diff changeset
35 local async_check = config.get("*", "storage_async_check") == true;
8654
425de10efde4 storagemanager: Log warning on storage access outside of async contexts
Matthew Wild <mwild1@gmail.com>
parents: 8555
diff changeset
36
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
37 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
38
8654
425de10efde4 storagemanager: Log warning on storage access outside of async contexts
Matthew Wild <mwild1@gmail.com>
parents: 8555
diff changeset
39 local function check_async_wrapper(event)
425de10efde4 storagemanager: Log warning on storage access outside of async contexts
Matthew Wild <mwild1@gmail.com>
parents: 8555
diff changeset
40 local store = event.store;
425de10efde4 storagemanager: Log warning on storage access outside of async contexts
Matthew Wild <mwild1@gmail.com>
parents: 8555
diff changeset
41 event.store = setmetatable({}, {
425de10efde4 storagemanager: Log warning on storage access outside of async contexts
Matthew Wild <mwild1@gmail.com>
parents: 8555
diff changeset
42 __index = function (t, method_name)
425de10efde4 storagemanager: Log warning on storage access outside of async contexts
Matthew Wild <mwild1@gmail.com>
parents: 8555
diff changeset
43 local original_method = store[method_name];
425de10efde4 storagemanager: Log warning on storage access outside of async contexts
Matthew Wild <mwild1@gmail.com>
parents: 8555
diff changeset
44 if type(original_method) ~= "function" then
425de10efde4 storagemanager: Log warning on storage access outside of async contexts
Matthew Wild <mwild1@gmail.com>
parents: 8555
diff changeset
45 if original_method then
425de10efde4 storagemanager: Log warning on storage access outside of async contexts
Matthew Wild <mwild1@gmail.com>
parents: 8555
diff changeset
46 rawset(t, method_name, original_method);
425de10efde4 storagemanager: Log warning on storage access outside of async contexts
Matthew Wild <mwild1@gmail.com>
parents: 8555
diff changeset
47 end
425de10efde4 storagemanager: Log warning on storage access outside of async contexts
Matthew Wild <mwild1@gmail.com>
parents: 8555
diff changeset
48 return original_method;
425de10efde4 storagemanager: Log warning on storage access outside of async contexts
Matthew Wild <mwild1@gmail.com>
parents: 8555
diff changeset
49 end
425de10efde4 storagemanager: Log warning on storage access outside of async contexts
Matthew Wild <mwild1@gmail.com>
parents: 8555
diff changeset
50 local wrapped_method = function (...)
425de10efde4 storagemanager: Log warning on storage access outside of async contexts
Matthew Wild <mwild1@gmail.com>
parents: 8555
diff changeset
51 if not async.ready() then
425de10efde4 storagemanager: Log warning on storage access outside of async contexts
Matthew Wild <mwild1@gmail.com>
parents: 8555
diff changeset
52 log("warn", "ASYNC-01: Attempt to access storage outside async context, "
425de10efde4 storagemanager: Log warning on storage access outside of async contexts
Matthew Wild <mwild1@gmail.com>
parents: 8555
diff changeset
53 .."see https://prosody.im/doc/developers/async - %s", debug.traceback());
425de10efde4 storagemanager: Log warning on storage access outside of async contexts
Matthew Wild <mwild1@gmail.com>
parents: 8555
diff changeset
54 end
425de10efde4 storagemanager: Log warning on storage access outside of async contexts
Matthew Wild <mwild1@gmail.com>
parents: 8555
diff changeset
55 return original_method(...);
425de10efde4 storagemanager: Log warning on storage access outside of async contexts
Matthew Wild <mwild1@gmail.com>
parents: 8555
diff changeset
56 end
425de10efde4 storagemanager: Log warning on storage access outside of async contexts
Matthew Wild <mwild1@gmail.com>
parents: 8555
diff changeset
57 rawset(t, method_name, wrapped_method);
425de10efde4 storagemanager: Log warning on storage access outside of async contexts
Matthew Wild <mwild1@gmail.com>
parents: 8555
diff changeset
58 return wrapped_method;
425de10efde4 storagemanager: Log warning on storage access outside of async contexts
Matthew Wild <mwild1@gmail.com>
parents: 8555
diff changeset
59 end;
425de10efde4 storagemanager: Log warning on storage access outside of async contexts
Matthew Wild <mwild1@gmail.com>
parents: 8555
diff changeset
60 });
425de10efde4 storagemanager: Log warning on storage access outside of async contexts
Matthew Wild <mwild1@gmail.com>
parents: 8555
diff changeset
61 end
425de10efde4 storagemanager: Log warning on storage access outside of async contexts
Matthew Wild <mwild1@gmail.com>
parents: 8555
diff changeset
62
6779
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6663
diff changeset
63 local function initialize_host(host)
3727
1bbd655975ca storagemanager: Fixed a nil global access.
Waqas Hussain <waqas20@gmail.com>
parents: 3662
diff changeset
64 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
65 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
66 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
67 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
68 end);
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5502
diff changeset
69
5121
b5a5643f8572 core.storagemanager, mod_storage_*: "data-driver" -> "storage-provider", to allow using module:provides().
Waqas Hussain <waqas20@gmail.com>
parents: 5111
diff changeset
70 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
71 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
72 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
73 end);
8654
425de10efde4 storagemanager: Log warning on storage access outside of async contexts
Matthew Wild <mwild1@gmail.com>
parents: 8555
diff changeset
74 if async_check then
425de10efde4 storagemanager: Log warning on storage access outside of async contexts
Matthew Wild <mwild1@gmail.com>
parents: 8555
diff changeset
75 host_session.events.add_handler("store-opened", check_async_wrapper);
425de10efde4 storagemanager: Log warning on storage access outside of async contexts
Matthew Wild <mwild1@gmail.com>
parents: 8555
diff changeset
76 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
77 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
78 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
79
6779
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6663
diff changeset
80 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
81 if driver_name == "null" then
4758
b8b050e76ee1 storagemanager: Fix incorrect variable name
Matthew Wild <mwild1@gmail.com>
parents: 4115
diff changeset
82 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
83 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
84 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
85 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
86 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
87 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
88 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
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 return stores_available:get(host, driver_name);
3401
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
91 end
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
92
6949
1c2c3d913172 storagemanager: Split config retrieval into its own function
Matthew Wild <mwild1@gmail.com>
parents: 6809
diff changeset
93 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
94 -- 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
95 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
96 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
97 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
98 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
99 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
100 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
101 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
102 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
103 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
104 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
105 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
106 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
107 if found_sql2 then
7947
24170d74b00b core: Split some very long lines [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7645
diff changeset
108 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
109 .."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
110 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
111 return storage_config;
6949
1c2c3d913172 storagemanager: Split config retrieval into its own function
Matthew Wild <mwild1@gmail.com>
parents: 6809
diff changeset
112 end
1c2c3d913172 storagemanager: Split config retrieval into its own function
Matthew Wild <mwild1@gmail.com>
parents: 6809
diff changeset
113
6779
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6663
diff changeset
114 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
115 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
116 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
117 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
118 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
119 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
120 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
121 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
122 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
123 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
124 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
125 end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5502
diff changeset
126
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
127 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
128 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
129 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
130 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
131 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
132 end
5036
be33164aa97e storagemanager: Split out driver choosing from the open() method
Kim Alvefur <zash@zash.se>
parents: 4758
diff changeset
133 return driver, driver_name;
5110
72a7427368f8 storagemanager: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 5041
diff changeset
134 end
72a7427368f8 storagemanager: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 5041
diff changeset
135
7150
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
136 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
137 __index = {
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
138 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
139 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
140 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
141 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
142 end;
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
143 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
144 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
145 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
146 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
147 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
148 else
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
149 current = {};
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
150 end
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
151 end
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
152 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
153 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
154 end;
7152
ca64255bf7cd storagemanager: Add set_keys method to map store shim
Kim Alvefur <zash@zash.se>
parents: 7151
diff changeset
155 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
156 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
157 if current == nil then
ca64255bf7cd storagemanager: Add set_keys method to map store shim
Kim Alvefur <zash@zash.se>
parents: 7151
diff changeset
158 if err then
ca64255bf7cd storagemanager: Add set_keys method to map store shim
Kim Alvefur <zash@zash.se>
parents: 7151
diff changeset
159 return nil, err;
ca64255bf7cd storagemanager: Add set_keys method to map store shim
Kim Alvefur <zash@zash.se>
parents: 7151
diff changeset
160 end
7244
8c6943918279 storagemanager: Fix saving data in map shim when no prior data exists
Kim Alvefur <zash@zash.se>
parents: 7152
diff changeset
161 current = {};
8c6943918279 storagemanager: Fix saving data in map shim when no prior data exists
Kim Alvefur <zash@zash.se>
parents: 7152
diff changeset
162 end
8c6943918279 storagemanager: Fix saving data in map shim when no prior data exists
Kim Alvefur <zash@zash.se>
parents: 7152
diff changeset
163 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
164 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
165 current[k] = v;
7152
ca64255bf7cd storagemanager: Add set_keys method to map store shim
Kim Alvefur <zash@zash.se>
parents: 7151
diff changeset
166 end
ca64255bf7cd storagemanager: Add set_keys method to map store shim
Kim Alvefur <zash@zash.se>
parents: 7151
diff changeset
167 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
168 end;
ca64255bf7cd storagemanager: Add set_keys method to map store shim
Kim Alvefur <zash@zash.se>
parents: 7151
diff changeset
169 remove = {};
10680
19692fc5c106 storagemanager, mod_storage_sql: Rename methods to :get_all() and :delete_all()
Matthew Wild <mwild1@gmail.com>
parents: 10679
diff changeset
170 get_all = function (self, key)
10679
b50b1eae711c storagemanager: Add support for :find_key() and :delete_key() to map store shim
Matthew Wild <mwild1@gmail.com>
parents: 8717
diff changeset
171 if type(key) ~= "string" or key == "" then
10680
19692fc5c106 storagemanager, mod_storage_sql: Rename methods to :get_all() and :delete_all()
Matthew Wild <mwild1@gmail.com>
parents: 10679
diff changeset
172 return nil, "get_all only supports non-empty string keys";
10679
b50b1eae711c storagemanager: Add support for :find_key() and :delete_key() to map store shim
Matthew Wild <mwild1@gmail.com>
parents: 8717
diff changeset
173 end
b50b1eae711c storagemanager: Add support for :find_key() and :delete_key() to map store shim
Matthew Wild <mwild1@gmail.com>
parents: 8717
diff changeset
174 local ret;
b50b1eae711c storagemanager: Add support for :find_key() and :delete_key() to map store shim
Matthew Wild <mwild1@gmail.com>
parents: 8717
diff changeset
175 for username in self.keyval_store:users() do
b50b1eae711c storagemanager: Add support for :find_key() and :delete_key() to map store shim
Matthew Wild <mwild1@gmail.com>
parents: 8717
diff changeset
176 local key_data = self:get(username, key);
b50b1eae711c storagemanager: Add support for :find_key() and :delete_key() to map store shim
Matthew Wild <mwild1@gmail.com>
parents: 8717
diff changeset
177 if key_data then
b50b1eae711c storagemanager: Add support for :find_key() and :delete_key() to map store shim
Matthew Wild <mwild1@gmail.com>
parents: 8717
diff changeset
178 if not ret then
b50b1eae711c storagemanager: Add support for :find_key() and :delete_key() to map store shim
Matthew Wild <mwild1@gmail.com>
parents: 8717
diff changeset
179 ret = {};
b50b1eae711c storagemanager: Add support for :find_key() and :delete_key() to map store shim
Matthew Wild <mwild1@gmail.com>
parents: 8717
diff changeset
180 end
b50b1eae711c storagemanager: Add support for :find_key() and :delete_key() to map store shim
Matthew Wild <mwild1@gmail.com>
parents: 8717
diff changeset
181 ret[username] = key_data;
b50b1eae711c storagemanager: Add support for :find_key() and :delete_key() to map store shim
Matthew Wild <mwild1@gmail.com>
parents: 8717
diff changeset
182 end
b50b1eae711c storagemanager: Add support for :find_key() and :delete_key() to map store shim
Matthew Wild <mwild1@gmail.com>
parents: 8717
diff changeset
183 end
b50b1eae711c storagemanager: Add support for :find_key() and :delete_key() to map store shim
Matthew Wild <mwild1@gmail.com>
parents: 8717
diff changeset
184 return ret;
b50b1eae711c storagemanager: Add support for :find_key() and :delete_key() to map store shim
Matthew Wild <mwild1@gmail.com>
parents: 8717
diff changeset
185 end;
10680
19692fc5c106 storagemanager, mod_storage_sql: Rename methods to :get_all() and :delete_all()
Matthew Wild <mwild1@gmail.com>
parents: 10679
diff changeset
186 delete_all = function (self, key)
10679
b50b1eae711c storagemanager: Add support for :find_key() and :delete_key() to map store shim
Matthew Wild <mwild1@gmail.com>
parents: 8717
diff changeset
187 if type(key) ~= "string" or key == "" then
10680
19692fc5c106 storagemanager, mod_storage_sql: Rename methods to :get_all() and :delete_all()
Matthew Wild <mwild1@gmail.com>
parents: 10679
diff changeset
188 return nil, "delete_all only supports non-empty string keys";
10679
b50b1eae711c storagemanager: Add support for :find_key() and :delete_key() to map store shim
Matthew Wild <mwild1@gmail.com>
parents: 8717
diff changeset
189 end
b50b1eae711c storagemanager: Add support for :find_key() and :delete_key() to map store shim
Matthew Wild <mwild1@gmail.com>
parents: 8717
diff changeset
190 local data = { [key] = self.remove };
b50b1eae711c storagemanager: Add support for :find_key() and :delete_key() to map store shim
Matthew Wild <mwild1@gmail.com>
parents: 8717
diff changeset
191 local last_err;
b50b1eae711c storagemanager: Add support for :find_key() and :delete_key() to map store shim
Matthew Wild <mwild1@gmail.com>
parents: 8717
diff changeset
192 for username in self.keyval_store:users() do
b50b1eae711c storagemanager: Add support for :find_key() and :delete_key() to map store shim
Matthew Wild <mwild1@gmail.com>
parents: 8717
diff changeset
193 local ok, err = self:set_keys(username, data);
b50b1eae711c storagemanager: Add support for :find_key() and :delete_key() to map store shim
Matthew Wild <mwild1@gmail.com>
parents: 8717
diff changeset
194 if not ok then
b50b1eae711c storagemanager: Add support for :find_key() and :delete_key() to map store shim
Matthew Wild <mwild1@gmail.com>
parents: 8717
diff changeset
195 last_err = err;
b50b1eae711c storagemanager: Add support for :find_key() and :delete_key() to map store shim
Matthew Wild <mwild1@gmail.com>
parents: 8717
diff changeset
196 end
b50b1eae711c storagemanager: Add support for :find_key() and :delete_key() to map store shim
Matthew Wild <mwild1@gmail.com>
parents: 8717
diff changeset
197 end
b50b1eae711c storagemanager: Add support for :find_key() and :delete_key() to map store shim
Matthew Wild <mwild1@gmail.com>
parents: 8717
diff changeset
198 if last_err then
b50b1eae711c storagemanager: Add support for :find_key() and :delete_key() to map store shim
Matthew Wild <mwild1@gmail.com>
parents: 8717
diff changeset
199 return nil, last_err;
b50b1eae711c storagemanager: Add support for :find_key() and :delete_key() to map store shim
Matthew Wild <mwild1@gmail.com>
parents: 8717
diff changeset
200 end
b50b1eae711c storagemanager: Add support for :find_key() and :delete_key() to map store shim
Matthew Wild <mwild1@gmail.com>
parents: 8717
diff changeset
201 return true;
b50b1eae711c storagemanager: Add support for :find_key() and :delete_key() to map store shim
Matthew Wild <mwild1@gmail.com>
parents: 8717
diff changeset
202 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
203 };
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
204 }
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
205
12956
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
206 local combined_store_mt = {
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
207 __index = {
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
208 -- keyval
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
209 get = function (self, name)
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
210 return self.keyval_store:get(name);
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
211 end;
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
212 set = function (self, name, data)
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
213 return self.keyval_store:set(name, data);
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
214 end;
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
215 items = function (self)
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
216 return self.keyval_store:users();
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
217 end;
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
218 -- map
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
219 get_key = function (self, name, key)
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
220 return self.map_store:get(name, key);
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
221 end;
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
222 set_key = function (self, name, key, value)
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
223 return self.map_store:set(name, key, value);
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
224 end;
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
225 set_keys = function (self, name, map)
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
226 return self.map_store:set_keys(name, map);
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
227 end;
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
228 get_key_from_all = function (self, key)
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
229 return self.map_store:get_all(key);
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
230 end;
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
231 delete_key_from_all = function (self, key)
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
232 return self.map_store:delete_all(key);
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
233 end;
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
234 };
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
235 };
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
236
6792
8b284787fe26 storagemanager: Add forward declaration to fix use of open() before it's defined
Kim Alvefur <zash@zash.se>
parents: 6791
diff changeset
237 local open; -- forward declaration
7150
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
238
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
239 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
240 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
241 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
242 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
243 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
244 }, 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
245 end
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
246
12956
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
247 local function open_combined(host, store)
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
248 local driver, driver_name = get_driver(host, store);
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
249
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
250 -- Open keyval
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
251 local keyval_store, err = driver:open(store, "keyval");
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
252 if not keyval_store then
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
253 if err == "unsupported-store" then
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
254 log("debug", "Storage driver %s does not support store %s (keyval), falling back to null driver",
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
255 driver_name, store);
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
256 keyval_store, err = null_storage_driver, nil;
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
257 end
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
258 end
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
259
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
260 local map_store;
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
261 if keyval_store then
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
262 -- Open map
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
263 map_store, err = driver:open(store, "map");
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
264 if not map_store then
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
265 if err == "unsupported-store" then
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
266 log("debug", "Storage driver %s does not support store %s (map), falling back to shim",
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
267 driver_name, store);
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
268 map_store, err = setmetatable({ keyval_store = keyval_store }, map_shim_mt), nil;
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
269 end
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
270 end
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
271 end
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
272
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
273 if not(keyval_store and map_store) then
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
274 return nil, err;
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
275 end
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
276 local combined_store = setmetatable({
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
277 keyval_store = keyval_store;
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
278 map_store = map_store;
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
279 remove = map_store.remove;
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
280 }, combined_store_mt);
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
281 local event_data = { host = host, store_name = store, store_type = "keyval+", store = combined_store };
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
282 hosts[host].events.fire_event("store-opened", event_data);
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
283 return event_data.store, event_data.store_err;
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
284 end
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
285
7150
fcaaafe4062f storagemanager: When map store isn't available, fallback to keyval store [backported from trunk]
daurnimator <quae@daurnimator.com>
parents: 6951
diff changeset
286 function open(host, store, typ)
12956
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
287 if typ == "keyval+" then -- TODO: default in some release?
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
288 return open_combined(host, store);
52fcdfe710ca storagemanager: Add keyval+ (combined keyval + map) store type
Matthew Wild <mwild1@gmail.com>
parents: 10680
diff changeset
289 end
5036
be33164aa97e storagemanager: Split out driver choosing from the open() method
Kim Alvefur <zash@zash.se>
parents: 4758
diff changeset
290 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
291 local ret, err = driver:open(store, typ);
3401
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
292 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
293 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
294 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
295 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
296 ret, err = create_map_shim(host, store);
44fe2aaf817e storagemanager: Simplify function flow
Matthew Wild <mwild1@gmail.com>
parents: 7325
diff changeset
297 else
44fe2aaf817e storagemanager: Simplify function flow
Matthew Wild <mwild1@gmail.com>
parents: 7325
diff changeset
298 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
299 driver_name, store, typ or "<nil>");
44fe2aaf817e storagemanager: Simplify function flow
Matthew Wild <mwild1@gmail.com>
parents: 7325
diff changeset
300 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
301 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
302 end
3401
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
303 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
304 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
305 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
306 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
307 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
308 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
309 return ret, err;
3401
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
310 end
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
311
6779
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6663
diff changeset
312 local function purge(user, host)
6949
1c2c3d913172 storagemanager: Split config retrieval into its own function
Matthew Wild <mwild1@gmail.com>
parents: 6809
diff changeset
313 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
314 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
315 -- 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
316 local purged = {};
6809
dd6b21862e3b storagemanager: Check if drivers support purging, warn otherwise
Kim Alvefur <zash@zash.se>
parents: 6807
diff changeset
317 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
318 if not purged[driver_name] then
dd6b21862e3b storagemanager: Check if drivers support purging, warn otherwise
Kim Alvefur <zash@zash.se>
parents: 6807
diff changeset
319 local driver = get_driver(host, store);
dd6b21862e3b storagemanager: Check if drivers support purging, warn otherwise
Kim Alvefur <zash@zash.se>
parents: 6807
diff changeset
320 if driver.purge then
dd6b21862e3b storagemanager: Check if drivers support purging, warn otherwise
Kim Alvefur <zash@zash.se>
parents: 6807
diff changeset
321 purged[driver_name] = driver:purge(user);
dd6b21862e3b storagemanager: Check if drivers support purging, warn otherwise
Kim Alvefur <zash@zash.se>
parents: 6807
diff changeset
322 else
7947
24170d74b00b core: Split some very long lines [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7645
diff changeset
323 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
324 .."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
325 end
5129
e8253c931166 storagemanager: Add purge() for purging user data from all backends in use
Kim Alvefur <zash@zash.se>
parents: 5111
diff changeset
326 end
e8253c931166 storagemanager: Add purge() for purging user data from all backends in use
Kim Alvefur <zash@zash.se>
parents: 5111
diff changeset
327 end
e8253c931166 storagemanager: Add purge() for purging user data from all backends in use
Kim Alvefur <zash@zash.se>
parents: 5111
diff changeset
328 end
e8253c931166 storagemanager: Add purge() for purging user data from all backends in use
Kim Alvefur <zash@zash.se>
parents: 5111
diff changeset
329 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
330
e8253c931166 storagemanager: Add purge() for purging user data from all backends in use
Kim Alvefur <zash@zash.se>
parents: 5111
diff changeset
331 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
332
e8253c931166 storagemanager: Add purge() for purging user data from all backends in use
Kim Alvefur <zash@zash.se>
parents: 5111
diff changeset
333 return true;
e8253c931166 storagemanager: Add purge() for purging user data from all backends in use
Kim Alvefur <zash@zash.se>
parents: 5111
diff changeset
334 end
e8253c931166 storagemanager: Add purge() for purging user data from all backends in use
Kim Alvefur <zash@zash.se>
parents: 5111
diff changeset
335
3401
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
336 function datamanager.load(username, host, datastore)
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
337 return open(host, datastore):get(username);
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
338 end
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
339 function datamanager.store(username, host, datastore, data)
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
340 return open(host, datastore):set(username, data);
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
341 end
5155
a207d4bff5a4 storagemanager: Support for iterating over users
Kim Alvefur <zash@zash.se>
parents: 5137
diff changeset
342 function datamanager.users(host, datastore, typ)
a207d4bff5a4 storagemanager: Support for iterating over users
Kim Alvefur <zash@zash.se>
parents: 5137
diff changeset
343 local driver = open(host, datastore, typ);
a207d4bff5a4 storagemanager: Support for iterating over users
Kim Alvefur <zash@zash.se>
parents: 5137
diff changeset
344 if not driver.users then
7325
7baf1b14defb storagemanager: Capitalize log message
Kim Alvefur <zash@zash.se>
parents: 7244
diff changeset
345 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
346 end
a207d4bff5a4 storagemanager: Support for iterating over users
Kim Alvefur <zash@zash.se>
parents: 5137
diff changeset
347 return driver:users();
a207d4bff5a4 storagemanager: Support for iterating over users
Kim Alvefur <zash@zash.se>
parents: 5137
diff changeset
348 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
349 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
350 return get_driver(host):stores(username, typ);
5037
c34fdcae6d29 storagemanager: Add method for listing stores
Kim Alvefur <zash@zash.se>
parents: 5036
diff changeset
351 end
5041
be204204cc5f storagemanager: Add method for removing all data belonging to a user
Kim Alvefur <zash@zash.se>
parents: 5037
diff changeset
352 function datamanager.purge(username, host)
5136
77ea38607a89 storagemanager: Fix argument (Thanks Maranda)
Kim Alvefur <zash@zash.se>
parents: 5133
diff changeset
353 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
354 end
3401
2387f35db5c8 storagemanager: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
355
6779
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6663
diff changeset
356 return {
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6663
diff changeset
357 initialize_host = initialize_host;
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6663
diff changeset
358 load_driver = load_driver;
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6663
diff changeset
359 get_driver = get_driver;
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6663
diff changeset
360 open = open;
6807
5e3242d349f2 storagemanager: Export purge (fixes deleting users from usermanager) (thanks mt)
Kim Alvefur <zash@zash.se>
parents: 6779
diff changeset
361 purge = purge;
6779
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6663
diff changeset
362
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6663
diff changeset
363 olddm = olddm;
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6663
diff changeset
364 };