public static Collection<Map.Entry<Cell, Value>> toConstantTimestampValues( final Collection<Map.Entry<Cell, byte[]>> cells, final long timestamp) { return Collections2.transform(cells, entry -> Maps.immutableEntry(entry.getKey(), Value.create(entry.getValue(), timestamp))); }
private void putIfNotUpdate( DbReadTable readTable, DbWriteTable writeTable, TableReference tableRef, List<Entry<Cell, byte[]>> batch, long timestamp, KeyAlreadyExistsException ex) { List<Entry<Cell, Value>> batchValues = Lists.transform(batch, input -> Maps.immutableEntry(input.getKey(), Value.create(input.getValue(), timestamp))); putIfNotUpdate(readTable, writeTable, tableRef, batchValues, ex); }
private void writeToCell(Cell cell, byte[] data) { Value val = Value.create(data, TEST_TIMESTAMP + 1); keyValueService.putWithTimestamps(TEST_TABLE, ImmutableMultimap.of(cell, val)); }
private static Multimap<Cell, Value> getVersions() { Multimap<Cell, Value> versions = ArrayListMultimap.create(); LongStream.rangeClosed(1, CELL_VERSIONS).boxed() .forEach(timestamp -> versions.put(cell, Value.create(value, timestamp))); return versions; } }
@Test public void deleteTimestampRangesEdgeCases() { long minTs = 0; keyValueService.addGarbageCollectionSentinelValues(TEST_TABLE, ImmutableSet.of(TEST_CELL)); keyValueService.putWithTimestamps(TEST_TABLE, ImmutableMultimap.of( TEST_CELL, Value.create(val(0, 5), minTs), TEST_CELL, Value.create(val(0, 7), TEST_TIMESTAMP - 1), TEST_CELL, Value.create(val(0, 9), TEST_TIMESTAMP))); keyValueService.deleteAllTimestamps(TEST_TABLE, ImmutableMap.of(TEST_CELL, TEST_TIMESTAMP), false); assertThat(getAllTimestampsForTestCell(), contains(Value.INVALID_VALUE_TIMESTAMP, TEST_TIMESTAMP)); }
@Test public void deleteTimestampRangesDeletesForSingleColumn() { long ts1 = 5L; long ts2 = 10L; long latestTs = 15L; keyValueService.putWithTimestamps(TEST_TABLE, ImmutableMultimap.of( TEST_CELL, Value.create(val(0, 5), ts1), TEST_CELL, Value.create(val(0, 7), ts2), TEST_CELL, Value.create(val(0, 9), latestTs))); keyValueService.deleteAllTimestamps(TEST_TABLE, ImmutableMap.of(TEST_CELL, latestTs), false); assertThat(getAllTimestampsForTestCell(), contains(latestTs)); assertThat(keyValueService.get(TEST_TABLE, ImmutableMap.of(TEST_CELL, Long.MAX_VALUE)).get(TEST_CELL), equalTo(Value.create(val(0, 9), latestTs))); }
@Test public void deleteTimestampRangesIncludingSentinelsDeletesForSingleColumn() { long ts1 = 5L; long ts2 = 10L; long latestTs = 15L; keyValueService.putWithTimestamps(TEST_TABLE, ImmutableMultimap.of( TEST_CELL, Value.create(val(0, 5), ts1), TEST_CELL, Value.create(val(0, 7), ts2), TEST_CELL, Value.create(val(0, 9), latestTs))); keyValueService.deleteAllTimestamps(TEST_TABLE, ImmutableMap.of(TEST_CELL, latestTs), true); assertThat(getAllTimestampsForTestCell(), contains(latestTs)); assertThat(keyValueService.get(TEST_TABLE, ImmutableMap.of(TEST_CELL, Long.MAX_VALUE)).get(TEST_CELL), equalTo(Value.create(val(0, 9), latestTs))); }
@Test public void deleteTimestampRangesIncludingSentinelsEdgeCases() { long minTs = 0; keyValueService.addGarbageCollectionSentinelValues(TEST_TABLE, ImmutableSet.of(TEST_CELL)); keyValueService.putWithTimestamps(TEST_TABLE, ImmutableMultimap.of( TEST_CELL, Value.create(val(0, 5), minTs), TEST_CELL, Value.create(val(0, 7), TEST_TIMESTAMP - 1), TEST_CELL, Value.create(val(0, 9), TEST_TIMESTAMP))); keyValueService.deleteAllTimestamps(TEST_TABLE, ImmutableMap.of(TEST_CELL, TEST_TIMESTAMP), true); assertThat(getAllTimestampsForTestCell(), contains(TEST_TIMESTAMP)); }
@Test public void deleteTimestampRangesLeavesSentinels() { long latestTs = 15L; keyValueService.addGarbageCollectionSentinelValues(TEST_TABLE, ImmutableSet.of(TEST_CELL)); keyValueService.put(TEST_TABLE, ImmutableMap.of(TEST_CELL, val(1, 0)), latestTs); keyValueService.deleteAllTimestamps(TEST_TABLE, ImmutableMap.of(TEST_CELL, latestTs), false); assertThat(getAllTimestampsForTestCell(), contains(Value.INVALID_VALUE_TIMESTAMP, latestTs)); assertThat(keyValueService.get(TEST_TABLE, ImmutableMap.of(TEST_CELL, Value.INVALID_VALUE_TIMESTAMP + 1L)) .get(TEST_CELL), equalTo(Value.create(new byte[0], Value.INVALID_VALUE_TIMESTAMP))); }
@Test public void testGetWithMultipleVersions() { putTestDataForMultipleTimestamps(); Map<Cell, Value> values = keyValueService.get(TEST_TABLE, ImmutableMap.of(TEST_CELL, TEST_TIMESTAMP + 2)); assertEquals("Incorrect number of values returned.", 1, values.size()); assertEquals("Incorrect value returned.", Value.create(val(0, 7), TEST_TIMESTAMP + 1), values.get(TEST_CELL)); }
private void assertOnlySentinelBeforeTs(TableReference tableRef, Cell cell, long ts) { Value sentinel = Value.create(PtBytes.EMPTY_BYTE_ARRAY, Value.INVALID_VALUE_TIMESTAMP); assertThat(keyValueService.get(tableRef, ImmutableMap.of(cell, ts)).get(cell)).isEqualTo(sentinel); }
private void assertLatestEntryBeforeTsIs(long getTs, TableReference tableRef, Cell cell, byte[] data, long ts) { Value expected = Value.create(data, ts); assertThat(keyValueService.get(tableRef, ImmutableMap.of(cell, getTs)).get(cell)).isEqualTo(expected); }
private static NavigableMap<byte[], SortedMap<byte[], Value>> breakUpValuesByRow(Result<? extends Record> records) { NavigableMap<byte[], SortedMap<byte[], Value>> ret = Maps.newTreeMap(UnsignedBytes.lexicographicalComparator()); for (Record record : records) { byte[] row = record.getValue(A_ROW_NAME); SortedMap<byte[], Value> colMap = ret.computeIfAbsent(row, rowName -> Maps.newTreeMap(UnsignedBytes.lexicographicalComparator())); colMap.put(record.getValue(A_COL_NAME), Value.create(record.getValue(A_VALUE), record.getValue(A_TIMESTAMP))); } return ret; }
@Test public void createColumnForDeleteThrowsIfCreatingWithNonEmptyValue() { assertThatThrownBy(() -> CassandraKeyValueServices.createColumnForDelete(CELL, Value.create(DATA, 1000), 2000)) .isInstanceOf(IllegalStateException.class); } }
@Test public void createColumnWithGivenValueCreatesItWithAssociatedCassandraTimestamp() { assertThat(CassandraKeyValueServices.createColumn(CELL, Value.create(DATA, 1000)).getTimestamp()) .isEqualTo(1000); assertThat(CassandraKeyValueServices.createColumn(CELL, Value.create(DATA, 5000)).getTimestamp()) .isEqualTo(5000); }
@Test public void createColumnForDeleteCreatesItWithSpecifiedCassandraTimestamp() { assertThat(CassandraKeyValueServices.createColumnForDelete( CELL, Value.create(PtBytes.EMPTY_BYTE_ARRAY, 1000), 2000) .getTimestamp()) .isEqualTo(2000); }
private Value createValue(long num) { SweepShardProgressTable.Value value = SweepShardProgressTable.Value.of(num); return Value.create(value.persistValue(), 0L); } }
@Test public void putUnlessExistsThrowsOnExists() { byte[] newContents = PtBytes.toBytes("new_value"); assertThatThrownBy(() -> getTestKvs().putUnlessExists(TEST_TABLE, ImmutableMap.of(NONEMPTY_CELL, newContents))) .isInstanceOf(KeyAlreadyExistsException.class); Map<Cell, Value> result = getTestKvs() .get(TEST_TABLE, ImmutableMap.of(NONEMPTY_CELL, AtlasDbConstants.TRANSACTION_TS)); assertThat(result.get(NONEMPTY_CELL)).isNotEqualTo(Value.create(newContents, AtlasDbConstants.TRANSACTION_TS)); }
@Test public void canPutUnlessExists() { getTestKvs().putUnlessExists(TEST_TABLE, ImmutableMap.of(EMPTY_CELL, CONTENTS)); assertLatestValueInCell(EMPTY_CELL, Value.create(CONTENTS, AtlasDbConstants.TRANSACTION_TS)); }
@Test public void deletionTakesPlaceAtFreshTimestamp() throws Exception { TableReference tableReference = TableReference.createFromFullyQualifiedName("test." + RandomStringUtils.randomAlphanumeric(16)); keyValueService.createTable(tableReference, AtlasDbConstants.GENERIC_TABLE_METADATA); byte[] data = PtBytes.toBytes("data"); byte[] moreData = PtBytes.toBytes("data2"); keyValueService.putWithTimestamps(tableReference, ImmutableListMultimap.of(CELL, Value.create(data, 8L))); keyValueService.putWithTimestamps(tableReference, ImmutableListMultimap.of(CELL, Value.create(moreData, 88L))); keyValueService.delete(tableReference, ImmutableListMultimap.of(CELL, 8L)); putDummyValueAtCellAndTimestamp(tableReference, CELL, 8L, STARTING_ATLAS_TIMESTAMP - 1); Map<Cell, Value> results = keyValueService.get(tableReference, ImmutableMap.of(CELL, 8L + 1)); assertThat(results.containsKey(CELL), is(false)); }