github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/soliton/prefix_helper_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 soliton_test
    15  
    16  import (
    17  	"context"
    18  	"fmt"
    19  	"testing"
    20  
    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"
    26  	"github.com/whtcorpsinc/milevadb/soliton/testleak"
    27  )
    28  
    29  const (
    30  	startIndex = 0
    31  	testCount  = 12
    32  	testPow    = 10
    33  )
    34  
    35  func TestT(t *testing.T) {
    36  	CustomVerboseFlag = true
    37  	TestingT(t)
    38  }
    39  
    40  var _ = Suite(&testPrefixSuite{})
    41  
    42  type testPrefixSuite struct {
    43  	s ekv.CausetStorage
    44  }
    45  
    46  func (s *testPrefixSuite) SetUpSuite(c *C) {
    47  	testleak.BeforeTest()
    48  	causetstore, err := mockstore.NewMockStore()
    49  	c.Assert(err, IsNil)
    50  	s.s = causetstore
    51  
    52  }
    53  
    54  func (s *testPrefixSuite) TearDownSuite(c *C) {
    55  	err := s.s.Close()
    56  	c.Assert(err, IsNil)
    57  	testleak.AfterTest(c)()
    58  }
    59  
    60  func encodeInt(n int) []byte {
    61  	return []byte(fmt.Sprintf("%d", n))
    62  }
    63  
    64  type MockContext struct {
    65  	prefix int
    66  	values map[fmt.Stringer]interface{}
    67  	ekv.CausetStorage
    68  	txn ekv.Transaction
    69  }
    70  
    71  func (c *MockContext) SetValue(key fmt.Stringer, value interface{}) {
    72  	c.values[key] = value
    73  }
    74  
    75  func (c *MockContext) Value(key fmt.Stringer) interface{} {
    76  	value := c.values[key]
    77  	return value
    78  }
    79  
    80  func (c *MockContext) ClearValue(key fmt.Stringer) {
    81  	delete(c.values, key)
    82  }
    83  
    84  func (c *MockContext) GetTxn(forceNew bool) (ekv.Transaction, error) {
    85  	var err error
    86  	c.txn, err = c.Begin()
    87  	if err != nil {
    88  		return nil, err
    89  	}
    90  
    91  	return c.txn, nil
    92  }
    93  
    94  func (c *MockContext) fillTxn() error {
    95  	if c.txn == nil {
    96  		return nil
    97  	}
    98  
    99  	var err error
   100  	for i := startIndex; i < testCount; i++ {
   101  		val := encodeInt(i + (c.prefix * testPow))
   102  		err = c.txn.Set(val, val)
   103  		if err != nil {
   104  			return err
   105  		}
   106  	}
   107  
   108  	return nil
   109  }
   110  
   111  func (c *MockContext) CommitTxn() error {
   112  	if c.txn == nil {
   113  		return nil
   114  	}
   115  	return c.txn.Commit(context.Background())
   116  }
   117  
   118  func (s *testPrefixSuite) TestPrefix(c *C) {
   119  	ctx := &MockContext{10000000, make(map[fmt.Stringer]interface{}), s.s, nil}
   120  	ctx.fillTxn()
   121  	txn, err := ctx.GetTxn(false)
   122  	c.Assert(err, IsNil)
   123  	err = soliton.DelKeyWithPrefix(txn, encodeInt(ctx.prefix))
   124  	c.Assert(err, IsNil)
   125  	err = ctx.CommitTxn()
   126  	c.Assert(err, IsNil)
   127  
   128  	txn, err = s.s.Begin()
   129  	c.Assert(err, IsNil)
   130  	k := []byte("key100jfowi878230")
   131  	err = txn.Set(k, []byte(`val32dfaskli384757^*&%^`))
   132  	c.Assert(err, IsNil)
   133  	err = soliton.ScanMetaWithPrefix(txn, k, func(ekv.Key, []byte) bool {
   134  		return true
   135  	})
   136  	c.Assert(err, IsNil)
   137  	err = soliton.ScanMetaWithPrefix(txn, k, func(ekv.Key, []byte) bool {
   138  		return false
   139  	})
   140  	c.Assert(err, IsNil)
   141  	err = soliton.DelKeyWithPrefix(txn, []byte("key"))
   142  	c.Assert(err, IsNil)
   143  	_, err = txn.Get(context.TODO(), k)
   144  	c.Assert(terror.ErrorEqual(ekv.ErrNotExist, err), IsTrue)
   145  
   146  	err = txn.Commit(context.Background())
   147  	c.Assert(err, IsNil)
   148  }
   149  
   150  func (s *testPrefixSuite) TestPrefixFilter(c *C) {
   151  	rowKey := []byte(`test@#$%l(le[0]..prefix) 2uio`)
   152  	rowKey[8] = 0x00
   153  	rowKey[9] = 0x00
   154  	f := soliton.RowKeyPrefixFilter(rowKey)
   155  	b := f(append(rowKey, []byte("akjdf3*(34")...))
   156  	c.Assert(b, IsFalse)
   157  	buf := f([]byte("sjfkdlsaf"))
   158  	c.Assert(buf, IsTrue)
   159  }