1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase;
20
21 import java.io.IOException;
22
23 import org.apache.hadoop.hbase.client.Delete;
24 import org.apache.hadoop.hbase.client.Get;
25 import org.apache.hadoop.hbase.client.Put;
26 import org.apache.hadoop.hbase.client.Result;
27 import org.apache.hadoop.hbase.client.Durability;
28 import org.apache.hadoop.hbase.util.Bytes;
29
30
31
32
33
34
35 public class TimestampTestBase extends HBaseTestCase {
36 private static final long T0 = 10L;
37 private static final long T1 = 100L;
38 private static final long T2 = 200L;
39
40 public static final byte [] FAMILY_NAME = Bytes.toBytes("colfamily11");
41 private static final byte [] QUALIFIER_NAME = Bytes.toBytes("contents");
42
43 private static final byte [] ROW = Bytes.toBytes("row");
44
45
46
47
48
49
50
51
52 public static void doTestDelete(final Incommon incommon, FlushCache flusher)
53 throws IOException {
54
55 put(incommon, T0);
56 put(incommon, T1);
57 put(incommon, T2);
58 put(incommon);
59
60 assertVersions(incommon, new long [] {HConstants.LATEST_TIMESTAMP, T2, T1});
61
62
63
64 delete(incommon);
65
66
67 assertVersions(incommon, new long [] {T2, T1, T0});
68
69
70 flusher.flushcache();
71 assertVersions(incommon, new long [] {T2, T1, T0});
72
73
74 put(incommon);
75 assertVersions(incommon, new long [] {HConstants.LATEST_TIMESTAMP, T2, T1});
76 delete(incommon, T2);
77 assertVersions(incommon, new long [] {HConstants.LATEST_TIMESTAMP, T1, T0});
78
79 flusher.flushcache();
80 assertVersions(incommon, new long [] {HConstants.LATEST_TIMESTAMP, T1, T0});
81
82
83
84
85 put(incommon, T2);
86 delete(incommon, T1);
87 put(incommon, T1);
88
89 Delete delete = new Delete(ROW);
90 delete.deleteColumns(FAMILY_NAME, QUALIFIER_NAME, T2);
91 incommon.delete(delete, true);
92
93
94 assertOnlyLatest(incommon, HConstants.LATEST_TIMESTAMP);
95
96
97 flusher.flushcache();
98 assertOnlyLatest(incommon, HConstants.LATEST_TIMESTAMP);
99 }
100
101 private static void assertOnlyLatest(final Incommon incommon,
102 final long currentTime)
103 throws IOException {
104 Get get = null;
105 get = new Get(ROW);
106 get.addColumn(FAMILY_NAME, QUALIFIER_NAME);
107 get.setMaxVersions(3);
108 Result result = incommon.get(get);
109 assertEquals(1, result.size());
110 long time = Bytes.toLong(CellUtil.cloneValue(result.rawCells()[0]));
111 assertEquals(time, currentTime);
112 }
113
114
115
116
117
118
119
120
121
122 public static void assertVersions(final Incommon incommon, final long [] tss)
123 throws IOException {
124
125 Get get = null;
126 get = new Get(ROW);
127 get.addColumn(FAMILY_NAME, QUALIFIER_NAME);
128 Result r = incommon.get(get);
129 byte [] bytes = r.getValue(FAMILY_NAME, QUALIFIER_NAME);
130 long t = Bytes.toLong(bytes);
131 assertEquals(tss[0], t);
132
133
134
135 get = new Get(ROW);
136 get.addColumn(FAMILY_NAME, QUALIFIER_NAME);
137 get.setMaxVersions(tss.length);
138 Result result = incommon.get(get);
139 Cell [] kvs = result.rawCells();
140 assertEquals(kvs.length, tss.length);
141 for(int i=0;i<kvs.length;i++) {
142 t = Bytes.toLong(CellUtil.cloneValue(kvs[i]));
143 assertEquals(tss[i], t);
144 }
145
146
147 long maxStamp = kvs[0].getTimestamp();
148
149
150 get = new Get(ROW);
151 get.addColumn(FAMILY_NAME, QUALIFIER_NAME);
152 get.setTimeRange(0, maxStamp);
153 get.setMaxVersions(kvs.length - 1);
154 result = incommon.get(get);
155 kvs = result.rawCells();
156 assertEquals(kvs.length, tss.length - 1);
157 for(int i=1;i<kvs.length;i++) {
158 t = Bytes.toLong(CellUtil.cloneValue(kvs[i-1]));
159 assertEquals(tss[i], t);
160 }
161
162
163 assertScanContentTimestamp(incommon, tss[0]);
164 }
165
166
167
168
169
170
171
172 public static void doTestTimestampScanning(final Incommon incommon,
173 final FlushCache flusher)
174 throws IOException {
175
176 put(incommon, T0);
177 put(incommon, T1);
178 put(incommon, HConstants.LATEST_TIMESTAMP);
179
180 int count = assertScanContentTimestamp(incommon,
181 HConstants.LATEST_TIMESTAMP);
182
183 assertEquals(count, assertScanContentTimestamp(incommon, T0));
184 assertEquals(count, assertScanContentTimestamp(incommon, T1));
185
186 flusher.flushcache();
187 assertEquals(count, assertScanContentTimestamp(incommon, T0));
188 assertEquals(count, assertScanContentTimestamp(incommon, T1));
189 }
190
191
192
193
194
195
196
197
198 public static int assertScanContentTimestamp(final Incommon in, final long ts)
199 throws IOException {
200 ScannerIncommon scanner =
201 in.getScanner(COLUMNS[0], null, HConstants.EMPTY_START_ROW, ts);
202 int count = 0;
203 try {
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218 } finally {
219 scanner.close();
220 }
221 return count;
222 }
223
224 public static void put(final Incommon loader, final long ts)
225 throws IOException {
226 put(loader, Bytes.toBytes(ts), ts);
227 }
228
229 public static void put(final Incommon loader)
230 throws IOException {
231 long ts = HConstants.LATEST_TIMESTAMP;
232 put(loader, Bytes.toBytes(ts), ts);
233 }
234
235
236
237
238
239
240
241
242 public static void put(final Incommon loader, final byte [] bytes,
243 final long ts)
244 throws IOException {
245 Put put = new Put(ROW, ts);
246 put.setDurability(Durability.SKIP_WAL);
247 put.add(FAMILY_NAME, QUALIFIER_NAME, bytes);
248 loader.put(put);
249 }
250
251 public static void delete(final Incommon loader) throws IOException {
252 delete(loader, null);
253 }
254
255 public static void delete(final Incommon loader, final byte [] column)
256 throws IOException {
257 delete(loader, column, HConstants.LATEST_TIMESTAMP);
258 }
259
260 public static void delete(final Incommon loader, final long ts)
261 throws IOException {
262 delete(loader, null, ts);
263 }
264
265 public static void delete(final Incommon loader, final byte [] column,
266 final long ts)
267 throws IOException {
268 Delete delete = ts == HConstants.LATEST_TIMESTAMP?
269 new Delete(ROW): new Delete(ROW, ts);
270 delete.deleteColumn(FAMILY_NAME, QUALIFIER_NAME, ts);
271 loader.delete(delete, true);
272 }
273
274 public static Result get(final Incommon loader) throws IOException {
275 return loader.get(new Get(ROW));
276 }
277 }