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.

unadorned.go 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright (c) 2020 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 segment
  15. import (
  16. "github.com/RoaringBitmap/roaring"
  17. "math"
  18. "reflect"
  19. )
  20. var reflectStaticSizeUnadornedPostingsIteratorBitmap int
  21. var reflectStaticSizeUnadornedPostingsIterator1Hit int
  22. var reflectStaticSizeUnadornedPosting int
  23. func init() {
  24. var pib UnadornedPostingsIteratorBitmap
  25. reflectStaticSizeUnadornedPostingsIteratorBitmap = int(reflect.TypeOf(pib).Size())
  26. var pi1h UnadornedPostingsIterator1Hit
  27. reflectStaticSizeUnadornedPostingsIterator1Hit = int(reflect.TypeOf(pi1h).Size())
  28. var up UnadornedPosting
  29. reflectStaticSizeUnadornedPosting = int(reflect.TypeOf(up).Size())
  30. }
  31. type UnadornedPostingsIteratorBitmap struct{
  32. actual roaring.IntPeekable
  33. actualBM *roaring.Bitmap
  34. }
  35. func (i *UnadornedPostingsIteratorBitmap) Next() (Posting, error) {
  36. return i.nextAtOrAfter(0)
  37. }
  38. func (i *UnadornedPostingsIteratorBitmap) Advance(docNum uint64) (Posting, error) {
  39. return i.nextAtOrAfter(docNum)
  40. }
  41. func (i *UnadornedPostingsIteratorBitmap) nextAtOrAfter(atOrAfter uint64) (Posting, error) {
  42. docNum, exists := i.nextDocNumAtOrAfter(atOrAfter)
  43. if !exists {
  44. return nil, nil
  45. }
  46. return UnadornedPosting(docNum), nil
  47. }
  48. func (i *UnadornedPostingsIteratorBitmap) nextDocNumAtOrAfter(atOrAfter uint64) (uint64, bool) {
  49. if i.actual == nil || !i.actual.HasNext() {
  50. return 0, false
  51. }
  52. i.actual.AdvanceIfNeeded(uint32(atOrAfter))
  53. if !i.actual.HasNext() {
  54. return 0, false // couldn't find anything
  55. }
  56. return uint64(i.actual.Next()), true
  57. }
  58. func (i *UnadornedPostingsIteratorBitmap) Size() int {
  59. return reflectStaticSizeUnadornedPostingsIteratorBitmap
  60. }
  61. func NewUnadornedPostingsIteratorFromBitmap(bm *roaring.Bitmap) PostingsIterator {
  62. return &UnadornedPostingsIteratorBitmap{
  63. actualBM: bm,
  64. actual: bm.Iterator(),
  65. }
  66. }
  67. const docNum1HitFinished = math.MaxUint64
  68. type UnadornedPostingsIterator1Hit struct{
  69. docNum uint64
  70. }
  71. func (i *UnadornedPostingsIterator1Hit) Next() (Posting, error) {
  72. return i.nextAtOrAfter(0)
  73. }
  74. func (i *UnadornedPostingsIterator1Hit) Advance(docNum uint64) (Posting, error) {
  75. return i.nextAtOrAfter(docNum)
  76. }
  77. func (i *UnadornedPostingsIterator1Hit) nextAtOrAfter(atOrAfter uint64) (Posting, error) {
  78. docNum, exists := i.nextDocNumAtOrAfter(atOrAfter)
  79. if !exists {
  80. return nil, nil
  81. }
  82. return UnadornedPosting(docNum), nil
  83. }
  84. func (i *UnadornedPostingsIterator1Hit) nextDocNumAtOrAfter(atOrAfter uint64) (uint64, bool) {
  85. if i.docNum == docNum1HitFinished {
  86. return 0, false
  87. }
  88. if i.docNum < atOrAfter {
  89. // advanced past our 1-hit
  90. i.docNum = docNum1HitFinished // consume our 1-hit docNum
  91. return 0, false
  92. }
  93. docNum := i.docNum
  94. i.docNum = docNum1HitFinished // consume our 1-hit docNum
  95. return docNum, true
  96. }
  97. func (i *UnadornedPostingsIterator1Hit) Size() int {
  98. return reflectStaticSizeUnadornedPostingsIterator1Hit
  99. }
  100. func NewUnadornedPostingsIteratorFrom1Hit(docNum1Hit uint64) PostingsIterator {
  101. return &UnadornedPostingsIterator1Hit{
  102. docNum1Hit,
  103. }
  104. }
  105. type UnadornedPosting uint64
  106. func (p UnadornedPosting) Number() uint64 {
  107. return uint64(p)
  108. }
  109. func (p UnadornedPosting) Frequency() uint64 {
  110. return 0
  111. }
  112. func (p UnadornedPosting) Norm() float64 {
  113. return 0
  114. }
  115. func (p UnadornedPosting) Locations() []Location {
  116. return nil
  117. }
  118. func (p UnadornedPosting) Size() int {
  119. return reflectStaticSizeUnadornedPosting
  120. }