github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/message/dse_continuous_paging_options.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  // ContinuousPagingOptions holds options for DSE continuous paging feature. Valid for QUERY and EXECUTE messages.
    25  // See QueryOptions.
    26  // +k8s:deepcopy-gen=true
    27  type ContinuousPagingOptions struct {
    28  	// The maximum number of pages that the server will send to the client in total, or zero to indicate no limit.
    29  	// Valid for both DSE v1 and v2.
    30  	MaxPages int32
    31  	// The maximum number of pages per second, or zero to indicate no limit.
    32  	// Valid for both DSE v1 and v2.
    33  	PagesPerSecond int32
    34  	// The number of pages that the client is ready to receive, or zero to indicate no limit.
    35  	// Valid for DSE v2 only.
    36  	// See Revise.
    37  	NextPages int32
    38  }
    39  
    40  func EncodeContinuousPagingOptions(options *ContinuousPagingOptions, dest io.Writer, version primitive.ProtocolVersion) (err error) {
    41  	if err = primitive.CheckDseProtocolVersion(version); err != nil {
    42  		return err
    43  	} else if err = primitive.WriteInt(options.MaxPages, dest); err != nil {
    44  		return fmt.Errorf("cannot write max num pages: %w", err)
    45  	} else if err = primitive.WriteInt(options.PagesPerSecond, dest); err != nil {
    46  		return fmt.Errorf("cannot write pages per second: %w", err)
    47  	} else if version >= primitive.ProtocolVersionDse2 {
    48  		if err = primitive.WriteInt(options.NextPages, dest); err != nil {
    49  			return fmt.Errorf("cannot write next pages: %w", err)
    50  		}
    51  	}
    52  	return nil
    53  }
    54  
    55  func LengthOfContinuousPagingOptions(_ *ContinuousPagingOptions, version primitive.ProtocolVersion) (length int, err error) {
    56  	if err = primitive.CheckDseProtocolVersion(version); err != nil {
    57  		return -1, err
    58  	}
    59  	length += primitive.LengthOfInt // max num pages
    60  	length += primitive.LengthOfInt // pages per second
    61  	if version >= primitive.ProtocolVersionDse2 {
    62  		length += primitive.LengthOfInt // next pages
    63  	}
    64  	return length, nil
    65  }
    66  
    67  func DecodeContinuousPagingOptions(source io.Reader, version primitive.ProtocolVersion) (options *ContinuousPagingOptions, err error) {
    68  	if err = primitive.CheckDseProtocolVersion(version); err != nil {
    69  		return nil, err
    70  	}
    71  	options = &ContinuousPagingOptions{}
    72  	if options.MaxPages, err = primitive.ReadInt(source); err != nil {
    73  		return nil, fmt.Errorf("cannot read max num pages: %w", err)
    74  	} else if options.PagesPerSecond, err = primitive.ReadInt(source); err != nil {
    75  		return nil, fmt.Errorf("cannot read pages per second: %w", err)
    76  	} else if version >= primitive.ProtocolVersionDse2 {
    77  		if options.NextPages, err = primitive.ReadInt(source); err != nil {
    78  			return nil, fmt.Errorf("cannot read next pages: %w", err)
    79  		}
    80  	}
    81  	return options, nil
    82  }