github.com/pingcap/badger@v1.5.1-0.20230103063557-828f39b09b6d/blob_test.go (about)

     1  package badger
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"math/rand"
     7  	"os"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestBlob(t *testing.T) {
    14  	dir, err := ioutil.TempDir("", "badger")
    15  	require.NoError(t, err)
    16  	defer os.RemoveAll(dir)
    17  	opts := getTestOptions(dir)
    18  	opts.ValueThreshold = 20
    19  	opts.TableBuilderOptions.MaxTableSize = 4 * 1024
    20  	opts.MaxMemTableSize = 4 * 1024
    21  	opts.NumMemtables = 2
    22  	opts.NumLevelZeroTables = 1
    23  	opts.NumLevelZeroTablesStall = 2
    24  	db, err := Open(opts)
    25  	require.NoError(t, err)
    26  	val := make([]byte, 128)
    27  	// Insert 50 entries that will be kept.
    28  	for i := 0; i < 100; i++ {
    29  		err = db.Update(func(txn *Txn) error {
    30  			key := []byte(fmt.Sprintf("key%d", i))
    31  			return txn.Set(key, val)
    32  		})
    33  		require.NoError(t, err)
    34  	}
    35  	err = db.View(func(txn *Txn) error {
    36  		for i := 0; i < 100; i++ {
    37  			key := []byte(fmt.Sprintf("key%d", i))
    38  			item, err := txn.Get(key)
    39  			require.Nil(t, err)
    40  			v, err := item.Value()
    41  			require.Nil(t, err)
    42  			require.Len(t, val, len(v))
    43  			require.Equal(t, val, v, i)
    44  		}
    45  		return nil
    46  	})
    47  	require.Nil(t, err)
    48  	db.Close()
    49  	db, err = Open(opts)
    50  	err = db.View(func(txn *Txn) error {
    51  		for i := 0; i < 100; i++ {
    52  			key := []byte(fmt.Sprintf("key%d", i))
    53  			item, err := txn.Get(key)
    54  			require.Nil(t, err)
    55  			v, err := item.Value()
    56  			require.Equal(t, v, val)
    57  		}
    58  		return nil
    59  	})
    60  	require.Nil(t, err)
    61  	db.Close()
    62  }
    63  
    64  func TestBlobGC(t *testing.T) {
    65  	minCandidateValidSize = 4 * 1024
    66  	maxCandidateValidSize = minCandidateValidSize * 4
    67  	maxCandidateDiscardSize = 1024 * 1024
    68  	dir, err := ioutil.TempDir("", "badger")
    69  	require.NoError(t, err)
    70  	defer os.RemoveAll(dir)
    71  	opts := getTestOptions(dir)
    72  	opts.ValueThreshold = 20
    73  	opts.TableBuilderOptions.MaxTableSize = 6 * 1024
    74  	opts.MaxMemTableSize = 6 * 1024
    75  	opts.NumMemtables = 2
    76  	opts.NumLevelZeroTables = 1
    77  	opts.NumLevelZeroTablesStall = 2
    78  
    79  	var db *DB
    80  	expectedMap := make(map[string]string)
    81  	for c := 0; c < 10; c++ {
    82  		db, err = Open(opts)
    83  		require.NoError(t, err)
    84  		for i := 0; i < 100; i++ {
    85  			err = db.Update(func(txn *Txn) error {
    86  				key := []byte(fmt.Sprintf("key%d", rand.Intn(100)))
    87  				val := make([]byte, 128)
    88  				_, _ = rand.Read(val)
    89  				expectedMap[string(key)] = fmt.Sprintf("%x", val)
    90  				return txn.Set(key, val)
    91  			})
    92  			require.Nil(t, err)
    93  		}
    94  		validateValue(t, db, expectedMap)
    95  		require.Nil(t, db.Close())
    96  	}
    97  }
    98  
    99  func validateValue(t *testing.T, db *DB, expectedMap map[string]string) {
   100  	err := db.View(func(txn *Txn) error {
   101  		for expectedKey, expectedVal := range expectedMap {
   102  			item, err := txn.Get([]byte(expectedKey))
   103  			require.Nil(t, err)
   104  			val, err := item.Value()
   105  			require.Equal(t, expectedVal, fmt.Sprintf("%x", val), "%s", expectedKey)
   106  		}
   107  		return nil
   108  	})
   109  	require.Nil(t, err)
   110  }