github.com/treeverse/lakefs@v1.24.1-0.20240520134607-95648127bfb0/pkg/kv/msg.go (about)

     1  package kv
     2  
     3  import (
     4  	"context"
     5  
     6  	"google.golang.org/protobuf/proto"
     7  	"google.golang.org/protobuf/reflect/protoreflect"
     8  )
     9  
    10  func GetMsg(ctx context.Context, s Store, partitionKey string, key []byte, msg protoreflect.ProtoMessage) (Predicate, error) {
    11  	res, err := s.Get(ctx, []byte(partitionKey), key)
    12  	if err != nil {
    13  		return nil, err
    14  	}
    15  	// conditional msg - make it work like Get just using key
    16  	if msg == nil {
    17  		return res.Predicate, nil
    18  	}
    19  	err = proto.Unmarshal(res.Value, msg)
    20  	if err != nil {
    21  		return nil, err
    22  	}
    23  	return res.Predicate, nil
    24  }
    25  
    26  func SetMsg(ctx context.Context, s Store, partitionKey string, key []byte, msg protoreflect.ProtoMessage) error {
    27  	val, err := proto.Marshal(msg)
    28  	if err != nil {
    29  		return err
    30  	}
    31  	return s.Set(ctx, []byte(partitionKey), key, val)
    32  }
    33  
    34  func SetMsgIf(ctx context.Context, s Store, partitionKey string, key []byte, msg protoreflect.ProtoMessage, predicate Predicate) error {
    35  	val, err := proto.Marshal(msg)
    36  	if err != nil {
    37  		return err
    38  	}
    39  	return s.SetIf(ctx, []byte(partitionKey), key, val, predicate)
    40  }