Annotate

util/datamanager.lua @ 532:c24471cadc2c

Added an error log message for this case
author Matthew Wild <mwild1@gmail.com>
date Wed, 03 Dec 2008 18:06:04 +0000
parent 519:cccd610a0ef9
child 550:56bdfae9e4ea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 456
diff changeset
1 -- Prosody IM v0.1
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 456
diff changeset
2 -- Copyright (C) 2008 Matthew Wild
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 456
diff changeset
3 -- Copyright (C) 2008 Waqas Hussain
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 456
diff changeset
4 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 456
diff changeset
5 -- This program is free software; you can redistribute it and/or
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 456
diff changeset
6 -- modify it under the terms of the GNU General Public License
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 456
diff changeset
7 -- as published by the Free Software Foundation; either version 2
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 456
diff changeset
8 -- of the License, or (at your option) any later version.
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 456
diff changeset
9 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 456
diff changeset
10 -- This program is distributed in the hope that it will be useful,
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 456
diff changeset
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 456
diff changeset
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 456
diff changeset
13 -- GNU General Public License for more details.
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 456
diff changeset
14 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 456
diff changeset
15 -- You should have received a copy of the GNU General Public License
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 456
diff changeset
16 -- along with this program; if not, write to the Free Software
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 456
diff changeset
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 456
diff changeset
18 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 456
diff changeset
19
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 456
diff changeset
20
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
21 local format = string.format;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
22 local setmetatable, type = setmetatable, type;
247
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
23 local pairs, ipairs = pairs, ipairs;
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
24 local char = string.char;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
25 local loadfile, setfenv, pcall = loadfile, setfenv, pcall;
456
27cb85d4059e Fixed logging in datamanager
Waqas Hussain <waqas20@gmail.com>
parents: 452
diff changeset
26 local log = require "util.logger".init("datamanager");
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
27 local io_open = io.open;
206
e30d0e30a0ff Datamanager now deletes files with no data
Waqas Hussain <waqas20@gmail.com>
parents: 182
diff changeset
28 local os_remove = os.remove;
267
6303b7454ad0 Some bugs fixed
Waqas Hussain <waqas20@gmail.com>
parents: 247
diff changeset
29 local tostring, tonumber = tostring, tonumber;
177
606c433955e7 Bug fixes and checks for presence subscriptions, etc
Waqas Hussain <waqas20@gmail.com>
parents: 129
diff changeset
30 local error = error;
206
e30d0e30a0ff Datamanager now deletes files with no data
Waqas Hussain <waqas20@gmail.com>
parents: 182
diff changeset
31 local next = next;
247
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
32 local t_insert = table.insert;
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
33
182
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
34 local indent = function(f, i)
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
35 for n = 1, i do
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
36 f:write("\t");
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
37 end
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
38 end
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
39
452
613c5c6bdce4 Added option core.data_path
Waqas Hussain <waqas20@gmail.com>
parents: 267
diff changeset
40 local data_path = "data";
613c5c6bdce4 Added option core.data_path
Waqas Hussain <waqas20@gmail.com>
parents: 267
diff changeset
41
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
42 module "datamanager"
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
43
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
44
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
45 ---- utils -----
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
46 local encode, decode;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
47
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
48 do
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
49 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
50
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
51 decode = function (s)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
52 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
53 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
54
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
55 encode = function (s)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
56 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
57 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
58 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
59
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
60 local function basicSerialize (o)
182
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
61 if type(o) == "number" or type(o) == "boolean" then
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
62 return tostring(o);
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
63 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
64 return (format("%q", tostring(o)):gsub("\\\n", "\\n"));
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
65 end
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
66 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
67
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
68
182
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
69 local function simplesave (f, o, ind)
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
70 if type(o) == "number" then
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
71 f:write(o)
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
72 elseif type(o) == "string" then
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
73 f:write((format("%q", o):gsub("\\\n", "\\n")))
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
74 elseif type(o) == "table" then
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
75 f:write("{\n")
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
76 for k,v in pairs(o) do
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
77 indent(f, ind);
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
78 f:write("[", basicSerialize(k), "] = ")
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
79 simplesave(f, v, ind+1)
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
80 f:write(",\n")
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
81 end
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
82 indent(f, ind-1);
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
83 f:write("}")
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
84 elseif type(o) == "boolean" then
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
85 f:write(o and "true" or "false");
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
86 else
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
87 error("cannot serialize a " .. type(o))
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
88 end
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
89 end
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
90
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
91 ------- API -------------
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
92
452
613c5c6bdce4 Added option core.data_path
Waqas Hussain <waqas20@gmail.com>
parents: 267
diff changeset
93 function set_data_path(path)
613c5c6bdce4 Added option core.data_path
Waqas Hussain <waqas20@gmail.com>
parents: 267
diff changeset
94 data_path = path;
613c5c6bdce4 Added option core.data_path
Waqas Hussain <waqas20@gmail.com>
parents: 267
diff changeset
95 end
613c5c6bdce4 Added option core.data_path
Waqas Hussain <waqas20@gmail.com>
parents: 267
diff changeset
96
247
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
97 function getpath(username, host, datastore, ext)
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
98 ext = ext or "dat";
84
d0a0bac6815e Added: Datastore support for hosts and global data in addition to users
Waqas Hussain <waqas20@gmail.com>
parents: 0
diff changeset
99 if username then
452
613c5c6bdce4 Added option core.data_path
Waqas Hussain <waqas20@gmail.com>
parents: 267
diff changeset
100 return format("%s/%s/%s/%s.%s", data_path, encode(host), datastore, encode(username), ext);
84
d0a0bac6815e Added: Datastore support for hosts and global data in addition to users
Waqas Hussain <waqas20@gmail.com>
parents: 0
diff changeset
101 elseif host then
452
613c5c6bdce4 Added option core.data_path
Waqas Hussain <waqas20@gmail.com>
parents: 267
diff changeset
102 return format("%s/%s/%s.%s", data_path, encode(host), datastore, ext);
84
d0a0bac6815e Added: Datastore support for hosts and global data in addition to users
Waqas Hussain <waqas20@gmail.com>
parents: 0
diff changeset
103 else
452
613c5c6bdce4 Added option core.data_path
Waqas Hussain <waqas20@gmail.com>
parents: 267
diff changeset
104 return format("%s/%s.%s", data_path, datastore, ext);
84
d0a0bac6815e Added: Datastore support for hosts and global data in addition to users
Waqas Hussain <waqas20@gmail.com>
parents: 0
diff changeset
105 end
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
106 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
107
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
108 function load(username, host, datastore)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
109 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
110 if not data then
182
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
111 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
112 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
113 end
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
114 setfenv(data, {});
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
115 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
116 if not success then
182
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
117 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
118 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
119 end
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
120 return ret;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
121 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
122
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
123 function store(username, host, datastore, data)
206
e30d0e30a0ff Datamanager now deletes files with no data
Waqas Hussain <waqas20@gmail.com>
parents: 182
diff changeset
124 if not data then
e30d0e30a0ff Datamanager now deletes files with no data
Waqas Hussain <waqas20@gmail.com>
parents: 182
diff changeset
125 data = {};
e30d0e30a0ff Datamanager now deletes files with no data
Waqas Hussain <waqas20@gmail.com>
parents: 182
diff changeset
126 end
e30d0e30a0ff Datamanager now deletes files with no data
Waqas Hussain <waqas20@gmail.com>
parents: 182
diff changeset
127 -- save the datastore
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
128 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
129 if not f then
182
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
130 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil"));
206
e30d0e30a0ff Datamanager now deletes files with no data
Waqas Hussain <waqas20@gmail.com>
parents: 182
diff changeset
131 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
132 end
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
133 f:write("return ");
182
f5cb6b5a6eb7 Datamanager Fixes and improvements
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
134 simplesave(f, data, 1);
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
135 f:close();
206
e30d0e30a0ff Datamanager now deletes files with no data
Waqas Hussain <waqas20@gmail.com>
parents: 182
diff changeset
136 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
137 os_remove(getpath(username, host, datastore));
e30d0e30a0ff Datamanager now deletes files with no data
Waqas Hussain <waqas20@gmail.com>
parents: 182
diff changeset
138 end
e30d0e30a0ff Datamanager now deletes files with no data
Waqas Hussain <waqas20@gmail.com>
parents: 182
diff changeset
139 -- 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
140 -- platform independent way of checking for non-exisitng files
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
141 return true;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
142 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
143
247
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
144 function list_append(username, host, datastore, data)
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
145 if not data then return; end
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
146 -- save the datastore
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
147 local f, msg = io_open(getpath(username, host, datastore, "list"), "a+");
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
148 if not f then
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
149 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil"));
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
150 return;
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
151 end
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
152 f:write("item(");
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
153 simplesave(f, data, 1);
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
154 f:write(");\n");
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
155 f:close();
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
156 return true;
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
157 end
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
158
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
159 function list_store(username, host, datastore, data)
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
160 if not data then
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
161 data = {};
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
162 end
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
163 -- save the datastore
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
164 local f, msg = io_open(getpath(username, host, datastore, "list"), "w+");
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
165 if not f then
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
166 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil"));
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
167 return;
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
168 end
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
169 for _, d in ipairs(data) do
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
170 f:write("item(");
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
171 simplesave(f, d, 1);
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
172 f:write(");\n");
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
173 end
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
174 f:close();
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
175 if not next(data) then -- try to delete empty datastore
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
176 os_remove(getpath(username, host, datastore, "list"));
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
177 end
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
178 -- we write data even when we are deleting because lua doesn't have a
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
179 -- platform independent way of checking for non-exisitng files
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
180 return true;
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
181 end
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
182
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
183 function list_load(username, host, datastore)
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
184 local data, ret = loadfile(getpath(username, host, datastore, "list"));
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
185 if not data then
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
186 log("warn", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
187 return nil;
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
188 end
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
189 local items = {};
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
190 setfenv(data, {item = function(i) t_insert(items, i); end});
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
191 local success, ret = pcall(data);
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
192 if not success then
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
193 log("error", "Unable to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
194 return nil;
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
195 end
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
196 return items;
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
197 end
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
198
681b29aa134f Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages)
Waqas Hussain <waqas20@gmail.com>
parents: 206
diff changeset
199 return _M;