Skip to content

Commit

Permalink
check index type
Browse files Browse the repository at this point in the history
  • Loading branch information
akiozihao committed Jan 9, 2024
1 parent f822035 commit 06fcf92
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
6 changes: 3 additions & 3 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/gofrs/flock"
"github.com/rosedblabs/diskhash"
"github.com/rosedblabs/wal"
"go.etcd.io/bbolt"
"golang.org/x/sync/errgroup"
)

Expand Down Expand Up @@ -575,6 +574,9 @@ func (db *DB) rewriteValidRecords(walFile *wal.WAL, validRecords []*ValueLogReco
}

func (db *DB) NewIterator(options IteratorOptions) (*MergeIterator, error) {
if db.options.IndexType != BTree {
return nil, ErrDBIteratorUnsupportedType
}
db.mu.Lock()
defer func() {
if r := recover(); r != nil {
Expand All @@ -583,14 +585,12 @@ func (db *DB) NewIterator(options IteratorOptions) (*MergeIterator, error) {
}()
itrs := make([]*SingleIter, 0, db.options.PartitionNum+len(db.immuMems)+1)
rank := 0
txs := make([]*bbolt.Tx, db.options.PartitionNum)
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
}
txs[i] = tx
itr, err := NewBptreeIterator(
tx,
options,
Expand Down
9 changes: 9 additions & 0 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -786,4 +786,13 @@ func TestDBIterator(t *testing.T) {
}
err = iter.Close()
assert.Nil(t, err)

// unsupported type
options = DefaultOptions
options.IndexType = Hash
db, err = Open(options)
assert.Nil(t, err)
itr, err := db.NewIterator(IteratorOptions{Reverse: false})
assert.Equal(t, ErrDBIteratorUnsupportedType, err)
assert.Nil(t, itr)
}
17 changes: 9 additions & 8 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package lotusdb
import "errors"

var (
ErrKeyIsEmpty = errors.New("the key is empty")
ErrKeyNotFound = errors.New("key not found in database")
ErrDatabaseIsUsing = errors.New("the database directory is used by another process")
ErrReadOnlyBatch = errors.New("the batch is read only")
ErrBatchCommitted = errors.New("the batch is committed")
ErrDBClosed = errors.New("the database is closed")
ErrDBDirectoryISEmpty = errors.New("the database directory path can not be empty")
ErrWaitMemtableSpaceTimeOut = errors.New("wait memtable space timeout, try again later")
ErrKeyIsEmpty = errors.New("the key is empty")
ErrKeyNotFound = errors.New("key not found in database")
ErrDatabaseIsUsing = errors.New("the database directory is used by another process")
ErrReadOnlyBatch = errors.New("the batch is read only")
ErrBatchCommitted = errors.New("the batch is committed")
ErrDBClosed = errors.New("the database is closed")
ErrDBDirectoryISEmpty = errors.New("the database directory path can not be empty")
ErrWaitMemtableSpaceTimeOut = errors.New("wait memtable space timeout, try again later")
ErrDBIteratorUnsupportedType = errors.New("iterator only support BTree index")
)

0 comments on commit 06fcf92

Please sign in to comment.