2015-08-25 20:52:42 +02:00
|
|
|
// Copyright (c) 2015 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 firestorm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/blevesearch/bleve/index"
|
|
|
|
"github.com/blevesearch/bleve/index/store"
|
|
|
|
)
|
|
|
|
|
|
|
|
// the functions in this file are only intended to be used by
|
|
|
|
// the bleve_dump utility and the debug http handlers
|
|
|
|
// if your application relies on them, you're doing something wrong
|
|
|
|
// they may change or be removed at any time
|
|
|
|
|
2015-11-24 20:32:33 +01:00
|
|
|
func (f *Firestorm) dumpPrefix(kvreader store.KVReader, rv chan interface{}, prefix []byte) error {
|
|
|
|
return visitPrefix(kvreader, prefix, func(key, val []byte) (bool, error) {
|
2015-08-25 20:52:42 +02:00
|
|
|
row, err := parseFromKeyValue(key, val)
|
|
|
|
if err != nil {
|
|
|
|
rv <- err
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
rv <- row
|
|
|
|
return true, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-11-24 20:32:33 +01:00
|
|
|
func (f *Firestorm) dumpDoc(kvreader store.KVReader, rv chan interface{}, docID []byte) error {
|
2015-08-25 20:52:42 +02:00
|
|
|
// without a back index we have no choice but to walk the term freq and stored rows
|
|
|
|
|
|
|
|
// walk the term freqs
|
2015-11-24 20:32:33 +01:00
|
|
|
err := visitPrefix(kvreader, TermFreqKeyPrefix, func(key, val []byte) (bool, error) {
|
2015-08-25 20:52:42 +02:00
|
|
|
tfr, err := NewTermFreqRowKV(key, val)
|
|
|
|
if err != nil {
|
|
|
|
rv <- err
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if bytes.Compare(tfr.DocID(), docID) == 0 {
|
|
|
|
rv <- tfr
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
})
|
|
|
|
|
2015-11-24 20:32:33 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-25 20:52:42 +02:00
|
|
|
// now walk the stored
|
2015-11-24 20:32:33 +01:00
|
|
|
err = visitPrefix(kvreader, StoredKeyPrefix, func(key, val []byte) (bool, error) {
|
2015-08-25 20:52:42 +02:00
|
|
|
sr, err := NewStoredRowKV(key, val)
|
|
|
|
if err != nil {
|
|
|
|
rv <- err
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if bytes.Compare(sr.DocID(), docID) == 0 {
|
|
|
|
rv <- sr
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
})
|
2015-11-24 20:32:33 +01:00
|
|
|
|
|
|
|
return err
|
2015-08-25 20:52:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseFromKeyValue(key, value []byte) (index.IndexRow, error) {
|
|
|
|
if len(key) > 0 {
|
|
|
|
switch key[0] {
|
|
|
|
case VersionKey[0]:
|
|
|
|
return NewVersionRowV(value)
|
|
|
|
case FieldKeyPrefix[0]:
|
|
|
|
return NewFieldRowKV(key, value)
|
|
|
|
case DictionaryKeyPrefix[0]:
|
|
|
|
return NewDictionaryRowKV(key, value)
|
|
|
|
case TermFreqKeyPrefix[0]:
|
|
|
|
return NewTermFreqRowKV(key, value)
|
|
|
|
case StoredKeyPrefix[0]:
|
|
|
|
return NewStoredRowKV(key, value)
|
|
|
|
case InternalKeyPrefix[0]:
|
|
|
|
return NewInternalRowKV(key, value)
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("Unknown field type '%s'", string(key[0]))
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("Invalid empty key")
|
|
|
|
}
|