Software /
code /
prosody
Annotate
teal-src/util/smqueue.tl @ 12057:e880f5a13080
util.smqueue: Add missing method to Teal record spec
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Thu, 16 Dec 2021 12:16:08 +0100 |
parent | 12055:daced16154fa |
child | 12058:4860da718e87 |
rev | line source |
---|---|
12055
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 local queue = require "util.queue"; |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 local record lib |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 -- T would typically be util.stanza |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 record smqueue<T> |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 _queue : queue.queue<T> |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 _head : integer |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 _tail : integer |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 enum ack_errors |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 "tail" |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 "head" |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 "pop" |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 end |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 push : function (smqueue, T) |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 ack : function (smqueue, integer) : { T }, ack_errors |
12057
e880f5a13080
util.smqueue: Add missing method to Teal record spec
Kim Alvefur <zash@zash.se>
parents:
12055
diff
changeset
|
17 resumable : function (smqueue<T>) : boolean |
e880f5a13080
util.smqueue: Add missing method to Teal record spec
Kim Alvefur <zash@zash.se>
parents:
12055
diff
changeset
|
18 resume : function (smqueue<T>) : queue.queue.iterator, any, integer |
12055
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 type consume_iter = function (smqueue<T>) : T |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 consume : function (smqueue<T>) : consume_iter |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 table : function (smqueue<T>) : { T } |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 end |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 new : function <T>(integer) : smqueue<T> |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 end |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 local type smqueue = lib.smqueue; |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 function smqueue:push(v) |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 self._head = self._head + 1; |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 -- Wraps instead of errors |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 assert(self._queue:push(v)); |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 end |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 function smqueue:ack(h : integer) : { any }, smqueue.ack_errors |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 if h < self._tail then |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 return nil, "tail"; |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 elseif h > self._head then |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 return nil, "head"; |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 end |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 -- TODO optimize? cache table fields |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 local acked = {}; |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 self._tail = h; |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 local expect = self._head - self._tail; |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
45 while expect < self._queue:count() do |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
46 local v = self._queue:pop(); |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 if not v then return nil, "pop"; end |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
48 table.insert(acked, v); |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
49 end |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
50 return acked; |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
51 end |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
52 |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
53 function smqueue:count_unacked() : integer |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
54 return self._head - self._tail; |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
55 end |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
56 |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
57 function smqueue:count_acked() : integer |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
58 return self._tail; |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
59 end |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
60 |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
61 function smqueue:resumable() : boolean |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
62 return self._queue:count() >= (self._head - self._tail); |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
63 end |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
64 |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
65 function smqueue:resume() : queue.queue.iterator, any, integer |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
66 return self._queue:items(); |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
67 end |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
68 |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
69 function smqueue:consume() : queue.queue.consume_iter |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
70 return self._queue:consume() |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
71 end |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
72 |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
73 -- Compatibility wrapper, meant to look like a plain ol' array |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
74 local record compat_mt |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
75 _queue : smqueue<any> |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
76 end |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
77 |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
78 function compat_mt:__index(i : integer) : any |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
79 if i < self._queue._tail then return nil end |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
80 return self._queue._queue._items[(i + self._queue._tail) % self._queue._queue.size]; |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
81 end |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
82 |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
83 function compat_mt:__len() : integer |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
84 return self._queue:count_unacked() |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
85 end |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
86 |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
87 function smqueue:table() : { any } |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
88 return setmetatable({ _queue = self }, compat_mt); |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
89 end |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
90 |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
91 local function freeze(q : smqueue<any>) : { string:integer } |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
92 return { head = q._head, tail = q._tail } |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
93 end |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
94 |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
95 local queue_mt = { |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
96 -- |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
97 __name = "smqueue"; |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
98 __index = smqueue; |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
99 __len = smqueue.count_unacked; |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
100 __freeze = freeze; |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
101 } |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
102 |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
103 function lib.new<T>(size : integer) : queue.queue<T> |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
104 assert(size>0); |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
105 return setmetatable({ _head = 0; _tail = 0; _queue = queue.new(size, true) }, queue_mt); |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
106 end |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
107 |
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
108 return lib; |