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