github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/dbnode/testdata/prototest/pools.go (about)

     1  // Copyright (c) 2019 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 prototest
    22  
    23  import (
    24  	"github.com/m3db/m3/src/dbnode/encoding"
    25  	"github.com/m3db/m3/src/dbnode/encoding/proto"
    26  	"github.com/m3db/m3/src/dbnode/x/xio"
    27  	"github.com/m3db/m3/src/x/pool"
    28  	xtime "github.com/m3db/m3/src/x/time"
    29  
    30  	"github.com/m3db/m3/src/dbnode/namespace"
    31  )
    32  
    33  var (
    34  	ProtoPools = newPools()
    35  )
    36  
    37  type Pools struct {
    38  	EncodingOpt         encoding.Options
    39  	EncoderPool         encoding.EncoderPool
    40  	ReaderIterPool      encoding.ReaderIteratorPool
    41  	MultiReaderIterPool encoding.MultiReaderIteratorPool
    42  	BytesPool           pool.CheckedBytesPool
    43  }
    44  
    45  func newPools() Pools {
    46  	bytesPool := pool.NewCheckedBytesPool(nil, nil, func(s []pool.Bucket) pool.BytesPool {
    47  		return pool.NewBytesPool(s, nil)
    48  	})
    49  	bytesPool.Init()
    50  	testEncodingOptions := encoding.NewOptions().
    51  		SetDefaultTimeUnit(xtime.Second).
    52  		SetBytesPool(bytesPool)
    53  
    54  	encoderPool := encoding.NewEncoderPool(nil)
    55  	readerIterPool := encoding.NewReaderIteratorPool(nil)
    56  	multiReaderIteratorPool := encoding.NewMultiReaderIteratorPool(nil)
    57  
    58  	encodingOpts := testEncodingOptions.SetEncoderPool(encoderPool)
    59  
    60  	encoderPool.Init(func() encoding.Encoder {
    61  		return proto.NewEncoder(0, encodingOpts)
    62  	})
    63  	readerIterPool.Init(func(r xio.Reader64, descr namespace.SchemaDescr) encoding.ReaderIterator {
    64  		return proto.NewIterator(r, descr, encodingOpts)
    65  	})
    66  	multiReaderIteratorPool.Init(func(r xio.Reader64, descr namespace.SchemaDescr) encoding.ReaderIterator {
    67  		i := readerIterPool.Get()
    68  		i.Reset(r, descr)
    69  		return i
    70  	})
    71  
    72  	return Pools{
    73  		EncodingOpt:         testEncodingOptions,
    74  		BytesPool:           bytesPool,
    75  		EncoderPool:         encoderPool,
    76  		ReaderIterPool:      readerIterPool,
    77  		MultiReaderIterPool: multiReaderIteratorPool,
    78  	}
    79  }