golang LMDB入门例子——key range查询

如下,使用gomb库

package main


import (
    "bytes"
    "fmt"
    "io/ioutil"
    "os"
    . "github.com/szferi/gomdb"
)

// Most mdb functions/methods can return errors. This example ignores errors
// for brevity. Real code should check all return values.
func main() {
    // create a directory to hold the database
    path, _ := ioutil.TempDir("", "mdb_test")
    defer os.RemoveAll(path)

    // open the db
    env, _ := NewEnv()
    env.SetMapSize(1 << 20) // max file size
    env.Open(path, 0, 0664)
    defer env.Close()
    txn, _ := env.BeginTxn(nil, 0)
    dbi, _ := txn.DBIOpen(nil, 0)
    defer env.DBIClose(dbi)
    txn.Commit()

    // write some data
    txn, _ = env.BeginTxn(nil, 0)
    num_entries := 10
    for i := 0; i < num_entries; i++ {
        key := fmt.Sprintf("Key-%d", i)
        val := fmt.Sprintf("Val-%d", i)
        txn.Put(dbi, []byte(key), []byte(val), 0)
    }
    txn.Commit()

    // inspect the database
    stat, _ := env.Stat()
    fmt.Println(stat.Entries)

    // scan the database
    txn, _ = env.BeginTxn(nil, RDONLY)
    defer txn.Abort()
    cursor, _ := txn.CursorOpen(dbi)
    defer cursor.Close()
    for {
        bkey, bval, err := cursor.Get(nil, nil, NEXT)
        if err == NotFound {
            break
        }
        if err != nil {
            panic(err)
        }
        fmt.Printf("%s: %s
", bkey, bval)
    }

    // random access
    bval, _ := txn.Get(dbi, []byte("Key-3"))
    fmt.Println(string(bval))

    fmt.Printf("*********range begin*********
")
    bkey := []byte("Key-2")
    bkeyEnd := []byte("Key-5")
    // var MDB_SET_RANGE uint = 17
    // var op = MDB_SET_RANGE
var op uint = SET_RANGE
for { bkey, bval, err := cursor.Get(bkey, nil, op) if err == NotFound || bytes.Compare(bkey, bkeyEnd) > 0 { break } if err != nil { panic(err) } fmt.Printf("%s: %s ", bkey, bval) op = NEXT } fmt.Printf("**********range end******** ") }

结果如下:

10
Key-0: Val-0
Key-1: Val-1
Key-2: Val-2
Key-3: Val-3
Key-4: Val-4
Key-5: Val-5
Key-6: Val-6
Key-7: Val-7
Key-8: Val-8
Key-9: Val-9
Val-3
*********range begin*********
Key-2: Val-2
Key-3: Val-3
Key-4: Val-4
Key-5: Val-5
**********range end********

尼玛,还是参考BDB写出来的!

参考:https://stackoverflow.com/questions/18707751/retrieving-a-range-of-data-from-berkeley-db

BDB的例子:

void get(DB *dbp, int key1, int key2){
  DBC *curs;
  DBT k,v;
  int fl;

  // Get a cursor
  dbp->cursor(dbp, NULL, &curs, 0);
  if (!curs) _dberr("can't get a cursor");

  // Set DBT for 1st key and value
  memset(&v, 0, sizeof(DBT));
  memset(&k, 0, sizeof(DBT));
  k.data = &key1;
  k.size = sizeof(key1);

  fl = DB_SET_RANGE; // first key will be >=key1
  while (curs->c_get(curs, &k, &v, fl)==0 &&
         key2 >= *(int *)k.data){
    fl = DB_NEXT;
    // use v.data
  }
}
原文地址:https://www.cnblogs.com/bonelee/p/6899979.html