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