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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright (c) 2017 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 mem
  15. import (
  16. "sort"
  17. "strings"
  18. "github.com/RoaringBitmap/roaring"
  19. "github.com/blevesearch/bleve/index"
  20. "github.com/blevesearch/bleve/index/scorch/segment"
  21. )
  22. // Dictionary is the in-memory representation of the term dictionary
  23. type Dictionary struct {
  24. segment *Segment
  25. field string
  26. fieldID uint16
  27. }
  28. // PostingsList returns the postings list for the specified term
  29. func (d *Dictionary) PostingsList(term string,
  30. except *roaring.Bitmap) (segment.PostingsList, error) {
  31. return &PostingsList{
  32. dictionary: d,
  33. term: term,
  34. postingsID: d.segment.Dicts[d.fieldID][term],
  35. except: except,
  36. }, nil
  37. }
  38. // Iterator returns an iterator for this dictionary
  39. func (d *Dictionary) Iterator() segment.DictionaryIterator {
  40. return &DictionaryIterator{
  41. d: d,
  42. }
  43. }
  44. // PrefixIterator returns an iterator which only visits terms having the
  45. // the specified prefix
  46. func (d *Dictionary) PrefixIterator(prefix string) segment.DictionaryIterator {
  47. offset := sort.SearchStrings(d.segment.DictKeys[d.fieldID], prefix)
  48. return &DictionaryIterator{
  49. d: d,
  50. prefix: prefix,
  51. offset: offset,
  52. }
  53. }
  54. // RangeIterator returns an iterator which only visits terms between the
  55. // start and end terms. NOTE: bleve.index API specifies the end is inclusive.
  56. func (d *Dictionary) RangeIterator(start, end string) segment.DictionaryIterator {
  57. offset := sort.SearchStrings(d.segment.DictKeys[d.fieldID], start)
  58. return &DictionaryIterator{
  59. d: d,
  60. offset: offset,
  61. end: end,
  62. }
  63. }
  64. // DictionaryIterator is an iterator for term dictionary
  65. type DictionaryIterator struct {
  66. d *Dictionary
  67. prefix string
  68. end string
  69. offset int
  70. dictEntry index.DictEntry // reused across Next()'s
  71. }
  72. // Next returns the next entry in the dictionary
  73. func (d *DictionaryIterator) Next() (*index.DictEntry, error) {
  74. if d.offset > len(d.d.segment.DictKeys[d.d.fieldID])-1 {
  75. return nil, nil
  76. }
  77. next := d.d.segment.DictKeys[d.d.fieldID][d.offset]
  78. // check prefix
  79. if d.prefix != "" && !strings.HasPrefix(next, d.prefix) {
  80. return nil, nil
  81. }
  82. // check end (bleve.index API demands inclusive end)
  83. if d.end != "" && next > d.end {
  84. return nil, nil
  85. }
  86. d.offset++
  87. postingID := d.d.segment.Dicts[d.d.fieldID][next]
  88. d.dictEntry.Term = next
  89. d.dictEntry.Count = d.d.segment.Postings[postingID-1].GetCardinality()
  90. return &d.dictEntry, nil
  91. }