-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgouch.go
272 lines (227 loc) · 7.44 KB
/
gouch.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
package gouch
import (
"encoding/json"
"fmt"
"io"
"os"
)
//Gouch handler for reading a index file
type Gouch struct {
file *os.File
pos int64
header *IndexHeader
ops Ops
isFDAllocated bool
}
//DocumentInfo Handler for capturing metadata
type DocumentInfo struct {
ID []byte `json:"id"` // document identifier
Key []byte `json:"key"` // emitted key
Value []byte `json:"value"` // emitted value
}
//GetFDStatus provides status of header FD
func (g *Gouch) GetFDStatus() bool {
return g.isFDAllocated
}
//SetStatus assists in caching index header location
func (g *Gouch) SetStatus(state bool) {
g.isFDAllocated = state
}
//DeepCopy copies one gouch struct into another
func (g *Gouch) DeepCopy() *Gouch {
rv := &Gouch{g.file, g.pos, g.header, g.ops, g.isFDAllocated}
return rv
}
//Open up index file with defined perms
func Open(filename string, options int) (*Gouch, error) {
return OpenEx(filename, options, NewBaseOps())
}
//OpenEx opens index file and looks for valid header
func OpenEx(filename string, options int, ops Ops) (*Gouch, error) {
gouch := Gouch{
ops: ops,
}
file, err := gouch.ops.OpenFile(filename, os.O_RDONLY, 0666)
if err != nil {
return nil, err
}
gouch.file = file
gouch.pos, err = gouch.ops.GotoEOF(gouch.file)
if err != nil {
fmt.Errorf("Failed while reading file from the end. file: %v\n", filename)
return nil, err
}
if gouch.pos == 0 {
fmt.Errorf("Empty file: %v\n", filename)
return nil, err
}
_, err = gouch.findLastHeader()
if err != nil {
return nil, err
}
return &gouch, nil
}
func (g *Gouch) GetHeaderCount() int64 {
var headerCount int64
var headerPos int64
err := fmt.Errorf("headercount")
headerPos, err = g.findLastHeader()
for err == nil && headerPos > 4096 {
headerCount = headerCount + 1
g.pos = headerPos - 1
fmt.Printf("header pos: %d\n", g.pos)
headerPos, err = g.findLastHeader()
}
return headerCount
}
//Close clears up the open file handle
func (g *Gouch) Close() error {
return g.ops.Close(g.file)
}
func (di *DocumentInfo) String() string {
return fmt.Sprintf("ID: '%s' Key: '%s' Value: '%s' ", di.ID, di.Key, di.Value)
}
func lookupCallback(req *lookupRequest, key []byte, value []byte) error {
if value == nil {
return nil
}
context := req.callbackContext.(*lookupContext)
docinfo := DocumentInfo{}
if context.indexType == IndexTypeByID || context.indexType == IndexTypeMapR {
sz := decodeRaw16(key[:2])
docinfo.ID = key[int(sz)+2:]
docinfo.Key = key[2 : int(sz)+2]
docinfo.Value = value[5:]
}
if context.walkTreeCallback != nil {
if context.indexType == IndexTypeLocalDocs {
// note we pass the non-initialized docinfo so we can at least detect that its a leaf
return context.walkTreeCallback(context.gouch, context.depth, &docinfo, key, 0, value, context.callbackContext)
}
return context.walkTreeCallback(context.gouch, context.depth, &docinfo, nil, 0, nil, context.callbackContext)
} /*else if context.documentInfoCallback != nil {
return context.documentInfoCallback(context.gouch, &docinfo, context.callbackContext)
}*/
return nil
}
func walkNodeCallback(req *lookupRequest, key []byte, value []byte) error {
context := req.callbackContext.(*lookupContext)
count := context.callbackContext.(map[string]int)["count"]
if count > req.limit {
return nil
}
if value == nil {
context.depth--
return nil
}
//valueNodePointer := decodeNodePointer(value)
valueNodePointer := &nodePointer{}
valueNodePointer.subtreeSize = decodeRaw48(value)
valueNodePointer.key = key
size := decodeRaw16(value)
valueNodePointer.reducedValue = value[14 : 14+size]
valueNodePointer.reducedValue = value[14:]
err := context.walkTreeCallback(context.gouch, context.depth, nil, key, valueNodePointer.subtreeSize, valueNodePointer.reducedValue, context.callbackContext)
context.depth++
return err
}
//DocumentInfoCallback callback for capturing metadata
type DocumentInfoCallback func(gouch *Gouch, documentInfo *DocumentInfo, userContext interface{}, w io.Writer) error
//AllDocuments dumps all documents based on startID and endID
func (g *Gouch) AllDocuments(startID, endID string, cb DocumentInfoCallback, userContext interface{}, w io.Writer, limit int) error {
wtCallback := func(gouch *Gouch, depth int, documentInfo *DocumentInfo, key []byte, subTreeSize uint64, reducedValue []byte, userContext interface{}) error {
if documentInfo != nil {
return cb(gouch, documentInfo, userContext, w)
}
return nil
}
return g.WalkIDTree(startID, endID, wtCallback, userContext, limit)
}
//WalkTreeCallback callback for traversing btree
type WalkTreeCallback func(gouch *Gouch, depth int, documentInfo *DocumentInfo, key []byte, subTreeSize uint64, reducedValue []byte, userContext interface{}) error
//WalkIDTree traverses a btree based on startID and endID
func (g *Gouch) WalkIDTree(startID, endID string, wtcb WalkTreeCallback, userContext interface{}, limit int) error {
if g.header.idBTreeState == nil {
return nil
}
wtcb(g, 0, nil, nil, g.header.idBTreeState.subtreeSize, g.header.idBTreeState.reducedValue, userContext)
lc := lookupContext{
gouch: g,
walkTreeCallback: wtcb,
callbackContext: userContext,
indexType: IndexTypeByID,
}
keys := [][]byte{[]byte(startID)}
if endID != "" {
keys = append(keys, []byte(endID))
}
lr := lookupRequest{
compare: IDComparator,
keys: keys,
fetchCallback: lookupCallback,
nodeCallback: walkNodeCallback,
fold: true,
callbackContext: &lc,
}
err := g.btreeLookup(&lr, g.header.idBTreeState.pointer)
if err != nil {
return err
}
return nil
}
//AllDocsMapReduce MapReduce tree dump
func (g *Gouch) AllDocsMapReduce(startID, endID string, mapR DocumentInfoCallback, userContext interface{}, w io.Writer, limit int) error {
mapRCallback := func(gouch *Gouch, depth int, documentInfo *DocumentInfo, key []byte, subTreeSize uint64, reducedValue []byte, userContext interface{}) error {
if documentInfo != nil {
return mapR(gouch, documentInfo, userContext, w)
}
return nil
}
return g.WalkMapReduceTree(startID, endID, mapRCallback, userContext, limit)
}
//WalkMapReduceTree MapReduce tree traversal
func (g *Gouch) WalkMapReduceTree(startID, endID string, mapR WalkTreeCallback, userContext interface{}, limit int) error {
if len(g.header.viewStates) == 0 {
return nil
}
for i := 0; i < len(g.header.viewStates); i++ {
mapR(g, 0, nil, nil, g.header.viewStates[i].subtreeSize, g.header.viewStates[i].reducedValue, userContext)
lc := lookupContext{
gouch: g,
walkTreeCallback: mapR,
callbackContext: userContext,
indexType: IndexTypeMapR,
}
keys := [][]byte{[]byte(startID)}
if endID != "" {
keys = append(keys, []byte(endID))
}
lr := lookupRequest{
compare: IDComparator,
keys: keys,
fetchCallback: lookupCallback,
nodeCallback: walkNodeCallback,
fold: true,
callbackContext: &lc,
limit: limit,
}
err := g.btreeLookup(&lr, g.header.viewStates[i].pointer)
if err != nil {
return err
}
}
return nil
}
//DefaultDocumentCallback sample document callback function
//TODO implement limit support
func DefaultDocumentCallback(g *Gouch, docInfo *DocumentInfo, userContext interface{}, w io.Writer) error {
//bytes, err := json.Marshal(docInfo)
_, err := json.Marshal(docInfo)
if err != nil {
fmt.Println(err)
} else {
userContext.(map[string]int)["count"]++
//fmt.Println(string(bytes))
}
return nil
}