annotate mod_storage_gdbm/mod_storage_gdbm.lua @ 1570:67fafebdceb7

mod_storage_gdbm: Storage backend based on lgdbm
author Kim Alvefur <zash@zash.se>
date Tue, 18 Nov 2014 13:05:04 +0100
parents
children d9f3c66ea938
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";
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 local serialize, deserialize = import("util.serialization", "serialize", "deserialize");
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 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
15 lfs.mkdir(base_path);
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 local cache = {};
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 driver = {};
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 local driver_mt = { __index = driver };
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 function driver:set(user, value)
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 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
24 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
25 return true;
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 end
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 function driver:get(user)
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 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
30 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
31 return deserialize(data);
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 end
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 function open(_, store, typ)
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 typ = typ or "keyval";
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 if typ ~= "keyval" then
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 return nil, "unsupported-store";
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 end
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 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
41
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 local db = cache[db_path];
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 if not db then
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 db = assert(gdbm.open(db_path, "c"));
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 cache[db_path] = db;
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 end
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 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
48 end
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 function module.unload()
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 for path, db in pairs(cache) do
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 gdbm.sync(db);
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 gdbm.close(db);
67fafebdceb7 mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 end
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 module:provides"storage";