golang LMDB入门例子——key range查询
2023-09-14 09:11:55 时间
如下,使用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\n", bkey, bval) } // random access bval, _ := txn.Get(dbi, []byte("Key-3")) fmt.Println(string(bval)) fmt.Printf("*********range begin*********\n") 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\n", bkey, bval) op = NEXT } fmt.Printf("**********range end********\n") }
结果如下:
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
}
}
相关文章
- GoLang入门1-安装-配置
- Golang 优雅实现责任链
- Golang语言循环、指针、结构体和切片(打卡第二天)|Go主题月
- Golang框架Gin入门实战--(3)HTML模板渲染及模板语法(上)
- Golang框架Gin入门实战--(6)Gin路由文件抽离
- Golang(六)time 包的用法整理
- Golang(十)TLS 相关知识(一)基本概念原理
- Golang-optimization「2」: 字符串
- Golang框架 - gin实现Http Server
- golang刷leetcode:设计LFU缓存结构
- Go语言学习笔记一 Golang的安装与基础
- Golang 库: golang slog 怎么设置日志 Debug 等级
- 不背锅运维:消息队列概念、kafka入门、Kafka Golang客户端库
- golang + vue3+vite 构建后台管理系统笔记。
- Golang升级1.18之后panic()报错
- MacOS系统安装golang教程