util/datamanager.lua

Thu, 13 Nov 2008 03:56:22 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 13 Nov 2008 03:56:22 +0000
changeset 244
0e3bda34f958
parent 205
e30d0e30a0ff
child 247
681b29aa134f
permissions
-rw-r--r--

Missed importing a function in last commit

0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
1 local format = string.format;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
2 local setmetatable, type = setmetatable, type;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
3 local pairs = pairs;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
4 local char = string.char;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
5 local loadfile, setfenv, pcall = loadfile, setfenv, pcall;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
6 local log = log;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
7 local io_open = io.open;
205
e30d0e30a0ff Datamanager now deletes files with no data
Waqas Hussain <waqas20@gmail.com>
parents: 182
diff changeset
8 local os_remove = os.remove;
88
023320150c65 Minor fix
Waqas Hussain <waqas20@gmail.com>
parents: 87
diff changeset
9 local tostring = tostring;
177
606c433955e7 Bug fixes and checks for presence subscriptions, etc
Waqas Hussain <waqas20@gmail.com>
parents: 129
diff changeset
10 local error = error;
205
e30d0e30a0ff Datamanager now deletes files with no data
Waqas Hussain <waqas20@gmail.com>
parents: 182
diff changeset
11 local next = next;
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
12
182
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
13 local indent = function(f, i)
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
14 for n = 1, i do
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
15 f:write("\t");
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
16 end
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
17 end
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
18
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
19 module "datamanager"
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
20
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
21
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
22 ---- utils -----
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
23 local encode, decode;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
24
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
25 local log = function (type, msg) return log(type, "datamanager", msg); end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
26
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
27 do
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
28 local urlcodes = setmetatable({}, { __index = function (t, k) t[k] = char(tonumber("0x"..k)); return t[k]; end });
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
29
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
30 decode = function (s)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
31 return s and (s:gsub("+", " "):gsub("%%([a-fA-F0-9][a-fA-F0-9])", urlcodes));
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
32 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
33
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
34 encode = function (s)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
35 return s and (s:gsub("%W", function (c) return format("%%%x", c:byte()); end));
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
36 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
37 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
38
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
39 local function basicSerialize (o)
182
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
40 if type(o) == "number" or type(o) == "boolean" then
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
41 return tostring(o);
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
42 else -- assume it is a string -- FIXME make sure it's a string. throw an error otherwise.
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
43 return (format("%q", tostring(o)):gsub("\\\n", "\\n"));
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
44 end
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
45 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
46
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
47
182
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
48 local function simplesave (f, o, ind)
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
49 if type(o) == "number" then
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
50 f:write(o)
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
51 elseif type(o) == "string" then
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
52 f:write((format("%q", o):gsub("\\\n", "\\n")))
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
53 elseif type(o) == "table" then
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
54 f:write("{\n")
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
55 for k,v in pairs(o) do
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
56 indent(f, ind);
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
57 f:write("[", basicSerialize(k), "] = ")
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
58 simplesave(f, v, ind+1)
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
59 f:write(",\n")
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
60 end
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
61 indent(f, ind-1);
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
62 f:write("}")
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
63 elseif type(o) == "boolean" then
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
64 f:write(o and "true" or "false");
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
65 else
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
66 error("cannot serialize a " .. type(o))
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
67 end
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
68 end
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
69
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
70 ------- API -------------
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
71
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
72 function getpath(username, host, datastore)
84
d0a0bac6815e Added: Datastore support for hosts and global data in addition to users
Waqas Hussain <waqas20@gmail.com>
parents: 0
diff changeset
73 if username then
d0a0bac6815e Added: Datastore support for hosts and global data in addition to users
Waqas Hussain <waqas20@gmail.com>
parents: 0
diff changeset
74 return format("data/%s/%s/%s.dat", encode(host), datastore, encode(username));
d0a0bac6815e Added: Datastore support for hosts and global data in addition to users
Waqas Hussain <waqas20@gmail.com>
parents: 0
diff changeset
75 elseif host then
d0a0bac6815e Added: Datastore support for hosts and global data in addition to users
Waqas Hussain <waqas20@gmail.com>
parents: 0
diff changeset
76 return format("data/%s/%s.dat", encode(host), datastore);
d0a0bac6815e Added: Datastore support for hosts and global data in addition to users
Waqas Hussain <waqas20@gmail.com>
parents: 0
diff changeset
77 else
d0a0bac6815e Added: Datastore support for hosts and global data in addition to users
Waqas Hussain <waqas20@gmail.com>
parents: 0
diff changeset
78 return format("data/%s.dat", datastore);
d0a0bac6815e Added: Datastore support for hosts and global data in addition to users
Waqas Hussain <waqas20@gmail.com>
parents: 0
diff changeset
79 end
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
80 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
81
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
82 function load(username, host, datastore)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
83 local data, ret = loadfile(getpath(username, host, datastore));
117
8e5c5e6a3240 Fixed: datamanager.store and datamanager.load could crash when username or host arguments were nil. (useful for server specific and global data).
Waqas Hussain <waqas20@gmail.com>
parents: 88
diff changeset
84 if not data then
182
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
85 log("warn", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
117
8e5c5e6a3240 Fixed: datamanager.store and datamanager.load could crash when username or host arguments were nil. (useful for server specific and global data).
Waqas Hussain <waqas20@gmail.com>
parents: 88
diff changeset
86 return nil;
8e5c5e6a3240 Fixed: datamanager.store and datamanager.load could crash when username or host arguments were nil. (useful for server specific and global data).
Waqas Hussain <waqas20@gmail.com>
parents: 88
diff changeset
87 end
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
88 setfenv(data, {});
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
89 local success, ret = pcall(data);
117
8e5c5e6a3240 Fixed: datamanager.store and datamanager.load could crash when username or host arguments were nil. (useful for server specific and global data).
Waqas Hussain <waqas20@gmail.com>
parents: 88
diff changeset
90 if not success then
182
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
91 log("error", "Unable to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
117
8e5c5e6a3240 Fixed: datamanager.store and datamanager.load could crash when username or host arguments were nil. (useful for server specific and global data).
Waqas Hussain <waqas20@gmail.com>
parents: 88
diff changeset
92 return nil;
8e5c5e6a3240 Fixed: datamanager.store and datamanager.load could crash when username or host arguments were nil. (useful for server specific and global data).
Waqas Hussain <waqas20@gmail.com>
parents: 88
diff changeset
93 end
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
94 return ret;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
95 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
96
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
97 function store(username, host, datastore, data)
205
e30d0e30a0ff Datamanager now deletes files with no data
Waqas Hussain <waqas20@gmail.com>
parents: 182
diff changeset
98 if not data then
e30d0e30a0ff Datamanager now deletes files with no data
Waqas Hussain <waqas20@gmail.com>
parents: 182
diff changeset
99 data = {};
e30d0e30a0ff Datamanager now deletes files with no data
Waqas Hussain <waqas20@gmail.com>
parents: 182
diff changeset
100 end
e30d0e30a0ff Datamanager now deletes files with no data
Waqas Hussain <waqas20@gmail.com>
parents: 182
diff changeset
101 -- save the datastore
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
102 local f, msg = io_open(getpath(username, host, datastore), "w+");
117
8e5c5e6a3240 Fixed: datamanager.store and datamanager.load could crash when username or host arguments were nil. (useful for server specific and global data).
Waqas Hussain <waqas20@gmail.com>
parents: 88
diff changeset
103 if not f then
182
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
104 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil"));
205
e30d0e30a0ff Datamanager now deletes files with no data
Waqas Hussain <waqas20@gmail.com>
parents: 182
diff changeset
105 return;
117
8e5c5e6a3240 Fixed: datamanager.store and datamanager.load could crash when username or host arguments were nil. (useful for server specific and global data).
Waqas Hussain <waqas20@gmail.com>
parents: 88
diff changeset
106 end
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
107 f:write("return ");
182
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
108 simplesave(f, data, 1);
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
109 f:close();
205
e30d0e30a0ff Datamanager now deletes files with no data
Waqas Hussain <waqas20@gmail.com>
parents: 182
diff changeset
110 if not next(data) then -- try to delete empty datastore
e30d0e30a0ff Datamanager now deletes files with no data
Waqas Hussain <waqas20@gmail.com>
parents: 182
diff changeset
111 os_remove(getpath(username, host, datastore));
e30d0e30a0ff Datamanager now deletes files with no data
Waqas Hussain <waqas20@gmail.com>
parents: 182
diff changeset
112 end
e30d0e30a0ff Datamanager now deletes files with no data
Waqas Hussain <waqas20@gmail.com>
parents: 182
diff changeset
113 -- we write data even when we are deleting because lua doesn't have a
e30d0e30a0ff Datamanager now deletes files with no data
Waqas Hussain <waqas20@gmail.com>
parents: 182
diff changeset
114 -- platform independent way of checking for non-exisitng files
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
115 return true;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
116 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
117
129
0f119bece309 Fixed: Some modules did not return anything
Waqas Hussain <waqas20@gmail.com>
parents: 117
diff changeset
118 return _M;

mercurial