Skip to content

Commit

Permalink
fix: merge not allowed when using list type (temporary fix) (#457)
Browse files Browse the repository at this point in the history
* fix: merge not allowed when using list type (temporary fix)

* style: remove useless code

* test: add unit test for merging list
  • Loading branch information
bigboss2063 committed Sep 1, 2023
1 parent 4d420a6 commit 15267dd
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 40 deletions.
2 changes: 1 addition & 1 deletion db.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func open(opt Options) (*DB, error) {
BPTreeKeyEntryPosMap: make(map[string]int64),
bucketMetas: make(map[string]*BucketMeta),
ActiveCommittedTxIdsIdx: NewTree(),
Index: NewIndex(),
Index: newIndex(),
fm: newFileManager(opt.RWMode, opt.MaxFdNumsInCache, opt.CleanFdsCacheThreshold),
mergeStartCh: make(chan struct{}),
mergeEndCh: make(chan error),
Expand Down
3 changes: 3 additions & 0 deletions db_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@ var (

// ErrIsMerging is returned when merge in progress
ErrIsMerging = errors.New("merge in progress")

// ErrNotSupportMergeWhenUsingList is returned calling 'Merge' when using list
ErrNotSupportMergeWhenUsingList = errors.New("not support merge when using list for now")
)
2 changes: 1 addition & 1 deletion index.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type index struct {
list ListIdx
}

func NewIndex() *index {
func newIndex() *index {
i := new(index)
i.list = map[string]*List{}
return i
Expand Down
23 changes: 4 additions & 19 deletions merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ func (db *DB) merge() error {
return ErrNotSupportHintBPTSparseIdxMode
}

if len(db.Index.list) != 0 {
return ErrNotSupportMergeWhenUsingList
}

// to prevent the initiation of multiple merges simultaneously.
db.mu.Lock()

Expand Down Expand Up @@ -265,24 +269,5 @@ func (db *DB) isPendingMergeEntry(entry *Entry) bool {
}
}

//if entry.Meta.Ds == DataStructureList {
// //check the key of list is expired or not
// //if expired, it will clear the items of index
// //so that nutsdb can clear entry of expiring list in the function isPendingMergeEntry
// db.checkListExpired()
//
// if listIdx := db.Index.getList(string(entry.Bucket)); listIdx != nil {
// items, _ := listIdx.LRange(string(entry.Key), 0, -1)
// if entry.Meta.Flag == DataRPushFlag || entry.Meta.Flag == DataLPushFlag {
// for _, item := range items {
// v, _ := db.getValueByRecord(item)
// if string(entry.Value) == string(v) {
// return true
// }
// }
// }
// }
//}

return false
}
27 changes: 8 additions & 19 deletions merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,25 +125,14 @@ func TestDB_MergeForZSet(t *testing.T) {
})
}

//func TestDB_MergeForList(t *testing.T) {
// opts := DefaultOptions
// opts.SegmentSize = 100
// opts.EntryIdxMode = HintKeyAndRAMIdxMode
// runNutsDBTest(t, &opts, func(t *testing.T, db *DB) {
// bucket := "bucket"
// key := GetTestBytes(0)
//
// for i := 0; i < 100; i++ {
// txPush(t, db, bucket, key, GetTestBytes(i), nil, true)
// }
// txRange(t, db, bucket, key, 0, 99, 100)
//
// txPop(t, db, bucket, key, GetTestBytes(99), nil, true)
// txPop(t, db, bucket, key, GetTestBytes(0), nil, false)
//
// require.NoError(t, db.Merge())
// })
//}
func TestDB_MergeForList(t *testing.T) {
bucket := "bucket"
key := GetTestBytes(0)
runNutsDBTest(t, nil, func(t *testing.T, db *DB) {
txPush(t, db, bucket, key, GetTestBytes(0), nil, true)
require.Error(t, ErrNotSupportMergeWhenUsingList, db.Merge())
})
}

func TestDB_MergeAutomatic(t *testing.T) {
opts := DefaultOptions
Expand Down

0 comments on commit 15267dd

Please sign in to comment.