Comparison

plugins/mod_storage_internal.lua @ 10837:f23363380599

mod_storage_internal: Implement key-value API
author Kim Alvefur <zash@zash.se>
date Mon, 11 May 2020 21:56:19 +0200
parent 10223:d185c4961ee0
child 10842:5a6ba2f38e2b
comparison
equal deleted inserted replaced
10836:93019f3edd68 10837:f23363380599
206 item = st.deserialize(item); 206 item = st.deserialize(item);
207 return key, item, when, with; 207 return key, item, when, with;
208 end, count; 208 end, count;
209 end 209 end
210 210
211 function archive:get(username, wanted_key)
212 local iter, err = self:find(username, { key = wanted_key })
213 if not iter then return iter, err; end
214 for key, stanza, when, with in iter do
215 if key == wanted_key then
216 return stanza, when, with;
217 end
218 end
219 return nil, "item-not-found";
220 end
221
222 function archive:set(username, key, new_value, new_when, new_with)
223 local items, err = datamanager.list_load(username, host, self.store);
224 if not items then
225 if err then
226 return items, err;
227 else
228 return nil, "item-not-found";
229 end
230 end
231
232 for i = 1, #items do
233 local old_item = items[i];
234 if old_item.key == key then
235 local item = st.preserialize(st.clone(new_value));
236
237 local when = new_when or item.when or datetime.parse(item.attr.stamp);
238 item.key = key;
239 item.when = when;
240 item.with = new_with or old_item.with;
241 item.attr.stamp = datetime.datetime(when);
242 item.attr.stamp_legacy = datetime.legacy(when);
243 items[i] = item;
244 return datamanager.list_store(username, host, self.store, items);
245 end
246 end
247
248 return nil, "item-not-found";
249 end
250
211 function archive:dates(username) 251 function archive:dates(username)
212 local items, err = datamanager.list_load(username, host, self.store); 252 local items, err = datamanager.list_load(username, host, self.store);
213 if not items then return items, err; end 253 if not items then return items, err; end
214 return array(items):pluck("when"):map(datetime.date):unique(); 254 return array(items):pluck("when"):map(datetime.date):unique();
215 end 255 end