All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.aws.sdk.kotlin.hll.dynamodbmapper.operations.IndexOperations.kt Maven / Gradle / Ivy

There is a newer version: 1.3.99-beta
Show newest version
// Code generated by dynamodb-mapper-ops-codegen. DO NOT EDIT!

package aws.sdk.kotlin.hll.dynamodbmapper.operations

import aws.sdk.kotlin.hll.dynamodbmapper.annotations.ManualPagination
import aws.sdk.kotlin.hll.dynamodbmapper.model.IndexSpec
import aws.smithy.kotlin.runtime.ExperimentalApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow

/**
 * Provides access to operations on a particular index, which will invoke low-level
 * operations after mapping objects to items and vice versa
 * @param T The type of objects which will be read from and/or written to this index
 */
@ExperimentalApi
public interface IndexOperations : ItemSourceOperations {

    @ManualPagination(paginatedEquivalent = "queryPaginated")
    public override suspend fun query(request: QueryRequest): QueryResponse

    @ManualPagination(paginatedEquivalent = "scanPaginated")
    public override suspend fun scan(request: ScanRequest): ScanResponse
}

@ExperimentalApi
public fun  IndexOperations.queryPaginated(initialRequest: QueryRequest): Flow> = flow {
    var cursor = initialRequest.exclusiveStartKey
    var hasNextPage = true

    while (hasNextPage) {
        val req = initialRequest.copy { exclusiveStartKey = cursor }

        @OptIn(ManualPagination::class)
        val res = [email protected](req)

        cursor = res.lastEvaluatedKey
        hasNextPage = cursor != null
        emit(res)
    }
}

@ExperimentalApi
public inline fun  IndexOperations.queryPaginated(crossinline block: QueryRequestBuilder.() -> Unit): Flow> =
    queryPaginated(QueryRequestBuilder().apply(block).build())

@ExperimentalApi
public fun  IndexOperations.scanPaginated(initialRequest: ScanRequest): Flow> = flow {
    var cursor = initialRequest.exclusiveStartKey
    var hasNextPage = true

    while (hasNextPage) {
        val req = initialRequest.copy { exclusiveStartKey = cursor }

        @OptIn(ManualPagination::class)
        val res = [email protected](req)

        cursor = res.lastEvaluatedKey
        hasNextPage = cursor != null
        emit(res)
    }
}

@ExperimentalApi
public inline fun  IndexOperations.scanPaginated(crossinline block: ScanRequestBuilder.() -> Unit): Flow> =
    scanPaginated(ScanRequestBuilder().apply(block).build())

internal class IndexOperationsImpl(private val spec: IndexSpec) : IndexOperations {

    @ManualPagination(paginatedEquivalent = "queryPaginated")
    override suspend fun query(request: QueryRequest) = queryOperation(spec).execute(request)

    @ManualPagination(paginatedEquivalent = "scanPaginated")
    override suspend fun scan(request: ScanRequest) = scanOperation(spec).execute(request)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy