Software /
code /
prosody-modules
Comparison
mod_storage_gdbm/mod_storage_gdbm.lua @ 1595:6288591d5edf
mod_storage_gdbm: Prepare for supporting multiple store types
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 25 Jan 2015 22:17:20 +0100 |
parent | 1593:d9f3c66ea938 |
child | 1596:b362e6c00fd1 |
comparison
equal
deleted
inserted
replaced
1594:620cc035ae1e | 1595:6288591d5edf |
---|---|
16 local base_path = path.resolve_relative_path(prosody.paths.data, module.host); | 16 local base_path = path.resolve_relative_path(prosody.paths.data, module.host); |
17 lfs.mkdir(base_path); | 17 lfs.mkdir(base_path); |
18 | 18 |
19 local cache = {}; | 19 local cache = {}; |
20 | 20 |
21 local driver = {}; | 21 local keyval = {}; |
22 local driver_mt = { __index = driver }; | 22 local keyval_mt = { __index = keyval, suffix = ".db" }; |
23 | 23 |
24 function driver:set(user, value) | 24 function keyval:set(user, value) |
25 local ok, err = gdbm.replace(self._db, user or "@", serialize(value)); | 25 local ok, err = gdbm.replace(self._db, user or "@", serialize(value)); |
26 if not ok then return nil, err; end | 26 if not ok then return nil, err; end |
27 return true; | 27 return true; |
28 end | 28 end |
29 | 29 |
30 function driver:get(user) | 30 function keyval:get(user) |
31 local data, err = gdbm.fetch(self._db, user or "@"); | 31 local data, err = gdbm.fetch(self._db, user or "@"); |
32 if not data then return nil, err; end | 32 if not data then return nil, err; end |
33 return deserialize(data); | 33 return deserialize(data); |
34 end | 34 end |
35 | 35 |
36 local drivers = { | |
37 keyval = keyval_mt; | |
38 } | |
39 | |
36 function open(_, store, typ) | 40 function open(_, store, typ) |
37 typ = typ or "keyval"; | 41 typ = typ or "keyval"; |
38 if typ ~= "keyval" then | 42 local driver_mt = drivers[typ]; |
43 if not driver_mt then | |
39 return nil, "unsupported-store"; | 44 return nil, "unsupported-store"; |
40 end | 45 end |
41 | 46 |
42 local db_path = path.join(base_path, store) .. ".db"; | 47 local db_path = path.join(base_path, store) .. driver_mt.suffix; |
43 | 48 |
44 local db = cache[db_path]; | 49 local db = cache[db_path]; |
45 if not db then | 50 if not db then |
46 db = assert(gdbm.open(db_path, "c")); | 51 db = assert(gdbm.open(db_path, "c")); |
47 cache[db_path] = db; | 52 cache[db_path] = db; |