protected void addToResultsMap(SortedMap<byte[], RowResult<byte[]>> map, String rowKey, String colShortName, byte[] value) { Cell resultCell = getCell(rowKey, colShortName); map.put(rowKey.getBytes(), RowResult.of(resultCell, value)); }
int getNextId() { RowColumnRangeIterator iterator = kvs.getRowsColumnRange( ID_TO_NAME, Collections.singleton(rowAsBytes), BatchColumnRangeSelection.create(null, null, 1), Long.MAX_VALUE).getOrDefault(rowAsBytes, emptyIterator()); if (!iterator.hasNext()) { return 1; } Map.Entry<Cell, Value> first = iterator.next(); RowResult<byte[]> rowResult = RowResult.of(first.getKey(), first.getValue().getContents()); SweepIdToNameRowResult deserializedRowResult = SweepIdToNameRowResult.of(rowResult); return Ints.checkedCast( Iterables.getOnlyElement(deserializedRowResult.getColumnValues()).getColumnName().getTableId()) + 1; }
private void updateWatermarkForTable(long newWatermark, TableReference table) { while (true) { Cell cell = cell(table); Optional<Value> currentValue = Optional.ofNullable( kvs.get(CLEARS, ImmutableMap.of(cell, Long.MAX_VALUE)).get(cell)); Optional<Long> currentWatermark = currentValue.map(value -> RowResult.of(cell, value.getContents())) .map(TableClearsRowResult::of) .map(TableClearsRowResult::getLastClearedTimestamp); if (currentWatermark.isPresent() && currentWatermark.get() > newWatermark) { return; } CheckAndSetRequest request = currentWatermark .map(watermark -> CheckAndSetRequest.singleCell( CLEARS, cell, EncodingUtils.encodeVarLong(watermark), EncodingUtils.encodeVarLong(newWatermark))) .orElseGet(() -> CheckAndSetRequest.newCell( CLEARS, cell, EncodingUtils.encodeVarLong(newWatermark))); try { kvs.checkAndSet(request); return; } catch (CheckAndSetException e) { // do nothing, we spin } } }
@Override public Map<TableReference, Long> getWatermarks(Set<TableReference> tableReferences) { Set<Cell> cells = tableReferences.stream().map(table -> cell(table)).collect(Collectors.toSet()); Map<Cell, Value> fetched = kvs.get(CLEARS, Maps.asMap(cells, ignored -> Long.MAX_VALUE)); return KeyedStream.stream(fetched) .map((cell, value) -> RowResult.of(cell, value.getContents())) .map(TableClearsRowResult::of) .mapKeys((cell, rowResult) -> tableRef(rowResult.getRowName())) .map(TableClearsRowResult::getLastClearedTimestamp) .collectToMap(); }
@Test public void testRowRangeSecondColumn() { AbstractTransaction transaction = mock(AbstractTransaction.class); Cell expectedCell = getCell(TEST_ROW_KEY, SECOND_COL_SHORT_NAME); Cell anotherExpectedCell = getCell(TEST_ROW_KEY2, SECOND_COL_SHORT_NAME); RangeRequest expectedRange = RangeRequest.builder() .startRowInclusive(TEST_ROW_KEY.getBytes()) .endRowExclusive(RANGE_END_ROW_KEY.getBytes()) .retainColumns(SECOND_COLUMN_SELECTION) .build(); when(transaction.getRange(tableRef, expectedRange)).thenReturn( BatchingVisitableFromIterable.create(Arrays.asList( RowResult.of(expectedCell, STRING_VALUE_PERSISTER.persistToBytes(TEST_VALUE_STRING)), RowResult.of(anotherExpectedCell, STRING_VALUE_PERSISTER.persistToBytes(TEST_VALUE_STRING2)) )) ); Map<String, StringValue> result = getRangeSecondColumn(transaction, TEST_ROW_KEY, RANGE_END_ROW_KEY); assertThat(result) .isEqualTo(ImmutableMap.of(TEST_ROW_KEY, TEST_VALUE_STRING, TEST_ROW_KEY2, TEST_VALUE_STRING2)); verify(transaction, times(1)).getRange(tableRef, expectedRange); }
@Test public void testRowRangeSecondColumnFirstTwoResults() { AbstractTransaction transaction = mock(AbstractTransaction.class); Cell expectedCell = getCell(TEST_ROW_KEY, SECOND_COL_SHORT_NAME); Cell anotherExpectedCell = getCell(TEST_ROW_KEY2, SECOND_COL_SHORT_NAME); Cell cellToBeDroppedFromResults = getCell(TEST_ROW_KEY3, SECOND_COL_SHORT_NAME); RangeRequest expectedRange = RangeRequest.builder() .startRowInclusive(TEST_ROW_KEY.getBytes()) .endRowExclusive(RANGE_END_ROW_KEY.getBytes()) .retainColumns(SECOND_COLUMN_SELECTION) .batchHint(2) .build(); when(transaction.getRange(tableRef, expectedRange)).thenReturn( BatchingVisitableFromIterable.create(Arrays.asList( RowResult.of(expectedCell, STRING_VALUE_PERSISTER.persistToBytes(TEST_VALUE_STRING)), RowResult.of(anotherExpectedCell, STRING_VALUE_PERSISTER.persistToBytes(TEST_VALUE_STRING2)), RowResult.of(cellToBeDroppedFromResults, STRING_VALUE_PERSISTER.persistToBytes(TEST_VALUE_STRING3)) )) ); Map<String, StringValue> result = getRangeSecondColumnOnlyFirstTwoResults(transaction, TEST_ROW_KEY, RANGE_END_ROW_KEY); assertThat(result) .isEqualTo(ImmutableMap.of(TEST_ROW_KEY, TEST_VALUE_STRING, TEST_ROW_KEY2, TEST_VALUE_STRING2)); verify(transaction, times(1)).getRange(tableRef, expectedRange); }
private void updateWatermarkForTable(long newWatermark, TableReference table) { while (true) { Cell cell = cell(table); Optional<Value> currentValue = Optional.ofNullable( kvs.get(CLEARS, ImmutableMap.of(cell, Long.MAX_VALUE)).get(cell)); Optional<Long> currentWatermark = currentValue.map(value -> RowResult.of(cell, value.getContents())) .map(TableClearsRowResult::of) .map(TableClearsRowResult::getLastClearedTimestamp); if (currentWatermark.isPresent() && currentWatermark.get() > newWatermark) { return; } CheckAndSetRequest request = currentWatermark .map(watermark -> CheckAndSetRequest.singleCell( CLEARS, cell, EncodingUtils.encodeVarLong(watermark), EncodingUtils.encodeVarLong(newWatermark))) .orElseGet(() -> CheckAndSetRequest.newCell( CLEARS, cell, EncodingUtils.encodeVarLong(newWatermark))); try { kvs.checkAndSet(request); return; } catch (CheckAndSetException e) { // do nothing, we spin } } }
int getNextId() { RowColumnRangeIterator iterator = kvs.getRowsColumnRange( ID_TO_NAME, Collections.singleton(rowAsBytes), BatchColumnRangeSelection.create(null, null, 1), Long.MAX_VALUE).getOrDefault(rowAsBytes, emptyIterator()); if (!iterator.hasNext()) { return 1; } Map.Entry<Cell, Value> first = iterator.next(); RowResult<byte[]> rowResult = RowResult.of(first.getKey(), first.getValue().getContents()); SweepIdToNameRowResult deserializedRowResult = SweepIdToNameRowResult.of(rowResult); return Ints.checkedCast( Iterables.getOnlyElement(deserializedRowResult.getColumnValues()).getColumnName().getTableId()) + 1; }
@Test public void testGetRows() { final Set<byte[]> oneRow = ImmutableSortedSet.orderedBy(PtBytes.BYTES_COMPARATOR).add(ROW_BYTES).build(); final ColumnSelection oneColumn = ColumnSelection.create(ImmutableList.of(COL_BYTES)); final Set<byte[]> noRows = ImmutableSortedSet.orderedBy(PtBytes.BYTES_COMPARATOR).build(); final SortedMap<byte[], RowResult<byte[]>> emptyResults = ImmutableSortedMap.<byte[], RowResult<byte[]>>orderedBy(PtBytes.BYTES_COMPARATOR).build(); final RowResult<byte[]> rowResult = RowResult.of(Cell.create(ROW_BYTES, COL_BYTES), VALUE_BYTES); final SortedMap<byte[], RowResult<byte[]>> oneResult = ImmutableSortedMap.<byte[], RowResult<byte[]>>orderedBy(PtBytes.BYTES_COMPARATOR) .put(ROW_BYTES, rowResult) .build(); mockery.checking(new Expectations() { { // row result is cached after first call, so second call requests no rows oneOf(txn).getRows(table, oneRow, oneColumn); will(returnValue(oneResult)); oneOf(txn).getRows(table, noRows, oneColumn); will(returnValue(emptyResults)); } }); Assert.assertEquals(oneResult, ct.getRows(table, oneRow, oneColumn)); Assert.assertEquals(oneResult, ct.getRows(table, oneRow, oneColumn)); mockery.assertIsSatisfied(); }
@Override public Map<TableReference, Long> getWatermarks(Set<TableReference> tableReferences) { Set<Cell> cells = tableReferences.stream().map(table -> cell(table)).collect(Collectors.toSet()); Map<Cell, Value> fetched = kvs.get(CLEARS, Maps.asMap(cells, ignored -> Long.MAX_VALUE)); return KeyedStream.stream(fetched) .map((cell, value) -> RowResult.of(cell, value.getContents())) .map(TableClearsRowResult::of) .mapKeys((cell, rowResult) -> tableRef(rowResult.getRowName())) .map(TableClearsRowResult::getLastClearedTimestamp) .collectToMap(); }