Skip to content

Commit 864cfa4

Browse files
ef: add .Has() method (#19436)
1 parent 71ef069 commit 864cfa4

File tree

6 files changed

+26
-0
lines changed

6 files changed

+26
-0
lines changed

db/recsplit/eliasfano32/elias_fano_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,11 @@ func TestEliasFano(t *testing.T) {
269269
assert.Equal(t, ef2.Max(), Max(buf.Bytes()))
270270
assert.Equal(t, ef2.Min(), Min(buf.Bytes()))
271271
assert.Equal(t, ef2.Count(), Count(buf.Bytes()))
272+
273+
ref := RebasedEliasFano{}
274+
ref.Reset(1000, buf.Bytes())
275+
assert.True(t, ref.Has(1037))
276+
assert.False(t, ref.Has(1038))
272277
}
273278

274279
func BenchmarkRead(b *testing.B) {

db/recsplit/eliasfano32/rebased_elias_fano.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ func (ref *RebasedEliasFano) Seek(v uint64) (uint64, bool) {
4646
return ref.baseNum + n, found
4747
}
4848

49+
func (ref *RebasedEliasFano) Has(v uint64) bool {
50+
n, ok := ref.Seek(v)
51+
return ok && n == v
52+
}
53+
4954
func (ref *RebasedEliasFano) Iterator() *RebasedIterWrapper {
5055
it := &RebasedIterWrapper{}
5156
it.Reset(ref, false)

db/recsplit/multiencseq/sequence_reader.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ func (s *SequenceReader) Seek(v uint64) (uint64, bool) {
151151
panic(fmt.Sprintf("unknown sequence encoding: %d", s.currentEnc))
152152
}
153153

154+
func (s *SequenceReader) Has(v uint64) bool {
155+
n, ok := s.Seek(v)
156+
return ok && n == v
157+
}
158+
154159
func (s *SequenceReader) Iterator(from int) stream.U64 {
155160
switch s.currentEnc {
156161
case SimpleEncoding:

db/recsplit/multiencseq/sequence_reader_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,9 @@ func requireSequenceChecks(t *testing.T, s *SequenceReader) {
211211
// check reverse iterator + seek before base num
212212
it = s.ReverseIterator(999)
213213
require.False(t, it.HasNext())
214+
215+
require.True(t, s.Has(1015))
216+
require.False(t, s.Has(1014))
214217
}
215218

216219
func requireRawDataChecks(t *testing.T, b []byte) {

db/recsplit/simpleseq/simple_sequence.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ func (s *SimpleSequence) Seek(v uint64) (uint64, bool) {
117117
return s.Get(uint64(idx)), true
118118
}
119119

120+
func (s *SimpleSequence) Has(v uint64) bool {
121+
n, ok := s.Seek(v)
122+
return ok && n == v
123+
}
124+
120125
func (s *SimpleSequence) Iterator() *SimpleSequenceIterator {
121126
return &SimpleSequenceIterator{
122127
seq: s,

db/recsplit/simpleseq/simple_sequence_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ func TestSimpleSequence(t *testing.T) {
6969
v, found = s.Seek(1028)
7070
require.False(t, found)
7171
require.Equal(t, uint64(0), v)
72+
73+
require.True(t, s.Has(1007))
74+
require.False(t, s.Has(1008))
7275
})
7376

7477
t.Run("iterator", func(t *testing.T) {

0 commit comments

Comments
 (0)