2014-07-30 18:30:38 +02:00
|
|
|
// Copyright (c) 2014 Couchbase, Inc.
|
2016-10-02 16:13:14 +02:00
|
|
|
//
|
|
|
|
// 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.
|
2014-09-02 16:54:50 +02:00
|
|
|
|
2014-07-30 18:30:38 +02:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
2016-09-30 17:30:17 +02:00
|
|
|
"github.com/blevesearch/bleve/index/upsidedown"
|
2014-07-30 18:30:38 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// DebugDocumentHandler allows you to debug the index content
|
2014-10-31 19:44:15 +01:00
|
|
|
// for a given document id.
|
2014-07-30 18:30:38 +02:00
|
|
|
type DebugDocumentHandler struct {
|
|
|
|
defaultIndexName string
|
2014-10-31 19:44:15 +01:00
|
|
|
IndexNameLookup varLookupFunc
|
|
|
|
DocIDLookup varLookupFunc
|
2014-07-30 18:30:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewDebugDocumentHandler(defaultIndexName string) *DebugDocumentHandler {
|
|
|
|
return &DebugDocumentHandler{
|
|
|
|
defaultIndexName: defaultIndexName,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *DebugDocumentHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
|
|
|
|
|
|
|
// find the index to operate on
|
2014-10-31 19:44:15 +01:00
|
|
|
var indexName string
|
|
|
|
if h.IndexNameLookup != nil {
|
|
|
|
indexName = h.IndexNameLookup(req)
|
|
|
|
}
|
2014-07-30 18:30:38 +02:00
|
|
|
if indexName == "" {
|
|
|
|
indexName = h.defaultIndexName
|
|
|
|
}
|
|
|
|
index := IndexByName(indexName)
|
|
|
|
if index == nil {
|
|
|
|
showError(w, req, fmt.Sprintf("no such index '%s'", indexName), 404)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// find the docID
|
2014-10-31 19:44:15 +01:00
|
|
|
var docID string
|
|
|
|
if h.DocIDLookup != nil {
|
|
|
|
docID = h.DocIDLookup(req)
|
|
|
|
}
|
2014-08-15 19:12:55 +02:00
|
|
|
|
2016-09-13 18:40:01 +02:00
|
|
|
internalIndex, _, err := index.Advanced()
|
|
|
|
if err != nil {
|
|
|
|
showError(w, req, fmt.Sprintf("error getting index: %v", err), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
internalIndexReader, err := internalIndex.Reader()
|
|
|
|
if err != nil {
|
|
|
|
showError(w, req, fmt.Sprintf("error operning index reader: %v", err), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var rv []interface{}
|
|
|
|
rowChan := internalIndexReader.DumpDoc(docID)
|
2014-08-15 19:12:55 +02:00
|
|
|
for row := range rowChan {
|
|
|
|
switch row := row.(type) {
|
|
|
|
case error:
|
|
|
|
showError(w, req, fmt.Sprintf("error debugging document: %v", row), 500)
|
|
|
|
return
|
2016-09-30 17:30:17 +02:00
|
|
|
case upsidedown.UpsideDownCouchRow:
|
2014-07-30 18:30:38 +02:00
|
|
|
tmp := struct {
|
|
|
|
Key []byte `json:"key"`
|
|
|
|
Val []byte `json:"val"`
|
|
|
|
}{
|
2014-08-15 19:12:55 +02:00
|
|
|
Key: row.Key(),
|
|
|
|
Val: row.Value(),
|
2014-07-30 18:30:38 +02:00
|
|
|
}
|
|
|
|
rv = append(rv, tmp)
|
|
|
|
}
|
|
|
|
}
|
2016-09-13 18:40:01 +02:00
|
|
|
err = internalIndexReader.Close()
|
|
|
|
if err != nil {
|
|
|
|
showError(w, req, fmt.Sprintf("error closing index reader: %v", err), 500)
|
|
|
|
return
|
|
|
|
}
|
2014-07-30 18:30:38 +02:00
|
|
|
mustEncode(w, rv)
|
|
|
|
}
|