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

     1  // Copyright (C) 2019  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  	"context"
    10  	"errors"
    11  
    12  	"github.com/tsuna/gohbase/pb"
    13  	"google.golang.org/protobuf/proto"
    14  )
    15  
    16  type snap struct {
    17  	name         string
    18  	table        string
    19  	snapshotType *pb.SnapshotDescription_Type
    20  	version      *int32
    21  	owner        string
    22  }
    23  
    24  func (s *snap) ToProto() *pb.SnapshotDescription {
    25  	return &pb.SnapshotDescription{
    26  		Type:    s.snapshotType,
    27  		Table:   proto.String(s.table),
    28  		Name:    proto.String(s.name),
    29  		Version: s.version,
    30  		Owner:   proto.String(s.owner),
    31  	}
    32  }
    33  
    34  func (s *snap) Version(v int32) {
    35  	s.version = &v
    36  }
    37  
    38  func (s *snap) Owner(o string) {
    39  	s.owner = o
    40  }
    41  
    42  func (s *snap) Type(t pb.SnapshotDescription_Type) {
    43  	s.snapshotType = &t
    44  }
    45  
    46  type snapshotSettable interface {
    47  	Version(int32)
    48  	Owner(string)
    49  	Type(pb.SnapshotDescription_Type)
    50  }
    51  
    52  // SnapshotVersion sets the version of the snapshot.
    53  func SnapshotVersion(v int32) func(Call) error {
    54  	return func(g Call) error {
    55  		sn, ok := g.(snapshotSettable)
    56  		if !ok {
    57  			return errors.New("'SnapshotVersion' option can only be used with Snapshot queries")
    58  		}
    59  		sn.Version(v)
    60  		return nil
    61  	}
    62  }
    63  
    64  // SnapshotOwner sets the owner of the snapshot.
    65  func SnapshotOwner(o string) func(Call) error {
    66  	return func(g Call) error {
    67  		sn, ok := g.(snapshotSettable)
    68  		if !ok {
    69  			return errors.New("'SnapshotOwner' option can only be used with Snapshot queries")
    70  		}
    71  		sn.Owner(o)
    72  		return nil
    73  	}
    74  }
    75  
    76  // SnapshotSkipFlush disables hbase flushing when creating the snapshot.
    77  func SnapshotSkipFlush() func(Call) error {
    78  	return func(g Call) error {
    79  		sn, ok := g.(snapshotSettable)
    80  		if !ok {
    81  			return errors.New("'SnapshotSkipFlush' option can only be used with Snapshot queries")
    82  		}
    83  		sn.Type(pb.SnapshotDescription_SKIPFLUSH)
    84  		return nil
    85  	}
    86  }
    87  
    88  // Snapshot represents a Snapshot HBase call
    89  type Snapshot struct {
    90  	base
    91  	snap
    92  }
    93  
    94  // NewSnapshot creates a new Snapshot request that will request a
    95  // new snapshot in HBase.
    96  func NewSnapshot(ctx context.Context, name string, table string,
    97  	opts ...func(Call) error) (*Snapshot, error) {
    98  	sn := &Snapshot{
    99  		base{
   100  			table:    []byte(table),
   101  			ctx:      ctx,
   102  			resultch: make(chan RPCResult, 1),
   103  		},
   104  		snap{
   105  			name: name, table: table,
   106  		},
   107  	}
   108  	if err := applyOptions(sn, opts...); err != nil {
   109  		return nil, err
   110  	}
   111  	return sn, nil
   112  }
   113  
   114  // Name returns the name of this RPC call.
   115  func (sr *Snapshot) Name() string {
   116  	return "Snapshot"
   117  }
   118  
   119  // Description returns the description of this RPC call.
   120  func (sr *Snapshot) Description() string {
   121  	return sr.Name()
   122  }
   123  
   124  // ToProto converts the RPC into a protobuf message.
   125  func (sr *Snapshot) ToProto() proto.Message {
   126  	return &pb.SnapshotRequest{Snapshot: sr.snap.ToProto()}
   127  }
   128  
   129  // NewResponse creates an empty protobuf message to read the response of this
   130  // RPC.
   131  func (sr *Snapshot) NewResponse() proto.Message {
   132  	return &pb.SnapshotResponse{}
   133  }
   134  
   135  // SnapshotDone represents an IsSnapshotDone HBase call.
   136  type SnapshotDone struct {
   137  	*Snapshot
   138  }
   139  
   140  // NewSnapshotDone creates a new SnapshotDone request that will check if
   141  // the given snapshot has been complete.
   142  func NewSnapshotDone(t *Snapshot) *SnapshotDone {
   143  	return &SnapshotDone{t}
   144  }
   145  
   146  // Name returns the name of this RPC call.
   147  func (sr *SnapshotDone) Name() string {
   148  	return "IsSnapshotDone"
   149  }
   150  
   151  // NewResponse creates an empty protobuf message to read the response of this
   152  // RPC.
   153  func (sr *SnapshotDone) NewResponse() proto.Message {
   154  	return &pb.IsSnapshotDoneResponse{}
   155  }
   156  
   157  // DeleteSnapshot represents a DeleteSnapshot HBase call.
   158  type DeleteSnapshot struct {
   159  	*Snapshot
   160  }
   161  
   162  // NewDeleteSnapshot creates a new DeleteSnapshot request that will delete
   163  // the given snapshot.
   164  func NewDeleteSnapshot(t *Snapshot) *DeleteSnapshot {
   165  	return &DeleteSnapshot{t}
   166  }
   167  
   168  // Name returns the name of this RPC call.
   169  func (sr *DeleteSnapshot) Name() string {
   170  	return "DeleteSnapshot"
   171  }
   172  
   173  // NewResponse creates an empty protobuf message to read the response of this
   174  // RPC.
   175  func (sr *DeleteSnapshot) NewResponse() proto.Message {
   176  	return &pb.DeleteSnapshotResponse{}
   177  }
   178  
   179  // ListSnapshots represents a new GetCompletedSnapshots request that will
   180  // list all snapshots.
   181  type ListSnapshots struct {
   182  	base
   183  }
   184  
   185  // NewListSnapshots creates a new GetCompletedSnapshots request that will
   186  // list all snapshots.
   187  func NewListSnapshots(ctx context.Context) *ListSnapshots {
   188  	return &ListSnapshots{
   189  		base{
   190  			ctx:      ctx,
   191  			resultch: make(chan RPCResult, 1),
   192  		},
   193  	}
   194  }
   195  
   196  // Name returns the name of this RPC call.
   197  func (sr *ListSnapshots) Name() string {
   198  	return "GetCompletedSnapshots"
   199  }
   200  
   201  // Description returns the description of this RPC call.
   202  func (sr *ListSnapshots) Description() string {
   203  	return sr.Name()
   204  }
   205  
   206  // NewResponse creates an empty protobuf message to read the response of this
   207  // RPC.
   208  func (sr *ListSnapshots) NewResponse() proto.Message {
   209  	return &pb.GetCompletedSnapshotsResponse{}
   210  }
   211  
   212  // ToProto converts the RPC into a protobuf message.
   213  func (sr *ListSnapshots) ToProto() proto.Message {
   214  	return &pb.GetCompletedSnapshotsRequest{}
   215  }
   216  
   217  // RestoreSnapshot represents a RestoreSnapshot HBase call.
   218  type RestoreSnapshot struct {
   219  	*Snapshot
   220  }
   221  
   222  // NewRestoreSnapshot creates a new RestoreSnapshot request that will restore
   223  // the given snapshot.
   224  func NewRestoreSnapshot(t *Snapshot) *RestoreSnapshot {
   225  	return &RestoreSnapshot{t}
   226  }
   227  
   228  // Name returns the name of this RPC call.
   229  func (sr *RestoreSnapshot) Name() string {
   230  	return "RestoreSnapshot"
   231  }
   232  
   233  // NewResponse creates an empty protobuf message to read the response of this
   234  // RPC.
   235  func (sr *RestoreSnapshot) NewResponse() proto.Message {
   236  	return &pb.RestoreSnapshotResponse{}
   237  }
   238  
   239  // RestoreSnapshotDone represents an IsRestoreSnapshotDone HBase call.
   240  type RestoreSnapshotDone struct {
   241  	*Snapshot
   242  }
   243  
   244  // NewRestoreSnapshotDone creates a new RestoreSnapshotDone request that will check if
   245  // the given snapshot has been complete.
   246  func NewRestoreSnapshotDone(t *Snapshot) *RestoreSnapshotDone {
   247  	return &RestoreSnapshotDone{t}
   248  }
   249  
   250  // Name returns the name of this RPC call.
   251  func (sr *RestoreSnapshotDone) Name() string {
   252  	return "IsRestoreSnapshotDone"
   253  }
   254  
   255  // NewResponse creates an empty protobuf message to read the response of this
   256  // RPC.
   257  func (sr *RestoreSnapshotDone) NewResponse() proto.Message {
   258  	return &pb.IsRestoreSnapshotDoneResponse{}
   259  }