github.com/turingchain2020/turingchain@v1.1.21/common/db/merge_iter_test.go (about)

     1  package db
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func newGoMemDB(t *testing.T) DB {
    13  	dir, err := ioutil.TempDir("", "gomemdb")
    14  	require.NoError(t, err)
    15  	memdb, err := NewGoMemDB("gomemdb", dir, 128)
    16  	require.NoError(t, err)
    17  	return memdb
    18  }
    19  
    20  func TestMergeIter(t *testing.T) {
    21  	db1 := newGoMemDB(t)
    22  	db2 := newGoMemDB(t)
    23  	db1.Set([]byte("1"), []byte("1"))
    24  	db2.Set([]byte("2"), []byte("2"))
    25  
    26  	//合并以后:
    27  	db := NewMergedIteratorDB([]IteratorDB{db1, db2})
    28  	it0 := NewListHelper(db)
    29  	list0 := it0.List(nil, nil, 100, 0)
    30  	assert.Equal(t, 2, len(list0))
    31  	assert.Equal(t, "2", string(list0[0]))
    32  	assert.Equal(t, "1", string(list0[1]))
    33  	/*
    34  		list0 = it0.List(nil, nil, 100, 1)
    35  		assert.Equal(t, 2, len(list0))
    36  		assert.Equal(t, "1", string(list0[0]))
    37  		assert.Equal(t, "2", string(list0[1]))
    38  	*/
    39  }
    40  
    41  func newGoLevelDB(t *testing.T) (DB, string) {
    42  	dir, err := ioutil.TempDir("", "goleveldb")
    43  	assert.Nil(t, err)
    44  	db, err := NewGoLevelDB("test", dir, 16)
    45  	assert.Nil(t, err)
    46  	return db, dir
    47  }
    48  
    49  func TestMergeIterSeek1(t *testing.T) {
    50  	db1 := newGoMemDB(t)
    51  	db1.Set([]byte("1"), []byte("1"))
    52  
    53  	it0 := NewListHelper(db1)
    54  	list0 := it0.List(nil, []byte("2"), 1, ListSeek)
    55  	assert.Equal(t, 2, len(list0))
    56  	assert.Equal(t, "1", string(list0[0]))
    57  }
    58  
    59  func TestMergeIterSeek(t *testing.T) {
    60  	db1 := newGoMemDB(t)
    61  	db2 := newGoMemDB(t)
    62  	db3, dir := newGoLevelDB(t)
    63  	defer os.RemoveAll(dir) // clean up
    64  	os.RemoveAll(dir)       //删除已存在目录
    65  	db1.Set([]byte("1"), []byte("1"))
    66  	db2.Set([]byte("3"), []byte("3"))
    67  	db3.Set([]byte("5"), []byte("5"))
    68  	//合并以后:
    69  	db := NewMergedIteratorDB([]IteratorDB{db1, db2, db3})
    70  	it0 := NewListHelper(db)
    71  	list0 := it0.List(nil, []byte("2"), 1, ListSeek)
    72  	assert.Equal(t, 2, len(list0))
    73  	assert.Equal(t, "1", string(list0[1]))
    74  
    75  	list0 = it0.List(nil, []byte("3"), 1, ListSeek)
    76  	assert.Equal(t, 2, len(list0))
    77  	assert.Equal(t, "3", string(list0[1]))
    78  }
    79  
    80  func TestMergeIterSeekPrefix(t *testing.T) {
    81  	db1 := newGoMemDB(t)
    82  	db2 := newGoMemDB(t)
    83  	db3, dir := newGoLevelDB(t)
    84  	defer os.RemoveAll(dir) // clean up
    85  	os.RemoveAll(dir)       //删除已存在目录
    86  	db1.Set([]byte("key1"), []byte("1"))
    87  	db2.Set([]byte("key3"), []byte("3"))
    88  	db3.Set([]byte("key5"), []byte("5"))
    89  	//合并以后:
    90  	db := NewMergedIteratorDB([]IteratorDB{db1, db2, db3})
    91  	it0 := NewListHelper(db)
    92  	list0 := it0.List([]byte("key"), []byte("key2"), 1, ListSeek)
    93  	assert.Equal(t, 2, len(list0))
    94  	assert.Equal(t, "1", string(list0[1]))
    95  
    96  	list0 = it0.List([]byte("key"), []byte("key3"), 1, ListSeek)
    97  	assert.Equal(t, 2, len(list0))
    98  	assert.Equal(t, "3", string(list0[1]))
    99  
   100  	list0 = it0.List([]byte("key"), []byte("key6"), 1, ListSeek)
   101  	assert.Equal(t, 2, len(list0))
   102  	assert.Equal(t, "5", string(list0[1]))
   103  }
   104  
   105  func TestMergeIterDup1(t *testing.T) {
   106  	db1 := newGoMemDB(t)
   107  	db2 := newGoMemDB(t)
   108  	db1.Set([]byte("1"), []byte("1"))
   109  	db2.Set([]byte("2"), []byte("2"))
   110  
   111  	//合并以后:
   112  	db := NewMergedIteratorDB([]IteratorDB{db1, db2})
   113  	it0 := NewListHelper(db)
   114  	//测试修改
   115  	db1.Set([]byte("2"), []byte("12"))
   116  	list0 := it0.List(nil, nil, 100, 0)
   117  	for k, v := range list0 {
   118  		println(k, string(v))
   119  	}
   120  	assert.Equal(t, 2, len(list0))
   121  	assert.Equal(t, "12", string(list0[0]))
   122  	assert.Equal(t, "1", string(list0[1]))
   123  }
   124  
   125  func TestMergeIterDup2(t *testing.T) {
   126  	db1 := newGoMemDB(t)
   127  	db2 := newGoMemDB(t)
   128  	db1.Set([]byte("key-1"), []byte("db1-key-1"))
   129  	db1.Set([]byte("key-3"), []byte("db1-key-3"))
   130  	db2.Set([]byte("key-2"), []byte("db2-key-2"))
   131  
   132  	//合并以后:
   133  	db := NewMergedIteratorDB([]IteratorDB{db1, db2})
   134  	it0 := NewListHelper(db)
   135  	//测试修改
   136  	db2.Set([]byte("key-3"), []byte("db2-key-3"))
   137  	list0 := it0.List(nil, nil, 100, 0)
   138  	for k, v := range list0 {
   139  		println(k, string(v))
   140  	}
   141  	assert.Equal(t, 3, len(list0))
   142  	assert.Equal(t, "db1-key-3", string(list0[0]))
   143  	assert.Equal(t, "db2-key-2", string(list0[1]))
   144  	assert.Equal(t, "db1-key-1", string(list0[2]))
   145  
   146  	list0 = it0.List(nil, nil, 100, 1)
   147  	for k, v := range list0 {
   148  		println(k, string(v))
   149  	}
   150  	assert.Equal(t, 3, len(list0))
   151  	assert.Equal(t, "db1-key-1", string(list0[0]))
   152  	assert.Equal(t, "db2-key-2", string(list0[1]))
   153  	assert.Equal(t, "db1-key-3", string(list0[2]))
   154  }
   155  
   156  func TestMergeIterDup3(t *testing.T) {
   157  	db1 := newGoMemDB(t)
   158  	db2 := newGoMemDB(t)
   159  	db1.Set([]byte("key-1"), []byte("db1-key-1"))
   160  	db1.Set([]byte("key-3"), []byte("db1-key-3"))
   161  	db2.Set([]byte("key-2"), []byte("db2-key-2"))
   162  
   163  	//合并以后:
   164  	db := NewMergedIteratorDB([]IteratorDB{db1, db2})
   165  	it0 := NewListHelper(db)
   166  	//测试修改
   167  	db1.Set([]byte("key-2"), []byte("db1-key-2"))
   168  	list0 := it0.List(nil, nil, 100, 0)
   169  	for k, v := range list0 {
   170  		println(k, string(v))
   171  	}
   172  	assert.Equal(t, 3, len(list0))
   173  	assert.Equal(t, "db1-key-3", string(list0[0]))
   174  	assert.Equal(t, "db1-key-2", string(list0[1]))
   175  	assert.Equal(t, "db1-key-1", string(list0[2]))
   176  
   177  	list0 = it0.List(nil, nil, 100, 1)
   178  	for k, v := range list0 {
   179  		println(k, string(v))
   180  	}
   181  	assert.Equal(t, 3, len(list0))
   182  	assert.Equal(t, "db1-key-1", string(list0[0]))
   183  	assert.Equal(t, "db1-key-2", string(list0[1]))
   184  	assert.Equal(t, "db1-key-3", string(list0[2]))
   185  }
   186  
   187  func TestMergeIter3(t *testing.T) {
   188  	db1 := newGoMemDB(t)
   189  	db2 := newGoMemDB(t)
   190  	db3 := newGoMemDB(t)
   191  	db3.Set([]byte("key-1"), []byte("db3-key-1"))
   192  	db3.Set([]byte("key-2"), []byte("db3-key-2"))
   193  	db3.Set([]byte("key-3"), []byte("db3-key-3"))
   194  
   195  	//合并以后:
   196  	db := NewMergedIteratorDB([]IteratorDB{db1, db2, db3})
   197  	it0 := NewListHelper(db)
   198  	list0 := it0.List([]byte("key-"), nil, 0, 0)
   199  	for k, v := range list0 {
   200  		println(k, string(v))
   201  	}
   202  	assert.Equal(t, 3, len(list0))
   203  	assert.Equal(t, "db3-key-3", string(list0[0]))
   204  	assert.Equal(t, "db3-key-2", string(list0[1]))
   205  	assert.Equal(t, "db3-key-1", string(list0[2]))
   206  }
   207  
   208  func TestMergeIter1(t *testing.T) {
   209  	db1 := newGoMemDB(t)
   210  	db2 := newGoMemDB(t)
   211  	db3 := newGoMemDB(t)
   212  	db1.Set([]byte("key-1"), []byte("db1-key-1"))
   213  	db1.Set([]byte("key-2"), []byte("db1-key-2"))
   214  	db1.Set([]byte("key-3"), []byte("db1-key-3"))
   215  
   216  	//合并以后:
   217  	db := NewMergedIteratorDB([]IteratorDB{db1, db2, db3})
   218  	it0 := NewListHelper(db)
   219  	list0 := it0.List(nil, nil, 100, 0)
   220  	for k, v := range list0 {
   221  		println(k, string(v))
   222  	}
   223  	assert.Equal(t, 3, len(list0))
   224  	assert.Equal(t, "db1-key-3", string(list0[0]))
   225  	assert.Equal(t, "db1-key-2", string(list0[1]))
   226  	assert.Equal(t, "db1-key-1", string(list0[2]))
   227  }
   228  
   229  func TestMergeIterSearch(t *testing.T) {
   230  	db1 := newGoMemDB(t)
   231  	db2 := newGoMemDB(t)
   232  	db1.Set([]byte("key-1"), []byte("db1-key-1"))
   233  	db1.Set([]byte("key-2"), []byte("db1-key-2"))
   234  	db2.Set([]byte("key-2"), []byte("db2-key-2"))
   235  	db2.Set([]byte("key-3"), []byte("db2-key-3"))
   236  	db2.Set([]byte("key-4"), []byte("db2-key-4"))
   237  
   238  	//合并以后:
   239  	db := NewMergedIteratorDB([]IteratorDB{db1, db2})
   240  	it0 := NewListHelper(db)
   241  	list0 := it0.List([]byte("key-"), []byte("key-2"), 100, 0)
   242  	for k, v := range list0 {
   243  		println(k, string(v))
   244  	}
   245  	assert.Equal(t, 1, len(list0))
   246  	assert.Equal(t, "db1-key-1", string(list0[0]))
   247  
   248  	list0 = it0.List([]byte("key-"), []byte("key-2"), 100, 1)
   249  	for k, v := range list0 {
   250  		println(k, string(v))
   251  	}
   252  	assert.Equal(t, 2, len(list0))
   253  	assert.Equal(t, "db2-key-3", string(list0[0]))
   254  	assert.Equal(t, "db2-key-4", string(list0[1]))
   255  }
   256  
   257  func TestIterSearch(t *testing.T) {
   258  	db1 := newGoMemDB(t)
   259  	defer db1.Close()
   260  	db1.Set([]byte("key-1"), []byte("db1-key-1"))
   261  	db1.Set([]byte("key-2"), []byte("db2-key-2"))
   262  	db1.Set([]byte("key-2"), []byte("db1-key-2"))
   263  	db1.Set([]byte("key-3"), []byte("db2-key-3"))
   264  	db1.Set([]byte("key-4"), []byte("db2-key-4"))
   265  
   266  	it0 := NewListHelper(db1)
   267  	list0 := it0.List([]byte("key-"), []byte("key-2"), 100, 0)
   268  	for k, v := range list0 {
   269  		println(k, string(v))
   270  	}
   271  	assert.Equal(t, 1, len(list0))
   272  	assert.Equal(t, "db1-key-1", string(list0[0]))
   273  
   274  	list0 = it0.List([]byte("key-"), []byte("key-2"), 100, 1)
   275  	for k, v := range list0 {
   276  		println(k, string(v))
   277  	}
   278  	assert.Equal(t, 2, len(list0))
   279  	assert.Equal(t, "db2-key-3", string(list0[0]))
   280  	assert.Equal(t, "db2-key-4", string(list0[1]))
   281  }