-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathheader.go
208 lines (174 loc) · 5.29 KB
/
header.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package gouch
import (
"fmt"
"sort"
)
//IndexStateTransition capture the state of different partitions
type IndexStateTransition struct {
active seqList
passive seqList
unindexable seqList
}
type nodePointerList []*nodePointer
//IndexHeader main header of index file
type IndexHeader struct {
version uint8
signature [16]byte
numViews uint8
numPartitions uint16
activeBitmask *Bitmap
passiveBitmask *Bitmap
cleanupBitmask *Bitmap
seqs partSeqList
idBTreeState *nodePointer
viewStates nodePointerList
hasReplica int
replicasOnTransfer seqList
pendingTransaction IndexStateTransition
unindexableSeqs partSeqList
partVersions partVersionList
}
func (g *Gouch) findLastHeader() (int64, error) {
pos := g.pos
var h *IndexHeader
err := fmt.Errorf("start")
var headerPos int64
for h == nil && err != nil {
headerPos, err = g.seekLastHeaderBlockFrom(pos)
if err != nil {
return headerPos, err
}
h, err = g.readHeaderAt(headerPos)
if err != nil {
pos = headerPos - 1
}
}
g.header = h
return headerPos, nil
}
func (g *Gouch) readHeaderAt(pos int64) (*IndexHeader, error) {
chunk, err := g.readChunkAt(pos, true)
if err != nil {
return nil, err
}
header := DecodeIndexHeader(chunk)
return header, nil
}
//DecodeIndexHeader decodes the main index header
func DecodeIndexHeader(bytes []byte) *IndexHeader {
if len(bytes) <= 16 {
return nil
}
var data []byte
arrayIndex := 0
data, err := SnappyDecode(nil, bytes[16:])
if err != nil {
return nil
}
h := IndexHeader{}
for k, v := range bytes[0:16] {
h.signature[k] = v
}
h.version = decodeRaw08(data[arrayIndex : arrayIndex+1])
arrayIndex++
h.numPartitions = decodeRaw16(data[arrayIndex : arrayIndex+2])
arrayIndex += 2
// BitmapSize == 128
h.activeBitmask = &Bitmap{data: data[arrayIndex : arrayIndex+BitmapSize]}
arrayIndex += BitmapSize
h.passiveBitmask = &Bitmap{data: data[arrayIndex : arrayIndex+BitmapSize]}
arrayIndex += BitmapSize
h.cleanupBitmask = &Bitmap{data: data[arrayIndex : arrayIndex+BitmapSize]}
arrayIndex += BitmapSize
numSeqs := decodeRaw16(data[arrayIndex : arrayIndex+2])
arrayIndex += 2
for i := 0; i < int(numSeqs); i++ {
ps := partSeq{}
ps.partID = decodeRaw16(data[arrayIndex : arrayIndex+2])
arrayIndex += 2
ps.seq = decodeRaw48(data[arrayIndex : arrayIndex+6])
arrayIndex += 6
h.seqs = append(h.seqs, ps)
}
sort.Sort(h.seqs)
sz := decodeRaw16(data[arrayIndex : arrayIndex+2])
arrayIndex += 2
h.idBTreeState = decodeRootNodePointer(data[arrayIndex : arrayIndex+int(sz)])
arrayIndex += int(sz)
h.numViews = decodeRaw08(data[arrayIndex : arrayIndex+1])
arrayIndex++
h.viewStates = make([]*nodePointer, int(h.numViews))
for i := 0; i < int(h.numViews); i++ {
sz = decodeRaw16(data[arrayIndex : arrayIndex+2])
arrayIndex += 2
h.viewStates[i] = decodeRootNodePointer(data[arrayIndex : arrayIndex+int(sz)])
arrayIndex += int(sz)
}
h.hasReplica = int(decodeRaw08(data[arrayIndex : arrayIndex+1]))
arrayIndex++
sz = decodeRaw16(data[arrayIndex : arrayIndex+2])
arrayIndex += 2
for i := 0; i < int(sz); i++ {
partID := decodeRaw16(data[arrayIndex : arrayIndex+2])
arrayIndex += 2
h.replicasOnTransfer = append(h.replicasOnTransfer, uint64(partID))
}
sort.Sort(h.replicasOnTransfer)
sz = decodeRaw16(data[arrayIndex : arrayIndex+2])
arrayIndex += 2
for i := 0; i < int(sz); i++ {
partID := decodeRaw16(data[arrayIndex : arrayIndex+2])
arrayIndex += 2
h.pendingTransaction.active = append(h.pendingTransaction.active, uint64(partID))
}
sort.Sort(h.pendingTransaction.active)
sz = decodeRaw16(data[arrayIndex : arrayIndex+2])
arrayIndex += 2
for i := 0; i < int(sz); i++ {
partID := decodeRaw16(data[arrayIndex : arrayIndex+2])
arrayIndex += 2
h.pendingTransaction.passive = append(h.pendingTransaction.passive, uint64(partID))
}
sort.Sort(h.pendingTransaction.passive)
sz = decodeRaw16(data[arrayIndex : arrayIndex+2])
arrayIndex += 2
for i := 0; i < int(sz); i++ {
partID := decodeRaw16(data[arrayIndex : arrayIndex+2])
arrayIndex += 2
h.pendingTransaction.unindexable = append(h.pendingTransaction.unindexable, uint64(partID))
}
sort.Sort(h.pendingTransaction.unindexable)
numSeqs = decodeRaw16(data[arrayIndex : arrayIndex+2])
arrayIndex += 2
for i := 0; i < int(numSeqs); i++ {
pSeq := partSeq{}
pSeq.partID = decodeRaw16(data[arrayIndex : arrayIndex+2])
arrayIndex += 2
pSeq.seq = decodeRaw64(data[arrayIndex : arrayIndex+6])
arrayIndex += 6
h.unindexableSeqs = append(h.unindexableSeqs, pSeq)
}
sort.Sort(h.unindexableSeqs)
if h.version >= 2 {
numPartVersions := decodeRaw16(data[arrayIndex : arrayIndex+2])
arrayIndex += 2
for i := 0; i < int(numPartVersions); i++ {
pver := partVersion{}
pver.partID = decodeRaw16(data[arrayIndex : arrayIndex+2])
arrayIndex += 2
pver.numFailoverLog = decodeRaw16(data[arrayIndex : arrayIndex+2])
arrayIndex += 2
for j := 0; j < int(pver.numFailoverLog); j++ {
fl := FailoverLog{}
fl.uuid = data[arrayIndex : arrayIndex+8]
arrayIndex += 8
fl.seq = decodeRaw64(data[arrayIndex : arrayIndex+8])
arrayIndex += 8
pver.failoverLog = append(pver.failoverLog, fl)
}
h.partVersions = append(h.partVersions, pver)
}
sort.Sort(h.partVersions)
}
return &h
}