github.com/tsuna/gohbase@v0.0.0-20250731002811-4ffcadfba63e/hrpc/checkandput.go (about)

     1  // Copyright (C) 2016  The GoHBase Authors.  All rights reserved.
     2  // This file is part of GoHBase.
     3  // Use of this source code is governed by the Apache License 2.0
     4  // that can be found in the COPYING file.
     5  
     6  package hrpc
     7  
     8  import (
     9  	"fmt"
    10  
    11  	"github.com/tsuna/gohbase/filter"
    12  	"github.com/tsuna/gohbase/pb"
    13  	"google.golang.org/protobuf/proto"
    14  )
    15  
    16  // CheckAndPut performs a provided Put operation if the value specified
    17  // by condition equals to the one set in the HBase.
    18  type CheckAndPut struct {
    19  	*Mutate
    20  
    21  	family    []byte
    22  	qualifier []byte
    23  
    24  	comparator *pb.Comparator
    25  }
    26  
    27  // NewCheckAndPut creates a new CheckAndPut request that will compare provided
    28  // expectedValue with the on in HBase located at put's row and provided family:qualifier,
    29  // and if they are equal, perform the provided put request on the row
    30  func NewCheckAndPut(put *Mutate, family string,
    31  	qualifier string, expectedValue []byte) (*CheckAndPut, error) {
    32  	if put.mutationType != pb.MutationProto_PUT {
    33  		return nil, fmt.Errorf("'CheckAndPut' only takes 'Put' request")
    34  	}
    35  
    36  	// The condition that needs to match for the edit to be applied.
    37  	exp := filter.NewByteArrayComparable(expectedValue)
    38  	cmp, err := filter.NewBinaryComparator(exp).ConstructPBComparator()
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  
    43  	// CheckAndPut is not batchable as MultiResponse doesn't return Processed field
    44  	// for Mutate Action
    45  	put.setSkipBatch(true)
    46  
    47  	return &CheckAndPut{
    48  		Mutate:     put,
    49  		family:     []byte(family),
    50  		qualifier:  []byte(qualifier),
    51  		comparator: cmp,
    52  	}, nil
    53  }
    54  
    55  // ToProto converts the RPC into a protobuf message
    56  func (cp *CheckAndPut) ToProto() proto.Message {
    57  	mutateRequest, _, _ := cp.toProto(false, nil)
    58  	mutateRequest.Condition = &pb.Condition{
    59  		Row:         cp.key,
    60  		Family:      cp.family,
    61  		Qualifier:   cp.qualifier,
    62  		CompareType: pb.CompareType_EQUAL.Enum(),
    63  		Comparator:  cp.comparator,
    64  	}
    65  	return mutateRequest
    66  }
    67  
    68  func (cp *CheckAndPut) CellBlocksEnabled() bool {
    69  	// cellblocks are not supported for check and put request
    70  	return false
    71  }