AtlasLockDescriptorUtils { public static List<CellReference> candidateCells(LockDescriptor lockDescriptor) { Optional<TableRefAndRemainder> tableRefAndRemainder = tryParseTableRef(lockDescriptor); if (!tableRefAndRemainder.isPresent()) { return ImmutableList.of(); } TableReference tableRef = tableRefAndRemainder.get().tableRef(); ByteString remainingBytes = tableRefAndRemainder.get().remainder(); return IntStream.range(1, remainingBytes.size() - 2) .filter(index -> isZeroDelimiterIndex(remainingBytes, index)) .mapToObj(index -> createCellFromByteString(remainingBytes, index)) .map(cell -> CellReference.of(tableRef, cell)) .collect(Collectors.toList()); } private  AtlasLockDescriptorUtils(); static List<CellReference> candidateCells(LockDescriptor lockDescriptor); static Optional<TableRefAndRemainder> tryParseTableRef(LockDescriptor lockDescriptor);  }
@Test public void lockDescriptorWithNoZerosReturnsEmptyForCells() { LockDescriptor descriptor = StringLockDescriptor.of("test"); assertThat(AtlasLockDescriptorUtils.candidateCells(descriptor)).isEmpty(); }
@Test public void lockDescriptorWithOneZeroReturnsEmptyForCells() { LockDescriptor descriptor = AtlasRowLockDescriptor.of(TABLE.getQualifiedName(), NO_ZERO_ROW); assertThat(AtlasLockDescriptorUtils.candidateCells(descriptor)).isEmpty(); }
@Test public void uniqueCellLockDescriptorIsDecodedCorrectlyForCells() { LockDescriptor descriptor = AtlasCellLockDescriptor.of(TABLE.getQualifiedName(), NO_ZERO_ROW, NO_ZERO_COL); assertThat(AtlasLockDescriptorUtils.candidateCells(descriptor)) .containsExactly(CellReference.of(TABLE, Cell.create(NO_ZERO_ROW, NO_ZERO_COL))); }
@Test public void rowWithNullsParsesAllCombinationsForCells() { LockDescriptor descriptor = AtlasCellLockDescriptor.of(TABLE.getQualifiedName(), ROW_WITH_ZEROS, NO_ZERO_COL); List<CellReference> expected = ImmutableList.of( Cell.create(new byte[] {1}, new byte[] {2, 0, 3, 0, 4, 5, 6}), Cell.create(new byte[] {1, 0, 2}, new byte[] {3, 0, 4, 5, 6}), Cell.create(new byte[] {1, 0, 2, 0, 3}, new byte[] {4, 5, 6})) .stream() .map(cell -> CellReference.of(TABLE, cell)) .collect(Collectors.toList()); assertThat(AtlasLockDescriptorUtils.candidateCells(descriptor)).isEqualTo(expected); assertThat(AtlasLockDescriptorUtils.candidateCells(descriptor)) .contains(CellReference.of(TABLE, Cell.create(ROW_WITH_ZEROS, NO_ZERO_COL))); }
@Test public void colWithNullsParsesAllCombinationsForCells() { LockDescriptor descriptor = AtlasCellLockDescriptor.of(TABLE.getQualifiedName(), NO_ZERO_ROW, COL_WITH_ZEROS); List<CellReference> expected = ImmutableList.of( Cell.create(new byte[] {1, 2, 3}, new byte[] {4, 0, 0, 5, 6}), Cell.create(new byte[] {1, 2, 3, 0, 4}, new byte[] {0, 5, 6}), Cell.create(new byte[] {1, 2, 3, 0, 4, 0}, new byte[] {5, 6})) .stream() .map(cell -> CellReference.of(TABLE, cell)) .collect(Collectors.toList()); assertThat(AtlasLockDescriptorUtils.candidateCells(descriptor)).isEqualTo(expected); assertThat(AtlasLockDescriptorUtils.candidateCells(descriptor)) .contains(CellReference.of(TABLE, Cell.create(NO_ZERO_ROW, COL_WITH_ZEROS))); }
@Test public void rowEndingWithZeroIsCorrectlyParsedForCombinationsForCells() { LockDescriptor descriptor = AtlasCellLockDescriptor.of(TABLE.getQualifiedName(), END_WITH_ZERO, NO_ZERO_COL); List<CellReference> expected = ImmutableList.of( Cell.create(new byte[] {4, 5, 6}, new byte[] {0, 4, 5, 6}), Cell.create(new byte[] {4, 5, 6, 0}, new byte[] {4, 5, 6})) .stream() .map(cell -> CellReference.of(TABLE, cell)) .collect(Collectors.toList()); assertThat(AtlasLockDescriptorUtils.candidateCells(descriptor)).isEqualTo(expected); assertThat(AtlasLockDescriptorUtils.candidateCells(descriptor)) .contains(CellReference.of(TABLE, Cell.create(END_WITH_ZERO, NO_ZERO_COL))); }
@Test public void rowStartingWithZeroIsIgnoredForCells() { LockDescriptor descriptor = AtlasCellLockDescriptor.of(TABLE.getQualifiedName(), START_WITH_ZERO, NO_ZERO_COL); assertThat(AtlasLockDescriptorUtils.candidateCells(descriptor)) .containsExactly(CellReference.of(TABLE, Cell.create(START_WITH_ZERO, NO_ZERO_COL))); }
@Test public void columnEndingWithZeroIsIgnoredForCells() { LockDescriptor descriptor = AtlasCellLockDescriptor.of(TABLE.getQualifiedName(), NO_ZERO_ROW, END_WITH_ZERO); assertThat(AtlasLockDescriptorUtils.candidateCells(descriptor)) .containsExactly(CellReference.of(TABLE, Cell.create(NO_ZERO_ROW, END_WITH_ZERO))); }