Mercurial > prosody-modules
annotate mod_storage_gdbm/mod_storage_gdbm.lua @ 1632:050d101af207
mod_storage_gdbm: Prefix archive item keys with username to prevent collisions
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Wed, 25 Mar 2015 17:44:03 +0100 |
parents | 98427ed3d53f |
children | 1d2dc6c74581 |
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 |
1629
36eb0dbea7ba
mod_storage_gdbm: Minor reorganization, more locals
Kim Alvefur <zash@zash.se>
parents:
1628
diff
changeset
|
2 -- Copyright (C) 2014-2015 Kim Alvefur |
1570
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"; |
1629
36eb0dbea7ba
mod_storage_gdbm: Minor reorganization, more locals
Kim Alvefur <zash@zash.se>
parents:
1628
diff
changeset
|
12 local st = require"util.stanza"; |
1596
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
13 local uuid = require"util.uuid".generate; |
1629
36eb0dbea7ba
mod_storage_gdbm: Minor reorganization, more locals
Kim Alvefur <zash@zash.se>
parents:
1628
diff
changeset
|
14 |
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
|
15 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
|
16 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
|
17 local deserialize = serialization.deserialize; |
1570
67fafebdceb7
mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 |
1629
36eb0dbea7ba
mod_storage_gdbm: Minor reorganization, more locals
Kim Alvefur <zash@zash.se>
parents:
1628
diff
changeset
|
19 local g_set, g_get, g_del = gdbm.replace, gdbm.fetch, gdbm.delete; |
36eb0dbea7ba
mod_storage_gdbm: Minor reorganization, more locals
Kim Alvefur <zash@zash.se>
parents:
1628
diff
changeset
|
20 local g_first, g_next = gdbm.firstkey, gdbm.nextkey; |
36eb0dbea7ba
mod_storage_gdbm: Minor reorganization, more locals
Kim Alvefur <zash@zash.se>
parents:
1628
diff
changeset
|
21 |
36eb0dbea7ba
mod_storage_gdbm: Minor reorganization, more locals
Kim Alvefur <zash@zash.se>
parents:
1628
diff
changeset
|
22 local t_remove = table.remove; |
36eb0dbea7ba
mod_storage_gdbm: Minor reorganization, more locals
Kim Alvefur <zash@zash.se>
parents:
1628
diff
changeset
|
23 |
1628
5e4b37b9cde1
mod_storage_gdbm: Fix traceback if query is nil or no metadata exists
Kim Alvefur <zash@zash.se>
parents:
1604
diff
changeset
|
24 local empty = {}; |
5e4b37b9cde1
mod_storage_gdbm: Fix traceback if query is nil or no metadata exists
Kim Alvefur <zash@zash.se>
parents:
1604
diff
changeset
|
25 |
1596
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
26 local function id(v) return v; end |
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
27 |
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
28 local function is_stanza(s) |
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
29 return getmetatable(s) == st.stanza_mt; |
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
30 end |
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
31 |
1629
36eb0dbea7ba
mod_storage_gdbm: Minor reorganization, more locals
Kim Alvefur <zash@zash.se>
parents:
1628
diff
changeset
|
32 local function t(c, a, b) |
36eb0dbea7ba
mod_storage_gdbm: Minor reorganization, more locals
Kim Alvefur <zash@zash.se>
parents:
1628
diff
changeset
|
33 if c then return a; end return b; |
1596
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
34 end |
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
35 |
1570
67fafebdceb7
mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 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
|
37 lfs.mkdir(base_path); |
67fafebdceb7
mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 |
67fafebdceb7
mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 local cache = {}; |
67fafebdceb7
mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 |
1595
6288591d5edf
mod_storage_gdbm: Prepare for supporting multiple store types
Kim Alvefur <zash@zash.se>
parents:
1593
diff
changeset
|
41 local keyval = {}; |
6288591d5edf
mod_storage_gdbm: Prepare for supporting multiple store types
Kim Alvefur <zash@zash.se>
parents:
1593
diff
changeset
|
42 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
|
43 |
1595
6288591d5edf
mod_storage_gdbm: Prepare for supporting multiple store types
Kim Alvefur <zash@zash.se>
parents:
1593
diff
changeset
|
44 function keyval:set(user, value) |
1630
0fcd63818aa1
mod_storage_gdbm: Make write operation delete data when passed 'nil' as value instead of storing a literal "nil" string
Kim Alvefur <zash@zash.se>
parents:
1629
diff
changeset
|
45 if type(value) == "table" and next(value) == nil then |
0fcd63818aa1
mod_storage_gdbm: Make write operation delete data when passed 'nil' as value instead of storing a literal "nil" string
Kim Alvefur <zash@zash.se>
parents:
1629
diff
changeset
|
46 value = nil; |
0fcd63818aa1
mod_storage_gdbm: Make write operation delete data when passed 'nil' as value instead of storing a literal "nil" string
Kim Alvefur <zash@zash.se>
parents:
1629
diff
changeset
|
47 end |
0fcd63818aa1
mod_storage_gdbm: Make write operation delete data when passed 'nil' as value instead of storing a literal "nil" string
Kim Alvefur <zash@zash.se>
parents:
1629
diff
changeset
|
48 if value ~= nil then |
0fcd63818aa1
mod_storage_gdbm: Make write operation delete data when passed 'nil' as value instead of storing a literal "nil" string
Kim Alvefur <zash@zash.se>
parents:
1629
diff
changeset
|
49 value = serialize(value); |
0fcd63818aa1
mod_storage_gdbm: Make write operation delete data when passed 'nil' as value instead of storing a literal "nil" string
Kim Alvefur <zash@zash.se>
parents:
1629
diff
changeset
|
50 end |
0fcd63818aa1
mod_storage_gdbm: Make write operation delete data when passed 'nil' as value instead of storing a literal "nil" string
Kim Alvefur <zash@zash.se>
parents:
1629
diff
changeset
|
51 local ok, err = (value and g_set or g_del)(self._db, user or "@", value); |
1570
67fafebdceb7
mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
52 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
|
53 return true; |
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 |
1595
6288591d5edf
mod_storage_gdbm: Prepare for supporting multiple store types
Kim Alvefur <zash@zash.se>
parents:
1593
diff
changeset
|
56 function keyval:get(user) |
1629
36eb0dbea7ba
mod_storage_gdbm: Minor reorganization, more locals
Kim Alvefur <zash@zash.se>
parents:
1628
diff
changeset
|
57 local data, err = g_get(self._db, user or "@"); |
1570
67fafebdceb7
mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
58 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
|
59 return deserialize(data); |
67fafebdceb7
mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
60 end |
67fafebdceb7
mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
61 |
1596
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
62 local archive = {}; |
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
63 local archive_mt = { __index = archive, suffix = ".adb" }; |
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
64 |
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
65 archive.get = keyval.get; |
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
66 archive.set = keyval.set; |
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
67 |
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
68 function archive:append(username, key, when, with, value) |
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
69 key = key or uuid(); |
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
70 local meta = self:get(username); |
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
71 if not meta then |
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
72 meta = {}; |
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
73 end |
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
74 local i = meta[key] or #meta+1; |
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
75 local type; |
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
76 if is_stanza(value) then |
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
77 type, value = "stanza", st.preserialize(value); |
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
78 end |
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
79 meta[i] = { key = key, when = when, with = with, type = type }; |
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
80 meta[key] = i; |
1632
050d101af207
mod_storage_gdbm: Prefix archive item keys with username to prevent collisions
Kim Alvefur <zash@zash.se>
parents:
1631
diff
changeset
|
81 local prefix = (username or "@") .. "#"; |
050d101af207
mod_storage_gdbm: Prefix archive item keys with username to prevent collisions
Kim Alvefur <zash@zash.se>
parents:
1631
diff
changeset
|
82 local ok, err = self:set(prefix..key, value); |
1596
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
83 if not ok then return nil, err; end |
1631
98427ed3d53f
mod_storage_gdbm: Update archive metadata only if data write succeeds
Kim Alvefur <zash@zash.se>
parents:
1630
diff
changeset
|
84 ok, err = self:set(username, meta); |
1596
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
85 if not ok then return nil, err; end |
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
86 return key; |
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
87 end |
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
88 |
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
89 local deserialize = { |
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
90 stanza = st.deserialize; |
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
91 }; |
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
92 |
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
93 function archive:find(username, query) |
1628
5e4b37b9cde1
mod_storage_gdbm: Fix traceback if query is nil or no metadata exists
Kim Alvefur <zash@zash.se>
parents:
1604
diff
changeset
|
94 query = query or empty_query; |
5e4b37b9cde1
mod_storage_gdbm: Fix traceback if query is nil or no metadata exists
Kim Alvefur <zash@zash.se>
parents:
1604
diff
changeset
|
95 local meta = self:get(username) or empty; |
1632
050d101af207
mod_storage_gdbm: Prefix archive item keys with username to prevent collisions
Kim Alvefur <zash@zash.se>
parents:
1631
diff
changeset
|
96 local prefix = (username or "@") .. "#"; |
1596
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
97 local r = query.reverse; |
1629
36eb0dbea7ba
mod_storage_gdbm: Minor reorganization, more locals
Kim Alvefur <zash@zash.se>
parents:
1628
diff
changeset
|
98 local d = t(r, -1, 1); |
36eb0dbea7ba
mod_storage_gdbm: Minor reorganization, more locals
Kim Alvefur <zash@zash.se>
parents:
1628
diff
changeset
|
99 local s = meta[t(r, query.before, query.after)]; |
1603
1fbec16996f5
mod_storage_gdbm: Add support for 'limit' query parameter
Kim Alvefur <zash@zash.se>
parents:
1602
diff
changeset
|
100 local limit = query.limit; |
1596
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
101 if s then |
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
102 s = s + d; |
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
103 else |
1629
36eb0dbea7ba
mod_storage_gdbm: Minor reorganization, more locals
Kim Alvefur <zash@zash.se>
parents:
1628
diff
changeset
|
104 s = t(r, #meta, 1) |
1596
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
105 end |
1629
36eb0dbea7ba
mod_storage_gdbm: Minor reorganization, more locals
Kim Alvefur <zash@zash.se>
parents:
1628
diff
changeset
|
106 local e = t(r, 1, #meta); |
1603
1fbec16996f5
mod_storage_gdbm: Add support for 'limit' query parameter
Kim Alvefur <zash@zash.se>
parents:
1602
diff
changeset
|
107 local c = 0; |
1596
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
108 return function () |
1603
1fbec16996f5
mod_storage_gdbm: Add support for 'limit' query parameter
Kim Alvefur <zash@zash.se>
parents:
1602
diff
changeset
|
109 if limit and c >= limit then return end |
1596
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
110 local item, value; |
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
111 for i = s, e, d do |
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
112 item = meta[i]; |
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
113 if (not query.with or item.with == query.with) |
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
114 and (not query.start or item.when >= query.start) |
1604
53052a610c67
mod_storage_gdbm: Fix comparison of 'end'
Kim Alvefur <zash@zash.se>
parents:
1603
diff
changeset
|
115 and (not query["end"] or item.when <= query["end"]) then |
1603
1fbec16996f5
mod_storage_gdbm: Add support for 'limit' query parameter
Kim Alvefur <zash@zash.se>
parents:
1602
diff
changeset
|
116 s = i + d; c = c + 1; |
1632
050d101af207
mod_storage_gdbm: Prefix archive item keys with username to prevent collisions
Kim Alvefur <zash@zash.se>
parents:
1631
diff
changeset
|
117 value = self:get(prefix..item.key); |
1596
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
118 return item.key, (deserialize[item.type] or id)(value), item.when, item.with; |
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
119 end |
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
120 end |
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
121 end |
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
122 end |
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
123 |
1595
6288591d5edf
mod_storage_gdbm: Prepare for supporting multiple store types
Kim Alvefur <zash@zash.se>
parents:
1593
diff
changeset
|
124 local drivers = { |
6288591d5edf
mod_storage_gdbm: Prepare for supporting multiple store types
Kim Alvefur <zash@zash.se>
parents:
1593
diff
changeset
|
125 keyval = keyval_mt; |
1596
b362e6c00fd1
mod_storage_gdbm: Add archive support
Kim Alvefur <zash@zash.se>
parents:
1595
diff
changeset
|
126 archive = archive_mt; |
1595
6288591d5edf
mod_storage_gdbm: Prepare for supporting multiple store types
Kim Alvefur <zash@zash.se>
parents:
1593
diff
changeset
|
127 } |
6288591d5edf
mod_storage_gdbm: Prepare for supporting multiple store types
Kim Alvefur <zash@zash.se>
parents:
1593
diff
changeset
|
128 |
1570
67fafebdceb7
mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
129 function open(_, store, typ) |
67fafebdceb7
mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
130 typ = typ or "keyval"; |
1595
6288591d5edf
mod_storage_gdbm: Prepare for supporting multiple store types
Kim Alvefur <zash@zash.se>
parents:
1593
diff
changeset
|
131 local driver_mt = drivers[typ]; |
6288591d5edf
mod_storage_gdbm: Prepare for supporting multiple store types
Kim Alvefur <zash@zash.se>
parents:
1593
diff
changeset
|
132 if not driver_mt then |
1570
67fafebdceb7
mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
133 return nil, "unsupported-store"; |
67fafebdceb7
mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
134 end |
67fafebdceb7
mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
135 |
1595
6288591d5edf
mod_storage_gdbm: Prepare for supporting multiple store types
Kim Alvefur <zash@zash.se>
parents:
1593
diff
changeset
|
136 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
|
137 |
67fafebdceb7
mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
138 local db = cache[db_path]; |
67fafebdceb7
mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
139 if not db then |
67fafebdceb7
mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
140 db = assert(gdbm.open(db_path, "c")); |
67fafebdceb7
mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
141 cache[db_path] = db; |
67fafebdceb7
mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
142 end |
67fafebdceb7
mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
143 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
|
144 end |
67fafebdceb7
mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
145 |
67fafebdceb7
mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
146 function module.unload() |
67fafebdceb7
mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
147 for path, db in pairs(cache) do |
67fafebdceb7
mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
148 gdbm.sync(db); |
67fafebdceb7
mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
149 gdbm.close(db); |
67fafebdceb7
mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
150 end |
67fafebdceb7
mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
151 end |
67fafebdceb7
mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
152 |
67fafebdceb7
mod_storage_gdbm: Storage backend based on lgdbm
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
153 module:provides"storage"; |