github.com/m3db/m3@v1.5.0/src/query/test/mock_pools.go (about)

     1  // Copyright (c) 2022 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package test
    22  
    23  import (
    24  	"sync"
    25  
    26  	"github.com/m3db/m3/src/dbnode/encoding"
    27  	"github.com/m3db/m3/src/dbnode/x/xpool"
    28  	"github.com/m3db/m3/src/x/ident"
    29  	"github.com/m3db/m3/src/x/pool"
    30  	"github.com/m3db/m3/src/x/serialize"
    31  )
    32  
    33  var (
    34  	buckets  = []pool.Bucket{{Capacity: 100, Count: 100}}
    35  	poolOpts = pool.NewObjectPoolOptions().SetSize(1)
    36  	mu       sync.Mutex
    37  )
    38  
    39  // MakeMockIteratorPool builds a mock iterator pool
    40  func MakeMockIteratorPool() *MockIteratorPool {
    41  	return &MockIteratorPool{}
    42  }
    43  
    44  // MockIteratorPool is an iterator pool used for testing
    45  type MockIteratorPool struct {
    46  	MriPoolUsed, SiPoolUsed, MriaPoolUsed,
    47  	CbwPoolUsed, IdentPoolUsed, EncodePoolUsed, DecodePoolUsed bool
    48  }
    49  
    50  // MultiReaderIterator exposes the session's MultiReaderIteratorPool
    51  func (ip *MockIteratorPool) MultiReaderIterator() encoding.MultiReaderIteratorPool {
    52  	ip.MriPoolUsed = true
    53  	mriPool := encoding.NewMultiReaderIteratorPool(nil)
    54  	mriPool.Init(testIterAlloc)
    55  	return mriPool
    56  }
    57  
    58  // SeriesIterator exposes the session's SeriesIteratorPool
    59  func (ip *MockIteratorPool) SeriesIterator() encoding.SeriesIteratorPool {
    60  	ip.SiPoolUsed = true
    61  	siPool := encoding.NewSeriesIteratorPool(nil)
    62  	siPool.Init()
    63  	return siPool
    64  }
    65  
    66  // MultiReaderIteratorArray exposes the session's MultiReaderIteratorArrayPool
    67  func (ip *MockIteratorPool) MultiReaderIteratorArray() encoding.MultiReaderIteratorArrayPool {
    68  	ip.MriaPoolUsed = true
    69  	mriaPool := encoding.NewMultiReaderIteratorArrayPool(nil)
    70  	mriaPool.Init()
    71  	return mriaPool
    72  }
    73  
    74  // CheckedBytesWrapper exposes the session's CheckedBytesWrapperPool
    75  func (ip *MockIteratorPool) CheckedBytesWrapper() xpool.CheckedBytesWrapperPool {
    76  	ip.CbwPoolUsed = true
    77  	cbwPool := xpool.NewCheckedBytesWrapperPool(nil)
    78  	cbwPool.Init()
    79  	return cbwPool
    80  }
    81  
    82  // ID exposes the session's identity pool
    83  func (ip *MockIteratorPool) ID() ident.Pool {
    84  	ip.IdentPoolUsed = true
    85  	bytesPool := pool.NewCheckedBytesPool(buckets, nil,
    86  		func(sizes []pool.Bucket) pool.BytesPool {
    87  			return pool.NewBytesPool(sizes, nil)
    88  		})
    89  	bytesPool.Init()
    90  	return ident.NewPool(bytesPool, ident.PoolOptions{})
    91  }
    92  
    93  // TagDecoder exposes the session's tag decoder pool
    94  func (ip *MockIteratorPool) TagDecoder() serialize.TagDecoderPool {
    95  	ip.DecodePoolUsed = true
    96  	decoderPool := serialize.NewTagDecoderPool(
    97  		serialize.NewTagDecoderOptions(serialize.TagDecoderOptionsConfig{}),
    98  		poolOpts)
    99  	decoderPool.Init()
   100  	return decoderPool
   101  }
   102  
   103  // TagEncoder exposes the session's tag encoder pool
   104  func (ip *MockIteratorPool) TagEncoder() serialize.TagEncoderPool {
   105  	mu.Lock()
   106  	ip.EncodePoolUsed = true
   107  	encoderPool := serialize.NewTagEncoderPool(serialize.NewTagEncoderOptions(), poolOpts)
   108  	encoderPool.Init()
   109  	mu.Unlock()
   110  	return encoderPool
   111  }