-
Notifications
You must be signed in to change notification settings - Fork 1
/
msgnode.go
87 lines (75 loc) · 1.67 KB
/
msgnode.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package dbpipe
// msgNode is a single message.
type msgNode struct {
Id int64
Subject string
Data []byte
list *msgList
next *msgNode
}
// msgList is a list of messages.
type msgList struct {
head *msgNode
tail *msgNode
n int
}
// msgStream is a stream (unknown length) of messages.
// The next message (or nil if no more message) should be returned when calling with true.
// If calling with false, then the stream should be closed and release resource.
type msgStream func(next bool) (*msgNode, error)
// Append appends node to list. node must not belong to any list.
func (list *msgList) Append(node *msgNode) {
if node.list != nil {
panic("msgNode already belongs to some msgList")
}
node.list = list
if list.n == 0 {
list.head = node
list.tail = node
} else {
list.tail.next = node
list.tail = node
}
list.n += 1
}
// Pop pops the first node from list. It returns nil if the list is empty.
func (list *msgList) Pop() *msgNode {
node := list.head
if node == nil {
return nil
}
list.n -= 1
if list.n == 0 {
list.head = nil
list.tail = nil
} else {
list.head = node.next
}
node.list = nil
node.next = nil
return node
}
// Reset deletes all msgNode in a msgList.
func (list *msgList) Reset() {
list.head = nil
list.tail = nil
list.n = 0
}
// Iterate returns an iterator of the list.
func (list *msgList) Iterate() func() *msgNode {
node := list.head
return func() *msgNode {
if node == nil {
return nil
}
ret := node
node = node.next
return ret
}
}
// newErrStream creates a msgStream that always returns an error.
func newErrStream(err error) msgStream {
return func(next bool) (*msgNode, error) {
return nil, err
}
}