annotate mod_mam/fallback_archive.lib.lua @ 2552:18b6a55dd5d6

mod_firewall: Support expressions in TO/FROM EXACTLY
author Matthew Wild <mwild1@gmail.com>
date Thu, 23 Feb 2017 14:05:39 +0000
parents 03f6d9ed2903
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2023
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 -- luacheck: ignore 212/self
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 local uuid = require "util.uuid".generate;
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 local store = module:shared("archive");
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 local archive_store = { _provided_by = "mam"; name = "fallback"; };
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 function archive_store:append(username, key, value, when, with)
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 local archive = store[username];
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 if not archive then
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 archive = { [0] = 0 };
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 store[username] = archive;
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 end
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 local index = (archive[0] or #archive)+1;
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 local item = { key = key, when = when, with = with, value = value };
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 if not key or archive[key] then
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 key = uuid();
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 item.key = key;
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 end
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 archive[index] = item;
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 archive[key] = index;
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 archive[0] = index;
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 return key;
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 end
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 function archive_store:find(username, query)
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 local archive = store[username] or {};
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 local start, stop, step = 1, archive[0] or #archive, 1;
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 local qstart, qend, qwith = -math.huge, math.huge;
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 local limit;
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 if query then
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 if query.reverse then
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 start, stop, step = stop, start, -1;
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 if query.before and archive[query.before] then
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 start = archive[query.before] - 1;
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 end
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 elseif query.after and archive[query.after] then
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 start = archive[query.after] + 1;
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 end
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 qwith = query.with;
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 limit = query.limit;
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 qstart = query.start or qstart;
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 qend = query["end"] or qend;
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 end
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 return function ()
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 if limit and limit <= 0 then return end
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 for i = start, stop, step do
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 local item = archive[i];
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 if (not qwith or qwith == item.with) and item.when >= qstart and item.when <= qend then
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 if limit then limit = limit - 1; end
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 start = i + step; -- Start on next item
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 return item.key, item.value, item.when, item.with;
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 end
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 end
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 end
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 end
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 function archive_store:delete(username, query)
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 if not query or next(query) == nil then
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 -- no specifics, delete everything
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 store[username] = nil;
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 return true;
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 end
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 local archive = store[username];
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 if not archive then return true; end -- no messages, nothing to delete
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 local qstart = query.start or -math.huge;
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 local qend = query["end"] or math.huge;
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 local qwith = query.with;
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 store[username] = nil;
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 for i = 1, #archive do
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 local item = archive[i];
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 local when, with = item.when, item.when;
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 -- Add things that don't match the query
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 if not ((not qwith or qwith == item.with) and item.when >= qstart and item.when <= qend) then
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 self:append(username, item.key, item.value, when, with);
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 end
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 end
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 return true;
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 end
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82
98b4794b72e4 mod_mam: Include an in-memory fallback driver
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 return archive_store;