@SuppressWarnings("checkstyle:RegexpSinglelineJava") static Cell getOldMetadataCell(TableReference tableRef) { return Cell.create( tableRef.getQualifiedName().getBytes(Charset.defaultCharset()), METADATA_COL); }
public static <T extends Persistable, V extends Persistable> Set<Cell> toCells(Multimap<T, V> map) { Set<Cell> ret = Sets.newHashSetWithExpectedSize(map.size()); for (Entry<T, Collection<V>> e : map.asMap().entrySet()) { byte[] rowName = e.getKey().persistToBytes(); for (Persistable val : e.getValue()) { ret.add(Cell.create(rowName, val.persistToBytes())); } } return ret; }
static Map<Cell, byte[]> generateRandomBatch(Random random, int size) { Map<Cell, byte[]> map = Maps.newHashMapWithExpectedSize(size); for (int j = 0; j < size; j++) { byte[] key = generateKey(random); byte[] value = generateValue(random); map.put(Cell.create(key, Tables.COLUMN_NAME_IN_BYTES.array()), value); } return map; }
Optional<TableReference> get(int tableId) { SweepIdToNameColumn column = SweepIdToNameColumn.of(tableId); Cell cell = Cell.create(rowAsBytes, column.persistToBytes()); Map<Cell, Value> values = kvs.get(ID_TO_NAME, Collections.singletonMap(cell, Long.MAX_VALUE)); return Optional.ofNullable(values.get(cell)) .map(Value::getContents) .map(SweepIdToNameColumnValue::hydrateValue) .map(TableReference::fromString); }
@Override public void removeTable(TableReference tableRef) { Cell key = Cell.create(getBytesForTableRef(tableRef), AtlasDbConstants.NAMESPACE_SHORT_COLUMN_BYTES); if (kv.getAllTableNames().contains(AtlasDbConstants.NAMESPACE_TABLE)) { kv.delete(AtlasDbConstants.NAMESPACE_TABLE, ImmutableMultimap.of(key, 0L)); } // Need to invalidate the table ref in case we end up re-creating the same table // again. Frequently when we drop one table we end up dropping a bunch of tables, // so just invalidate everything. tableMap.set(HashBiMap.<TableReference, TableReference>create()); }
Optional<SweepTableIdentifier> currentMapping(TableReference table) { SweepNameToIdRow row = SweepNameToIdRow.of(table.getQualifiedName()); Cell cell = Cell.create(row.persistToBytes(), SweepNameToIdNamedColumn.ID.getShortName()); return Optional.ofNullable(kvs.get(NAME_TO_ID, Collections.singletonMap(cell, Long.MAX_VALUE)).get(cell)) .map(Value::getContents) .map(SweepTableIdentifier.BYTES_HYDRATOR::hydrateFromBytes); } }
private void commit(long timestamp) { Cell cell = Cell.create( TransactionConstants.getValueForTimestamp(timestamp), TransactionConstants.COMMIT_TS_COLUMN); byte[] value = TransactionConstants.getValueForTimestamp(timestamp); delegate().putUnlessExists(TransactionConstants.TRANSACTION_TABLE, ImmutableMap.of(cell, value)); } }
private static CellWithTimestamp getCellFromKeylessRow(CqlRow row, byte[] key) { byte[] rowName = key; byte[] columnName = row.getColumns().get(0).getValue(); long timestamp = extractTimestamp(row, 1); return CellWithTimestamp.of(Cell.create(rowName, columnName), timestamp); }
private static CellWithTimestamp getCellFromRow(CqlRow row) { byte[] rowName = row.getColumns().get(0).getValue(); byte[] columnName = row.getColumns().get(1).getValue(); long timestamp = extractTimestamp(row, 2); return CellWithTimestamp.of(Cell.create(rowName, columnName), timestamp); }
private String get(KeyValueService kvs, TableReference table, String row, long ts) { Cell cell = Cell.create(row.getBytes(StandardCharsets.UTF_8), COL.getBytes(StandardCharsets.UTF_8)); Value val = kvs.get(table, ImmutableMap.of(cell, ts)).get(cell); return val == null ? null : new String(val.getContents()); }
@Test public void testGetCell() { final Cell cell = Cell.create(ROW_BYTES, COL_BYTES); final Map<Cell, byte[]> cellValueMap = ImmutableMap.<Cell, byte[]>builder() .put(cell, VALUE_BYTES) .build(); // cell is cached after first call, so second call requests no cells testGetCellResults(cell, cellValueMap); }
@Test public void putUnlessExistsDoesNotConflictForMultipleCellsSameColumn() { Assume.assumeTrue(checkAndSetSupported()); Cell firstTestCell = Cell.create(row(0), column(0)); Cell nextTestCell = Cell.create(row(1), column(0)); keyValueService.putUnlessExists(TEST_TABLE, ImmutableMap.of(firstTestCell, val(0, 0))); keyValueService.putUnlessExists(TEST_TABLE, ImmutableMap.of(nextTestCell, val(0, 1))); // Legal as the cells are different }
private long put(SerializableTransactionManager txm, TableReference table, String row, String val) { Cell cell = Cell.create(row.getBytes(StandardCharsets.UTF_8), COL.getBytes(StandardCharsets.UTF_8)); return txm.runTaskWithRetry(t -> { t.put(table, ImmutableMap.of(cell, val.getBytes(StandardCharsets.UTF_8))); return t.getTimestamp(); }); }
private List<WriteInfo> generateHundredWrites(int startCol, long startTs) { List<WriteInfo> writeInfos = new ArrayList<>(); for (int i = 0; i < 100; i++) { writeInfos.add(WriteInfo.write(TABLE_CONS, Cell.create(DEFAULT_CELL.getRowName(), PtBytes.toBytes(startCol * 100 + i)), startTs)); } return writeInfos; } }
boolean storeNewMapping(TableReference table, int id) { SweepIdToNameColumn column = SweepIdToNameColumn.of(id); SweepIdToNameColumnValue value = SweepIdToNameColumnValue.of(column, table.getQualifiedName()); Cell cell = Cell.create(rowAsBytes, value.persistColumnName()); CheckAndSetRequest request = CheckAndSetRequest.newCell(ID_TO_NAME, cell, value.persistValue()); try { kvs.checkAndSet(request); return true; } catch (CheckAndSetException e) { return get(id).get().equals(table); } }
private void setupTwoReadableBoundsInKv() { backupRunner.backupExistingTimestamp(); byte[] rowAndColumnNameBytes = PtBytes.toBytes(CassandraTimestampUtils.ROW_AND_COLUMN_NAME); kv.put(AtlasDbConstants.TIMESTAMP_TABLE, ImmutableMap.of(Cell.create(rowAndColumnNameBytes, rowAndColumnNameBytes), PtBytes.toBytes(0L)), Long.MAX_VALUE - 1); } }
@Test public void partiallyFilledRowsShouldBeVisible() { byte[] defaultRow = PtBytes.toBytes("row1"); final Cell emptyCell = Cell.create(defaultRow, PtBytes.toBytes("column1")); final Cell writtenCell = Cell.create(defaultRow, PtBytes.toBytes("column2")); writeCells(TABLE, ImmutableMap.of(writtenCell, PtBytes.toBytes("writtenCell"))); RowResult<byte[]> rowResult = readRow(defaultRow); assertThat(rowResult, is(notNullValue())); assertThat(rowResult.getCellSet(), hasItem(writtenCell)); assertThat(rowResult.getCellSet(), not(hasItem(emptyCell))); }
@Test public void testCheckAndSetToNewCellsInDistinctRows() { Assume.assumeTrue(checkAndSetSupported()); Cell firstTestCell = Cell.create(row(0), column(0)); Cell nextTestCell = Cell.create(row(0), column(1)); keyValueService.checkAndSet(CheckAndSetRequest.newCell(TEST_TABLE, firstTestCell, val(0, 0))); keyValueService.checkAndSet(CheckAndSetRequest.newCell(TEST_TABLE, nextTestCell, val(0, 1))); verifyCheckAndSet(firstTestCell, val(0, 0)); verifyCheckAndSet(nextTestCell, val(0, 1)); }
@Test public void testCheckAndSetIndependentlyWorks() { Assume.assumeTrue(checkAndSetSupported()); Cell firstTestCell = Cell.create(row(0), column(0)); Cell nextTestCell = Cell.create(row(0), column(1)); keyValueService.checkAndSet(CheckAndSetRequest.newCell(TEST_TABLE, firstTestCell, val(0, 0))); keyValueService.checkAndSet(CheckAndSetRequest.newCell(TEST_TABLE, nextTestCell, val(0, 1))); keyValueService.checkAndSet(CheckAndSetRequest.singleCell(TEST_TABLE, firstTestCell, val(0, 0), val(0, 1))); verifyCheckAndSet(firstTestCell, val(0, 1)); verifyCheckAndSet(nextTestCell, val(0, 1)); }
@Test public void testWriteChangedConflictsNoThrow() { overrideConflictHandlerForTable(TABLE, ConflictHandler.RETRY_ON_VALUE_CHANGED); final Cell cell = Cell.create(PtBytes.toBytes("row1"), PtBytes.toBytes("column1")); Transaction t1 = txManager.createNewTransaction(); Transaction t2 = txManager.createNewTransaction(); t1.delete(TABLE, ImmutableSet.of(cell)); t2.delete(TABLE, ImmutableSet.of(cell)); t1.commit(); t2.commit(); }