github.com/rosedblabs/rosedb/v2@v2.3.7-0.20240423093736-a89ea823e5b9/examples/merge/main.go (about)

     1  package main
     2  
     3  import (
     4  	"github.com/rosedblabs/rosedb/v2"
     5  	"github.com/rosedblabs/rosedb/v2/utils"
     6  )
     7  
     8  // this file shows how to use the Merge feature of rosedb.
     9  // Merge is used to merge the data files in the database.
    10  // It is recommended to use it when the database is not busy.
    11  
    12  func main() {
    13  	// specify the options
    14  	options := rosedb.DefaultOptions
    15  	options.DirPath = "/tmp/rosedb_merge"
    16  
    17  	// open a database
    18  	db, err := rosedb.Open(options)
    19  	if err != nil {
    20  		panic(err)
    21  	}
    22  	defer func() {
    23  		_ = db.Close()
    24  	}()
    25  
    26  	// write some data
    27  	for i := 0; i < 100000; i++ {
    28  		_ = db.Put([]byte(utils.GetTestKey(i)), utils.RandomValue(128))
    29  	}
    30  	// delete some data
    31  	for i := 0; i < 100000/2; i++ {
    32  		_ = db.Delete([]byte(utils.GetTestKey(i)))
    33  	}
    34  
    35  	// then merge the data files
    36  	// all the invalid data will be removed, and the valid data will be merged into the new data files.
    37  	_ = db.Merge(true)
    38  }