Skip to content

Commit c4c91d0

Browse files
authored
Add primitive exclusive start key support to Table and SecondaryIndex (#1639)
Solves: #1638
1 parent 948e656 commit c4c91d0

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

scanamo/src/main/scala/org/scanamo/SecondaryIndex.scala

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,32 @@ sealed abstract class SecondaryIndex[V] {
9393
def descending: SecondaryIndex[V]
9494

9595
def from[K: UniqueKeyCondition](key: UniqueKey[K]): SecondaryIndex[V]
96+
97+
/** Primes a search request with a key to start from:
98+
*
99+
* @param exclusiveStartKey
100+
* A [[DynamoObject]] containing attributes that match the partition key and sort key of the secondary index as
101+
* well as the partition key of the table itself
102+
* @return
103+
* A new [[SecondaryIndex]] which when queried will return items after the provided exclusive start key
104+
* @example
105+
* {{{
106+
* import org.scanamo._
107+
* import org.scanamo.syntax._
108+
*
109+
* val table: Table[_] = ???
110+
* val secondaryIndex: SecondaryIndex[_] = table.index("myIndex")
111+
* val exclusiveStartKey = DynamoObject(
112+
* Map(
113+
* "myIndexPartitionKeyName" -> myIndexPartitionKeyValue.asDynamoValue,
114+
* "myIndexSortKeyName" -> myIndexSortKeyValue.asDynamoValue,
115+
* "myTablePartitionKeyName" -> myTablePartitionKeyValue.asDynamoValue
116+
* )
117+
* )
118+
* val indexStartingFromExclusiveStartKey: SecondaryIndex[_] = secondaryIndex.from(exclusiveStartKey)
119+
* }}}
120+
*/
121+
def from(exclusiveStartKey: DynamoObject): SecondaryIndex[V]
96122
}
97123

98124
private[scanamo] case class SecondaryIndexWithOptions[V: DynamoFormat](
@@ -103,6 +129,8 @@ private[scanamo] case class SecondaryIndexWithOptions[V: DynamoFormat](
103129
def limit(n: Int): SecondaryIndexWithOptions[V] = copy(queryOptions = queryOptions.copy(limit = Some(n)))
104130
def from[K: UniqueKeyCondition](key: UniqueKey[K]) =
105131
copy(queryOptions = queryOptions.copy(exclusiveStartKey = Some(key.toDynamoObject)))
132+
def from(exclusiveStartKey: DynamoObject) =
133+
copy(queryOptions = queryOptions.copy(exclusiveStartKey = Some(exclusiveStartKey)))
106134
def filter[C: ConditionExpression](condition: C) =
107135
SecondaryIndexWithOptions[V](tableName, indexName, ScanamoQueryOptions.default).filter(Condition(condition))
108136
def filter[T](c: Condition[T]): SecondaryIndexWithOptions[V] =

scanamo/src/main/scala/org/scanamo/Table.scala

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,29 @@ case class Table[V: DynamoFormat](name: String) {
7878
*/
7979
def from[K: UniqueKeyCondition](key: UniqueKey[K]) = TableWithOptions(name, ScanamoQueryOptions.default).from(key)
8080

81+
/** Primes a search request with a key to start from:
82+
* @param exclusiveStartKey
83+
* A [[DynamoObject]] containing attributes that match the partition key and sort key of the table
84+
* @return
85+
* A new [[Table]] which when queried will return items after the provided exclusive start key
86+
* @example
87+
* {{{
88+
* import org.scanamo._
89+
* import org.scanamo.syntax._
90+
*
91+
* val table: Table[_] = ???
92+
* val exclusiveStartKey = DynamoObject(
93+
* Map(
94+
* "myPartitionKeyName" -> myPartitionKeyValue.asDynamoValue,
95+
* "mySortKeyName" -> mySortKeyValue.asDynamoValue
96+
* )
97+
* )
98+
* val tableStartingFromExclusiveStartKey: Table[_] = table.from(exclusiveStartKey)
99+
* }}}
100+
*/
101+
def from(exclusiveStartKey: DynamoObject) =
102+
TableWithOptions(name, ScanamoQueryOptions.default).from(exclusiveStartKey)
103+
81104
/** Scans all elements of a table
82105
*/
83106
def scan(): ScanamoOps[List[Either[DynamoReadError, V]]] = ScanamoFree.scan[V](name)
@@ -170,6 +193,8 @@ private[scanamo] case class ConsistentlyReadTable[V: DynamoFormat](tableName: St
170193
TableWithOptions(tableName, ScanamoQueryOptions.default).consistently.descending
171194
def from[K: UniqueKeyCondition](key: UniqueKey[K]) =
172195
TableWithOptions(tableName, ScanamoQueryOptions.default).consistently.from(key)
196+
def from(exclusiveStartKey: DynamoObject) =
197+
TableWithOptions(tableName, ScanamoQueryOptions.default).consistently.from(exclusiveStartKey)
173198
def filter[T](c: Condition[T]): TableWithOptions[V] =
174199
TableWithOptions(tableName, ScanamoQueryOptions.default).consistently.filter(c)
175200
def scan(): ScanamoOps[List[Either[DynamoReadError, V]]] =
@@ -199,6 +224,8 @@ private[scanamo] case class TableWithOptions[V: DynamoFormat](tableName: String,
199224
def descending: TableWithOptions[V] = copy(queryOptions = queryOptions.copy(ascending = false))
200225
def from[K: UniqueKeyCondition](key: UniqueKey[K]) =
201226
copy(queryOptions = queryOptions.copy(exclusiveStartKey = Some(key.toDynamoObject)))
227+
def from(exclusiveStartKey: DynamoObject) =
228+
copy(queryOptions = queryOptions.copy(exclusiveStartKey = Some(exclusiveStartKey)))
202229
def filter[T](c: Condition[T]): TableWithOptions[V] =
203230
copy(queryOptions = queryOptions.copy(filter = Some(c)))
204231

0 commit comments

Comments
 (0)