snapshot.go 6.0 KB

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