rosedblabs / rosedb

Lightweight, fast and reliable key/value storage engine based on Bitcask.

Home Page:https://rosedblabs.github.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Merge() seems to have a bug

taroim opened this issue · comments

commented
package main

import (
	"fmt"

	"github.com/rosedblabs/rosedb/v2"
	"github.com/rosedblabs/rosedb/v2/utils"
)

func main() {
	options := rosedb.DefaultOptions
	options.DirPath = "./tmp/rosedb_merge"

	// merge after insert
	db, err := rosedb.Open(options)
	if err != nil {
		panic(err)
	}

	// output:KeysNum=0, DiskSize=0.00MiB
	fmt.Printf("KeysNum=%v, DiskSize=%.2fMiB\n", db.Stat().KeysNum, float64(db.Stat().DiskSize)/1024/1024)

	for i := 0; i < 10; i++ {
		_ = db.Put([]byte(utils.GetTestKey(i)), utils.RandomValue(100*1024))
	}
	_ = db.Merge()
	_ = db.Close()

	// merge after deletion
	db, err = rosedb.Open(options)
	if err != nil {
		panic(err)
	}

	// output:KeysNum=10, DiskSize=0.98MiB
	fmt.Printf("KeysNum=%v, DiskSize=%.2fMiB\n", db.Stat().KeysNum, float64(db.Stat().DiskSize)/1024/1024)

	for i := 0; i < 10; i++ {
		_ = db.Delete([]byte(utils.GetTestKey(i)))
	}
	_ = db.Merge()
	_ = db.Close()

	// open statistics again
	db, err = rosedb.Open(options)
	if err != nil {
		panic(err)
	}

	// output:KeysNum=10, DiskSize=0.00MiB
	fmt.Printf("KeysNum=%v, DiskSize=%.2fMiB\n", db.Stat().KeysNum, float64(db.Stat().DiskSize)/1024/1024)

	_ = db.Close()
}

执行结果:
KeysNum=0, DiskSize=0.00MiB
KeysNum=10, DiskSize=0.98MiB
KeysNum=10, DiskSize=0.00MiB # 请注意:此处 KeysNum 应该为 0

问题描述:
Put 数据后执行 Merge(),然后 Delete 数据后再执行 Merge(),这时候就会出现所有的 Key 都还在!?

commented

@taroim Thank you for your feedback, this PR can solve the problem you mentioned.

commented

In version 2.2.3, the Iterator method has been removed. Why?

commented

@Jeremy-Run After updating to version 2.2.3, the execution result remains unchanged, and the issue doesn't seem to be resolved.

commented

Sorry, I pulled the wrong branch, my bad.

commented

In version 2.2.3, the Iterator method has been removed. Why?

@facework
The version 2.2.3 does not exist, do you mean 2.3.x? 2.3.x has not been released yet.
As to why the iterator is removed, this Issue should help you.

commented

@Jeremy-Run After updating to version 2.2.3, the execution result remains unchanged, and the issue doesn't seem to be resolved.

@taroim pr hasn't been merged into the main branch yet, wait for @roseduan review.

In version 2.2.3, the Iterator method has been removed. Why?

The iterator replies on the IRadix data structure, but after our tests, the IRadix will occupy lots of memory when the database grows larger.

So we decide to use BTree as the memory data structure instead of IRadix.
Then we add some similar methods to do the same thing with the previous iterator: Ascend and Descend.

So next release the iterator will be removed, if you have any troubles, please feel free to give us feedback.

This problem has been resolved, please update to the latest commit, thanks. @taroim

commented

It's usable, thank you.

commented
package main

import (
	"fmt"

	"github.com/rosedblabs/rosedb/v2"
	"github.com/rosedblabs/rosedb/v2/utils"
)

func main() {
	options := rosedb.DefaultOptions
	options.DirPath = "./tmp/rosedb_merge"

	// merge after insert
	db, err := rosedb.Open(options)
	if err != nil {
		panic(err)
	}

	for i := 0; i < 10; i++ {
		_ = db.Put([]byte(utils.GetTestKey(i)), utils.RandomValue(100*1024))
	}
	_ = db.Merge(true)
	_ = db.Close()

	// merge after deletion
	db, err = rosedb.Open(options)
	if err != nil {
		panic(err)
	}

	fmt.Printf("KeysNum=%v, DiskSize=%.2fMiB\n", db.Stat().KeysNum, float64(db.Stat().DiskSize)/1024/1024)
	for i := 0; i < 10; i++ {
		_ = db.Delete([]byte(utils.GetTestKey(i)))
	}
	_ = db.Merge(true)
	_ = db.Close()

	// open statistics again
	db, err = rosedb.Open(options)
	if err != nil {
		panic(err)
	}

	fmt.Printf("KeysNum=%v, DiskSize=%.2fMiB\n", db.Stat().KeysNum, float64(db.Stat().DiskSize)/1024/1024)
	_ = db.Close()
}

First Execution Result:
KeysNum=10, DiskSize=0.98MiB
KeysNum=0, DiskSize=0.00MiB

Second Execution Result:
KeysNum=10, DiskSize=0.00MiB // Here, DiskSize = 0 !???
KeysNum=0, DiskSize=0.00MiB

I will fix it later.
The Merge operation has some changes recently, so it may be unstable.

package main

import (
	"fmt"

	"github.com/rosedblabs/rosedb/v2"
	"github.com/rosedblabs/rosedb/v2/utils"
)

func main() {
	options := rosedb.DefaultOptions
	options.DirPath = "./tmp/rosedb_merge"

	// merge after insert
	db, err := rosedb.Open(options)
	if err != nil {
		panic(err)
	}

	for i := 0; i < 10; i++ {
		_ = db.Put([]byte(utils.GetTestKey(i)), utils.RandomValue(100*1024))
	}
	_ = db.Merge(true)
	_ = db.Close()

	// merge after deletion
	db, err = rosedb.Open(options)
	if err != nil {
		panic(err)
	}

	fmt.Printf("KeysNum=%v, DiskSize=%.2fMiB\n", db.Stat().KeysNum, float64(db.Stat().DiskSize)/1024/1024)
	for i := 0; i < 10; i++ {
		_ = db.Delete([]byte(utils.GetTestKey(i)))
	}
	_ = db.Merge(true)
	_ = db.Close()

	// open statistics again
	db, err = rosedb.Open(options)
	if err != nil {
		panic(err)
	}

	fmt.Printf("KeysNum=%v, DiskSize=%.2fMiB\n", db.Stat().KeysNum, float64(db.Stat().DiskSize)/1024/1024)
	_ = db.Close()
}

First Execution Result: KeysNum=10, DiskSize=0.98MiB KeysNum=0, DiskSize=0.00MiB

Second Execution Result: KeysNum=10, DiskSize=0.00MiB // Here, DiskSize = 0 !??? KeysNum=0, DiskSize=0.00MiB

I have fixed this in the latest commit, enjoy!

Feel free to give us any feedback, thanks.

commented

OK, thanks.