Comparison

util/datamanager.lua @ 932:1ed3e5fe165a

Added: datamanager: Allow a callback to be installed which selectively prevents disk writes
author Waqas Hussain <waqas20@gmail.com>
date Mon, 30 Mar 2009 03:22:34 +0500
parent 915:0fe5bf7ab81d
child 1097:c5b33640a5f0
comparison
equal deleted inserted replaced
931:4514ed5ee943 932:1ed3e5fe165a
48 end 48 end
49 return path; 49 return path;
50 end 50 end
51 51
52 local data_path = "data"; 52 local data_path = "data";
53 local callback;
53 54
54 ------- API ------------- 55 ------- API -------------
55 56
56 function set_data_path(path) 57 function set_data_path(path)
57 log("info", "Setting data path to: %s", path); 58 log("info", "Setting data path to: %s", path);
58 data_path = path; 59 data_path = path;
60 end
61 function set_callback(func)
62 callback = func;
59 end 63 end
60 64
61 function getpath(username, host, datastore, ext, create) 65 function getpath(username, host, datastore, ext, create)
62 ext = ext or "dat"; 66 ext = ext or "dat";
63 host = host and encode(host); 67 host = host and encode(host);
91 95
92 function store(username, host, datastore, data) 96 function store(username, host, datastore, data)
93 if not data then 97 if not data then
94 data = {}; 98 data = {};
95 end 99 end
100 if callback and callback(username, host, datastore) then return true; end
96 -- save the datastore 101 -- save the datastore
97 local f, msg = io_open(getpath(username, host, datastore, nil, true), "w+"); 102 local f, msg = io_open(getpath(username, host, datastore, nil, true), "w+");
98 if not f then 103 if not f then
99 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil")); 104 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil"));
100 return; 105 return;
111 return true; 116 return true;
112 end 117 end
113 118
114 function list_append(username, host, datastore, data) 119 function list_append(username, host, datastore, data)
115 if not data then return; end 120 if not data then return; end
121 if callback and callback(username, host, datastore) then return true; end
116 -- save the datastore 122 -- save the datastore
117 local f, msg = io_open(getpath(username, host, datastore, "list", true), "a+"); 123 local f, msg = io_open(getpath(username, host, datastore, "list", true), "a+");
118 if not f then 124 if not f then
119 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil")); 125 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil"));
120 return; 126 return;
128 134
129 function list_store(username, host, datastore, data) 135 function list_store(username, host, datastore, data)
130 if not data then 136 if not data then
131 data = {}; 137 data = {};
132 end 138 end
139 if callback and callback(username, host, datastore) then return true; end
133 -- save the datastore 140 -- save the datastore
134 local f, msg = io_open(getpath(username, host, datastore, "list", true), "w+"); 141 local f, msg = io_open(getpath(username, host, datastore, "list", true), "w+");
135 if not f then 142 if not f then
136 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil")); 143 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil"));
137 return; 144 return;