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.

snapshot_index_dict.go 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 scorch
  15. import (
  16. "container/heap"
  17. index "github.com/blevesearch/bleve_index_api"
  18. segment "github.com/blevesearch/scorch_segment_api/v2"
  19. )
  20. type segmentDictCursor struct {
  21. dict segment.TermDictionary
  22. itr segment.DictionaryIterator
  23. curr index.DictEntry
  24. }
  25. type IndexSnapshotFieldDict struct {
  26. snapshot *IndexSnapshot
  27. cursors []*segmentDictCursor
  28. entry index.DictEntry
  29. }
  30. func (i *IndexSnapshotFieldDict) Len() int { return len(i.cursors) }
  31. func (i *IndexSnapshotFieldDict) Less(a, b int) bool {
  32. return i.cursors[a].curr.Term < i.cursors[b].curr.Term
  33. }
  34. func (i *IndexSnapshotFieldDict) Swap(a, b int) {
  35. i.cursors[a], i.cursors[b] = i.cursors[b], i.cursors[a]
  36. }
  37. func (i *IndexSnapshotFieldDict) Push(x interface{}) {
  38. i.cursors = append(i.cursors, x.(*segmentDictCursor))
  39. }
  40. func (i *IndexSnapshotFieldDict) Pop() interface{} {
  41. n := len(i.cursors)
  42. x := i.cursors[n-1]
  43. i.cursors = i.cursors[0 : n-1]
  44. return x
  45. }
  46. func (i *IndexSnapshotFieldDict) Next() (*index.DictEntry, error) {
  47. if len(i.cursors) == 0 {
  48. return nil, nil
  49. }
  50. i.entry = i.cursors[0].curr
  51. next, err := i.cursors[0].itr.Next()
  52. if err != nil {
  53. return nil, err
  54. }
  55. if next == nil {
  56. // at end of this cursor, remove it
  57. heap.Pop(i)
  58. } else {
  59. // modified heap, fix it
  60. i.cursors[0].curr = *next
  61. heap.Fix(i, 0)
  62. }
  63. // look for any other entries with the exact same term
  64. for len(i.cursors) > 0 && i.cursors[0].curr.Term == i.entry.Term {
  65. i.entry.Count += i.cursors[0].curr.Count
  66. next, err := i.cursors[0].itr.Next()
  67. if err != nil {
  68. return nil, err
  69. }
  70. if next == nil {
  71. // at end of this cursor, remove it
  72. heap.Pop(i)
  73. } else {
  74. // modified heap, fix it
  75. i.cursors[0].curr = *next
  76. heap.Fix(i, 0)
  77. }
  78. }
  79. return &i.entry, nil
  80. }
  81. func (i *IndexSnapshotFieldDict) Close() error {
  82. return nil
  83. }
  84. func (i *IndexSnapshotFieldDict) Contains(key []byte) (bool, error) {
  85. if len(i.cursors) == 0 {
  86. return false, nil
  87. }
  88. for _, cursor := range i.cursors {
  89. if found, _ := cursor.dict.Contains(key); found {
  90. return true, nil
  91. }
  92. }
  93. return false, nil
  94. }