github.com/flower-corp/rosedb@v1.1.2-0.20230117132829-21dc4f7b319a/db_test.go (about)

     1  package rosedb
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"github.com/flower-corp/rosedb/logger"
     7  	"github.com/stretchr/testify/assert"
     8  	"math/rand"
     9  	"os"
    10  	"path/filepath"
    11  	"runtime"
    12  	"testing"
    13  	"time"
    14  )
    15  
    16  func TestOpen(t *testing.T) {
    17  	path := filepath.Join("/tmp", "rosedb")
    18  	t.Run("default", func(t *testing.T) {
    19  		opts := DefaultOptions(path)
    20  		db, err := Open(opts)
    21  		defer destroyDB(db)
    22  		assert.Nil(t, err)
    23  		assert.NotNil(t, db)
    24  	})
    25  
    26  	t.Run("mmap", func(t *testing.T) {
    27  		opts := DefaultOptions(path)
    28  		opts.IoType = MMap
    29  		db, err := Open(opts)
    30  		defer destroyDB(db)
    31  		assert.Nil(t, err)
    32  		assert.NotNil(t, db)
    33  	})
    34  }
    35  
    36  func TestLogFileGC(t *testing.T) {
    37  	path := filepath.Join("/tmp", "rosedb")
    38  	opts := DefaultOptions(path)
    39  	opts.LogFileGCInterval = time.Second * 7
    40  	opts.LogFileGCRatio = 0.00001
    41  	db, err := Open(opts)
    42  	defer destroyDB(db)
    43  	if err != nil {
    44  		t.Error("open db err ", err)
    45  	}
    46  
    47  	writeCount := 800000
    48  	for i := 0; i < writeCount; i++ {
    49  		err := db.Set(GetKey(i), GetValue16B())
    50  		assert.Nil(t, err)
    51  	}
    52  
    53  	var deleted [][]byte
    54  	rand.Seed(time.Now().Unix())
    55  	for i := 0; i < 100000; i++ {
    56  		k := rand.Intn(writeCount)
    57  		key := GetKey(k)
    58  		err := db.Delete(key)
    59  		assert.Nil(t, err)
    60  		deleted = append(deleted, key)
    61  	}
    62  
    63  	time.Sleep(time.Second * 12)
    64  	for _, key := range deleted {
    65  		_, err := db.Get(key)
    66  		assert.Equal(t, err, ErrKeyNotFound)
    67  	}
    68  }
    69  
    70  func TestRoseDB_Backup(t *testing.T) {
    71  	path := filepath.Join("/tmp", "rosedb")
    72  	opts := DefaultOptions(path)
    73  	db, err := Open(opts)
    74  	defer destroyDB(db)
    75  	if err != nil {
    76  		t.Error("open db err ", err)
    77  	}
    78  
    79  	for i := 0; i < 10; i++ {
    80  		err := db.Set(GetKey(i), GetValue128B())
    81  		assert.Nil(t, err)
    82  	}
    83  
    84  	backupPath := filepath.Join("/tmp", "rosedb-backup")
    85  	err = db.Backup(backupPath)
    86  	assert.Nil(t, err)
    87  
    88  	// open the backup database
    89  	opts2 := DefaultOptions(backupPath)
    90  	db2, err := Open(opts2)
    91  	assert.Nil(t, err)
    92  	defer destroyDB(db2)
    93  	val, err := db2.Get(GetKey(4))
    94  	assert.Nil(t, err)
    95  	assert.NotNil(t, val)
    96  }
    97  
    98  func destroyDB(db *RoseDB) {
    99  	if db != nil {
   100  		_ = db.Close()
   101  		if runtime.GOOS == "windows" {
   102  			time.Sleep(time.Millisecond * 100)
   103  		}
   104  		err := os.RemoveAll(db.opts.DBPath)
   105  		if err != nil {
   106  			logger.Errorf("destroy db err: %v", err)
   107  		}
   108  	}
   109  }
   110  
   111  const alphabet = "abcdefghijklmnopqrstuvwxyz0123456789"
   112  
   113  func init() {
   114  	rand.Seed(time.Now().Unix())
   115  }
   116  
   117  // GetKey length: 32 Bytes
   118  func GetKey(n int) []byte {
   119  	return []byte("kvstore-bench-key------" + fmt.Sprintf("%09d", n))
   120  }
   121  
   122  func GetValue16B() []byte {
   123  	return GetValue(16)
   124  }
   125  
   126  func GetValue128B() []byte {
   127  	return GetValue(128)
   128  }
   129  
   130  func GetValue4K() []byte {
   131  	return GetValue(4096)
   132  }
   133  
   134  func GetValue(n int) []byte {
   135  	var str bytes.Buffer
   136  	for i := 0; i < n; i++ {
   137  		str.WriteByte(alphabet[rand.Int()%36])
   138  	}
   139  	return str.Bytes()
   140  }