Skip to content

Commit

Permalink
refactor some code and fix warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
roseduan committed Apr 13, 2024
1 parent 669e9b0 commit e9f8e9e
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 31 deletions.
7 changes: 3 additions & 4 deletions bptree.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,10 @@ type bptreeIterator struct {
options IteratorOptions
}

// NewBptreeIterator
func NewBptreeIterator(tx *bbolt.Tx, options IteratorOptions) *bptreeIterator {
cursor := tx.Bucket(indexBucketName).Cursor()
// create a boltdb based btree iterator
func newBptreeIterator(tx *bbolt.Tx, options IteratorOptions) *bptreeIterator {
return &bptreeIterator{
cursor: cursor,
cursor: tx.Bucket(indexBucketName).Cursor(),
options: options,
tx: tx,
}
Expand Down
10 changes: 5 additions & 5 deletions bptree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ func Test_bptreeIterator(t *testing.T) {
Reverse: false,
}

itr := NewBptreeIterator(tx, iteratorOptions)
itr := newBptreeIterator(tx, iteratorOptions)
assert.Nil(t, err)
var prev []byte
itr.Rewind()
Expand All @@ -390,7 +390,7 @@ func Test_bptreeIterator(t *testing.T) {
}
prev = nil

itr = NewBptreeIterator(tx, iteratorOptions)
itr = newBptreeIterator(tx, iteratorOptions)
assert.Nil(t, err)
itr.Rewind()
for itr.Valid() {
Expand Down Expand Up @@ -418,7 +418,7 @@ func Test_bptreeIterator(t *testing.T) {
}
prev = nil

itr = NewBptreeIterator(tx, iteratorOptions)
itr = newBptreeIterator(tx, iteratorOptions)
assert.Nil(t, err)
itr.Rewind()
for itr.Valid() {
Expand Down Expand Up @@ -450,7 +450,7 @@ func Test_bptreeIterator(t *testing.T) {
Prefix: []byte("not valid"),
}

itr = NewBptreeIterator(tx, iteratorOptions)
itr = newBptreeIterator(tx, iteratorOptions)
assert.Nil(t, err)
itr.Rewind()
assert.False(t, itr.Valid())
Expand All @@ -464,7 +464,7 @@ func Test_bptreeIterator(t *testing.T) {
Prefix: []byte("abc"),
}

itr = NewBptreeIterator(tx, iteratorOptions)
itr = newBptreeIterator(tx, iteratorOptions)
assert.Nil(t, err)
itr.Rewind()
assert.True(t, itr.Valid())
Expand Down
16 changes: 8 additions & 8 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ func TestDBIterator(t *testing.T) {
db.immuMems[i], err = openMemtable(opts)
assert.Nil(t, err)
}
logRecord_0 := []*LogRecord{
logRecord0 := []*LogRecord{
// 0
{[]byte("k3"), nil, LogRecordDeleted, 0},
{[]byte("k1"), []byte("v1"), LogRecordNormal, 0},
Expand All @@ -602,20 +602,20 @@ func TestDBIterator(t *testing.T) {
{[]byte("abc1"), []byte("v1_1"), LogRecordNormal, 0},
{[]byte("abc2"), []byte("v1_1"), LogRecordNormal, 0},
}
logRecord_1 := []*LogRecord{
logRecord1 := []*LogRecord{
{[]byte("k1"), []byte("v2_1"), LogRecordNormal, 0},
{[]byte("k2"), []byte("v2_1"), LogRecordNormal, 0},
{[]byte("k2"), []byte("v2_2"), LogRecordNormal, 0},
{[]byte("abc1"), []byte("v2_1"), LogRecordNormal, 0},
{[]byte("abc2"), []byte("v2_1"), LogRecordNormal, 0},
{[]byte("abc2"), []byte("v2_2"), LogRecordNormal, 0},
}
logRecord_2 := []*LogRecord{
logRecord2 := []*LogRecord{
// 2
{[]byte("k2"), nil, LogRecordDeleted, 0},
{[]byte("abc2"), nil, LogRecordDeleted, 0},
}
logRecord_3 := []*LogRecord{
logRecord3 := []*LogRecord{
{[]byte("k3"), []byte("v3_1"), LogRecordNormal, 0},
{[]byte("abc3"), []byte("v3_1"), LogRecordNormal, 0},
}
Expand All @@ -627,13 +627,13 @@ func TestDBIterator(t *testing.T) {
}
return
}
err = db.immuMems[0].putBatch(list2Map(logRecord_0), 0, nil)
err = db.immuMems[0].putBatch(list2Map(logRecord0), 0, nil)
assert.Nil(t, err)
err = db.immuMems[1].putBatch(list2Map(logRecord_1), 1, nil)
err = db.immuMems[1].putBatch(list2Map(logRecord1), 1, nil)
assert.Nil(t, err)
err = db.immuMems[2].putBatch(list2Map(logRecord_2), 2, nil)
err = db.immuMems[2].putBatch(list2Map(logRecord2), 2, nil)
assert.Nil(t, err)
err = db.activeMem.putBatch(list2Map(logRecord_3), 3, nil)
err = db.activeMem.putBatch(list2Map(logRecord3), 3, nil)
assert.Nil(t, err)

expectedKey := [][]byte{
Expand Down
12 changes: 8 additions & 4 deletions iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,23 +207,25 @@ func (db *DB) NewIterator(options IteratorOptions) (*Iterator, error) {
db.mu.Unlock()
}
}()

itrs := make([]*singleIter, 0, db.options.PartitionNum+len(db.immuMems)+1)
itrsM := make(map[int]*singleIter)
rank := 0
index := db.index.(*BPTree)

for i := 0; i < db.options.PartitionNum; i++ {
tx, err := index.trees[i].Begin(false)
if err != nil {
return nil, err
}
itr := NewBptreeIterator(
itr := newBptreeIterator(
tx,
options,
)
itr.Rewind()
// is empty
if !itr.Valid() {
itr.Close()
_ = itr.Close()
continue
}
itrs = append(itrs, &singleIter{
Expand All @@ -236,13 +238,14 @@ func (db *DB) NewIterator(options IteratorOptions) (*Iterator, error) {
itrsM[rank] = itrs[len(itrs)-1]
rank++
}

memtableList := append(db.immuMems, db.activeMem)
for i := 0; i < len(memtableList); i++ {
itr := NewMemtableIterator(options, memtableList[i])
itr := newMemtableIterator(options, memtableList[i])
itr.Rewind()
// is empty
if !itr.Valid() {
itr.Close()
_ = itr.Close()
continue
}
itrs = append(itrs, &singleIter{
Expand All @@ -255,6 +258,7 @@ func (db *DB) NewIterator(options IteratorOptions) (*Iterator, error) {
itrsM[rank] = itrs[len(itrs)-1]
rank++
}

h := iterHeap(itrs)
heap.Init(&h)

Expand Down
3 changes: 1 addition & 2 deletions memtable.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,7 @@ type memtableIterator struct {
iter *arenaskl.UniIterator
}

// NewMemtableIterator
func NewMemtableIterator(options IteratorOptions, memtable *memtable) *memtableIterator {
func newMemtableIterator(options IteratorOptions, memtable *memtable) *memtableIterator {
return &memtableIterator{
options: options,
iter: memtable.skl.NewUniIterator(options.Reverse),
Expand Down
19 changes: 11 additions & 8 deletions memtable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,13 +486,16 @@ func TestNewMemtableIterator(t *testing.T) {
}

table, err := openMemtable(opts)
defer table.close()
defer func() {
err = table.close()
assert.Nil(t, err)
}()
assert.Nil(t, err)

options := IteratorOptions{
Reverse: false,
}
iter := NewMemtableIterator(options, table)
iter := newMemtableIterator(options, table)
assert.Nil(t, err)

err = iter.Close()
Expand Down Expand Up @@ -540,7 +543,7 @@ func Test_memtableIterator(t *testing.T) {
iteratorOptions := IteratorOptions{
Reverse: false,
}
itr := NewMemtableIterator(iteratorOptions, table)
itr := newMemtableIterator(iteratorOptions, table)
assert.Nil(t, err)
var prev []byte
itr.Rewind()
Expand All @@ -557,7 +560,7 @@ func Test_memtableIterator(t *testing.T) {

iteratorOptions.Reverse = true
prev = nil
itr = NewMemtableIterator(iteratorOptions, table)
itr = newMemtableIterator(iteratorOptions, table)
assert.Nil(t, err)
itr.Rewind()
for itr.Valid() {
Expand All @@ -572,7 +575,7 @@ func Test_memtableIterator(t *testing.T) {
assert.Nil(t, err)

iteratorOptions.Reverse = false
itr = NewMemtableIterator(iteratorOptions, table)
itr = newMemtableIterator(iteratorOptions, table)
assert.Nil(t, err)
itr.Seek([]byte("key 0"))
assert.Equal(t, []byte("key 0"), itr.Key())
Expand All @@ -585,7 +588,7 @@ func Test_memtableIterator(t *testing.T) {
assert.Nil(t, err)

iteratorOptions.Reverse = true
itr = NewMemtableIterator(iteratorOptions, table)
itr = newMemtableIterator(iteratorOptions, table)
assert.Nil(t, err)
itr.Seek([]byte("key 4"))
assert.Equal(t, []byte("key 2"), itr.Key())
Expand All @@ -605,7 +608,7 @@ func Test_memtableIterator(t *testing.T) {

iteratorOptions.Reverse = false
iteratorOptions.Prefix = []byte("not valid")
itr = NewMemtableIterator(iteratorOptions, table)
itr = newMemtableIterator(iteratorOptions, table)
assert.Nil(t, err)
itr.Rewind()
assert.False(t, itr.Valid())
Expand All @@ -614,7 +617,7 @@ func Test_memtableIterator(t *testing.T) {

iteratorOptions.Reverse = false
iteratorOptions.Prefix = []byte("abc")
itr = NewMemtableIterator(iteratorOptions, table)
itr = newMemtableIterator(iteratorOptions, table)
assert.Nil(t, err)
itr.Rewind()
for itr.Valid() {
Expand Down

0 comments on commit e9f8e9e

Please sign in to comment.