refactored to remove panics, return errors, and fewer type assertions
This commit is contained in:
parent
a3e04d8697
commit
f92f274665
|
@ -34,10 +34,12 @@ func newUpsideDownCouchTermFieldReader(index *UpsideDownCouch, term []byte, fiel
|
|||
var count uint64 = 0
|
||||
if it.Valid() {
|
||||
if bytes.Equal(it.Key(), tfr.Key()) {
|
||||
tfr = ParseFromKeyValue(it.Key(), it.Value()).(*TermFrequencyRow)
|
||||
tfr, err := NewTermFrequencyRowKV(it.Key(), it.Value())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
count = tfr.freq
|
||||
}
|
||||
|
||||
} else {
|
||||
return nil, it.GetError()
|
||||
}
|
||||
|
@ -58,12 +60,15 @@ func (r *UpsideDownCouchTermFieldReader) Count() uint64 {
|
|||
func (r *UpsideDownCouchTermFieldReader) Next() (*index.TermFieldDoc, error) {
|
||||
r.iterator.Next()
|
||||
if r.iterator.Valid() {
|
||||
tfr := NewTermFrequencyRow(r.term, r.field, "", 0, 0)
|
||||
if !bytes.HasPrefix(r.iterator.Key(), tfr.Key()) {
|
||||
testfr := NewTermFrequencyRow(r.term, r.field, "", 0, 0)
|
||||
if !bytes.HasPrefix(r.iterator.Key(), testfr.Key()) {
|
||||
// end of the line
|
||||
return nil, nil
|
||||
}
|
||||
tfr = ParseFromKeyValue(r.iterator.Key(), r.iterator.Value()).(*TermFrequencyRow)
|
||||
tfr, err := NewTermFrequencyRowKV(r.iterator.Key(), r.iterator.Value())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &index.TermFieldDoc{
|
||||
ID: string(tfr.doc),
|
||||
Freq: tfr.freq,
|
||||
|
@ -79,12 +84,15 @@ func (r *UpsideDownCouchTermFieldReader) Advance(docId string) (*index.TermField
|
|||
tfr := NewTermFrequencyRow(r.term, r.field, docId, 0, 0)
|
||||
r.iterator.Seek(tfr.Key())
|
||||
if r.iterator.Valid() {
|
||||
tfr := NewTermFrequencyRow(r.term, r.field, "", 0, 0)
|
||||
if !bytes.HasPrefix(r.iterator.Key(), tfr.Key()) {
|
||||
testfr := NewTermFrequencyRow(r.term, r.field, "", 0, 0)
|
||||
if !bytes.HasPrefix(r.iterator.Key(), testfr.Key()) {
|
||||
// end of the line
|
||||
return nil, nil
|
||||
}
|
||||
tfr = ParseFromKeyValue(r.iterator.Key(), r.iterator.Value()).(*TermFrequencyRow)
|
||||
tfr, err := NewTermFrequencyRowKV(r.iterator.Key(), r.iterator.Value())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &index.TermFieldDoc{
|
||||
ID: string(tfr.doc),
|
||||
Freq: tfr.freq,
|
||||
|
|
|
@ -25,7 +25,7 @@ type UpsideDownCouchRow interface {
|
|||
Value() []byte
|
||||
}
|
||||
|
||||
func ParseFromKeyValue(key, value []byte) UpsideDownCouchRow {
|
||||
func ParseFromKeyValue(key, value []byte) (UpsideDownCouchRow, error) {
|
||||
switch key[0] {
|
||||
case 'v':
|
||||
return NewVersionRowKV(key, value)
|
||||
|
@ -36,7 +36,7 @@ func ParseFromKeyValue(key, value []byte) UpsideDownCouchRow {
|
|||
case 'b':
|
||||
return NewBackIndexRowKV(key, value)
|
||||
}
|
||||
return nil
|
||||
return nil, fmt.Errorf("Unknown field type '%s'", key[0])
|
||||
}
|
||||
|
||||
// VERSION
|
||||
|
@ -65,14 +65,14 @@ func NewVersionRow(version uint8) *VersionRow {
|
|||
}
|
||||
}
|
||||
|
||||
func NewVersionRowKV(key, value []byte) *VersionRow {
|
||||
func NewVersionRowKV(key, value []byte) (*VersionRow, error) {
|
||||
rv := VersionRow{}
|
||||
buf := bytes.NewBuffer(value)
|
||||
err := binary.Read(buf, binary.LittleEndian, &rv.version)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("binary.Read failed: %v", err))
|
||||
return nil, err
|
||||
}
|
||||
return &rv
|
||||
return &rv, nil
|
||||
}
|
||||
|
||||
// FIELD definition
|
||||
|
@ -109,24 +109,24 @@ func NewFieldRow(index uint16, name string) *FieldRow {
|
|||
}
|
||||
}
|
||||
|
||||
func NewFieldRowKV(key, value []byte) *FieldRow {
|
||||
func NewFieldRowKV(key, value []byte) (*FieldRow, error) {
|
||||
rv := FieldRow{}
|
||||
|
||||
buf := bytes.NewBuffer(key)
|
||||
buf.ReadByte() // type
|
||||
err := binary.Read(buf, binary.LittleEndian, &rv.index)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("binary.Read failed: %v", err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
buf = bytes.NewBuffer(value)
|
||||
rv.name, err = buf.ReadString(BYTE_SEPARATOR)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("Buffer.ReadString failed: %v", err))
|
||||
return nil, err
|
||||
}
|
||||
rv.name = rv.name[:len(rv.name)-1] // trim off separator byte
|
||||
|
||||
return &rv
|
||||
return &rv, nil
|
||||
}
|
||||
|
||||
// TERM FIELD FREQUENCY
|
||||
|
@ -217,7 +217,7 @@ func NewTermFrequencyRowWithTermVectors(term []byte, field uint16, doc string, f
|
|||
}
|
||||
}
|
||||
|
||||
func NewTermFrequencyRowKV(key, value []byte) *TermFrequencyRow {
|
||||
func NewTermFrequencyRowKV(key, value []byte) (*TermFrequencyRow, error) {
|
||||
rv := TermFrequencyRow{
|
||||
doc: []byte(""),
|
||||
}
|
||||
|
@ -227,18 +227,18 @@ func NewTermFrequencyRowKV(key, value []byte) *TermFrequencyRow {
|
|||
var err error
|
||||
rv.term, err = buf.ReadBytes(BYTE_SEPARATOR)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("Buffer.ReadString failed: %v", err))
|
||||
return nil, err
|
||||
}
|
||||
rv.term = rv.term[:len(rv.term)-1] // trim off separator byte
|
||||
|
||||
err = binary.Read(buf, binary.LittleEndian, &rv.field)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("binary.Read failed: %v", err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
doc, err := buf.ReadBytes(BYTE_SEPARATOR)
|
||||
if err != io.EOF {
|
||||
panic(fmt.Sprintf("expected binary.ReadString to end in EOF: %v", err))
|
||||
return nil, err
|
||||
}
|
||||
if doc != nil {
|
||||
rv.doc = doc
|
||||
|
@ -247,17 +247,17 @@ func NewTermFrequencyRowKV(key, value []byte) *TermFrequencyRow {
|
|||
buf = bytes.NewBuffer((value))
|
||||
err = binary.Read(buf, binary.LittleEndian, &rv.freq)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("binary.Read failed: %v", err))
|
||||
return nil, err
|
||||
}
|
||||
err = binary.Read(buf, binary.LittleEndian, &rv.norm)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("binary.Read failed: %v", err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var field uint16
|
||||
err = binary.Read(buf, binary.LittleEndian, &field)
|
||||
if err != nil && err != io.EOF {
|
||||
panic(fmt.Sprintf("binary.Read failed: %v", err))
|
||||
return nil, err
|
||||
}
|
||||
for err != io.EOF {
|
||||
tv := TermVector{}
|
||||
|
@ -269,22 +269,22 @@ func NewTermFrequencyRowKV(key, value []byte) *TermFrequencyRow {
|
|||
|
||||
err = binary.Read(buf, binary.LittleEndian, &tv.pos)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("binary.Read failed: %v", err))
|
||||
return nil, err
|
||||
}
|
||||
err = binary.Read(buf, binary.LittleEndian, &tv.start)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("binary.Read failed: %v", err))
|
||||
return nil, err
|
||||
}
|
||||
err = binary.Read(buf, binary.LittleEndian, &tv.end)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("binary.Read failed: %v", err))
|
||||
return nil, err
|
||||
}
|
||||
rv.vectors = append(rv.vectors, &tv)
|
||||
// try to read next record (may not exist)
|
||||
err = binary.Read(buf, binary.LittleEndian, &field)
|
||||
}
|
||||
|
||||
return &rv
|
||||
return &rv, nil
|
||||
|
||||
}
|
||||
|
||||
|
@ -332,7 +332,7 @@ func NewBackIndexRow(doc string, entries []*BackIndexEntry) *BackIndexRow {
|
|||
}
|
||||
}
|
||||
|
||||
func NewBackIndexRowKV(key, value []byte) *BackIndexRow {
|
||||
func NewBackIndexRowKV(key, value []byte) (*BackIndexRow, error) {
|
||||
rv := BackIndexRow{}
|
||||
|
||||
buf := bytes.NewBuffer(key)
|
||||
|
@ -341,7 +341,7 @@ func NewBackIndexRowKV(key, value []byte) *BackIndexRow {
|
|||
var err error
|
||||
rv.doc, err = buf.ReadBytes(BYTE_SEPARATOR)
|
||||
if err != io.EOF {
|
||||
panic(fmt.Sprintf("expected binary.ReadString to end in EOF: %v", err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
buf = bytes.NewBuffer(value)
|
||||
|
@ -350,7 +350,7 @@ func NewBackIndexRowKV(key, value []byte) *BackIndexRow {
|
|||
var term []byte
|
||||
term, err = buf.ReadBytes(BYTE_SEPARATOR)
|
||||
if err != nil && err != io.EOF {
|
||||
panic(fmt.Sprintf("Buffer.ReadString failed: %v", err))
|
||||
return nil, err
|
||||
}
|
||||
for err != io.EOF {
|
||||
ent := BackIndexEntry{}
|
||||
|
@ -358,15 +358,15 @@ func NewBackIndexRowKV(key, value []byte) *BackIndexRow {
|
|||
|
||||
err = binary.Read(buf, binary.LittleEndian, &ent.field)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("binary.Read failed: %v", err))
|
||||
return nil, err
|
||||
}
|
||||
rv.entries = append(rv.entries, &ent)
|
||||
|
||||
term, err = buf.ReadBytes(BYTE_SEPARATOR)
|
||||
if err != nil && err != io.EOF {
|
||||
panic(fmt.Sprintf("Buffer.ReadString failed: %v", err))
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return &rv
|
||||
return &rv, nil
|
||||
}
|
||||
|
|
|
@ -80,7 +80,10 @@ func TestRows(t *testing.T) {
|
|||
|
||||
// now test going back from k/v bytes to struct
|
||||
for _, test := range tests {
|
||||
row := ParseFromKeyValue(test.outKey, test.outVal)
|
||||
row, err := ParseFromKeyValue(test.outKey, test.outVal)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if !reflect.DeepEqual(row, test.input) {
|
||||
t.Fatalf("Expected: %#v got: %#v", test.input, row)
|
||||
}
|
||||
|
|
|
@ -73,7 +73,10 @@ func (udc *UpsideDownCouch) loadSchema() (err error) {
|
|||
if !bytes.HasPrefix(it.Key(), keyPrefix) {
|
||||
break
|
||||
}
|
||||
fieldRow := NewFieldRowKV(it.Key(), it.Value())
|
||||
fieldRow, err := NewFieldRowKV(it.Key(), it.Value())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
udc.fieldIndexes[fieldRow.name] = fieldRow.index
|
||||
if int(fieldRow.index) > udc.lastFieldIndex {
|
||||
udc.lastFieldIndex = int(fieldRow.index)
|
||||
|
@ -104,7 +107,10 @@ func (udc *UpsideDownCouch) batchRows(addRows []UpsideDownCouchRow, updateRows [
|
|||
return err
|
||||
}
|
||||
if val != nil {
|
||||
tr = ParseFromKeyValue(tr.Key(), val).(*TermFrequencyRow)
|
||||
tr, err = NewTermFrequencyRowKV(tr.Key(), val)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tr.freq += 1 // incr
|
||||
} else {
|
||||
tr = NewTermFrequencyRow(tfr.term, tfr.field, "", 1, 0)
|
||||
|
@ -132,7 +138,10 @@ func (udc *UpsideDownCouch) batchRows(addRows []UpsideDownCouchRow, updateRows [
|
|||
return err
|
||||
}
|
||||
if val != nil {
|
||||
tr = ParseFromKeyValue(tr.Key(), val).(*TermFrequencyRow)
|
||||
tr, err = NewTermFrequencyRowKV(tr.Key(), val)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tr.freq -= 1 // incr
|
||||
} else {
|
||||
log.Panic(fmt.Sprintf("unexpected missing row, deleting term, expected count row to exit: %v", tr.Key()))
|
||||
|
@ -375,7 +384,10 @@ func (udc *UpsideDownCouch) backIndexRowForDoc(docId string) (*BackIndexRow, err
|
|||
if value == nil {
|
||||
return nil, nil
|
||||
}
|
||||
backIndexRow := ParseFromKeyValue(key, value).(*BackIndexRow)
|
||||
backIndexRow, err := NewBackIndexRowKV(key, value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return backIndexRow, nil
|
||||
}
|
||||
|
||||
|
@ -387,7 +399,11 @@ func (udc *UpsideDownCouch) Dump() {
|
|||
it.SeekToFirst()
|
||||
for it = it; it.Valid(); it.Next() {
|
||||
//fmt.Printf("Key: `%v` Value: `%v`\n", string(it.Key()), string(it.Value()))
|
||||
row := ParseFromKeyValue(it.Key(), it.Value())
|
||||
row, err := ParseFromKeyValue(it.Key(), it.Value())
|
||||
if err != nil {
|
||||
fmt.Printf("error parsing key/value: %v", err)
|
||||
return
|
||||
}
|
||||
if row != nil {
|
||||
fmt.Printf("%v\n", row)
|
||||
fmt.Printf("Key: % -100x\nValue: % -100x\n\n", it.Key(), it.Value())
|
||||
|
|
Loading…
Reference in New Issue