annotate 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
parents d9f3c66ea938
children b362e6c00fd1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1570
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 -- mod_storage_gdbm
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 -- Copyright (C) 2014 Kim Alvefur
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 --
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 -- This file is MIT/X11 licensed.
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 --
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 -- Depends on lgdbm:
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 -- http://webserver2.tecgraf.puc-rio.br/~lhf/ftp/lua/#lgdbm
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 local gdbm = require"gdbm";
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 local path = require"util.paths";
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 local lfs = require"lfs";
1593
d9f3c66ea938 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents: 1570
diff changeset
12 local serialization = require"util.serialization";
d9f3c66ea938 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents: 1570
diff changeset
13 local serialize = serialization.serialize;
d9f3c66ea938 mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
Kim Alvefur <zash@zash.se>
parents: 1570
diff changeset
14 local deserialize = serialization.deserialize;
1570
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 local base_path = path.resolve_relative_path(prosody.paths.data, module.host);
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 lfs.mkdir(base_path);
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 local cache = {};
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20
1595
6288591d5edf mod_storage_gdbm: Prepare for supporting multiple store types
Kim Alvefur <zash@zash.se>
parents: 1593
diff changeset
21 local keyval = {};
6288591d5edf mod_storage_gdbm: Prepare for supporting multiple store types
Kim Alvefur <zash@zash.se>
parents: 1593
diff changeset
22 local keyval_mt = { __index = keyval, suffix = ".db" };
1570
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23
1595
6288591d5edf mod_storage_gdbm: Prepare for supporting multiple store types
Kim Alvefur <zash@zash.se>
parents: 1593
diff changeset
24 function keyval:set(user, value)
1570
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 local ok, err = gdbm.replace(self._db, user or "@", serialize(value));
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 if not ok then return nil, err; end
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 return true;
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 end
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29
1595
6288591d5edf mod_storage_gdbm: Prepare for supporting multiple store types
Kim Alvefur <zash@zash.se>
parents: 1593
diff changeset
30 function keyval:get(user)
1570
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 local data, err = gdbm.fetch(self._db, user or "@");
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 if not data then return nil, err; end
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 return deserialize(data);
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 end
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35
1595
6288591d5edf mod_storage_gdbm: Prepare for supporting multiple store types
Kim Alvefur <zash@zash.se>
parents: 1593
diff changeset
36 local drivers = {
6288591d5edf mod_storage_gdbm: Prepare for supporting multiple store types
Kim Alvefur <zash@zash.se>
parents: 1593
diff changeset
37 keyval = keyval_mt;
6288591d5edf mod_storage_gdbm: Prepare for supporting multiple store types
Kim Alvefur <zash@zash.se>
parents: 1593
diff changeset
38 }
6288591d5edf mod_storage_gdbm: Prepare for supporting multiple store types
Kim Alvefur <zash@zash.se>
parents: 1593
diff changeset
39
1570
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 function open(_, store, typ)
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 typ = typ or "keyval";
1595
6288591d5edf mod_storage_gdbm: Prepare for supporting multiple store types
Kim Alvefur <zash@zash.se>
parents: 1593
diff changeset
42 local driver_mt = drivers[typ];
6288591d5edf mod_storage_gdbm: Prepare for supporting multiple store types
Kim Alvefur <zash@zash.se>
parents: 1593
diff changeset
43 if not driver_mt then
1570
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 return nil, "unsupported-store";
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 end
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46
1595
6288591d5edf mod_storage_gdbm: Prepare for supporting multiple store types
Kim Alvefur <zash@zash.se>
parents: 1593
diff changeset
47 local db_path = path.join(base_path, store) .. driver_mt.suffix;
1570
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 local db = cache[db_path];
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 if not db then
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 db = assert(gdbm.open(db_path, "c"));
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 cache[db_path] = db;
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 end
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 return setmetatable({ _db = db; _path = db_path; store = store, typ = type }, driver_mt);
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 end
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 function module.unload()
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 for path, db in pairs(cache) do
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 gdbm.sync(db);
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 gdbm.close(db);
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 end
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 end
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 module:provides"storage";