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

     1  package rosedb
     2  
     3  import (
     4  	"github.com/stretchr/testify/assert"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  )
     9  
    10  func TestDiscard_listenUpdates(t *testing.T) {
    11  	path := filepath.Join("/tmp", "rosedb")
    12  	opts := DefaultOptions(path)
    13  	db, err := Open(opts)
    14  	defer destroyDB(db)
    15  	assert.Nil(t, err)
    16  
    17  	writeCount := 600000
    18  	for i := 0; i < writeCount; i++ {
    19  		err := db.Set(GetKey(i), GetValue16B())
    20  		assert.Nil(t, err)
    21  	}
    22  	// delete
    23  	for i := 0; i < 300000; i++ {
    24  		err := db.Delete(GetKey(i))
    25  		assert.Nil(t, err)
    26  	}
    27  
    28  	ccl, err := db.discards[String].getCCL(10, 0.001)
    29  	assert.Nil(t, err)
    30  	assert.Equal(t, len(ccl), 1)
    31  }
    32  
    33  func TestDiscard_newDiscard(t *testing.T) {
    34  	t.Run("init", func(t *testing.T) {
    35  		path := filepath.Join("/tmp", "rosedb-discard")
    36  		_ = os.MkdirAll(path, os.ModePerm)
    37  		dis, err := newDiscard(path, discardFileName, 4096)
    38  		assert.Nil(t, err)
    39  		defer func() {
    40  			_ = dis.file.Close()
    41  			_ = os.RemoveAll(path)
    42  		}()
    43  
    44  		assert.Equal(t, len(dis.freeList), 682)
    45  		assert.Equal(t, len(dis.location), 0)
    46  	})
    47  
    48  	t.Run("with-data", func(t *testing.T) {
    49  		path := filepath.Join("/tmp", "rosedb-discard")
    50  		_ = os.MkdirAll(path, os.ModePerm)
    51  		dis, err := newDiscard(path, discardFileName, 4096)
    52  		assert.Nil(t, err)
    53  		defer func() {
    54  			_ = dis.file.Close()
    55  			_ = os.RemoveAll(path)
    56  		}()
    57  
    58  		for i := 1; i < 300; i = i * 5 {
    59  			dis.setTotal(uint32(i), 223)
    60  			dis.incrDiscard(uint32(i), i*10)
    61  		}
    62  
    63  		assert.Equal(t, len(dis.freeList), 678)
    64  		assert.Equal(t, len(dis.location), 4)
    65  
    66  		// reopen
    67  		dis2, err := newDiscard(path, discardFileName, 4096)
    68  		defer func() {
    69  			_ = dis2.file.Close()
    70  		}()
    71  		assert.Nil(t, err)
    72  		assert.Equal(t, len(dis2.freeList), 678)
    73  		assert.Equal(t, len(dis2.location), 4)
    74  	})
    75  }
    76  
    77  func TestDiscard_setTotal(t *testing.T) {
    78  	path := filepath.Join("/tmp", "rosedb-discard")
    79  	_ = os.MkdirAll(path, os.ModePerm)
    80  	dis, err := newDiscard(path, discardFileName, 4096)
    81  	assert.Nil(t, err)
    82  	defer func() {
    83  		_ = dis.file.Close()
    84  		_ = os.RemoveAll(path)
    85  	}()
    86  
    87  	type args struct {
    88  		fid       uint32
    89  		totalSize int
    90  	}
    91  	tests := []struct {
    92  		name string
    93  		dis  *discard
    94  		args args
    95  	}{
    96  		{
    97  			"zero", dis, args{0, 10},
    98  		},
    99  		{
   100  			"normal", dis, args{334, 123224},
   101  		},
   102  		{
   103  			"set-again-1", dis, args{194, 100},
   104  		},
   105  		{
   106  			"set-again-2", dis, args{194, 150},
   107  		},
   108  	}
   109  	for _, tt := range tests {
   110  		t.Run(tt.name, func(t *testing.T) {
   111  			tt.dis.setTotal(tt.args.fid, uint32(tt.args.totalSize))
   112  		})
   113  	}
   114  }
   115  
   116  func TestDiscard_clear(t *testing.T) {
   117  	path := filepath.Join("/tmp", "rosedb-discard")
   118  	_ = os.MkdirAll(path, os.ModePerm)
   119  	dis, err := newDiscard(path, discardFileName, 4096)
   120  	assert.Nil(t, err)
   121  	defer func() {
   122  		_ = dis.file.Close()
   123  		_ = os.RemoveAll(path)
   124  	}()
   125  
   126  	for i := 0; i < 682; i++ {
   127  		dis.setTotal(uint32(i), uint32(i+100))
   128  		dis.incrDiscard(uint32(i), i+10)
   129  	}
   130  
   131  	type args struct {
   132  		fid uint32
   133  	}
   134  	tests := []struct {
   135  		name string
   136  		dis  *discard
   137  		args args
   138  	}{
   139  		{
   140  			"0", dis, args{0},
   141  		},
   142  		{
   143  			"33", dis, args{33},
   144  		},
   145  		{
   146  			"198", dis, args{198},
   147  		},
   148  		{
   149  			"340", dis, args{340},
   150  		},
   151  		{
   152  			"680", dis, args{680},
   153  		},
   154  	}
   155  	for _, tt := range tests {
   156  		t.Run(tt.name, func(t *testing.T) {
   157  			tt.dis.clear(tt.args.fid)
   158  		})
   159  	}
   160  }
   161  
   162  func TestDiscard_incrDiscard(t *testing.T) {
   163  	path := filepath.Join("/tmp", "rosedb-discard")
   164  	_ = os.MkdirAll(path, os.ModePerm)
   165  	dis, err := newDiscard(path, discardFileName, 4096)
   166  	assert.Nil(t, err)
   167  	defer func() {
   168  		_ = dis.file.Close()
   169  		_ = os.RemoveAll(path)
   170  	}()
   171  
   172  	for i := 1; i < 600; i = i * 5 {
   173  		dis.setTotal(uint32(i-1), uint32(i+1000))
   174  	}
   175  	for i := 1; i < 600; i = i * 5 {
   176  		dis.incrDiscard(uint32(i-1), i+100)
   177  	}
   178  
   179  	ccl, err := dis.getCCL(10, 0.0000001)
   180  	assert.Nil(t, err)
   181  	assert.Equal(t, len(ccl), 4)
   182  }
   183  
   184  func TestDiscard_getCCL(t *testing.T) {
   185  	path := filepath.Join("/tmp", "rosedb-discard")
   186  	_ = os.MkdirAll(path, os.ModePerm)
   187  	dis, err := newDiscard(path, discardFileName, 4096)
   188  	assert.Nil(t, err)
   189  	defer func() {
   190  		_ = dis.file.Close()
   191  		_ = os.RemoveAll(path)
   192  	}()
   193  
   194  	for i := 1; i < 2000; i = i * 5 {
   195  		dis.setTotal(uint32(i-1), uint32(i+1000))
   196  	}
   197  	for i := 1; i < 2000; i = i * 5 {
   198  		dis.incrDiscard(uint32(i-1), i+100)
   199  	}
   200  
   201  	t.Run("normal", func(t *testing.T) {
   202  		ccl, err := dis.getCCL(624, 0.0000001)
   203  		assert.Nil(t, err)
   204  		assert.Equal(t, len(ccl), 4)
   205  	})
   206  
   207  	t.Run("filter-some", func(t *testing.T) {
   208  		ccl, err := dis.getCCL(100, 0.2)
   209  		assert.Nil(t, err)
   210  		assert.Equal(t, len(ccl), 2)
   211  	})
   212  	t.Run("clear and get", func(t *testing.T) {
   213  		dis.clear(124)
   214  		ccl, err := dis.getCCL(100, 0.0001)
   215  		assert.Nil(t, err)
   216  		assert.Equal(t, len(ccl), 4)
   217  	})
   218  }