/
Backup.java
161 lines (138 loc) · 4.88 KB
/
Backup.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.bigtable.admin.v2.models;
import com.google.api.core.InternalApi;
import com.google.bigtable.admin.v2.BackupName;
import com.google.cloud.bigtable.admin.v2.internal.NameUtil;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.protobuf.util.Timestamps;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.threeten.bp.Instant;
/**
* A backup lets you save a copy of a table's schema and data and restore the backup to a new table
* at a later time.
*/
public class Backup {
public enum State {
/** Not specified. */
STATE_UNSPECIFIED(com.google.bigtable.admin.v2.Backup.State.STATE_UNSPECIFIED),
/**
* The pending backup is still being created. Operations on the backup may fail with
* `FAILED_PRECONDITION` in this state.
*/
CREATING(com.google.bigtable.admin.v2.Backup.State.CREATING),
/** The backup is complete and ready for use. */
READY(com.google.bigtable.admin.v2.Backup.State.READY),
/** The state of the backup is not known by this client. Please upgrade your client. */
UNRECOGNIZED(com.google.bigtable.admin.v2.Backup.State.UNRECOGNIZED);
private final com.google.bigtable.admin.v2.Backup.State proto;
State(com.google.bigtable.admin.v2.Backup.State proto) {
this.proto = proto;
}
/**
* Wraps the protobuf. This method is considered an internal implementation detail and not meant
* to be used by applications.
*/
@InternalApi
public static Backup.State fromProto(com.google.bigtable.admin.v2.Backup.State proto) {
for (Backup.State state : values()) {
if (state.proto.equals(proto)) {
return state;
}
}
return STATE_UNSPECIFIED;
}
/**
* Creates the request protobuf. This method is considered an internal implementation detail and
* not meant to be used by applications.
*/
@InternalApi
public com.google.bigtable.admin.v2.Backup.State toProto() {
return proto;
}
}
@Nonnull private final com.google.bigtable.admin.v2.Backup proto;
@Nonnull private final String id;
@Nonnull private final String instanceId;
@InternalApi
public static Backup fromProto(@Nonnull com.google.bigtable.admin.v2.Backup proto) {
return new Backup(proto);
}
private Backup(@Nonnull com.google.bigtable.admin.v2.Backup proto) {
Preconditions.checkNotNull(proto);
Preconditions.checkArgument(!proto.getName().isEmpty(), "Name must be set");
Preconditions.checkArgument(!proto.getSourceTable().isEmpty(), "Source table must be set");
BackupName name = BackupName.parse(proto.getName());
this.id = name.getBackup();
this.instanceId = name.getInstance();
this.proto = proto;
}
/** Get the ID of this backup. */
public String getId() {
return id;
}
/** Get the source table ID from which the backup is created. */
public String getSourceTableId() {
return NameUtil.extractTableIdFromTableName(proto.getSourceTable());
}
/** Get the instance ID where this backup is located. */
public String getInstanceId() {
return instanceId;
}
/** Get the expire time of this backup. */
public Instant getExpireTime() {
return Instant.ofEpochMilli(Timestamps.toMillis(proto.getExpireTime()));
}
/** Get the start time when this backup is taken. */
public @Nullable Instant getStartTime() {
if (proto.hasStartTime()) {
return Instant.ofEpochMilli(Timestamps.toMillis(proto.getStartTime()));
}
return null;
}
/** Get the end time when the creation of this backup has completed. */
public @Nullable Instant getEndTime() {
if (proto.hasEndTime()) {
return Instant.ofEpochMilli(Timestamps.toMillis(proto.getEndTime()));
}
return null;
}
/** Get the size of this backup. */
public long getSizeBytes() {
return proto.getSizeBytes();
}
/** Get the state of this backup. */
public State getState() {
return State.fromProto(proto.getState());
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Backup backup = (Backup) o;
return Objects.equal(proto, backup.proto);
}
@Override
public int hashCode() {
return Objects.hashCode(proto);
}
}