/
QueryNScan.kt
134 lines (121 loc) · 4.06 KB
/
QueryNScan.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* Copyright 2021 Square Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package app.cash.tempest2.guides
import app.cash.tempest2.BeginsWith
import app.cash.tempest2.Between
import app.cash.tempest2.Offset
import app.cash.tempest2.Page
import app.cash.tempest2.musiclibrary.AlbumTrack
import app.cash.tempest2.musiclibrary.MusicTable
import software.amazon.awssdk.enhanced.dynamodb.Expression
import software.amazon.awssdk.services.dynamodb.model.AttributeValue
import java.time.Duration
class QueryNScan(
private val table: MusicTable
) {
// Query - Key Condition - Partition Key and Entity Type.
fun loadAlbumTracks(albumToken: String): List<AlbumTrack> {
val page = table.albumTracks.query(
keyCondition = BeginsWith(
prefix = AlbumTrack.Key(albumToken)
)
)
return page.contents
}
// Query - Key Condition - Partition Key and Sort Key Prefix.
fun loadAlbumTracks2(albumToken: String): List<AlbumTrack> {
val page = table.albumTracksByTitle.query(
keyCondition = BeginsWith(
prefix = AlbumTrack.TitleIndexOffset(albumToken, track_title = "I want ")
)
)
return page.contents
}
// Query - Key Condition - Partition Key and Sort Key Range.
fun loadAlbumTracks3(albumToken: String): List<AlbumTrack> {
val page = table.albumTracks.query(
keyCondition = Between(
startInclusive = AlbumTrack.Key(albumToken, track_number = 5),
endInclusive = AlbumTrack.Key(albumToken, track_number = 9)
)
)
return page.contents
}
// Query - Descending Order.
fun loadAlbumTracks4(albumToken: String): List<AlbumTrack> {
val page = table.albumTracks.query(
keyCondition = BeginsWith(
prefix = AlbumTrack.Key(albumToken)
),
asc = false
)
return page.contents
}
// Query - Filter Expression
fun loadAlbumTracks5(albumToken: String): List<AlbumTrack> {
val page = table.albumTracks.query(
keyCondition = BeginsWith(prefix = AlbumTrack.Key(albumToken)),
filterExpression = runLengthLongerThan(Duration.ofMinutes(3))
)
return page.contents
}
private fun runLengthLongerThan(duration: Duration): Expression {
return Expression.builder()
.expression("run_length > :duration")
.expressionValues(
mapOf(
":duration" to AttributeValue.builder().s(duration.toString()).build()
)
)
.build()
}
// Query - Pagination.
fun loadAlbumTracks6(albumToken: String): List<AlbumTrack> {
val tracks = mutableListOf<AlbumTrack>()
var page: Page<AlbumTrack.Key, AlbumTrack>? = null
do {
page = table.albumTracks.query(
keyCondition = BeginsWith(AlbumTrack.Key(albumToken)),
pageSize = 10,
initialOffset = page?.offset
)
tracks.addAll(page.contents)
} while (page?.hasMorePages == true)
return tracks.toList()
}
// Query - Specified Offset
fun loadAlbumTracksAfterTrack(albumToken: String, trackToken: String): List<AlbumTrack> {
val tracks = mutableListOf<AlbumTrack>()
var page: Page<AlbumTrack.Key, AlbumTrack>? = null
val offset = Offset(AlbumTrack.Key(trackToken))
do {
page = table.albumTracks.query(
keyCondition = BeginsWith(AlbumTrack.Key(albumToken)),
pageSize = 10,
initialOffset = page?.offset ?: offset
)
tracks.addAll(page.contents)
} while (page?.hasMorePages == true)
return tracks.toList()
}
// Scan.
fun loadAllAlbumTracks(): List<AlbumTrack> {
val page = table.albumTracks.scan()
return page.contents
}
// Scan - Parallel.
// Not supported.
}