/
indexer.go
449 lines (371 loc) · 10.8 KB
/
indexer.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
// Copyright (c) 2013 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.
// Server application serves index engine via rest API.
// TODO :
// - Create a goroutine that will pull mutations from UPR and update the
// index.
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"github.com/couchbaselabs/indexing/api"
"github.com/couchbaselabs/indexing/catalog"
"github.com/couchbaselabs/indexing/engine/leveldb"
"log"
"net/http"
"sync"
)
var c catalog.IndexCatalog
var ddlLock sync.Mutex
var chnotify chan ddlNotification
var engineMap map[string]api.Finder
var options struct {
debugLog bool
}
func main() {
var err error
argParse()
// Create index catalog
if c, err = catalog.NewIndexCatalog("./", "icatalog.dat"); err != nil {
log.Fatalf("Fatal error opening catalog: %v", err)
}
engineMap = make(map[string]api.Finder)
//open engine for existing indexes and assign to engineMap
openIndexEngine()
//FIXME add error handing to this
if chnotify, err = StartMutationManager(engineMap); err != nil {
log.Printf("Error Starting Mutation Manager %v", err)
return
}
addr := ":8095"
// Subscribe to HTTP server handlers
http.HandleFunc("/create", handleCreate)
http.HandleFunc("/drop", handleDrop)
http.HandleFunc("/scan", handleScan)
http.HandleFunc("/stats", handleStats)
//FIXME This doesn't work on Ctrl-C
defer freeResourcesOnExit()
log.Println("Indexer Listening on", addr)
if err := http.ListenAndServe(addr, nil); err != nil {
log.Fatalf("Fatal: %v", err)
}
}
// /create
func handleCreate(w http.ResponseWriter, r *http.Request) {
var res api.IndexMetaResponse
var err error
indexinfo := indexRequest(r).Index // Get IndexInfo
ddlLock.Lock()
defer ddlLock.Unlock()
if err = assignIndexEngine(&indexinfo); err == nil {
if _, err = c.Create(indexinfo); err == nil {
//notify mutation manager about the new index
notification := ddlNotification{
ddltype: api.CREATE,
engine: engineMap[indexinfo.Uuid],
indexinfo: indexinfo,
}
chnotify <- notification
res = api.IndexMetaResponse{
Status: api.SUCCESS,
}
log.Printf("Created index(%v) %v", indexinfo.Uuid, indexinfo.Name)
}
}
if err != nil {
res = createMetaResponseFromError(err)
log.Println("ERROR: Failed to create index", err)
}
sendResponse(w, res)
}
// /drop
func handleDrop(w http.ResponseWriter, r *http.Request) {
var res api.IndexMetaResponse
var err error
indexinfo := indexRequest(r).Index
ddlLock.Lock()
defer ddlLock.Unlock()
if indexinfo, err = c.Index(indexinfo.Uuid); err == nil {
//Notify mutation manager before destroying the engine
notification := ddlNotification{
ddltype: api.DROP,
engine: engineMap[indexinfo.Uuid],
indexinfo: indexinfo,
}
chnotify <- notification
if err = engineMap[indexinfo.Uuid].Destroy(); err == nil {
if _, err = c.Drop(indexinfo.Uuid); err == nil {
res = api.IndexMetaResponse{
Status: api.SUCCESS,
}
log.Printf("Dropped index(%v) %v", indexinfo.Uuid, indexinfo.Name)
}
}
if err != nil {
//FIXME need to renotify the mutation manager??
}
}
if err != nil {
res = createMetaResponseFromError(err)
log.Println("ERROR: Failed to drop index", err)
}
sendResponse(w, res)
}
// /scan
func handleScan(w http.ResponseWriter, r *http.Request) {
var err error
indexreq := indexRequest(r) // Gather request
uuid := indexreq.Index.Uuid
q := indexreq.Params
if options.debugLog {
log.Printf("Received Scan Index %v Params %v %v", uuid, q.Low, q.High)
}
// Scan
rows := make([]api.IndexRow, 0)
var totalRows uint64
var lowkey, highkey api.Key
if lowkey, err = api.NewKey(q.Low, ""); err != nil {
sendScanResponse(w, nil, 0, err)
return
}
if highkey, err = api.NewKey(q.High, ""); err != nil {
sendScanResponse(w, nil, 0, err)
return
}
var indexinfo api.IndexInfo
if indexinfo, err = c.Index(uuid); err == nil {
switch q.ScanType {
case api.COUNT:
totalRows, err = countQuery(&indexinfo, q.Limit)
case api.EXISTS:
var exists bool
exists, err = existsQuery(&indexinfo, lowkey)
if exists {
totalRows = 1
}
case api.LOOKUP:
rows, err = lookupQuery(&indexinfo, lowkey, q.Limit)
totalRows = uint64(len(rows))
case api.RANGESCAN:
rows, err = rangeQuery(&indexinfo, lowkey, highkey, q.Inclusion, q.Limit)
totalRows = uint64(len(rows))
case api.FULLSCAN:
rows, err = scanQuery(&indexinfo, q.Limit)
totalRows = uint64(len(rows))
case api.RANGECOUNT:
totalRows, err = rangeCountQuery(&indexinfo, lowkey, highkey, q.Inclusion, q.Limit)
}
}
// send back the response
sendScanResponse(w, rows, totalRows, err)
}
// /stats.
func handleStats(w http.ResponseWriter, r *http.Request) {
panic("Not yet implemented")
}
//---- helper functions
func countQuery(indexinfo *api.IndexInfo, limit int64) (
uint64, error) {
if counter, ok := engineMap[indexinfo.Uuid].(api.Counter); ok {
count, err := counter.CountTotal()
return count, err
}
err := errors.New("Index does not support Looker interface")
return uint64(0), err
}
func existsQuery(indexinfo *api.IndexInfo, key api.Key) (bool, error) {
if exister, ok := engineMap[indexinfo.Uuid].(api.Exister); ok {
exists := exister.Exists(key)
return exists, nil
}
err := errors.New("Index does not support Exister interface")
return false, err
}
func scanQuery(indexinfo *api.IndexInfo, limit int64) (
[]api.IndexRow, error) {
if looker, ok := engineMap[indexinfo.Uuid].(api.Looker); ok {
ch, cherr := looker.ValueSet()
return receiveValue(ch, cherr, limit)
}
err := errors.New("Index does not support Looker interface")
return nil, err
}
func rangeQuery(
indexinfo *api.IndexInfo, low, high api.Key, incl api.Inclusion,
limit int64) ([]api.IndexRow, error) {
if ranger, ok := engineMap[indexinfo.Uuid].(api.Ranger); ok {
ch, cherr, _ := ranger.ValueRange(low, high, incl)
return receiveValue(ch, cherr, limit)
}
err := errors.New("Index does not support ranger interface")
return nil, err
}
func lookupQuery(indexinfo *api.IndexInfo, key api.Key, limit int64) (
[]api.IndexRow, error) {
if looker, ok := engineMap[indexinfo.Uuid].(api.Looker); ok {
if options.debugLog {
log.Printf("Looking up key %s", key.String())
}
ch, cherr := looker.Lookup(key)
return receiveValue(ch, cherr, limit)
}
err := errors.New("Index does not support looker interface")
return nil, err
}
func rangeCountQuery(
indexinfo *api.IndexInfo, low, high api.Key, incl api.Inclusion,
limit int64) (uint64, error) {
if rangeCounter, ok := engineMap[indexinfo.Uuid].(api.RangeCounter); ok {
totalRows, err := rangeCounter.CountRange(low, high, incl)
return totalRows, err
}
err := errors.New("Index does not support RangeCounter interface")
return 0, err
}
func sendResponse(w http.ResponseWriter, res interface{}) {
var buf []byte
var err error
header := w.Header()
header["Content-Type"] = []string{"application/json"}
if buf, err = json.Marshal(&res); err != nil {
log.Println("Unable to marshal response", res)
}
w.Write(buf)
}
func sendScanResponse(w http.ResponseWriter, rows []api.IndexRow, totalRows uint64, err error) {
var res api.IndexScanResponse
if err == nil {
res = api.IndexScanResponse{
Status: api.SUCCESS,
TotalRows: totalRows,
Rows: rows,
Errors: nil,
}
} else {
indexerr := api.IndexError{Code: string(api.ERROR), Msg: err.Error()}
res = api.IndexScanResponse{
Status: api.ERROR,
TotalRows: uint64(0),
Rows: nil,
Errors: []api.IndexError{indexerr},
}
}
sendResponse(w, res)
}
func receiveValue(ch chan api.Value, cherr chan error, limit int64) (
[]api.IndexRow, error) {
//FIXME limit should be sent to the engine and only limit response be sent on the
//channel
rows := make([]api.IndexRow, 0)
var nolimit = false
if limit == 0 {
nolimit = true
}
ok := true
var value api.Value
var err error
for ok && (limit > 0 || nolimit) {
select {
case value, ok = <-ch:
if ok {
if options.debugLog {
log.Printf("Indexer Received Value %s", value.String())
}
row := api.IndexRow{
Key: value.KeyBytes(),
Value: value.Docid(),
}
rows = append(rows, row)
limit--
}
case err, ok = <-cherr:
if err != nil {
return rows, err
}
}
}
return rows, nil
}
// Parse HTTP Request to get IndexInfo.
func indexRequest(r *http.Request) *api.IndexRequest {
indexreq := api.IndexRequest{}
buf := make([]byte, r.ContentLength, r.ContentLength)
r.Body.Read(buf)
json.Unmarshal(buf, &indexreq)
return &indexreq
}
func createMetaResponseFromError(err error) api.IndexMetaResponse {
indexerr := api.IndexError{Code: string(api.ERROR), Msg: err.Error()}
res := api.IndexMetaResponse{
Status: api.ERROR,
Errors: []api.IndexError{indexerr},
}
return res
}
// Instantiate index engine
func assignIndexEngine(indexinfo *api.IndexInfo) error {
var err error
switch indexinfo.Using {
case api.LevelDB:
engineMap[indexinfo.Uuid] = leveldb.NewIndexEngine(indexinfo.Uuid)
default:
err = errors.New(fmt.Sprintf("Invalid index-type, `%v`", indexinfo.Using))
}
return err
}
func openIndexEngine() error {
var err error
var indexinfos []api.IndexInfo
//For the existing indexes, open the existing engine
if _, indexinfos, err = c.List(""); err != nil {
log.Printf("Error while retrieving index list %v", err)
return err
}
for _, indexinfo := range indexinfos {
log.Printf("Try Finding Existing Engine for Index %v", indexinfo)
switch indexinfo.Using {
case api.LevelDB:
engineMap[indexinfo.Uuid] = leveldb.OpenIndexEngine(indexinfo.Uuid)
log.Printf("Got Existing Engine for Index %v", indexinfo.Uuid)
default:
err = errors.New(fmt.Sprintf("Unknown Index Type. Skipping Opening Engine"))
}
}
return err
}
func freeResourcesOnExit() {
//purge the catalog
if err := c.Purge(); err != nil {
log.Printf("Error Purging Catalog %v", err)
}
//close the index engines
if err := closeIndexEngines(); err != nil {
log.Printf("Error Closing Index Engine %v", err)
}
//FIXME close the mutation manager?
}
func closeIndexEngines() error {
if _, indexinfos, err := c.List(""); err == nil {
for _, indexinfo := range indexinfos {
if err := engineMap[indexinfo.Uuid].Close(); err != nil {
return err
}
}
} else {
return err
}
return nil
}
func argParse() {
flag.BoolVar(&options.debugLog, "debugLog", false, "Debug Logging Enabled")
flag.Parse()
api.DebugLog = options.debugLog
}