Skip to content

Commit 89f9bed

Browse files
committed
ReverseOrderingTest
1 parent 9c44815 commit 89f9bed

File tree

2 files changed

+48
-15
lines changed

2 files changed

+48
-15
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package ordering
2+
3+
import org.junit.jupiter.api.Assertions.assertEquals
4+
import org.junit.jupiter.api.Test
5+
import swaydb.java.memory.MapConfig
6+
import swaydb.java.serializers.Default
7+
8+
internal class ReverseOrderingTest {
9+
@Test
10+
fun reverse() {
11+
val map =
12+
MapConfig.functionsOff(Default.intSerializer(), Default.intSerializer()) //provide a typed comparator that reverses ordering
13+
.setTypedComparator { key1: Int, key2: Int? -> key1.compareTo(key2!!) * -1 }
14+
.get()
15+
16+
//insert in natural ordering from 1 to 100
17+
(1..100).forEach { map.put(it, it) }
18+
19+
val actual =
20+
map
21+
.keys()
22+
.stream()
23+
.materialize()
24+
25+
//print out the stream. Since ordering is in reverse this will print from 100 to 1.
26+
actual.forEach(::println)
27+
28+
val expected = (1..100).map { 100 - it + 1 }
29+
30+
assertEquals(expected, actual)
31+
}
32+
}

src/main/kotlin/quickstart/QuickStart.kt

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,19 @@ import java.time.Duration
99
object QuickStart {
1010

1111
//some function with random logic that we want to use in our map
12-
val function = PureFunction.OnKeyValue<Int, Int, Return.Map<Int>> { key, value, _ ->
13-
when {
14-
key < 25 -> //remove if key is less than 25
15-
Return.remove()
16-
key < 50 -> //expire after 2 seconds if key is less than 50
17-
Return.expire(Duration.ofSeconds(2))
18-
key < 75 -> //update if key is < 75.
19-
Return.update(value!! + 10000000)
20-
else -> //else do nothing
21-
Return.nothing()
12+
private val function =
13+
PureFunction.OnKeyValue<Int, Int, Return.Map<Int>> { key, value, _ ->
14+
when {
15+
key < 25 -> //remove if key is less than 25
16+
Return.remove()
17+
key < 50 -> //expire after 2 seconds if key is less than 50
18+
Return.expire(Duration.ofSeconds(2))
19+
key < 75 -> //update if key is < 75.
20+
Return.update(value!! + 10000000)
21+
else -> //else do nothing
22+
Return.nothing()
23+
}
2224
}
23-
}
2425

2526
@JvmStatic
2627
fun main(args: Array<String>) {
@@ -37,15 +38,15 @@ object QuickStart {
3738
map.remove(1) //basic remove
3839

3940
//atomic write a Stream of key-value
40-
map.put(Stream.range(1, 100).map { item -> KeyVal.create(item) })
41+
map.put(Stream.range(1, 100).map { KeyVal.create(it) })
4142

4243
//create a read stream from 10th key-value to 90th, increment values by 1000000 and insert.
4344
val updatedKeyValuesStream =
4445
map
4546
.from(10)
4647
.stream()
47-
.takeWhile { keyVal -> keyVal.key() <= 90 }
48-
.map { keyVal -> KeyVal.create(keyVal.key(), keyVal.value() + 5000000) }
48+
.takeWhile { it.key() <= 90 }
49+
.map { KeyVal.create(it.key(), it.value() + 5000000) }
4950

5051
map.put(updatedKeyValuesStream)
5152

@@ -54,6 +55,6 @@ object QuickStart {
5455
//print all key-values to view the update.
5556
map
5657
.stream()
57-
.forEach { println(it) }
58+
.forEach(::println)
5859
}
5960
}

0 commit comments

Comments
 (0)