-
Notifications
You must be signed in to change notification settings - Fork 5
/
int_set.go
102 lines (83 loc) · 1.84 KB
/
int_set.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
95
96
97
98
99
100
101
102
package rdb
import (
"fmt"
"io"
)
type intSetIterator struct {
DataKey DataKey
Reader byteReader
Mapper collectionMapper
buf byteReader
done bool
encoding uint32
index int
length int
values []interface{}
}
func (i *intSetIterator) Next() (interface{}, error) {
if i.done {
return nil, io.EOF
}
if i.buf == nil {
buf, err := readStringEncoding(i.Reader)
if err != nil {
return nil, fmt.Errorf("failed to read intset buffer: %w", err)
}
i.buf = newSliceReader(buf)
if i.encoding, err = readUint32(i.buf); err != nil {
return nil, fmt.Errorf("failed to read intset encoding: %w", err)
}
length, err := readUint32(i.buf)
if err != nil {
return nil, fmt.Errorf("failed to read intset length: %w", err)
}
i.length = int(length)
head, err := i.Mapper.MapHead(&collectionHead{
DataKey: i.DataKey,
Length: i.length,
})
if err != nil {
return nil, fmt.Errorf("failed to map head in intset: %w", err)
}
return head, nil
}
if i.index == i.length {
i.done = true
i.buf = nil
slice, err := i.Mapper.MapSlice(&collectionSlice{
DataKey: i.DataKey,
Value: i.values,
})
if err != nil {
return nil, fmt.Errorf("failed to map slice in intset: %w", err)
}
return slice, nil
}
value, err := i.readValue()
if err != nil {
return nil, err
}
entry, err := i.Mapper.MapEntry(&collectionEntry{
DataKey: i.DataKey,
Index: i.index,
Length: i.length,
Value: value,
})
if err != nil {
return nil, fmt.Errorf("failed to map entry in intset: %w", err)
}
i.index++
i.values = append(i.values, value)
return entry, nil
}
func (i *intSetIterator) readValue() (interface{}, error) {
switch i.encoding {
case 8:
return readInt64(i.buf)
case 4:
return readInt32(i.buf)
case 2:
return readInt16(i.buf)
}
return nil, IntSetEncodingError{Encoding: i.encoding}
}