dongsi3826 2019-04-08 11:37
浏览 108
已采纳

如何使用SeekToLast获取最后的键值

I want the best performance code to get the last key value in the database. I google this problem and find SeekToLast function but I don't know how to use it.

  • 写回答

1条回答 默认 最新

  • dongyi6269 2019-04-10 13:37
    关注

    Do you mean goleveldb?

    If so then it will look like

    iter := db.NewIterator(nil, nil)
    ok := iter.Last()
    if ok {
        key = iter.Key()
        value = iter.Value()
    }
    iter.Release() // Note: you should first get data and then release iterator
    err = iter.Error()
    

    Or to iterate from the end to the beginning.

    iter := db.NewIterator(nil, nil)
    for ok := iter.Last(); ok; ok = iter.Prev() {
        // Use key/value.
        // ...
    }
    iter.Release()
    err = iter.Error()
    

    You may also retrieve last key-value with specific prefix, or in general from some range (more details: https://godoc.org/github.com/syndtr/goleveldb/leveldb#DB.NewIterator)

    iter := db.NewIterator(util.BytesPrefix([]byte("prefix_")), nil)
    // or
    iter := db.NewIterator(util.Range{
        Start: []byte("prefix_03"),
        Limit: []byte("prefix_04"),
    }), nil)
    

    Does it help?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?