/
docvalue.go
280 lines (242 loc) · 8.3 KB
/
docvalue.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Copyright (c) 2017 Couchbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"bytes"
"encoding/binary"
"fmt"
"log"
"math"
"sort"
"strconv"
zap "github.com/blevesearch/zapx/v12"
"github.com/golang/snappy"
"github.com/spf13/cobra"
)
// docvalueCmd represents the docvalue command
var docvalueCmd = &cobra.Command{
Use: "docvalue [path] <field> optional <docNum> optional",
Short: "docvalue prints the docvalue details by field, and docNum",
Long: `The docvalue command lets you explore the docValues in order of field and by doc number.`,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("must specify index file path")
}
data := segment.Data()
crcOffset := len(data) - 4
verOffset := crcOffset - 4
chunkOffset := verOffset - 4
fieldsOffset := chunkOffset - 16
fieldsIndexOffset := binary.BigEndian.Uint64(data[fieldsOffset : fieldsOffset+8])
fieldsIndexEnd := uint64(len(data) - zap.FooterSize)
// iterate through fields index
var fieldInv []string
var id uint64
for fieldsIndexOffset+(8*id) < fieldsIndexEnd {
addr := binary.BigEndian.Uint64(
data[fieldsIndexOffset+(8*id) : fieldsIndexOffset+(8*id)+8])
var n uint64
_, read := binary.Uvarint(data[addr+n : fieldsIndexEnd])
n += uint64(read)
var nameLen uint64
nameLen, read = binary.Uvarint(data[addr+n : fieldsIndexEnd])
n += uint64(read)
name := string(data[addr+n : addr+n+nameLen])
id++
fieldInv = append(fieldInv, name)
}
dvLoc := segment.DocValueOffset()
var n int
var fieldName string
var fieldID uint16
var fieldDvSize float64
var read, fieldStartLoc, fieldEndLoc uint64
var fieldChunkCount, fieldDvStart, fieldDvEnd, totalDvSize uint64
var fieldChunkLens []uint64
// if no fields are specified then print the docValue offsets for all fields set
for id, field := range fieldInv {
fieldStartLoc, n = binary.Uvarint(
data[dvLoc+read : dvLoc+read+binary.MaxVarintLen64])
if n <= 0 {
return fmt.Errorf("loadDvIterators: failed to read the "+
" docvalue offsets for field %d", fieldID)
}
read += uint64(n)
fieldEndLoc, n = binary.Uvarint(
data[dvLoc+read : dvLoc+read+binary.MaxVarintLen64])
if n <= 0 {
return fmt.Errorf("Failed to read the docvalue offset "+
"end for field %d", fieldID)
}
read += uint64(n)
if fieldStartLoc == math.MaxUint64 && len(args) == 1 {
fmt.Printf("FieldID: %d '%s' docvalue at %d (%x) not "+
" persisted \n", id, field, fieldStartLoc, fieldStartLoc)
continue
}
var chunkOffsetsPosition, offset, numChunks uint64
if fieldEndLoc-fieldStartLoc > 16 {
numChunks = binary.BigEndian.Uint64(data[fieldEndLoc-8 : fieldEndLoc])
// read the length of chunk offsets
chunkOffsetsLen := binary.BigEndian.Uint64(data[fieldEndLoc-16 : fieldEndLoc-8])
// acquire position of chunk offsets
chunkOffsetsPosition = (fieldEndLoc - 16) - chunkOffsetsLen
}
// read the chunk offsets
chunkLens := make([]uint64, numChunks)
dvSize := uint64(0)
for i := 0; i < int(numChunks); i++ {
length, read := binary.Uvarint(
data[chunkOffsetsPosition+offset : chunkOffsetsPosition+offset+
binary.MaxVarintLen64])
if read <= 0 {
return fmt.Errorf("Corrupted chunk offset during segment load")
}
offset += uint64(read)
chunkLens[i] = length
dvSize += length
}
totalDvSize += dvSize
// if no field args are given, then print out the dv locations for all fields
if len(args) == 1 {
mbsize := float64(dvSize) / (1024 * 1024)
fmt.Printf("FieldID: %d '%s' docvalue at %d (%x) numChunks "+
"%d diskSize %.6f MB\n", id, field, fieldStartLoc,
fieldStartLoc, numChunks, mbsize)
continue
}
// if the field is the requested one for more details,
// then remember the details
if field == args[1] {
fieldDvStart = fieldStartLoc
fieldDvEnd = fieldEndLoc
fieldName = field
fieldID = uint16(id)
fieldDvSize = float64(dvSize) / (1024 * 1024)
fieldChunkLens = append(fieldChunkLens, chunkLens...)
fieldChunkCount = numChunks
}
}
mbsize := float64(totalDvSize) / (1024 * 1024)
fmt.Printf("Total Doc Values Size on Disk: %.6f MB\n", mbsize)
// done with the fields dv locs printing for the given zap file
if len(args) == 1 {
return nil
}
if fieldName == "" || fieldDvEnd == 0 {
return fmt.Errorf("No docvalue persisted for given field arg: %s",
args[1])
}
if len(args) == 2 {
fmt.Printf("FieldID: %d '%s' docvalue at %d (%x) numChunks "+
"%d diskSize %.6f MB\n", fieldID, fieldName, fieldDvStart,
fieldDvStart, fieldChunkCount, fieldDvSize)
fmt.Printf("Number of docvalue chunks: %d\n", fieldChunkCount)
return nil
}
localDocNum, err := strconv.Atoi(args[2])
if err != nil {
return fmt.Errorf("Unable to parse doc number: %v", err)
}
if localDocNum >= int(segment.NumDocs()) {
return fmt.Errorf("Invalid doc number %d (valid 0 - %d)",
localDocNum, segment.NumDocs()-1)
}
// find the chunkNumber where the docValues are stored
docInChunk := uint64(localDocNum) / uint64(1024)
if fieldChunkCount < docInChunk {
return fmt.Errorf("No chunk exists for chunk number: %d for "+
"localDocNum: %d", docInChunk, localDocNum)
}
start, end := readChunkBoundary(int(docInChunk), fieldChunkLens)
destChunkDataLoc := fieldDvStart + start
curChunkEnd := fieldDvStart + end
// read the number of docs reside in the chunk
var numDocs uint64
var nr int
numDocs, nr = binary.Uvarint(
data[destChunkDataLoc : destChunkDataLoc+binary.MaxVarintLen64])
if nr <= 0 {
return fmt.Errorf("Failed to read the chunk")
}
chunkMetaLoc := destChunkDataLoc + uint64(nr)
curChunkHeader := make([]zap.MetaData, int(numDocs))
offset := uint64(0)
for i := 0; i < int(numDocs); i++ {
curChunkHeader[i].DocNum, nr = binary.Uvarint(
data[chunkMetaLoc+offset : chunkMetaLoc+offset+binary.MaxVarintLen64])
offset += uint64(nr)
curChunkHeader[i].DocDvOffset, nr = binary.Uvarint(
data[chunkMetaLoc+offset : chunkMetaLoc+offset+binary.MaxVarintLen64])
offset += uint64(nr)
}
compressedDataLoc := chunkMetaLoc + offset
dataLength := curChunkEnd - compressedDataLoc
curChunkData := data[compressedDataLoc : compressedDataLoc+dataLength]
start, end = getDocValueLocs(uint64(localDocNum), curChunkHeader)
if start == math.MaxUint64 || end == math.MaxUint64 {
fmt.Printf("No field values found for localDocNum: %d\n",
localDocNum)
fmt.Printf("Try docNums present in chunk: %s\n",
metaDataDocNums(curChunkHeader))
return nil
}
// uncompress the already loaded data
uncompressed, err := snappy.Decode(nil, curChunkData)
if err != nil {
log.Printf("snappy err %+v ", err)
return err
}
var termSeparator byte = 0xff
var termSeparatorSplitSlice = []byte{termSeparator}
// pick the terms for the given docNum
uncompressed = uncompressed[start:end]
for {
i := bytes.Index(uncompressed, termSeparatorSplitSlice)
if i < 0 {
break
}
fmt.Printf(" %s ", uncompressed[0:i])
uncompressed = uncompressed[i+1:]
}
fmt.Printf(" \n ")
return nil
},
}
func getDocValueLocs(docNum uint64, metaHeader []zap.MetaData) (uint64, uint64) {
i := sort.Search(len(metaHeader), func(i int) bool {
return metaHeader[i].DocNum >= docNum
})
if i < len(metaHeader) && metaHeader[i].DocNum == docNum {
return zap.ReadDocValueBoundary(i, metaHeader)
}
return math.MaxUint64, math.MaxUint64
}
func metaDataDocNums(metaHeader []zap.MetaData) string {
docNums := ""
for _, meta := range metaHeader {
docNums += fmt.Sprintf("%d", meta.DocNum) + ", "
}
return docNums
}
func readChunkBoundary(chunk int, offsets []uint64) (uint64, uint64) {
var start uint64
if chunk > 0 {
start = offsets[chunk-1]
}
return start, offsets[chunk]
}
func init() {
RootCmd.AddCommand(docvalueCmd)
}