github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/causetstore/petri/acyclic/structure/structure_test.go (about)

     1  // Copyright 2020 WHTCORPS INC, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package structure_test
    15  
    16  import (
    17  	"context"
    18  	"testing"
    19  
    20  	"github.com/whtcorpsinc/BerolinaSQL/allegrosql"
    21  	"github.com/whtcorpsinc/BerolinaSQL/terror"
    22  	. "github.com/whtcorpsinc/check"
    23  	"github.com/whtcorpsinc/milevadb/causetstore/mockstore"
    24  	"github.com/whtcorpsinc/milevadb/ekv"
    25  	"github.com/whtcorpsinc/milevadb/soliton/testleak"
    26  	"github.com/whtcorpsinc/milevadb/structure"
    27  )
    28  
    29  func TestTxStructure(t *testing.T) {
    30  	CustomVerboseFlag = true
    31  	TestingT(t)
    32  }
    33  
    34  var _ = Suite(&testTxStructureSuite{})
    35  
    36  type testTxStructureSuite struct {
    37  	causetstore ekv.CausetStorage
    38  }
    39  
    40  func (s *testTxStructureSuite) SetUpSuite(c *C) {
    41  	testleak.BeforeTest()
    42  	causetstore, err := mockstore.NewMockStore()
    43  	c.Assert(err, IsNil)
    44  	s.causetstore = causetstore
    45  }
    46  
    47  func (s *testTxStructureSuite) TearDownSuite(c *C) {
    48  	err := s.causetstore.Close()
    49  	c.Assert(err, IsNil)
    50  	testleak.AfterTest(c)()
    51  }
    52  
    53  func (s *testTxStructureSuite) TestString(c *C) {
    54  	txn, err := s.causetstore.Begin()
    55  	c.Assert(err, IsNil)
    56  	defer txn.Rollback()
    57  
    58  	tx := structure.NewStructure(txn, txn, []byte{0x00})
    59  
    60  	key := []byte("a")
    61  	value := []byte("1")
    62  	err = tx.Set(key, value)
    63  	c.Assert(err, IsNil)
    64  
    65  	v, err := tx.Get(key)
    66  	c.Assert(err, IsNil)
    67  	c.Assert(v, DeepEquals, value)
    68  
    69  	n, err := tx.Inc(key, 1)
    70  	c.Assert(err, IsNil)
    71  	c.Assert(n, Equals, int64(2))
    72  
    73  	v, err = tx.Get(key)
    74  	c.Assert(err, IsNil)
    75  	c.Assert(v, DeepEquals, []byte("2"))
    76  
    77  	n, err = tx.GetInt64(key)
    78  	c.Assert(err, IsNil)
    79  	c.Assert(n, Equals, int64(2))
    80  
    81  	err = tx.Clear(key)
    82  	c.Assert(err, IsNil)
    83  
    84  	v, err = tx.Get(key)
    85  	c.Assert(err, IsNil)
    86  	c.Assert(v, IsNil)
    87  
    88  	tx1 := structure.NewStructure(txn, nil, []byte{0x01})
    89  	err = tx1.Set(key, value)
    90  	c.Assert(err, NotNil)
    91  
    92  	_, err = tx1.Inc(key, 1)
    93  	c.Assert(err, NotNil)
    94  
    95  	err = tx1.Clear(key)
    96  	c.Assert(err, NotNil)
    97  
    98  	err = txn.Commit(context.Background())
    99  	c.Assert(err, IsNil)
   100  }
   101  
   102  func (s *testTxStructureSuite) TestList(c *C) {
   103  	txn, err := s.causetstore.Begin()
   104  	c.Assert(err, IsNil)
   105  	defer txn.Rollback()
   106  
   107  	tx := structure.NewStructure(txn, txn, []byte{0x00})
   108  
   109  	key := []byte("a")
   110  	err = tx.LPush(key, []byte("3"), []byte("2"), []byte("1"))
   111  	c.Assert(err, IsNil)
   112  
   113  	// Test LGetAll.
   114  	err = tx.LPush(key, []byte("11"))
   115  	c.Assert(err, IsNil)
   116  	values, err := tx.LGetAll(key)
   117  	c.Assert(err, IsNil)
   118  	c.Assert(values, DeepEquals, [][]byte{[]byte("3"), []byte("2"), []byte("1"), []byte("11")})
   119  	value, err := tx.LPop(key)
   120  	c.Assert(err, IsNil)
   121  	c.Assert(value, DeepEquals, []byte("11"))
   122  
   123  	l, err := tx.LLen(key)
   124  	c.Assert(err, IsNil)
   125  	c.Assert(l, Equals, int64(3))
   126  
   127  	value, err = tx.LIndex(key, 1)
   128  	c.Assert(err, IsNil)
   129  	c.Assert(value, DeepEquals, []byte("2"))
   130  
   131  	err = tx.LSet(key, 1, []byte("4"))
   132  	c.Assert(err, IsNil)
   133  
   134  	value, err = tx.LIndex(key, 1)
   135  	c.Assert(err, IsNil)
   136  	c.Assert(value, DeepEquals, []byte("4"))
   137  
   138  	err = tx.LSet(key, 1, []byte("2"))
   139  	c.Assert(err, IsNil)
   140  
   141  	err = tx.LSet(key, 100, []byte("2"))
   142  	c.Assert(err, NotNil)
   143  
   144  	value, err = tx.LIndex(key, -1)
   145  	c.Assert(err, IsNil)
   146  	c.Assert(value, DeepEquals, []byte("3"))
   147  
   148  	value, err = tx.LPop(key)
   149  	c.Assert(err, IsNil)
   150  	c.Assert(value, DeepEquals, []byte("1"))
   151  
   152  	l, err = tx.LLen(key)
   153  	c.Assert(err, IsNil)
   154  	c.Assert(l, Equals, int64(2))
   155  
   156  	err = tx.RPush(key, []byte("4"))
   157  	c.Assert(err, IsNil)
   158  
   159  	l, err = tx.LLen(key)
   160  	c.Assert(err, IsNil)
   161  	c.Assert(l, Equals, int64(3))
   162  
   163  	value, err = tx.LIndex(key, -1)
   164  	c.Assert(err, IsNil)
   165  	c.Assert(value, DeepEquals, []byte("4"))
   166  
   167  	value, err = tx.RPop(key)
   168  	c.Assert(err, IsNil)
   169  	c.Assert(value, DeepEquals, []byte("4"))
   170  
   171  	value, err = tx.RPop(key)
   172  	c.Assert(err, IsNil)
   173  	c.Assert(value, DeepEquals, []byte("3"))
   174  
   175  	value, err = tx.RPop(key)
   176  	c.Assert(err, IsNil)
   177  	c.Assert(value, DeepEquals, []byte("2"))
   178  
   179  	l, err = tx.LLen(key)
   180  	c.Assert(err, IsNil)
   181  	c.Assert(l, Equals, int64(0))
   182  
   183  	err = tx.LPush(key, []byte("1"))
   184  	c.Assert(err, IsNil)
   185  
   186  	err = tx.LClear(key)
   187  	c.Assert(err, IsNil)
   188  
   189  	l, err = tx.LLen(key)
   190  	c.Assert(err, IsNil)
   191  	c.Assert(l, Equals, int64(0))
   192  
   193  	tx1 := structure.NewStructure(txn, nil, []byte{0x01})
   194  	err = tx1.LPush(key, []byte("1"))
   195  	c.Assert(err, NotNil)
   196  
   197  	_, err = tx1.RPop(key)
   198  	c.Assert(err, NotNil)
   199  
   200  	err = tx1.LSet(key, 1, []byte("2"))
   201  	c.Assert(err, NotNil)
   202  
   203  	err = tx1.LClear(key)
   204  	c.Assert(err, NotNil)
   205  
   206  	err = txn.Commit(context.Background())
   207  	c.Assert(err, IsNil)
   208  }
   209  
   210  func (s *testTxStructureSuite) TestHash(c *C) {
   211  	txn, err := s.causetstore.Begin()
   212  	c.Assert(err, IsNil)
   213  	defer txn.Rollback()
   214  
   215  	tx := structure.NewStructure(txn, txn, []byte{0x00})
   216  
   217  	key := []byte("a")
   218  
   219  	tx.EncodeHashAutoIDKeyValue(key, key, 5)
   220  
   221  	err = tx.HSet(key, []byte("1"), []byte("1"))
   222  	c.Assert(err, IsNil)
   223  
   224  	err = tx.HSet(key, []byte("2"), []byte("2"))
   225  	c.Assert(err, IsNil)
   226  
   227  	l, err := tx.HLen(key)
   228  	c.Assert(err, IsNil)
   229  	c.Assert(l, Equals, int64(2))
   230  
   231  	value, err := tx.HGet(key, []byte("1"))
   232  	c.Assert(err, IsNil)
   233  	c.Assert(value, DeepEquals, []byte("1"))
   234  
   235  	value, err = tx.HGet(key, []byte("fake"))
   236  	c.Assert(err, IsNil)
   237  	c.Assert(value, IsNil)
   238  
   239  	keys, err := tx.HKeys(key)
   240  	c.Assert(err, IsNil)
   241  	c.Assert(keys, DeepEquals, [][]byte{[]byte("1"), []byte("2")})
   242  
   243  	res, err := tx.HGetAll(key)
   244  	c.Assert(err, IsNil)
   245  	c.Assert(res, DeepEquals, []structure.HashPair{
   246  		{Field: []byte("1"), Value: []byte("1")},
   247  		{Field: []byte("2"), Value: []byte("2")}})
   248  
   249  	res, err = tx.HGetLastN(key, 1)
   250  	c.Assert(err, IsNil)
   251  	c.Assert(res, DeepEquals, []structure.HashPair{
   252  		{Field: []byte("2"), Value: []byte("2")}})
   253  
   254  	res, err = tx.HGetLastN(key, 2)
   255  	c.Assert(err, IsNil)
   256  	c.Assert(res, DeepEquals, []structure.HashPair{
   257  		{Field: []byte("2"), Value: []byte("2")},
   258  		{Field: []byte("1"), Value: []byte("1")}})
   259  
   260  	err = tx.HDel(key, []byte("1"))
   261  	c.Assert(err, IsNil)
   262  
   263  	value, err = tx.HGet(key, []byte("1"))
   264  	c.Assert(err, IsNil)
   265  	c.Assert(value, IsNil)
   266  
   267  	l, err = tx.HLen(key)
   268  	c.Assert(err, IsNil)
   269  	c.Assert(l, Equals, int64(1))
   270  
   271  	n, err := tx.HInc(key, []byte("1"), 1)
   272  	c.Assert(err, IsNil)
   273  	c.Assert(n, Equals, int64(1))
   274  
   275  	l, err = tx.HLen(key)
   276  	c.Assert(err, IsNil)
   277  	c.Assert(l, Equals, int64(2))
   278  
   279  	// Test set new value which equals to old value.
   280  	value, err = tx.HGet(key, []byte("1"))
   281  	c.Assert(err, IsNil)
   282  	c.Assert(value, DeepEquals, []byte("1"))
   283  
   284  	err = tx.HSet(key, []byte("1"), []byte("1"))
   285  	c.Assert(err, IsNil)
   286  
   287  	value, err = tx.HGet(key, []byte("1"))
   288  	c.Assert(err, IsNil)
   289  	c.Assert(value, DeepEquals, []byte("1"))
   290  
   291  	l, err = tx.HLen(key)
   292  	c.Assert(err, IsNil)
   293  	c.Assert(l, Equals, int64(2))
   294  
   295  	n, err = tx.HInc(key, []byte("1"), 1)
   296  	c.Assert(err, IsNil)
   297  	c.Assert(n, Equals, int64(2))
   298  
   299  	l, err = tx.HLen(key)
   300  	c.Assert(err, IsNil)
   301  	c.Assert(l, Equals, int64(2))
   302  
   303  	n, err = tx.HInc(key, []byte("1"), 1)
   304  	c.Assert(err, IsNil)
   305  	c.Assert(n, Equals, int64(3))
   306  
   307  	l, err = tx.HLen(key)
   308  	c.Assert(err, IsNil)
   309  	c.Assert(l, Equals, int64(2))
   310  
   311  	n, err = tx.HGetInt64(key, []byte("1"))
   312  	c.Assert(err, IsNil)
   313  	c.Assert(n, Equals, int64(3))
   314  
   315  	l, err = tx.HLen(key)
   316  	c.Assert(err, IsNil)
   317  	c.Assert(l, Equals, int64(2))
   318  
   319  	err = tx.HClear(key)
   320  	c.Assert(err, IsNil)
   321  
   322  	l, err = tx.HLen(key)
   323  	c.Assert(err, IsNil)
   324  	c.Assert(l, Equals, int64(0))
   325  
   326  	err = tx.HDel(key, []byte("fake_key"))
   327  	c.Assert(err, IsNil)
   328  
   329  	// Test set nil value.
   330  	value, err = tx.HGet(key, []byte("nil_key"))
   331  	c.Assert(err, IsNil)
   332  	c.Assert(value, IsNil)
   333  
   334  	l, err = tx.HLen(key)
   335  	c.Assert(err, IsNil)
   336  	c.Assert(l, Equals, int64(0))
   337  
   338  	err = tx.HSet(key, []byte("nil_key"), nil)
   339  	c.Assert(err, IsNil)
   340  
   341  	l, err = tx.HLen(key)
   342  	c.Assert(err, IsNil)
   343  	c.Assert(l, Equals, int64(0))
   344  
   345  	err = tx.HSet(key, []byte("nil_key"), []byte("1"))
   346  	c.Assert(err, IsNil)
   347  
   348  	l, err = tx.HLen(key)
   349  	c.Assert(err, IsNil)
   350  	c.Assert(l, Equals, int64(1))
   351  
   352  	value, err = tx.HGet(key, []byte("nil_key"))
   353  	c.Assert(err, IsNil)
   354  	c.Assert(value, DeepEquals, []byte("1"))
   355  
   356  	err = tx.HSet(key, []byte("nil_key"), nil)
   357  	c.Assert(err, NotNil)
   358  
   359  	l, err = tx.HLen(key)
   360  	c.Assert(err, IsNil)
   361  	c.Assert(l, Equals, int64(1))
   362  
   363  	value, err = tx.HGet(key, []byte("nil_key"))
   364  	c.Assert(err, IsNil)
   365  	c.Assert(value, DeepEquals, []byte("1"))
   366  
   367  	err = tx.HSet(key, []byte("nil_key"), []byte("2"))
   368  	c.Assert(err, IsNil)
   369  
   370  	l, err = tx.HLen(key)
   371  	c.Assert(err, IsNil)
   372  	c.Assert(l, Equals, int64(1))
   373  
   374  	value, err = tx.HGet(key, []byte("nil_key"))
   375  	c.Assert(err, IsNil)
   376  	c.Assert(value, DeepEquals, []byte("2"))
   377  
   378  	tx1 := structure.NewStructure(txn, nil, []byte{0x01})
   379  	_, err = tx1.HInc(key, []byte("1"), 1)
   380  	c.Assert(err, NotNil)
   381  
   382  	err = tx1.HDel(key, []byte("1"))
   383  	c.Assert(err, NotNil)
   384  
   385  	err = txn.Commit(context.Background())
   386  	c.Assert(err, IsNil)
   387  
   388  	err = ekv.RunInNewTxn(s.causetstore, false, func(txn ekv.Transaction) error {
   389  		t := structure.NewStructure(txn, txn, []byte{0x00})
   390  		err = t.Set(key, []byte("abc"))
   391  		c.Assert(err, IsNil)
   392  
   393  		value, err = t.Get(key)
   394  		c.Assert(err, IsNil)
   395  		c.Assert(value, DeepEquals, []byte("abc"))
   396  		return nil
   397  	})
   398  	c.Assert(err, IsNil)
   399  }
   400  
   401  func (*testTxStructureSuite) TestError(c *C) {
   402  	ekvErrs := []*terror.Error{
   403  		structure.ErrInvalidHashKeyFlag,
   404  		structure.ErrInvalidListIndex,
   405  		structure.ErrInvalidListMetaData,
   406  		structure.ErrWriteOnSnapshot,
   407  	}
   408  	for _, err := range ekvErrs {
   409  		code := terror.ToALLEGROSQLError(err).Code
   410  		c.Assert(code != allegrosql.ErrUnknown && code == uint16(err.Code()), IsTrue, Commentf("err: %v", err))
   411  	}
   412  }