annotate mod_storage_gdbm/mod_storage_gdbm.lua @ 1593:d9f3c66ea938

mod_storage_gdbm: Use require directly instead of util.import (which is not available in prosodyctl, breaks adduser etc)
author Kim Alvefur <zash@zash.se>
date Sun, 25 Jan 2015 13:04:02 +0100
parents 67fafebdceb7
children 6288591d5edf
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
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 local driver = {};
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 local driver_mt = { __index = driver };
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 function driver:set(user, value)
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
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 function driver:get(user)
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
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 function open(_, store, typ)
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 typ = typ or "keyval";
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 if typ ~= "keyval" then
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 return nil, "unsupported-store";
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 end
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 local db_path = path.join(base_path, store) .. ".db";
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 local db = cache[db_path];
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 if not db then
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 db = assert(gdbm.open(db_path, "c"));
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 cache[db_path] = db;
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 end
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 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
50 end
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 function module.unload()
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 for path, db in pairs(cache) do
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 gdbm.sync(db);
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 gdbm.close(db);
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 end
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 end
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 module:provides"storage";