github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/causetstore/milevadb-server/einsteindb/scan_mock_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 einsteindb
    15  
    16  import (
    17  	"context"
    18  
    19  	. "github.com/whtcorpsinc/check"
    20  	"github.com/whtcorpsinc/milevadb/ekv"
    21  )
    22  
    23  type testScanMockSuite struct {
    24  	OneByOneSuite
    25  }
    26  
    27  var _ = Suite(&testScanMockSuite{})
    28  
    29  func (s *testScanMockSuite) TestScanMultipleRegions(c *C) {
    30  	causetstore := NewTestStore(c).(*einsteindbStore)
    31  	defer causetstore.Close()
    32  
    33  	txn, err := causetstore.Begin()
    34  	c.Assert(err, IsNil)
    35  	for ch := byte('a'); ch <= byte('z'); ch++ {
    36  		err = txn.Set([]byte{ch}, []byte{ch})
    37  		c.Assert(err, IsNil)
    38  	}
    39  	err = txn.Commit(context.Background())
    40  	c.Assert(err, IsNil)
    41  
    42  	txn, err = causetstore.Begin()
    43  	c.Assert(err, IsNil)
    44  	snapshot := newEinsteinDBSnapshot(causetstore, ekv.Version{Ver: txn.StartTS()}, 0)
    45  	scanner, err := newScanner(snapshot, []byte("a"), nil, 10, false)
    46  	c.Assert(err, IsNil)
    47  	for ch := byte('a'); ch <= byte('z'); ch++ {
    48  		c.Assert([]byte{ch}, BytesEquals, []byte(scanner.Key()))
    49  		c.Assert(scanner.Next(), IsNil)
    50  	}
    51  	c.Assert(scanner.Valid(), IsFalse)
    52  
    53  	scanner, err = newScanner(snapshot, []byte("a"), []byte("i"), 10, false)
    54  	c.Assert(err, IsNil)
    55  	for ch := byte('a'); ch <= byte('h'); ch++ {
    56  		c.Assert([]byte{ch}, BytesEquals, []byte(scanner.Key()))
    57  		c.Assert(scanner.Next(), IsNil)
    58  	}
    59  	c.Assert(scanner.Valid(), IsFalse)
    60  }
    61  
    62  func (s *testScanMockSuite) TestReverseScan(c *C) {
    63  	causetstore := NewTestStore(c).(*einsteindbStore)
    64  	defer causetstore.Close()
    65  
    66  	txn, err := causetstore.Begin()
    67  	c.Assert(err, IsNil)
    68  	for ch := byte('a'); ch <= byte('z'); ch++ {
    69  		err = txn.Set([]byte{ch}, []byte{ch})
    70  		c.Assert(err, IsNil)
    71  	}
    72  	err = txn.Commit(context.Background())
    73  	c.Assert(err, IsNil)
    74  
    75  	txn, err = causetstore.Begin()
    76  	c.Assert(err, IsNil)
    77  	snapshot := newEinsteinDBSnapshot(causetstore, ekv.Version{Ver: txn.StartTS()}, 0)
    78  	scanner, err := newScanner(snapshot, nil, []byte("z"), 10, true)
    79  	c.Assert(err, IsNil)
    80  	for ch := byte('y'); ch >= byte('a'); ch-- {
    81  		c.Assert(string([]byte{ch}), Equals, string(scanner.Key()))
    82  		c.Assert(scanner.Next(), IsNil)
    83  	}
    84  	c.Assert(scanner.Valid(), IsFalse)
    85  
    86  	scanner, err = newScanner(snapshot, []byte("a"), []byte("i"), 10, true)
    87  	c.Assert(err, IsNil)
    88  	for ch := byte('h'); ch >= byte('a'); ch-- {
    89  		c.Assert(string([]byte{ch}), Equals, string(scanner.Key()))
    90  		c.Assert(scanner.Next(), IsNil)
    91  	}
    92  	c.Assert(scanner.Valid(), IsFalse)
    93  }