本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.2KB

  1. // Copyright (c) 2014 Couchbase, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package upsidedown
  15. import (
  16. "fmt"
  17. "github.com/blevesearch/bleve/index"
  18. "github.com/blevesearch/bleve/index/store"
  19. )
  20. type UpsideDownCouchFieldDict struct {
  21. indexReader *IndexReader
  22. iterator store.KVIterator
  23. dictRow *DictionaryRow
  24. dictEntry *index.DictEntry
  25. field uint16
  26. }
  27. func newUpsideDownCouchFieldDict(indexReader *IndexReader, field uint16, startTerm, endTerm []byte) (*UpsideDownCouchFieldDict, error) {
  28. startKey := NewDictionaryRow(startTerm, field, 0).Key()
  29. if endTerm == nil {
  30. endTerm = []byte{ByteSeparator}
  31. } else {
  32. endTerm = incrementBytes(endTerm)
  33. }
  34. endKey := NewDictionaryRow(endTerm, field, 0).Key()
  35. it := indexReader.kvreader.RangeIterator(startKey, endKey)
  36. return &UpsideDownCouchFieldDict{
  37. indexReader: indexReader,
  38. iterator: it,
  39. dictRow: &DictionaryRow{}, // Pre-alloced, reused row.
  40. dictEntry: &index.DictEntry{}, // Pre-alloced, reused entry.
  41. field: field,
  42. }, nil
  43. }
  44. func (r *UpsideDownCouchFieldDict) Next() (*index.DictEntry, error) {
  45. key, val, valid := r.iterator.Current()
  46. if !valid {
  47. return nil, nil
  48. }
  49. err := r.dictRow.parseDictionaryK(key)
  50. if err != nil {
  51. return nil, fmt.Errorf("unexpected error parsing dictionary row key: %v", err)
  52. }
  53. err = r.dictRow.parseDictionaryV(val)
  54. if err != nil {
  55. return nil, fmt.Errorf("unexpected error parsing dictionary row val: %v", err)
  56. }
  57. r.dictEntry.Term = string(r.dictRow.term)
  58. r.dictEntry.Count = r.dictRow.count
  59. // advance the iterator to the next term
  60. r.iterator.Next()
  61. return r.dictEntry, nil
  62. }
  63. func (r *UpsideDownCouchFieldDict) Close() error {
  64. return r.iterator.Close()
  65. }
上海开阖软件有限公司 沪ICP备12045867号-1