github.com/petermattis/pebble@v0.0.0-20190905164901-ab51a2166067/internal_test.go (about) 1 // Copyright 2019 The LevelDB-Go and Pebble Authors. All rights reserved. Use 2 // of this source code is governed by a BSD-style license that can be found in 3 // the LICENSE file. 4 5 package pebble 6 7 import ( 8 "bytes" 9 "fmt" 10 11 "github.com/petermattis/pebble/internal/base" 12 ) 13 14 // internalIterAdapter adapts the new internalIterator interface which returns 15 // the key and value from positioning methods (Seek*, First, Last, Next, Prev) 16 // to the old interface which returned a boolean corresponding to Valid. Only 17 // used by test code. 18 type internalIterAdapter struct { 19 internalIterator 20 } 21 22 func (i *internalIterAdapter) verify(key *InternalKey, val []byte) bool { 23 valid := key != nil 24 if valid != i.Valid() { 25 panic(fmt.Sprintf("inconsistent valid: %t != %t", valid, i.Valid())) 26 } 27 if valid { 28 if base.InternalCompare(bytes.Compare, *key, i.Key()) != 0 { 29 panic(fmt.Sprintf("inconsistent key: %s != %s", *key, i.Key())) 30 } 31 if !bytes.Equal(val, i.Value()) { 32 panic(fmt.Sprintf("inconsistent value: [% x] != [% x]", val, i.Value())) 33 } 34 } 35 return valid 36 } 37 38 func (i *internalIterAdapter) SeekGE(key []byte) bool { 39 return i.verify(i.internalIterator.SeekGE(key)) 40 } 41 42 func (i *internalIterAdapter) SeekPrefixGE(prefix, key []byte) bool { 43 return i.verify(i.internalIterator.SeekPrefixGE(prefix, key)) 44 } 45 46 func (i *internalIterAdapter) SeekLT(key []byte) bool { 47 return i.verify(i.internalIterator.SeekLT(key)) 48 } 49 50 func (i *internalIterAdapter) First() bool { 51 return i.verify(i.internalIterator.First()) 52 } 53 54 func (i *internalIterAdapter) Last() bool { 55 return i.verify(i.internalIterator.Last()) 56 } 57 58 func (i *internalIterAdapter) Next() bool { 59 return i.verify(i.internalIterator.Next()) 60 } 61 62 func (i *internalIterAdapter) Prev() bool { 63 return i.verify(i.internalIterator.Prev()) 64 } 65 66 func (i *internalIterAdapter) Key() InternalKey { 67 return *i.internalIterator.Key() 68 }