github.com/turingchain2020/turingchain@v1.1.21/common/db/mvcc_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  	"fmt"
     9  	"io/ioutil"
    10  	"testing"
    11  
    12  	"github.com/turingchain2020/turingchain/common"
    13  	"github.com/turingchain2020/turingchain/types"
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  func TestPrefix(t *testing.T) {
    18  	assert.Equal(t, []byte(".-mvcc-."), mvccPrefix)
    19  	assert.Equal(t, []byte(".-mvcc-.m."), mvccMeta)
    20  	assert.Equal(t, []byte(".-mvcc-.d."), mvccData)
    21  }
    22  
    23  func TestMVCC(t *testing.T) {
    24  	m := getMVCC2()
    25  	_, ok := m.(MVCC)
    26  	assert.True(t, ok)
    27  }
    28  func TestPad(t *testing.T) {
    29  	assert.Equal(t, []byte("00000000000000000001"), pad(1))
    30  }
    31  
    32  func getMVCC2() MVCC {
    33  	return getMVCC()
    34  }
    35  
    36  func getMVCC() *MVCCHelper {
    37  	dir, err := ioutil.TempDir("", "goleveldb")
    38  	if err != nil {
    39  		panic(err)
    40  	}
    41  	leveldb, err := NewGoLevelDB("goleveldb", dir, 128)
    42  	if err != nil {
    43  		panic(err)
    44  	}
    45  	return NewMVCC(leveldb)
    46  }
    47  
    48  func closeMVCC(m *MVCCHelper) {
    49  	m.db.Close()
    50  }
    51  
    52  func TestSetVersion(t *testing.T) {
    53  	m := getMVCC()
    54  	defer m.db.Close()
    55  	hash := common.Sha256([]byte("1"))
    56  	err := m.SetVersion(hash, int64(1))
    57  	assert.Nil(t, err)
    58  	v, err := m.GetVersion(hash)
    59  	assert.Nil(t, err)
    60  	assert.Equal(t, int64(1), v)
    61  
    62  	err = m.DelVersion(hash)
    63  	assert.Nil(t, err)
    64  	_, err = m.GetVersion(hash)
    65  	assert.Equal(t, err, types.ErrNotFound)
    66  }
    67  
    68  func TestGetSaveKV(t *testing.T) {
    69  	m := getMVCC()
    70  	defer closeMVCC(m)
    71  	kv, _ := m.GetSaveKV([]byte("key"), []byte("value"), 1)
    72  	assert.Equal(t, []byte(".-mvcc-.d.key.00000000000000000001"), kv.GetKey())
    73  	assert.Equal(t, []byte("value"), kv.GetValue())
    74  }
    75  
    76  func TestGetDelKV(t *testing.T) {
    77  	m := getMVCC()
    78  	defer closeMVCC(m)
    79  	kv, _ := m.GetDelKV([]byte("key"), 1)
    80  	assert.Equal(t, []byte(".-mvcc-.d.key.00000000000000000001"), kv.GetKey())
    81  	assert.Equal(t, []byte(nil), kv.GetValue())
    82  }
    83  
    84  func TestVersionString(t *testing.T) {
    85  	s, err := getVersionString([]byte(".-mvcc-.d.key.00000000000000000001"))
    86  	assert.Nil(t, err)
    87  	assert.Equal(t, "00000000000000000001", s)
    88  
    89  	v, err := getVersion([]byte(".-mvcc-.d.key.00330721199901011111"))
    90  	assert.Nil(t, err)
    91  	assert.Equal(t, int64(330721199901011111), v)
    92  }
    93  
    94  func TestVersionSetAndGet(t *testing.T) {
    95  	m := getMVCC()
    96  	defer closeMVCC(m)
    97  	b, err := m.GetV([]byte("key"), 0)
    98  	assert.Equal(t, err, types.ErrNotFound)
    99  	assert.Nil(t, b)
   100  
   101  	//模拟这样的一个场景
   102  	//0,k0,v0
   103  	//1,k1,v1,k0,v01
   104  	//2,k2,v2,k0,v02
   105  	//3,k3,v3,k0,v03
   106  	//4,kv,v4
   107  	m.SetVersion(common.Sha256([]byte("0")), 0)
   108  	m.SetV([]byte("k0"), []byte("v0"), 0)
   109  	m.SetVersion(common.Sha256([]byte("1")), 1)
   110  	m.SetV([]byte("k1"), []byte("v1"), 1)
   111  	m.SetV([]byte("k0"), []byte("v01"), 1)
   112  	m.SetVersion(common.Sha256([]byte("2")), 2)
   113  	m.SetV([]byte("k2"), []byte("v2"), 2)
   114  	m.SetVersion(common.Sha256([]byte("3")), 3)
   115  	m.SetV([]byte("k3"), []byte("v3"), 3)
   116  	m.SetV([]byte("k0"), []byte("v03"), 3)
   117  	m.SetVersion(common.Sha256([]byte("4")), 4)
   118  	m.SetV([]byte("k4"), []byte("v4"), 4)
   119  
   120  	_, err = m.GetVersion(common.Sha256([]byte("5")))
   121  	assert.Equal(t, err, types.ErrNotFound)
   122  
   123  	version, err := m.GetVersion(common.Sha256([]byte("4")))
   124  	assert.Nil(t, err)
   125  	assert.Equal(t, version, int64(4))
   126  
   127  	v, err := m.GetV([]byte("k0"), version)
   128  	assert.Nil(t, err)
   129  	assert.Equal(t, v, []byte("v03"))
   130  
   131  	v, err = m.GetV([]byte("k4"), version)
   132  	assert.Nil(t, err)
   133  	assert.Equal(t, v, []byte("v4"))
   134  
   135  	v, err = m.GetV([]byte("k0"), 0)
   136  	assert.Nil(t, err)
   137  	assert.Equal(t, v, []byte("v0"))
   138  
   139  	v, err = m.GetV([]byte("k0"), 1)
   140  	assert.Nil(t, err)
   141  	assert.Equal(t, v, []byte("v01"))
   142  
   143  	v, err = m.GetV([]byte("k0"), 2)
   144  	assert.Nil(t, err)
   145  	assert.Equal(t, v, []byte("v01"))
   146  
   147  	v, err = m.GetV([]byte("k0"), 3)
   148  	assert.Nil(t, err)
   149  	assert.Equal(t, v, []byte("v03"))
   150  
   151  	m.Trash(3)
   152  	_, err = m.GetV([]byte("k0"), 0)
   153  	assert.Equal(t, err, types.ErrNotFound)
   154  
   155  	v, err = m.GetV([]byte("k3"), 4)
   156  	assert.Nil(t, err)
   157  	assert.Equal(t, v, []byte("v3"))
   158  }
   159  
   160  func hashN(n int) []byte {
   161  	s := fmt.Sprint(n)
   162  	return common.Sha256([]byte(s))
   163  }
   164  
   165  func genkv(n int) (kvlist []*types.KeyValue) {
   166  	for i := 0; i < n; i++ {
   167  		kvlist = append(kvlist, &types.KeyValue{Key: []byte(common.GetRandPrintString(10, 10)), Value: []byte(common.GetRandPrintString(10, 10))})
   168  	}
   169  	return kvlist
   170  }
   171  
   172  func TestAddDelMVCC(t *testing.T) {
   173  	m := getMVCC()
   174  	defer closeMVCC(m)
   175  	kvlist, err := m.AddMVCC(genkv(2), hashN(0), nil, 0)
   176  	assert.Nil(t, err)
   177  	for _, v := range kvlist {
   178  		m.db.Set(v.Key, v.Value)
   179  	}
   180  	_, err = m.AddMVCC(genkv(2), hashN(1), nil, 1)
   181  	assert.Equal(t, err, types.ErrPrevVersion)
   182  
   183  	kv1 := genkv(2)
   184  	kvlist, err = m.AddMVCC(kv1, hashN(1), hashN(0), 1)
   185  	assert.Nil(t, err)
   186  	for _, v := range kvlist {
   187  		m.db.Set(v.Key, v.Value)
   188  	}
   189  
   190  	_, err = m.AddMVCC(genkv(2), hashN(2), hashN(1), 1)
   191  	assert.Equal(t, err, types.ErrPrevVersion)
   192  
   193  	//hash 2 还不存在
   194  	_, err = m.AddMVCC(genkv(2), hashN(2), hashN(0), 3)
   195  	assert.Equal(t, err, types.ErrNotFound)
   196  
   197  	_, err = m.AddMVCC(genkv(2), hashN(2), hashN(3), 3)
   198  	assert.Equal(t, err, types.ErrNotFound)
   199  
   200  	maxv, err := m.GetMaxVersion()
   201  	assert.Nil(t, err)
   202  	assert.Equal(t, int64(1), maxv)
   203  
   204  	_, err = m.DelMVCC(hashN(2), 1, true)
   205  	assert.Equal(t, err, types.ErrNotFound)
   206  
   207  	_, err = m.DelMVCC(hashN(0), 0, true)
   208  	assert.Equal(t, err, types.ErrCanOnlyDelTopVersion)
   209  
   210  	kvlist, err = m.DelMVCC(hashN(1), 1, true)
   211  	assert.Nil(t, err)
   212  	m.PrintAll()
   213  	for _, v := range kvlist {
   214  		m.db.Delete(v.Key)
   215  	}
   216  	m.PrintAll()
   217  	maxv, err = m.GetMaxVersion()
   218  	assert.Nil(t, err)
   219  	assert.Equal(t, int64(0), maxv)
   220  
   221  }