@Nullable @Override public byte[] get(byte[] row, byte[] column) { return v3HBaseTable.get(row, column); }
private synchronized long getLong(byte[] rowKey, byte[] column) { byte[] result = metaTable.get(rowKey, column); if (result == null) { return 0; } return Bytes.toLong(result); }
@Override public byte[] call() { return delegate.get(row, column); } });
private synchronized long getLong(byte[] rowKey, byte[] column) { byte[] result = metaTable.get(rowKey, column); if (result == null) { return 0; } return Bytes.toLong(result); }
/** * Gets the value as a long in the {@link MetricsTable} of a given key. * * @param metaKey Object form of the key to get value with. * @return The value or {@code -1} if the value is not found. * @throws Exception If there is an error when fetching. */ public synchronized <T extends MetricsMetaKey> long get(T metaKey) throws Exception { byte[] result = metaTable.get(metaKey.getKey(), OFFSET_COLUMN); if (result == null) { return -1; } return Bytes.toLong(result); }
/** * Gets the value as a long in the {@link MetricsTable} of a given key. * * @param metaKey Object form of the key to get value with. * @return The value or {@code -1} if the value is not found. * @throws Exception If there is an error when fetching. */ public synchronized <T extends MetricsMetaKey> long get(T metaKey) throws Exception { byte[] result = metaTable.get(metaKey.getKey(), OFFSET_COLUMN); if (result == null) { return -1; } return Bytes.toLong(result); }
byte[] value = v3MetricsTable.get(rowKey, entry.getKey()); if (value == null) {
@Test public void testDeleteIncrements() throws Exception { // note: this is pretty important test case for tables with counters, e.g. metrics MetricsTable table = getTable("testDeleteIncrements"); // delete increment and increment again table.increment(A, ImmutableMap.of(B, 5L)); table.increment(A, ImmutableMap.of(B, 2L)); Assert.assertEquals(7L, Bytes.toLong(table.get(A, B))); table.delete(A, new byte[][]{B}); Assert.assertNull(table.get(A, B)); table.increment(A, ImmutableMap.of(B, 3L)); Assert.assertEquals(3L, Bytes.toLong(table.get(A, B))); }
@Override public EntityName load(EntityId key) throws Exception { // Lookup the reverse mapping byte[] rowKey = Bytes.concat(Bytes.toBytes(key.getType()), DOT, Bytes.toBytes(key.getId())); byte[] result = table.get(rowKey, NAME); if (result == null) { throw new IllegalArgumentException("Entity name not found for type " + key.getType() + ", id " + key.getId()); } return new EntityName(key.getType(), Bytes.toString(result)); } };
@Override public EntityName load(EntityId key) throws Exception { // Lookup the reverse mapping byte[] rowKey = Bytes.concat(Bytes.toBytes(key.getType()), DOT, Bytes.toBytes(key.getId())); byte[] result = table.get(rowKey, NAME); if (result == null) { throw new IllegalArgumentException("Entity name not found for type " + key.getType() + ", id " + key.getId()); } return new EntityName(key.getType(), Bytes.toString(result)); } };
/** * Gets the value as a byte array in the {@link MetricsTable} of a given key. * * @param metaKey Object form of the key to get value with. * @return The value or {@code null} if the value is not found. * @throws Exception If there is an error when fetching. */ @Nullable public synchronized <T extends MetricsMetaKey> TopicProcessMeta getTopicProcessMeta(T metaKey) throws Exception { // TODO : update to use get with multiple columns after CDAP-12459 is fixed byte[] messageId = metaTable.get(metaKey.getKey(), MESSAGE_ID_COLUMN); if (messageId == null) { return null; } long processedCount = getLong(metaKey.getKey(), PROCESS_COUNT); long oldestTs = getLong(metaKey.getKey(), PROCESS_TIMESTAMP_OLDEST); long latestTs = getLong(metaKey.getKey(), PROCESS_TIMESTAMP_LATEST); long lastProcessedTs = getLong(metaKey.getKey(), LAST_PROCESS_TIMESTAMP); return new TopicProcessMeta(messageId, oldestTs, latestTs, processedCount, lastProcessedTs); } }
/** * Gets the value as a byte array in the {@link MetricsTable} of a given key. * * @param metaKey Object form of the key to get value with. * @return The value or {@code null} if the value is not found. * @throws Exception If there is an error when fetching. */ @Nullable public synchronized <T extends MetricsMetaKey> TopicProcessMeta getTopicProcessMeta(T metaKey) throws Exception { // TODO : update to use get with multiple columns after CDAP-12459 is fixed byte[] messageId = metaTable.get(metaKey.getKey(), MESSAGE_ID_COLUMN); if (messageId == null) { return null; } long processedCount = getLong(metaKey.getKey(), PROCESS_COUNT); long oldestTs = getLong(metaKey.getKey(), PROCESS_TIMESTAMP_OLDEST); long latestTs = getLong(metaKey.getKey(), PROCESS_TIMESTAMP_LATEST); long lastProcessedTs = getLong(metaKey.getKey(), LAST_PROCESS_TIMESTAMP); return new TopicProcessMeta(messageId, oldestTs, latestTs, processedCount, lastProcessedTs); } }
@Test public void testConcurrentSwap() throws Exception { MetricsTable table = getTable("testConcurrentSwap"); final long rounds = 500; table.put(ImmutableSortedMap.<byte[], SortedMap<byte[], Long>>orderedBy(Bytes.BYTES_COMPARATOR) .put(A, mapOf(B, 0L)).build()); AtomicInteger[] counts = { new AtomicInteger(), new AtomicInteger() }; // [0] for success, [1] for failures Thread t1 = new SwapThread(table, A, B, counts, rounds); Thread t2 = new SwapThread(table, A, B, counts, rounds); t1.start(); t2.start(); t1.join(); t2.join(); Assert.assertEquals(rounds, Bytes.toLong(table.get(A, B))); Assert.assertEquals(rounds, counts[0].get()); // number of successful swaps Assert.assertEquals(rounds, counts[1].get()); // number of failed swaps }
@Test public void testDelete() throws Exception { MetricsTable table = getTable("testDelete"); NavigableMap<byte[], SortedMap<byte[], Long>> writes = Maps.newTreeMap(Bytes.BYTES_COMPARATOR); for (int i = 0; i < 1024; i++) { writes.put(Bytes.toBytes(i << 22), mapOf(A, Bytes.toLong(X))); } table.put(writes); // verify the first and last are there (sanity test for correctness of test logic) Assert.assertArrayEquals(X, table.get(Bytes.toBytes(0x00000000), A)); Assert.assertArrayEquals(X, table.get(Bytes.toBytes(0xffc00000), A)); List<byte[]> toDelete = ImmutableList.of( Bytes.toBytes(0xa1000000), Bytes.toBytes(0xb2000000), Bytes.toBytes(0xc3000000)); // verify these three are there, and delete them for (byte[] row : toDelete) { Assert.assertArrayEquals(X, table.get(row, A)); table.delete(row, new byte[][] {A}); } // verify these three are now gone. for (byte[] row : toDelete) { Assert.assertNull(table.get(row, A)); } // verify nothing else is gone by counting all entries in a scan Assert.assertEquals(1021, countRange(table, null, null)); }
.put(A, mapOf(P, Bytes.toLong(X), Q, Bytes.toLong(Y))) .put(B, mapOf(P, Bytes.toLong(X), R, Bytes.toLong(Z))).build()); Assert.assertEquals(Bytes.toLong(X), Bytes.toLong(table.get(A, P))); Assert.assertEquals(Bytes.toLong(Y), Bytes.toLong(table.get(A, Q))); Assert.assertNull(table.get(A, R)); Assert.assertEquals(Bytes.toLong(X), Bytes.toLong(table.get(B, P))); Assert.assertEquals(Bytes.toLong(Z), Bytes.toLong(table.get(B, R))); Assert.assertNull(table.get(B, Q)); Assert.assertEquals(Bytes.toLong(A), Bytes.toLong(table.get(A, P))); Assert.assertEquals(Bytes.toLong(Y), Bytes.toLong(table.get(A, Q))); Assert.assertEquals(Bytes.toLong(C), Bytes.toLong(table.get(A, R))); Assert.assertEquals(Bytes.toLong(B), Bytes.toLong(table.get(A, P))); Assert.assertArrayEquals(B, table.get(A, P)); Assert.assertNull(table.get(B, Q)); Assert.assertNull(table.get(A, P)); Assert.assertArrayEquals(Y, table.get(A, Q)); Assert.assertArrayEquals(Z, table.get(A, P)); Assert.assertArrayEquals(Y, table.get(A, Q));