github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/store/tikv/scan_test.go (about)

     1  // Copyright 2016 PingCAP, 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 tikv
    15  
    16  import (
    17  	"fmt"
    18  	"time"
    19  
    20  	. "github.com/insionng/yougam/libraries/pingcap/check"
    21  )
    22  
    23  type testScanSuite struct {
    24  	store   *tikvStore
    25  	prefix  string
    26  	rowNums []int
    27  }
    28  
    29  var _ = Suite(&testScanSuite{})
    30  
    31  func (s *testScanSuite) SetUpSuite(c *C) {
    32  	s.store = newTestStore(c)
    33  	s.prefix = fmt.Sprintf("seek_%d", time.Now().Unix())
    34  	s.rowNums = append(s.rowNums, 1, scanBatchSize, scanBatchSize+1)
    35  }
    36  
    37  func (s *testScanSuite) TearDownSuite(c *C) {
    38  	txn := s.beginTxn(c)
    39  	scanner, err := txn.Seek(encodeKey(s.prefix, ""))
    40  	c.Assert(err, IsNil)
    41  	c.Assert(scanner, NotNil)
    42  	for scanner.Valid() {
    43  		k := scanner.Key()
    44  		err = txn.Delete(k)
    45  		c.Assert(err, IsNil)
    46  		scanner.Next()
    47  	}
    48  	err = txn.Commit()
    49  	c.Assert(err, IsNil)
    50  	err = s.store.Close()
    51  	c.Assert(err, IsNil)
    52  }
    53  
    54  func (s *testScanSuite) beginTxn(c *C) *tikvTxn {
    55  	txn, err := s.store.Begin()
    56  	c.Assert(err, IsNil)
    57  	return txn.(*tikvTxn)
    58  }
    59  
    60  func (s *testScanSuite) TestSeek(c *C) {
    61  	for _, rowNum := range s.rowNums {
    62  		txn := s.beginTxn(c)
    63  		for i := 0; i < rowNum; i++ {
    64  			err := txn.Set(encodeKey(s.prefix, s08d("key", i)), valueBytes(i))
    65  			c.Assert(err, IsNil)
    66  		}
    67  		err := txn.Commit()
    68  		c.Assert(err, IsNil)
    69  
    70  		txn2 := s.beginTxn(c)
    71  		val, err := txn2.Get(encodeKey(s.prefix, s08d("key", 0)))
    72  		c.Assert(val, BytesEquals, valueBytes(0))
    73  		scan, err := txn2.Seek(encodeKey(s.prefix, ""))
    74  		c.Assert(err, IsNil)
    75  
    76  		for i := 0; i < rowNum; i++ {
    77  			k := scan.Key()
    78  			c.Assert([]byte(k), BytesEquals, encodeKey(s.prefix, s08d("key", i)))
    79  			v := scan.Value()
    80  			c.Assert(v, BytesEquals, valueBytes(i))
    81  			// Because newScan return first item without calling scan.Next() just like go-hbase,
    82  			// for-loop count will decrease 1.
    83  			if i < rowNum-1 {
    84  				scan.Next()
    85  			}
    86  		}
    87  		scan.Next()
    88  		c.Assert(scan.Valid(), IsFalse)
    89  	}
    90  }