Merge pull request #466 from steveyen/optimize-fieldDict-reader-with-prealloc
Optimize upside-down's field dict reader with preallocated objects
This commit is contained in:
commit
77b79a2684
@ -24,6 +24,8 @@ import (
|
|||||||
type UpsideDownCouchFieldDict struct {
|
type UpsideDownCouchFieldDict struct {
|
||||||
indexReader *IndexReader
|
indexReader *IndexReader
|
||||||
iterator store.KVIterator
|
iterator store.KVIterator
|
||||||
|
dictRow *DictionaryRow
|
||||||
|
dictEntry *index.DictEntry
|
||||||
field uint16
|
field uint16
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,6 +44,8 @@ func newUpsideDownCouchFieldDict(indexReader *IndexReader, field uint16, startTe
|
|||||||
return &UpsideDownCouchFieldDict{
|
return &UpsideDownCouchFieldDict{
|
||||||
indexReader: indexReader,
|
indexReader: indexReader,
|
||||||
iterator: it,
|
iterator: it,
|
||||||
|
dictRow: &DictionaryRow{}, // Pre-alloced, reused row.
|
||||||
|
dictEntry: &index.DictEntry{}, // Pre-alloced, reused entry.
|
||||||
field: field,
|
field: field,
|
||||||
}, nil
|
}, nil
|
||||||
|
|
||||||
@ -53,17 +57,19 @@ func (r *UpsideDownCouchFieldDict) Next() (*index.DictEntry, error) {
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
currRow, err := NewDictionaryRowKV(key, val)
|
err := r.dictRow.parseDictionaryK(key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unexpected error parsing dictionary row kv: %v", err)
|
return nil, fmt.Errorf("unexpected error parsing dictionary row key: %v", err)
|
||||||
}
|
}
|
||||||
rv := index.DictEntry{
|
err = r.dictRow.parseDictionaryV(val)
|
||||||
Term: string(currRow.term),
|
if err != nil {
|
||||||
Count: currRow.count,
|
return nil, fmt.Errorf("unexpected error parsing dictionary row val: %v", err)
|
||||||
}
|
}
|
||||||
|
r.dictEntry.Term = string(r.dictRow.term)
|
||||||
|
r.dictEntry.Count = r.dictRow.count
|
||||||
// advance the iterator to the next term
|
// advance the iterator to the next term
|
||||||
r.iterator.Next()
|
r.iterator.Next()
|
||||||
return &rv, nil
|
return r.dictEntry, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -242,9 +242,9 @@ func NewFieldRowKV(key, value []byte) (*FieldRow, error) {
|
|||||||
const DictionaryRowMaxValueSize = binary.MaxVarintLen64
|
const DictionaryRowMaxValueSize = binary.MaxVarintLen64
|
||||||
|
|
||||||
type DictionaryRow struct {
|
type DictionaryRow struct {
|
||||||
field uint16
|
|
||||||
term []byte
|
term []byte
|
||||||
count uint64
|
count uint64
|
||||||
|
field uint16
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dr *DictionaryRow) Key() []byte {
|
func (dr *DictionaryRow) Key() []byte {
|
||||||
@ -306,36 +306,29 @@ func NewDictionaryRowKV(key, value []byte) (*DictionaryRow, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewDictionaryRowK(key []byte) (*DictionaryRow, error) {
|
func NewDictionaryRowK(key []byte) (*DictionaryRow, error) {
|
||||||
rv := DictionaryRow{}
|
rv := &DictionaryRow{}
|
||||||
buf := bytes.NewBuffer(key)
|
err := rv.parseDictionaryK(key)
|
||||||
_, err := buf.ReadByte() // type
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
return rv, nil
|
||||||
err = binary.Read(buf, binary.LittleEndian, &rv.field)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
rv.term, err = buf.ReadBytes(ByteSeparator)
|
func (dr *DictionaryRow) parseDictionaryK(key []byte) error {
|
||||||
// there is no separator expected here, should get EOF
|
dr.field = binary.LittleEndian.Uint16(key[1:3])
|
||||||
if err != io.EOF {
|
if dr.term != nil {
|
||||||
return nil, err
|
dr.term = dr.term[:0]
|
||||||
}
|
}
|
||||||
|
dr.term = append(dr.term, key[3:]...)
|
||||||
return &rv, nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dr *DictionaryRow) parseDictionaryV(value []byte) error {
|
func (dr *DictionaryRow) parseDictionaryV(value []byte) error {
|
||||||
buf := bytes.NewBuffer(value)
|
count, nread := binary.Uvarint(value)
|
||||||
|
if nread <= 0 {
|
||||||
count, err := binary.ReadUvarint(buf)
|
return fmt.Errorf("DictionaryRow parse Uvarint error, nread: %d", nread)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
dr.count = count
|
dr.count = count
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user