comparison mod_archive/mod_archive.lua @ 230:0b9e8721b9c2

mod_archive: make sure result set are sorted in chronological order.
author shinysky<shinysky1986(AT)gmail.com>
date Thu, 29 Jul 2010 17:39:28 +0800
parents 56103bdb068c
children 4ff8068b4d94
comparison
equal deleted inserted replaced
228:56103bdb068c 230:0b9e8721b9c2
47 end 47 end
48 48
49 local function date_parse(s) 49 local function date_parse(s)
50 local year, month, day, hour, min, sec = s:match("(....)-?(..)-?(..)T(..):(..):(..)Z"); 50 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}); 51 return os.time({year=year, month=month, day=day, hour=hour, min=min, sec=sec});
52 end
53
54 local function list_reverse(list)
55 local t, n = {}, #list
56 for i = 1, n do t[i] = list[n-i+1] end -- reverse
57 for i = 1, n do list[i] = t[i] end -- copy back
52 end 58 end
53 59
54 -- local function list_push(node, host, collection) 60 -- local function list_push(node, host, collection)
55 -- local data = dm.list_load(node, host, ARCHIVE_DIR); 61 -- local data = dm.list_load(node, host, ARCHIVE_DIR);
56 -- if data then 62 -- if data then
101 local with = isfrom and msg.attr.to or msg.attr.from; 107 local with = isfrom and msg.attr.to or msg.attr.from;
102 local utc = os_date(); 108 local utc = os_date();
103 local utc_secs = os.time(utc); 109 local utc_secs = os.time(utc);
104 local utc_datetime = date_time(utc); 110 local utc_datetime = date_time(utc);
105 if data then 111 if data then
106 -- TODO assuming the collection list are in REVERSE chronological order 112 -- The collection list are in REVERSE chronological order
107 for k, v in ipairs(data) do 113 for k, v in ipairs(data) do
108 local collection = st.deserialize(v); 114 local collection = st.deserialize(v);
109 local do_save = function() 115 local do_save = function()
110 local dt = 1; 116 local dt = 1;
111 for i = #collection, 1, -1 do 117 for i = #collection, 1, -1 do
455 end 461 end
456 end 462 end
457 local reply = st.reply(stanza):tag('list', {xmlns='urn:xmpp:archive'}); 463 local reply = st.reply(stanza):tag('list', {xmlns='urn:xmpp:archive'});
458 local count = table.getn(resset); 464 local count = table.getn(resset);
459 if count > 0 then 465 if count > 0 then
466 list_reverse(resset);
460 local max = elem.tags[1]:child_with_name("max"); 467 local max = elem.tags[1]:child_with_name("max");
461 if max then 468 if max then
462 max = tonumber(max:get_text()) or DEFAULT_MAX; 469 max = tonumber(max:get_text()) or DEFAULT_MAX;
463 else max = DEFAULT_MAX; end 470 else max = DEFAULT_MAX; end
464 local after = elem.tags[1]:child_with_name("after"); 471 local after = elem.tags[1]:child_with_name("after");
491 s = tonumber(index:get_text()) + 1; -- 0-based 498 s = tonumber(index:get_text()) + 1; -- 0-based
492 e = s + max; 499 e = s + max;
493 end 500 end
494 if s < 1 then s = 1; end 501 if s < 1 then s = 1; end
495 if e > count + 1 then e = count + 1; end 502 if e > count + 1 then e = count + 1; end
496 -- Assuming result set is sorted.
497 for i = s, e-1 do 503 for i = s, e-1 do
498 reply:add_child(st.stanza('chat', resset[i].attr)); 504 reply:add_child(st.stanza('chat', resset[i].attr));
499 end 505 end
500 local set = st.stanza('set', {xmlns = xmlns_rsm}); 506 local set = st.stanza('set', {xmlns = xmlns_rsm});
501 if s <= e-1 then 507 if s <= e-1 then
573 s = tonumber(index:get_text()) + 1; -- 0-based 579 s = tonumber(index:get_text()) + 1; -- 0-based
574 e = s + max; 580 e = s + max;
575 end 581 end
576 if s < 1 then s = 1; end 582 if s < 1 then s = 1; end
577 if e > count + 1 then e = count + 1; end 583 if e > count + 1 then e = count + 1; end
578 -- Assuming result set is sorted.
579 for i = s, e-1 do 584 for i = s, e-1 do
580 reply:add_child(resset[i]); 585 reply:add_child(resset[i]);
581 end 586 end
582 local set = st.stanza('set', {xmlns = xmlns_rsm}); 587 local set = st.stanza('set', {xmlns = xmlns_rsm});
583 if s <= e-1 then 588 if s <= e-1 then
644 end 649 end
645 end 650 end
646 local reply = st.reply(stanza):tag('modified', {xmlns='urn:xmpp:archive'}); 651 local reply = st.reply(stanza):tag('modified', {xmlns='urn:xmpp:archive'});
647 local count = table.getn(resset); 652 local count = table.getn(resset);
648 if count > 0 then 653 if count > 0 then
654 list_reverse(resset);
649 local max = elem.tags[1]:child_with_name("max"); 655 local max = elem.tags[1]:child_with_name("max");
650 if max then 656 if max then
651 max = tonumber(max:get_text()) or DEFAULT_MAX; 657 max = tonumber(max:get_text()) or DEFAULT_MAX;
652 else max = DEFAULT_MAX; end 658 else max = DEFAULT_MAX; end
653 local after = elem.tags[1]:child_with_name("after"); 659 local after = elem.tags[1]:child_with_name("after");
680 s = tonumber(index:get_text()) + 1; -- 0-based 686 s = tonumber(index:get_text()) + 1; -- 0-based
681 e = s + max; 687 e = s + max;
682 end 688 end
683 if s < 1 then s = 1; end 689 if s < 1 then s = 1; end
684 if e > count + 1 then e = count + 1; end 690 if e > count + 1 then e = count + 1; end
685 -- Assuming result set is sorted.
686 for i = s, e-1 do 691 for i = s, e-1 do
687 if resset[i][1] then 692 if resset[i][1] then
688 reply:add_child(st.stanza('changed', resset[i].attr)); 693 reply:add_child(st.stanza('changed', resset[i].attr));
689 else 694 else
690 reply:add_child(st.stanza('removed', resset[i].attr)); 695 reply:add_child(st.stanza('removed', resset[i].attr));