无法仅保留 Aerospike 地图中的最后一个键(已排序)

问题描述 投票:0回答:1

我正在尝试编写外观代码,该代码始终在 Aerospike 的地图中保留一个(在本例中,但可能有更多键,但按顺序保留在 desc 中)。

这是门面代码:

import com.aerospike.client.Value
import com.aerospike.client.cdt.MapOperation
import com.aerospike.client.cdt.MapOrder
import com.aerospike.client.cdt.MapPolicy
import com.aerospike.client.cdt.MapReturnType
import com.aerospike.client.cdt.MapWriteMode
import com.aerospike.client.query.KeyRecord
import com.aerospike.client.reactor.AerospikeReactorClient
import reactor.core.publisher.Mono

class ForStackOverflow(
    private val client: AerospikeReactorClient,
    private val keyResolver: KeyResolver
) {

    fun updateRuidVersion(key: String, version: Long, time: Long): Mono<KeyRecord> {
        val key = getKey(key)
        return client.operate(
            null,
            key,

            // Put this version with now time
            MapOperation.put(
                MapPolicy(MapOrder.KEY_ORDERED, MapWriteMode.UPDATE),
                RuidMetadataTablesSchema.LATEST_VERSION_BIN,
                Value.get(version),
                Value.get(time)
                ),
//            // keep only the latest version
            MapOperation.removeByRankRange(
                RuidMetadataTablesSchema.LATEST_VERSION_BIN,
                -2,1,
                MapReturnType.KEY
            )

        )


    }

    private fun getKey(key: String) =
        keyResolver.getKey(key)

}

这是测试代码,显示它不是按排名删除 - 它按最后删除

@Test
fun `trying to keep only last version in table`(){
    val client = AerospikeReactorClientBuilder.getAerospikeReactorClient(config)
    val writerFacade = ForStackOverflow(
        client,
        simpleKeyResolver
    )
    val myId = "id-" + System.currentTimeMillis()
    val versions = (0..20).map {it}
    val allWrites = versions.reversed().map { version ->
        writerFacade.updateRuidVersion(
            myId, version.toLong(), System.currentTimeMillis()
        )
    }
    allWrites.forEach{
        it.block()
        val res = client.get(simpleKeyResolver.getKey(myId)).block()!!
        println(res.record.bins[RuidMetadataTablesSchema.LATEST_VERSION_BIN] as TreeMap<Long, Long>)
    }
}

并打印以下内容:

{20=1716813009091}
{19=1716813009114}
{19=1716813009114}
{19=1716813009114}
{19=1716813009114}
{19=1716813009114}
{19=1716813009114}
{19=1716813009114}
{19=1716813009114}
{19=1716813009114}
{19=1716813009114}
{19=1716813009114}
{19=1716813009114}
{19=1716813009114}
{19=1716813009114}
{19=1716813009114}
{19=1716813009114}
{19=1716813009114}
{19=1716813009114}
{1=1716813009115}
{1=1716813009115}

我希望它总是只打印 20= 并且不会用旧密钥覆盖它

kotlin aerospike
1个回答
0
投票

排名是按键值对中的值(示例中的时间戳)顺序排列的。如果值相同,则按键顺序确定排名。想知道在每个条目之间的循环中添加 1 毫秒睡眠是否会带来一些清晰度。即使时间戳值唯一。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.