Skip to content

Commit

Permalink
Fix issues with descending scans. Set ScanRange properly.
Browse files Browse the repository at this point in the history
  • Loading branch information
jurmous committed Feb 26, 2025
1 parent 6301cb4 commit bbe6886
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ data class ScanRange internal constructor(
when {
startKey != null && (end == null || end.isEmpty() || startKey < end) ->
if (inclusiveStartKey) startKey else startKey.prevByteInSameLength()
endInclusive -> end
else -> end?.prevByteInSameLength()
endInclusive -> end?.nextByteInSameLength()
else -> end
}

/** Get the ascending start key for scans */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ internal fun <DM : IsRootDataModel> scanIndex(

for (indexRange in indexScanRange.ranges.reversed()) {
val lastKey = indexRange.getDescendingStartKey(startKey, scanRequest.includeStart)?.let {
if (indexRange.endInclusive && indexRange.end === it) byteArrayOf() else it
if (indexRange.endInclusive && indexRange.end === it) null else it
}

val startIndex = lastKey?.let { endRange ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ internal fun <DM : IsRootDataModel> scanIndex(
for (indexRange in indexScanRange.ranges.reversed()) {
val indexStartKey = indexRange.getDescendingStartKey(startKey, keyScanRange.includeStart)?.let {
// If was not highered it was not possible so scan to lastIndex
if (indexRange.endInclusive && indexRange.end === it) byteArrayOf() else it
if (indexRange.endInclusive && indexRange.end === it) null else it
}

if (indexStartKey == null || indexStartKey.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fun <DM: IsRootDataModel> DM.optimizeTableScan(
val minimumKeyScanByteRange = this.Meta.minimumKeyScanByteRange ?: this.Meta.keyByteSize.toUInt()

if (keyScanRanges.equalBytes < minimumKeyScanByteRange) {
throw StorageException("Key scan bytes (${keyScanRanges.equalBytes}) must be more or equal than minimum key scan bytes ($minimumKeyScanByteRange). Or set an order to guide scan to an index")
throw StorageException("${this.Meta.name}: Key scan bytes (${keyScanRanges.equalBytes}) must be more or equal than minimum key scan bytes ($minimumKeyScanByteRange). Or set an order to guide scan to an index")
}

return tableScan
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class DataStoreScanMultiTypeTest(
index = byteArrayOf(17),
direction = Direction.ASC,
startKey = byteArrayOf(0, -36),
stopKey = byteArrayOf(0, -36),
stopKey = byteArrayOf(0, -35),
)) { scanResponse.dataFetchType }

// Mind that Measurement is sorted in reverse, so it goes back in time going forward
Expand Down Expand Up @@ -269,7 +269,7 @@ class DataStoreScanMultiTypeTest(
index = byteArrayOf(26, 17, 21, 9),
direction = Direction.ASC,
startKey = byteArrayOf(0, -86),
stopKey = byteArrayOf(0, -76),
stopKey = byteArrayOf(0, -75),
)) { scanResponse.dataFetchType }

// Reverse order
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ class DataStoreScanOnIndexTest(
direction = Direction.ASC,
index = byteArrayOf(10, 17),
startKey = byteArrayOf(0, 2),
stopKey = byteArrayOf(0, 2),
stopKey = byteArrayOf(0, 3),
)) { scanResponse.dataFetchType }

scanResponse.values[0].let {
Expand Down Expand Up @@ -369,11 +369,10 @@ class DataStoreScanOnIndexTest(
expect(FetchByIndexScan(
direction = Direction.DESC,
index = byteArrayOf(10, 17),
startKey = byteArrayOf(0, 2),
startKey = byteArrayOf(0, 3),
stopKey = byteArrayOf(),
)) { scanResponse.dataFetchType }


scanResponse.values[0].let {
expect(logs[1]) { it.values }
expect(keys[1]) { it.key }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class DataStoreScanOnIndexWithPersonTest(
direction = Direction.ASC,
index = byteArrayOf(4, 2, 10, 17, 2, 10, 9),
startKey = byteArrayOf(75, 97, 115, 116, 101, 110, 115),
stopKey = byteArrayOf(75, 97, 115, 116, 101, 110, 115),
stopKey = byteArrayOf(75, 97, 115, 116, 101, 110, 116),
)) { scanResponse.dataFetchType }

// Sorted on name
Expand Down Expand Up @@ -137,7 +137,7 @@ class DataStoreScanOnIndexWithPersonTest(
direction = Direction.ASC,
index = byteArrayOf(4, 2, 10, 17, 2, 10, 9),
startKey = byteArrayOf(75, 97, 115, 116, 101, 110, 115, 75, 97, 114, 101, 108),
stopKey = byteArrayOf(75, 97, 115, 116, 101, 110, 115, 75, 97, 114, 101, 108),
stopKey = byteArrayOf(75, 97, 115, 116, 101, 110, 115, 75, 97, 114, 101, 109),
)) { scanResponse.dataFetchType }

// Sorted on name
Expand Down

0 comments on commit bbe6886

Please sign in to comment.