github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/internal/grpcwrapper/rawtopic/rawtopiccommon/codec.go (about)

     1  package rawtopiccommon
     2  
     3  import (
     4  	"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Topic"
     5  )
     6  
     7  // Codec any int value, for example for custom codec
     8  type Codec int32
     9  
    10  const (
    11  	CodecUNSPECIFIED Codec = iota
    12  	CodecRaw               = Codec(Ydb_Topic.Codec_CODEC_RAW)
    13  	CodecGzip              = Codec(Ydb_Topic.Codec_CODEC_GZIP)
    14  	CodecLzop              = Codec(Ydb_Topic.Codec_CODEC_LZOP)
    15  	CodecZstd              = Codec(Ydb_Topic.Codec_CODEC_ZSTD)
    16  )
    17  
    18  const (
    19  	CodecCustomerFirst = 10000
    20  	CodecCustomerEnd   = 20000 // last allowed custom codec id is 19999
    21  )
    22  
    23  func (c Codec) IsCustomerCodec() bool {
    24  	return c >= CodecCustomerFirst && c <= CodecCustomerEnd
    25  }
    26  
    27  func (c *Codec) MustFromProto(codec Ydb_Topic.Codec) {
    28  	*c = Codec(codec)
    29  }
    30  
    31  func (c Codec) ToProto() Ydb_Topic.Codec {
    32  	return Ydb_Topic.Codec(c)
    33  }
    34  
    35  func (c Codec) ToInt32() int32 {
    36  	return int32(c)
    37  }
    38  
    39  type SupportedCodecs []Codec
    40  
    41  func (c *SupportedCodecs) AllowedByCodecsList(need Codec) bool {
    42  	// empty list allow any codec
    43  	if len(*c) == 0 {
    44  		return true
    45  	}
    46  
    47  	return c.Contains(need)
    48  }
    49  
    50  func (c *SupportedCodecs) Contains(need Codec) bool {
    51  	for _, v := range *c {
    52  		if v == need {
    53  			return true
    54  		}
    55  	}
    56  
    57  	return false
    58  }
    59  
    60  func (c *SupportedCodecs) Clone() SupportedCodecs {
    61  	res := make(SupportedCodecs, len(*c))
    62  	copy(res, *c)
    63  
    64  	return res
    65  }
    66  
    67  func (c *SupportedCodecs) IsEqualsTo(other SupportedCodecs) bool {
    68  	if len(*c) != len(other) {
    69  		return false
    70  	}
    71  
    72  	codecs := make(map[Codec]struct{})
    73  	for _, v := range *c {
    74  		codecs[v] = struct{}{}
    75  	}
    76  
    77  	for _, v := range other {
    78  		if _, ok := codecs[v]; !ok {
    79  			return false
    80  		}
    81  	}
    82  
    83  	return true
    84  }
    85  
    86  func (c *SupportedCodecs) ToProto() *Ydb_Topic.SupportedCodecs {
    87  	codecs := *c
    88  	proto := &Ydb_Topic.SupportedCodecs{
    89  		Codecs: make([]int32, len(codecs)),
    90  	}
    91  	for i := range codecs {
    92  		proto.Codecs[i] = int32(codecs[i].ToProto().Number())
    93  	}
    94  
    95  	return proto
    96  }
    97  
    98  func (c *SupportedCodecs) MustFromProto(proto *Ydb_Topic.SupportedCodecs) {
    99  	res := make([]Codec, len(proto.GetCodecs()))
   100  	for i := range proto.GetCodecs() {
   101  		res[i].MustFromProto(Ydb_Topic.Codec(proto.GetCodecs()[i]))
   102  	}
   103  	*c = res
   104  }