github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/segment/codec.go (about)

     1  // Copyright 2020 DataStax
     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  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package segment
    16  
    17  import (
    18  	"io"
    19  )
    20  
    21  const (
    22  	UncompressedHeaderLength = 3
    23  	CompressedHeaderLength   = 5
    24  )
    25  
    26  const (
    27  	Crc24Length = 3
    28  	Crc32Length = 4
    29  )
    30  
    31  type Encoder interface {
    32  
    33  	// EncodeSegment encodes the entire segment.
    34  	EncodeSegment(segment *Segment, dest io.Writer) error
    35  }
    36  
    37  type Decoder interface {
    38  
    39  	// DecodeSegment decodes the entire segment.
    40  	DecodeSegment(source io.Reader) (*Segment, error)
    41  }
    42  
    43  // Codec exposes basic encoding and decoding operations for Segment instances. It should be the preferred interface to
    44  // use in typical client applications such as drivers.
    45  type Codec interface {
    46  	Encoder
    47  	Decoder
    48  }
    49  
    50  type codec struct {
    51  	compressor PayloadCompressor
    52  }
    53  
    54  func NewCodec() Codec {
    55  	return NewCodecWithCompression(nil)
    56  }
    57  
    58  func NewCodecWithCompression(compressor PayloadCompressor) Codec {
    59  	return &codec{compressor: compressor}
    60  }