-
Notifications
You must be signed in to change notification settings - Fork 1
add support for rangeByCompoundKeyPrefix #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d8fcb18
add support for rangeByCompoundKeyPrefix
somdoron 5a7aca5
Update raft/src/main/scala/zio/raft/HMap.scala
somdoron 72eb8ba
Update raft/src/test/scala/zio/raft/HMapRangeByCompoundKeyPrefixSpec.…
somdoron eb3adc3
Update raft/src/test/scala/zio/raft/HMapRangeByCompoundKeyPrefixSpec.…
somdoron 4cf55e1
Update raft/src/test/scala/zio/raft/HMapPrefixRangeSpec.scala
somdoron 7b5ce39
Update raft/src/test/scala/zio/raft/HMapPrefixRangeSpec.scala
somdoron 7269653
fmt
somdoron File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
raft/src/test/scala/zio/raft/HMapRangeByCompoundKeyPrefixSpec.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| package zio.raft | ||
|
|
||
| import zio.test.* | ||
| import zio.test.Assertion.* | ||
| import java.nio.charset.StandardCharsets | ||
|
|
||
| object HMapRangeByCompoundKeyPrefixSpec extends ZIOSpecDefault: | ||
|
|
||
| // Compound key encoding: | ||
| // bytes = firstComponentUtf8 ++ 0x00 ++ secondComponentUtf8 | ||
| // This ensures that all keys that share the same first component are in a contiguous | ||
| // lexicographic range [first ++ 0x00, first ++ 0x01), which is what | ||
somdoron marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // rangeByCompoundKeyPrefix relies on by computing the upper bound via +1 on the last byte. | ||
somdoron marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| given HMap.KeyLike[(String, String)] with | ||
| def asBytes(key: (String, String)): Array[Byte] = | ||
| val (first, second) = key | ||
| val firstBytes = first.getBytes(StandardCharsets.UTF_8) | ||
| if second.isEmpty then | ||
| Array(firstBytes.length.toByte) ++ firstBytes | ||
| else | ||
| val secondBytes = second.getBytes(StandardCharsets.UTF_8) | ||
| Array(firstBytes.length.toByte) ++ firstBytes ++ Array(secondBytes.length.toByte) ++ secondBytes | ||
|
|
||
| def fromBytes(bytes: Array[Byte]): (String, String) = | ||
| val len1 = bytes(0) & 0xff | ||
| val first = new String(bytes.slice(1, 1 + len1), StandardCharsets.UTF_8) | ||
| if bytes.length == 1 + len1 then (first, "") | ||
| else | ||
| val len2Pos = 1 + len1 | ||
| val len2 = bytes(len2Pos) & 0xff | ||
| val second = new String(bytes.slice(len2Pos + 1, len2Pos + 1 + len2), StandardCharsets.UTF_8) | ||
| (first, second) | ||
|
|
||
| type Schema = ("users", (String, String), Int) *: EmptyTuple | ||
|
|
||
| def spec = suiteAll("HMap.rangeByCompoundKeyPrefix") { | ||
|
|
||
| test("computePrefixUpperBound works for compound key prefix") { | ||
| val prefix = ("r1", "") | ||
| val key = ("r1", "a") | ||
| val hmap = HMap.empty[Schema] | ||
| val keyBytes = summon[HMap.KeyLike[(String, String)]].asBytes(key) | ||
| val upper = hmap.computePrefixUpperBound(summon[HMap.KeyLike[(String, String)]].asBytes(prefix)) | ||
|
|
||
|
|
||
| assertTrue(HMap.byteArrayOrdering.compare(upper, keyBytes) > 0) | ||
| } | ||
|
|
||
| test("returns all entries that share the same first component only") { | ||
| val hmap = | ||
| HMap.empty[Schema] | ||
| .updated["users"](("r1", "a"), 1) | ||
| .updated["users"](("r1", "b"), 2) | ||
| .updated["users"](("r1", "c"), 3) | ||
| .updated["users"](("r2", "x"), 10) | ||
| .updated["users"](("r3", "y"), 20) | ||
|
|
||
| val results = hmap.rangeByCompoundKeyPrefix["users"](("r1", "")).toList | ||
| val keys = results.map(_._1) | ||
| val values = results.map(_._2) | ||
|
|
||
| assertTrue(results.length == 3) && | ||
| assertTrue(keys.toSet == Set(("r1", "a"), ("r1", "b"), ("r1", "c"))) && | ||
| assertTrue(values.toSet == Set(1, 2, 3)) | ||
| } | ||
|
|
||
| test("includes empty-second-component key and excludes other first components") { | ||
| val hmap = | ||
| HMap.empty[Schema] | ||
| .updated["users"](("ns", ""), 0) | ||
| .updated["users"](("ns", "k1"), 1) | ||
| .updated["users"](("ns", "k2"), 2) | ||
| .updated["users"](("ns2", ""), 100) | ||
| .updated["users"](("ns2", "k3"), 101) | ||
|
|
||
| val nsResults = hmap.rangeByCompoundKeyPrefix["users"](("ns", "")).toList | ||
| val nsKeys = nsResults.map(_._1).toSet | ||
| val nsValues = nsResults.map(_._2).toSet | ||
|
|
||
| val ns2Results = hmap.rangeByCompoundKeyPrefix["users"](("ns2", "")).toList | ||
| val ns2Keys = ns2Results.map(_._1).toSet | ||
| val ns2Values = ns2Results.map(_._2).toSet | ||
|
|
||
| assertTrue(nsKeys == Set(("ns", ""), ("ns", "k1"), ("ns", "k2"))) && | ||
| assertTrue(nsValues == Set(0, 1, 2)) && | ||
| assertTrue(ns2Keys == Set(("ns2", ""), ("ns2", "k3"))) && | ||
| assertTrue(ns2Values == Set(100, 101)) | ||
| } | ||
|
|
||
| test("works with unicode in first component and multiple seconds") { | ||
| val first = "régiön-𝟙" // unicode characters | ||
| val hmap = | ||
| HMap.empty[Schema] | ||
| .updated["users"]((first, "α"), 5) | ||
| .updated["users"]((first, "β"), 6) | ||
| .updated["users"]((first, "γ"), 7) | ||
| .updated["users"](("other", "δ"), 8) | ||
|
|
||
| val results = hmap.rangeByCompoundKeyPrefix["users"]((first, "")).toList | ||
|
|
||
| assertTrue(results.length == 3) && | ||
| assertTrue(results.map(_._1).toSet == Set((first, "α"), (first, "β"), (first, "γ"))) && | ||
| assertTrue(results.map(_._2).toSet == Set(5, 6, 7)) | ||
| } | ||
| } | ||
| end HMapRangeByCompoundKeyPrefixSpec | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.