comparison mod_archive/mod_archive.lua @ 233:4ff8068b4d94

mod_archive: fixed some serious bugs.
author shinysky<shinysky1986(AT)gmail.com>
date Wed, 04 Aug 2010 12:36:27 +0800
parents 0b9e8721b9c2
children d900be0dee3e
comparison
equal deleted inserted replaced
232:0ca669b18ef3 233:4ff8068b4d94
40 40
41 local function os_date() 41 local function os_date()
42 return os.date("!*t"); 42 return os.date("!*t");
43 end 43 end
44 44
45 local function date_time(t) 45 local function date_time(localtime)
46 return datetime.datetime(t); 46 return datetime.datetime(localtime);
47 end
48
49 local function date_format(s)
50 return os.date("%Y-%m-%dT%H:%M:%SZ", s);
47 end 51 end
48 52
49 local function date_parse(s) 53 local function date_parse(s)
50 local year, month, day, hour, min, sec = s:match("(....)-?(..)-?(..)T(..):(..):(..)Z"); 54 local year, month, day, hour, min, sec = s:match("(....)-?(..)-?(..)T(..):(..):(..)Z");
51 return os.time({year=year, month=month, day=day, hour=hour, min=min, sec=sec}); 55 return os.time({year=year, month=month, day=day, hour=hour, min=min, sec=sec});
58 end 62 end
59 63
60 -- local function list_push(node, host, collection) 64 -- local function list_push(node, host, collection)
61 -- local data = dm.list_load(node, host, ARCHIVE_DIR); 65 -- local data = dm.list_load(node, host, ARCHIVE_DIR);
62 -- if data then 66 -- if data then
63 -- table.insert(data, collection, 1); 67 -- table.insert(data, 1, collection);
64 -- dm.list_store(node, host, ARCHIVE_DIR, st.preserialize(data)); 68 -- dm.list_store(node, host, ARCHIVE_DIR, st.preserialize(data));
65 -- else 69 -- else
66 -- dm.list_append(node, host, ARCHIVE_DIR, st.preserialize(collection)); 70 -- dm.list_append(node, host, ARCHIVE_DIR, st.preserialize(collection));
67 -- end 71 -- end
68 -- end 72 -- end
72 if data then 76 if data then
73 local s, e = 1, #data; 77 local s, e = 1, #data;
74 while true do 78 while true do
75 local c = st.deserialize(data[s]); 79 local c = st.deserialize(data[s]);
76 if collection.attr["start"] >= c.attr["start"] then 80 if collection.attr["start"] >= c.attr["start"] then
77 table.insert(data, collection, s); 81 table.insert(data, s, collection);
78 break; 82 break;
79 end 83 end
80 c = st.deserialize(data[e]); 84 c = st.deserialize(data[e]);
81 if collection.attr["start"] <= c.attr["start"] then 85 if collection.attr["start"] <= c.attr["start"] then
82 table.insert(data, collection, e+1); 86 table.insert(data, e+1, collection);
83 break; 87 break;
84 end 88 end
85 local m = math.floor((s + e) / 2); 89 local m = math.floor((s + e) / 2);
86 c = st.deserialize(data[m]); 90 c = st.deserialize(data[m]);
87 if collection.attr["start"] > c.attr["start"] then 91 if collection.attr["start"] > c.attr["start"] then
88 e = m - 1; 92 e = m - 1;
89 elseif collection.attr["start"] < c.attr["start"] then 93 elseif collection.attr["start"] < c.attr["start"] then
90 s = m + 1; 94 s = m + 1;
91 else 95 else
92 table.insert(data, collection, m); 96 table.insert(data, m, collection);
93 break; 97 break;
94 end 98 end
95 end 99 end
96 dm.list_store(node, host, ARCHIVE_DIR, st.preserialize(data)); 100 dm.list_store(node, host, ARCHIVE_DIR, st.preserialize(data));
97 else 101 else
105 local data = dm.list_load(node, host, ARCHIVE_DIR); 109 local data = dm.list_load(node, host, ARCHIVE_DIR);
106 local tag = isfrom and "from" or "to"; 110 local tag = isfrom and "from" or "to";
107 local with = isfrom and msg.attr.to or msg.attr.from; 111 local with = isfrom and msg.attr.to or msg.attr.from;
108 local utc = os_date(); 112 local utc = os_date();
109 local utc_secs = os.time(utc); 113 local utc_secs = os.time(utc);
110 local utc_datetime = date_time(utc); 114 local utc_datetime = date_format(utc_secs);
111 if data then 115 if data then
112 -- The collection list are in REVERSE chronological order 116 -- The collection list are in REVERSE chronological order
113 for k, v in ipairs(data) do 117 for k, v in ipairs(data) do
114 local collection = st.deserialize(v); 118 local collection = st.deserialize(v);
115 local do_save = function() 119 local do_save = function()
134 return; 138 return;
135 end 139 end
136 else 140 else
137 local dt = os.difftime(utc_secs, date_parse(collection.attr["start"])); 141 local dt = os.difftime(utc_secs, date_parse(collection.attr["start"]));
138 if dt >= 14400 then break end 142 if dt >= 14400 then break end
139 if collection.attr["with"] == with then 143 if collection.attr["with"] == with then -- JID matching?
140 do_save(); 144 do_save();
141 return; 145 return;
142 end 146 end
143 end 147 end
144 end 148 end
418 end 422 end
419 423
420 ------------------------------------------------------------ 424 ------------------------------------------------------------
421 -- Archive Management 425 -- Archive Management
422 ------------------------------------------------------------ 426 ------------------------------------------------------------
423 local function match_jid(rule, jid) 427 local function match_jid(rule, id)
424 return not rule or jid.compare(jid, rule); 428 return not rule or jid.compare(id, rule);
425 end 429 end
426 430
427 local function is_earlier(start, coll_start) 431 local function is_earlier(start, coll_start)
428 return not start or start <= coll_start; 432 return not start or start <= coll_start;
429 end 433 end