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

     1  // Copyright Turing Corp. 2018 All Rights Reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package db
     6  
     7  import (
     8  	"io/ioutil"
     9  	"testing"
    10  
    11  	"fmt"
    12  	"os"
    13  
    14  	"github.com/turingchain2020/turingchain/types"
    15  	"github.com/stretchr/testify/assert"
    16  )
    17  
    18  func getMVCCIter() *MVCCIter {
    19  	dir, err := ioutil.TempDir("", "goleveldb")
    20  	if err != nil {
    21  		panic(err)
    22  	}
    23  	leveldb, err := NewGoLevelDB("goleveldb", dir, 128)
    24  	if err != nil {
    25  		panic(err)
    26  	}
    27  	return NewMVCCIter(leveldb)
    28  }
    29  
    30  func KeyValueList(kvlist ...[2]string) (kvs []*types.KeyValue) {
    31  	for _, list := range kvlist {
    32  		kvs = append(kvs, &types.KeyValue{Key: []byte(list[0]), Value: []byte(list[1])})
    33  	}
    34  	return kvs
    35  }
    36  
    37  func saveKVList(db DB, kvlist []*types.KeyValue) {
    38  	for _, v := range kvlist {
    39  		if v.Value == nil {
    40  			db.Delete(v.Key)
    41  		} else {
    42  			db.Set(v.Key, v.Value)
    43  		}
    44  	}
    45  }
    46  
    47  func TestAddDelMVCCIter(t *testing.T) {
    48  	dir, err := ioutil.TempDir("", "goleveldb")
    49  	assert.Nil(t, err)
    50  	defer os.RemoveAll(dir) // clean up
    51  	os.RemoveAll(dir)       //删除已存在目录
    52  
    53  	m := getMVCCIter()
    54  	defer m.db.Close()
    55  	kvlist, err := m.AddMVCC(KeyValueList([2]string{"0.0", "0/0"}, [2]string{"0.1", "0/1"}), hashN(0), nil, 0)
    56  	assert.Nil(t, err)
    57  	saveKVList(m.db, kvlist)
    58  
    59  	kvlist, err = m.AddMVCC(KeyValueList([2]string{"0.0", "0/1"}, [2]string{"1.1", "1/1"}), hashN(1), hashN(0), 1)
    60  	assert.Nil(t, err)
    61  	saveKVList(m.db, kvlist)
    62  
    63  	kvlist, err = m.AddMVCC(KeyValueList([2]string{"0.0", "0/2"}, [2]string{"2.1", "2/1"}), hashN(2), hashN(1), 2)
    64  	assert.Nil(t, err)
    65  	saveKVList(m.db, kvlist)
    66  
    67  	listhelper := NewListHelper(m)
    68  	values := listhelper.List(nil, nil, 100, 1)
    69  	assert.Equal(t, "0/2", string(values[0]))
    70  	assert.Equal(t, "0/1", string(values[1]))
    71  	assert.Equal(t, "1/1", string(values[2]))
    72  	assert.Equal(t, "2/1", string(values[3]))
    73  	//m.PrintAll()
    74  	//删除最新版本
    75  	kvlist, err = m.DelMVCC(hashN(2), 2, true)
    76  	assert.Nil(t, err)
    77  	saveKVList(m.db, kvlist)
    78  	values = listhelper.List(nil, nil, 100, 1)
    79  	assert.Equal(t, "0/1", string(values[0]))
    80  	assert.Equal(t, "0/1", string(values[1]))
    81  	assert.Equal(t, "1/1", string(values[2]))
    82  	//m.PrintAll()
    83  }
    84  
    85  func TestGetAllCoinsMVCCIter(t *testing.T) {
    86  	dir, err := ioutil.TempDir("", "goleveldb")
    87  	assert.Nil(t, err)
    88  	defer os.RemoveAll(dir) // clean up
    89  	os.RemoveAll(dir)       //删除已存在目录
    90  
    91  	m := getMVCCIter()
    92  	defer m.db.Close()
    93  	kvlist, err := m.AddMVCC(KeyValueList([2]string{"mavl-coins-trc-exec-16htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp", "1"}, [2]string{"mavl-coins-trc-16htvcBNSEA7fZhAdLJphDwQRQJaHpyHTq", "2"}), hashN(0), nil, 0)
    94  	assert.Nil(t, err)
    95  	saveKVList(m.db, kvlist)
    96  
    97  	kvlist, err = m.AddMVCC(KeyValueList([2]string{"mavl-coins-trc-16htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp", "3"}, [2]string{"mavl-coins-trc-16htvcBNSEA7fZhAdLJphDwQRQJaHpyHTs", "4"}), hashN(1), hashN(0), 1)
    98  	assert.Nil(t, err)
    99  	saveKVList(m.db, kvlist)
   100  
   101  	kvlist, err = m.AddMVCC(KeyValueList([2]string{"mavl-coins-trc-exec-16htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp", "5"}, [2]string{"mavl-coins-trc-16htvcBNSEA7fZhAdLJphDwQRQJaHpyHTt", "6"}), hashN(2), hashN(1), 2)
   102  	assert.Nil(t, err)
   103  	saveKVList(m.db, kvlist)
   104  
   105  	listhelper := NewListHelper(m)
   106  	fmt.Println("---case 1-1----")
   107  
   108  	values := listhelper.List([]byte("mavl-coins-trc-"), nil, 100, 1)
   109  	assert.Equal(t, "3", string(values[0]))
   110  	assert.Equal(t, "2", string(values[1]))
   111  	assert.Equal(t, "4", string(values[2]))
   112  	assert.Equal(t, "6", string(values[3]))
   113  	assert.Equal(t, "5", string(values[4]))
   114  
   115  	for i := 0; i < len(values); i++ {
   116  		fmt.Println(string(values[i]))
   117  	}
   118  
   119  	fmt.Println("---case 1-2----")
   120  	var matchValues [][]byte
   121  	listhelper.IteratorCallback([]byte("mavl-coins-trc-"), nil, 0, 1, func(key, value []byte) bool {
   122  		matchValues = append(matchValues, value)
   123  		return false
   124  	})
   125  	values = matchValues
   126  	assert.Equal(t, "3", string(values[0]))
   127  	assert.Equal(t, "2", string(values[1]))
   128  	assert.Equal(t, "4", string(values[2]))
   129  	assert.Equal(t, "6", string(values[3]))
   130  	assert.Equal(t, "5", string(values[4]))
   131  
   132  	for i := 0; i < len(values); i++ {
   133  		fmt.Println(string(values[i]))
   134  	}
   135  	fmt.Println("---case 2-1----")
   136  	//m.PrintAll()
   137  	//删除最新版本
   138  	kvlist, err = m.DelMVCC(hashN(2), 2, true)
   139  	assert.Nil(t, err)
   140  	saveKVList(m.db, kvlist)
   141  	values = listhelper.List(nil, nil, 0, 1)
   142  	assert.Equal(t, 4, len(values))
   143  	assert.Equal(t, "3", string(values[0]))
   144  	assert.Equal(t, "2", string(values[1]))
   145  	assert.Equal(t, "4", string(values[2]))
   146  	assert.Equal(t, "1", string(values[3]))
   147  
   148  	for i := 0; i < len(values); i++ {
   149  		fmt.Println(string(values[i]))
   150  	}
   151  	//m.PrintAll()
   152  	fmt.Println("---case 2-2----")
   153  	matchValues = nil
   154  	listhelper.IteratorCallback(([]byte("mavl-coins-trc-")), []byte("mavl-coins-trc-exec-"), 0, 1, func(key, value []byte) bool {
   155  		matchValues = append(matchValues, value)
   156  		return false
   157  	})
   158  	values = matchValues
   159  
   160  	for i := 0; i < len(values); i++ {
   161  		fmt.Println(string(values[i]))
   162  	}
   163  	assert.Equal(t, 3, len(values))
   164  	assert.Equal(t, "3", string(values[0]))
   165  	assert.Equal(t, "2", string(values[1]))
   166  	assert.Equal(t, "4", string(values[2]))
   167  
   168  	fmt.Println("---case 2-3----")
   169  	matchValues = nil
   170  	listhelper.IteratorCallback(([]byte("mavl-coins-trc-16htvcBNSEA7fZhAdLJphDwQRQJaHpyHTq")), []byte("mavl-coins-trc-exec-"), 0, 1, func(key, value []byte) bool {
   171  		matchValues = append(matchValues, value)
   172  		return false
   173  	})
   174  	values = matchValues
   175  
   176  	for i := 0; i < len(values); i++ {
   177  		fmt.Println(string(values[i]))
   178  	}
   179  	assert.Equal(t, 2, len(values))
   180  	//assert.Equal(t, "3", string(values[0]))
   181  	assert.Equal(t, "2", string(values[0]))
   182  	assert.Equal(t, "4", string(values[1]))
   183  }
   184  
   185  func TestSimpleMVCCLocalDB(t *testing.T) {
   186  	//use leveldb
   187  	//use localdb
   188  	db3, dir := newGoLevelDB(t)
   189  	defer os.RemoveAll(dir) // clean up
   190  	kvdb := NewLocalDB(db3, false)
   191  	m := NewSimpleMVCC(kvdb)
   192  
   193  	kvlist, err := m.AddMVCC(KeyValueList([2]string{"mavl-coins-trc-exec-16htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp", "1"}, [2]string{"mavl-coins-trc-16htvcBNSEA7fZhAdLJphDwQRQJaHpyHTq", "2"}), hashN(0), nil, 0)
   194  	assert.Nil(t, err)
   195  	setKVList(kvdb, kvlist)
   196  
   197  	kvlist, err = m.AddMVCC(KeyValueList([2]string{"mavl-coins-trc-16htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp", "3"}, [2]string{"mavl-coins-trc-16htvcBNSEA7fZhAdLJphDwQRQJaHpyHTs", "4"}), hashN(1), hashN(0), 1)
   198  	assert.Nil(t, err)
   199  	setKVList(kvdb, kvlist)
   200  
   201  	kvlist, err = m.AddMVCC(KeyValueList([2]string{"mavl-coins-trc-exec-16htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp", "5"}, [2]string{"mavl-coins-trc-16htvcBNSEA7fZhAdLJphDwQRQJaHpyHTt", "6"}), hashN(2), hashN(1), 2)
   202  	assert.Nil(t, err)
   203  	setKVList(kvdb, kvlist)
   204  
   205  	values, err := kvdb.List([]byte(".-mvcc-.d.mavl-coins-trc-"), nil, 100, 1)
   206  	assert.Nil(t, err)
   207  	assert.Equal(t, 6, len(values))
   208  	assert.Equal(t, "3", string(values[0]))
   209  	assert.Equal(t, "2", string(values[1]))
   210  	assert.Equal(t, "4", string(values[2]))
   211  	assert.Equal(t, "6", string(values[3]))
   212  	assert.Equal(t, "1", string(values[4]))
   213  	assert.Equal(t, "5", string(values[5]))
   214  
   215  	v, err := m.GetV([]byte("mavl-coins-trc-exec-16htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp"), 0)
   216  	assert.Nil(t, err)
   217  	assert.Equal(t, "1", string(v))
   218  
   219  	v, err = m.GetV([]byte("mavl-coins-trc-exec-16htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp"), 1)
   220  	assert.Nil(t, err)
   221  	assert.Equal(t, "1", string(v))
   222  
   223  	v, err = m.GetV([]byte("mavl-coins-trc-exec-16htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp"), 2)
   224  	assert.Nil(t, err)
   225  	assert.Equal(t, "5", string(v))
   226  
   227  	v, err = m.GetV([]byte("mavl-coins-trc-exec-16htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp"), 3)
   228  	assert.Nil(t, err)
   229  	assert.Equal(t, "5", string(v))
   230  
   231  }
   232  
   233  func setKVList(db KVDB, kvlist []*types.KeyValue) {
   234  	for _, v := range kvlist {
   235  		db.Set(v.Key, v.Value)
   236  	}
   237  }