comparison mod_storage_lmdb/mod_storage_lmdb.lua @ 1799:d2dd1db9ece6

mod_storage_lmdb: Rearrange module to allow using as a library
author Kim Alvefur <zash@zash.se>
date Fri, 28 Aug 2015 00:34:43 +0200
parents 0a21b16b9075
children 669d1208221a
comparison
equal deleted inserted replaced
1798:3ae8c81a348b 1799:d2dd1db9ece6
13 local path = require"util.paths"; 13 local path = require"util.paths";
14 local serialization = require"util.serialization"; 14 local serialization = require"util.serialization";
15 local serialize = serialization.serialize; 15 local serialize = serialization.serialize;
16 local deserialize = serialization.deserialize; 16 local deserialize = serialization.deserialize;
17 17
18 local base_path = path.resolve_relative_path(prosody.paths.data, module.host); 18 local drivers = {};
19 lfs.mkdir(base_path); 19 local provider = {};
20
21 local env = lmdb.env_create();
22 assert(env:set_maxdbs(module:get_option_number("lmdb_maxdbs", 20)));
23 local env_flags = 0;
24 for i, flag in ipairs(module:get_option_array("lmdb_flags", {})) do
25 env_flags = env_flags + assert(lmdb["MDB_"..flag:upper()], "No such flag "..flag);
26 end
27 env:open(base_path, env_flags, tonumber("640", 8));
28 20
29 local keyval = {}; 21 local keyval = {};
30 local keyval_mt = { __index = keyval, flags = lmdb.MDB_CREATE }; 22 local keyval_mt = { __index = keyval, flags = lmdb.MDB_CREATE };
23 drivers.keyval = keyval_mt;
31 24
32 function keyval:set(user, value) 25 function keyval:set(user, value)
33 local t = self.env:txn_begin(nil, 0); 26 local t = self.env:txn_begin(nil, 0);
34 if type(value) == "table" and next(value) == nil then 27 if type(value) == "table" and next(value) == nil then
35 value = nil; 28 value = nil;
63 56
64 local drivers = { 57 local drivers = {
65 keyval = keyval_mt; 58 keyval = keyval_mt;
66 } 59 }
67 60
68 function open(_, store, typ) 61
62 function provider:init(config)
63 if config.base_path then
64 lfs.mkdir(config.base_path);
65 end
66 local env = lmdb.env_create();
67 env:set_maxdbs(config.maxdbs or 20);
68 local env_flags = 0;
69 if config.flags then
70 for flag in config.flags do
71 env_flags = env_flags + assert(lmdb["MDB_"..flag:upper()], "No such flag "..flag);
72 end
73 end
74 env:open(config.base_path or ".", env_flags, tonumber("640", 8));
75 self.env = env;
76 end
77
78 function provider:open(store, typ)
69 typ = typ or "keyval"; 79 typ = typ or "keyval";
70 local driver_mt = drivers[typ]; 80 local driver_mt = drivers[typ];
71 if not driver_mt then 81 if not driver_mt then
72 return nil, "unsupported-store"; 82 return nil, "unsupported-store";
73 end 83 end
84 local env = self.env;
74 local t = env:txn_begin(nil, 0); 85 local t = env:txn_begin(nil, 0);
75 local db = t:dbi_open(store.."_"..typ, driver_mt.flags); 86 local db = t:dbi_open(store.."_"..typ, driver_mt.flags);
76 assert(t:commit()); 87 local ok, err = t:commit();
88 if not ok then
89 module:log("error", "Could not open database %s_%s: %s", store, typ, tostring(err));
90 return ok, err;
91 end
77 92
78 return setmetatable({ env = env, store = store, type = typ, db = db }, driver_mt); 93 return setmetatable({ env = env, store = store, type = typ, db = db }, driver_mt);
79 end 94 end
80 95
81 function module.unload() 96 if prosody then
82 env:sync(1); 97 provider:init({
83 env:close(); 98 base_path = path.resolve_relative_path(prosody.paths.data, module.host);
99 flags = module:get_option_set("lmdb_flags", {});
100 maxdbs = module:get_option_number("lmdb_maxdbs", 20);
101 });
102
103 function module.unload()
104 provider.env:sync(1);
105 provider.env:close();
106 end
107
108 module:provides("storage", provider);
109 else
110 return provider;
84 end 111 end
85
86 module:provides("storage");