github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/soliton/chunk/iterator_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 chunk
    15  
    16  import (
    17  	"github.com/whtcorpsinc/check"
    18  	"github.com/whtcorpsinc/BerolinaSQL/allegrosql"
    19  	"github.com/whtcorpsinc/milevadb/types"
    20  )
    21  
    22  func (s *testChunkSuite) TestIteratorOnSel(c *check.C) {
    23  	fields := []*types.FieldType{types.NewFieldType(allegrosql.TypeLonglong)}
    24  	chk := New(fields, 32, 1024)
    25  	sel := make([]int, 0, 1024)
    26  	for i := 0; i < 1024; i++ {
    27  		chk.AppendInt64(0, int64(i))
    28  		if i%2 == 0 {
    29  			sel = append(sel, i)
    30  		}
    31  	}
    32  	chk.SetSel(sel)
    33  	it := NewIterator4Chunk(chk)
    34  	cnt := 0
    35  	for event := it.Begin(); event != it.End(); event = it.Next() {
    36  		c.Assert(event.GetInt64(0)%2, check.Equals, int64(0))
    37  		cnt++
    38  	}
    39  	c.Assert(cnt, check.Equals, 1024/2)
    40  }
    41  
    42  func checkEqual(it Iterator, exp []int64, c *check.C) {
    43  	for event, i := it.Begin(), 0; event != it.End(); event, i = it.Next(), i+1 {
    44  		c.Assert(event.GetInt64(0), check.Equals, exp[i])
    45  	}
    46  }
    47  
    48  func (s *testChunkSuite) TestMultiIterator(c *check.C) {
    49  	it := NewMultiIterator(NewIterator4Chunk(new(Chunk)))
    50  	c.Assert(it.Begin(), check.Equals, it.End())
    51  
    52  	it = NewMultiIterator(NewIterator4Chunk(new(Chunk)), NewIterator4List(new(List)))
    53  	c.Assert(it.Begin(), check.Equals, it.End())
    54  
    55  	fields := []*types.FieldType{types.NewFieldType(allegrosql.TypeLonglong)}
    56  	chk := New(fields, 32, 1024)
    57  	n := 10
    58  	var expected []int64
    59  	for i := 0; i < n; i++ {
    60  		chk.AppendInt64(0, int64(i))
    61  		expected = append(expected, int64(i))
    62  	}
    63  	it = NewMultiIterator(NewIterator4Chunk(chk))
    64  	checkEqual(it, expected, c)
    65  
    66  	it = NewMultiIterator(NewIterator4Chunk(new(Chunk)), NewIterator4Chunk(chk), NewIterator4Chunk(new(Chunk)))
    67  	checkEqual(it, expected, c)
    68  
    69  	li := NewList(fields, 32, 1024)
    70  	chk2 := New(fields, 32, 1024)
    71  	for i := n; i < n*2; i++ {
    72  		expected = append(expected, int64(i))
    73  		chk2.AppendInt64(0, int64(i))
    74  	}
    75  	li.Add(chk2)
    76  
    77  	checkEqual(NewMultiIterator(NewIterator4Chunk(chk), NewIterator4Chunk(chk2)), expected, c)
    78  	checkEqual(NewMultiIterator(NewIterator4Chunk(chk), NewIterator4List(li)), expected, c)
    79  	rc := &RowContainer{}
    80  	rc.m.records = li
    81  	checkEqual(NewMultiIterator(NewIterator4Chunk(chk), NewIterator4RowContainer(rc)), expected, c)
    82  
    83  	li.Clear()
    84  	li.Add(chk)
    85  	checkEqual(NewMultiIterator(NewIterator4List(li), NewIterator4Chunk(chk2)), expected, c)
    86  	rc = &RowContainer{}
    87  	rc.m.records = new(List)
    88  	checkEqual(NewMultiIterator(NewIterator4RowContainer(rc), NewIterator4List(li), NewIterator4Chunk(chk2)), expected, c)
    89  }
    90  
    91  func (s *testChunkSuite) TestIterator(c *check.C) {
    92  	fields := []*types.FieldType{types.NewFieldType(allegrosql.TypeLonglong)}
    93  	chk := New(fields, 32, 1024)
    94  	n := 10
    95  	var expected []int64
    96  	for i := 0; i < n; i++ {
    97  		chk.AppendInt64(0, int64(i))
    98  		expected = append(expected, int64(i))
    99  	}
   100  	var rows []Row
   101  	li := NewList(fields, 1, 2)
   102  	li2 := NewList(fields, 8, 16)
   103  	var ptrs []RowPtr
   104  	var ptrs2 []RowPtr
   105  	for i := 0; i < n; i++ {
   106  		rows = append(rows, chk.GetRow(i))
   107  		ptr := li.AppendRow(chk.GetRow(i))
   108  		ptrs = append(ptrs, ptr)
   109  		ptr2 := li2.AppendRow(chk.GetRow(i))
   110  		ptrs2 = append(ptrs2, ptr2)
   111  	}
   112  
   113  	it := NewIterator4Slice(rows)
   114  	checkIterator(c, it, expected)
   115  	it.Begin()
   116  	for i := 0; i < 5; i++ {
   117  		c.Assert(it.Current(), check.Equals, rows[i])
   118  		it.Next()
   119  	}
   120  	it.ReachEnd()
   121  	c.Assert(it.Current(), check.Equals, it.End())
   122  	c.Assert(it.Begin(), check.Equals, rows[0])
   123  
   124  	it = NewIterator4Chunk(chk)
   125  	checkIterator(c, it, expected)
   126  	it.Begin()
   127  	for i := 0; i < 5; i++ {
   128  		c.Assert(it.Current(), check.Equals, chk.GetRow(i))
   129  		it.Next()
   130  	}
   131  	it.ReachEnd()
   132  	c.Assert(it.Current(), check.Equals, it.End())
   133  	c.Assert(it.Begin(), check.Equals, chk.GetRow(0))
   134  
   135  	it = NewIterator4List(li)
   136  	checkIterator(c, it, expected)
   137  	it.Begin()
   138  	for i := 0; i < 5; i++ {
   139  		c.Assert(it.Current(), check.Equals, li.GetRow(ptrs[i]))
   140  		it.Next()
   141  	}
   142  	it.ReachEnd()
   143  	c.Assert(it.Current(), check.Equals, it.End())
   144  	c.Assert(it.Begin(), check.Equals, li.GetRow(ptrs[0]))
   145  
   146  	it = NewIterator4RowPtr(li, ptrs)
   147  	checkIterator(c, it, expected)
   148  	it.Begin()
   149  	for i := 0; i < 5; i++ {
   150  		c.Assert(it.Current(), check.Equals, li.GetRow(ptrs[i]))
   151  		it.Next()
   152  	}
   153  	it.ReachEnd()
   154  	c.Assert(it.Current(), check.Equals, it.End())
   155  	c.Assert(it.Begin(), check.Equals, li.GetRow(ptrs[0]))
   156  
   157  	it = NewIterator4RowPtr(li2, ptrs2)
   158  	checkIterator(c, it, expected)
   159  	it.Begin()
   160  	for i := 0; i < 5; i++ {
   161  		c.Assert(it.Current(), check.Equals, li2.GetRow(ptrs2[i]))
   162  		it.Next()
   163  	}
   164  	it.ReachEnd()
   165  	c.Assert(it.Current(), check.Equals, it.End())
   166  	c.Assert(it.Begin(), check.Equals, li2.GetRow(ptrs2[0]))
   167  
   168  	rc := &RowContainer{}
   169  	rc.m.records = li
   170  	it = NewIterator4RowContainer(rc)
   171  	checkIterator(c, it, expected)
   172  	it.Begin()
   173  	for i := 0; i < 5; i++ {
   174  		c.Assert(it.Current(), check.Equals, li.GetRow(ptrs[i]))
   175  		it.Next()
   176  	}
   177  	it.ReachEnd()
   178  	c.Assert(it.Current(), check.Equals, it.End())
   179  	c.Assert(it.Begin(), check.Equals, li.GetRow(ptrs[0]))
   180  
   181  	it = NewIterator4Slice(nil)
   182  	c.Assert(it.Begin(), check.Equals, it.End())
   183  	it = NewIterator4Chunk(new(Chunk))
   184  	c.Assert(it.Begin(), check.Equals, it.End())
   185  	it = NewIterator4List(new(List))
   186  	c.Assert(it.Begin(), check.Equals, it.End())
   187  	it = NewIterator4RowPtr(li, nil)
   188  	c.Assert(it.Begin(), check.Equals, it.End())
   189  	rc = &RowContainer{}
   190  	rc.m.records = NewList(fields, 1, 1)
   191  	it = NewIterator4RowContainer(rc)
   192  	c.Assert(it.Begin(), check.Equals, it.End())
   193  }
   194  
   195  func checkIterator(c *check.C, it Iterator, expected []int64) {
   196  	var got []int64
   197  	for event := it.Begin(); event != it.End(); event = it.Next() {
   198  		got = append(got, event.GetInt64(0))
   199  	}
   200  	c.Assert(got, check.DeepEquals, expected)
   201  }