-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathquick_list.go
94 lines (75 loc) · 1.78 KB
/
quick_list.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
88
89
90
91
92
93
94
package rdb
import (
"fmt"
"io"
)
type quickListIterator struct {
DataKey DataKey
Reader byteReader
ValueReader valueReader
Mapper collectionMapper
index int
length int
initialized bool
done bool
values []interface{}
iterator iterator
}
func (q *quickListIterator) Next() (interface{}, error) {
if q.done {
return nil, io.EOF
}
if !q.initialized {
length, err := readLength(q.Reader)
if err != nil {
return nil, fmt.Errorf("failed to read quicklist buffer: %w", err)
}
q.initialized = true
q.length = length
head, err := q.Mapper.MapHead(&collectionHead{
DataKey: q.DataKey,
Length: length,
})
if err != nil {
return nil, fmt.Errorf("failed to map head in quicklist: %w", err)
}
return head, nil
}
if q.index == q.length {
q.done = true
slice, err := q.Mapper.MapSlice(&collectionSlice{
DataKey: q.DataKey,
Value: q.values,
})
if err != nil {
return nil, fmt.Errorf("failed to map slice in quicklist: %w", err)
}
return slice, nil
}
if q.iterator == nil {
q.iterator = &zipListIterator{
DataKey: q.DataKey,
Reader: q.Reader,
ValueReader: q.ValueReader,
Mapper: q,
ValueLength: 1,
}
}
return q.iterator.Next()
}
func (q *quickListIterator) MapHead(head *collectionHead) (interface{}, error) {
return nil, errContinueLoop
}
func (q *quickListIterator) MapEntry(entry *collectionEntry) (interface{}, error) {
mappedEntry, err := q.Mapper.MapEntry(entry)
if err != nil {
return nil, fmt.Errorf("failed to map entry in quicklist: %w", err)
}
q.values = append(q.values, entry.Value)
return mappedEntry, nil
}
func (q *quickListIterator) MapSlice(slice *collectionSlice) (interface{}, error) {
q.index++
q.iterator = nil
return nil, errContinueLoop
}