github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/message/dse_revise_request.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 message
    16  
    17  import (
    18  	"fmt"
    19  	"io"
    20  
    21  	"github.com/datastax/go-cassandra-native-protocol/primitive"
    22  )
    23  
    24  // Revise was called CANCEL in DSE protocol version 1 and was renamed to REVISE_REQUEST in version 2.
    25  // +k8s:deepcopy-gen=true
    26  // +k8s:deepcopy-gen:interfaces=github.com/datastax/go-cassandra-native-protocol/message.Message
    27  type Revise struct {
    28  	RevisionType   primitive.DseRevisionType
    29  	TargetStreamId int32
    30  	// The number of pages that the client is ready to receive, or zero to indicate no limit.
    31  	// Valid for DSE v2 only when RevisionType is 2 (DseRevisionTypeMoreContinuousPages).
    32  	// See ContinuousPagingOptions.
    33  	NextPages int32
    34  }
    35  
    36  func (m *Revise) IsResponse() bool {
    37  	return false
    38  }
    39  
    40  func (m *Revise) GetOpCode() primitive.OpCode {
    41  	return primitive.OpCodeDseRevise
    42  }
    43  
    44  func (m *Revise) String() string {
    45  	return fmt.Sprintf("REVISE_REQUEST operation type: %v, stream id: %v", m.RevisionType, m.TargetStreamId)
    46  }
    47  
    48  type reviseCodec struct{}
    49  
    50  func (c *reviseCodec) Encode(msg Message, dest io.Writer, version primitive.ProtocolVersion) error {
    51  	revise, ok := msg.(*Revise)
    52  	if !ok {
    53  		return fmt.Errorf("expected *message.Revise, got %T", msg)
    54  	}
    55  	if err := primitive.CheckDseProtocolVersion(version); err != nil {
    56  		return err
    57  	} else if err := primitive.CheckValidDseRevisionType(revise.RevisionType, version); err != nil {
    58  		return err
    59  	} else if err := primitive.WriteInt(int32(revise.RevisionType), dest); err != nil {
    60  		return fmt.Errorf("cannot write REVISE/CANCEL revision type: %w", err)
    61  	}
    62  	if err := primitive.WriteInt(revise.TargetStreamId, dest); err != nil {
    63  		return fmt.Errorf("cannot write REVISE/CANCEL target stream id: %w", err)
    64  	}
    65  	switch revise.RevisionType {
    66  	case primitive.DseRevisionTypeMoreContinuousPages:
    67  		if err := primitive.WriteInt(revise.NextPages, dest); err != nil {
    68  			return fmt.Errorf("cannot write REVISE/CANCEL next pages: %w", err)
    69  		}
    70  	}
    71  	return nil
    72  }
    73  
    74  func (c *reviseCodec) EncodedLength(msg Message, version primitive.ProtocolVersion) (length int, err error) {
    75  	if err := primitive.CheckDseProtocolVersion(version); err != nil {
    76  		return -1, err
    77  	}
    78  	revise, ok := msg.(*Revise)
    79  	if !ok {
    80  		return -1, fmt.Errorf("expected *message.Revise, got %T", msg)
    81  	}
    82  	length += primitive.LengthOfInt // revision type
    83  	length += primitive.LengthOfInt // stream id
    84  	switch revise.RevisionType {
    85  	case primitive.DseRevisionTypeMoreContinuousPages:
    86  		length += primitive.LengthOfInt // next pages
    87  	}
    88  	return length, nil
    89  }
    90  
    91  func (c *reviseCodec) Decode(source io.Reader, version primitive.ProtocolVersion) (msg Message, err error) {
    92  	if err := primitive.CheckDseProtocolVersion(version); err != nil {
    93  		return nil, err
    94  	}
    95  	revise := &Revise{}
    96  	var revisionType int32
    97  	if revisionType, err = primitive.ReadInt(source); err != nil {
    98  		return nil, fmt.Errorf("cannot read REVISE/CANCEL revision type: %w", err)
    99  	}
   100  	revise.RevisionType = primitive.DseRevisionType(revisionType)
   101  	if err := primitive.CheckValidDseRevisionType(revise.RevisionType, version); err != nil {
   102  		return nil, err
   103  	} else if revise.TargetStreamId, err = primitive.ReadInt(source); err != nil {
   104  		return nil, fmt.Errorf("cannot read REVISE/CANCEL target stream id: %w", err)
   105  	}
   106  	switch revise.RevisionType {
   107  	case primitive.DseRevisionTypeMoreContinuousPages:
   108  		if revise.NextPages, err = primitive.ReadInt(source); err != nil {
   109  			return nil, fmt.Errorf("cannot read REVISE/CANCEL next pages: %w", err)
   110  		}
   111  	}
   112  	return revise, nil
   113  }
   114  
   115  func (c *reviseCodec) GetOpCode() primitive.OpCode {
   116  	return primitive.OpCodeDseRevise
   117  }