GolangNote

Golang笔记

golang 基于bolt 做一个支持hash、zet 数据结构

Permalink

bolt 是一个很简单的嵌入式数据库,在为服务中使用很方便,但它操作不够简洁。

youdb 是一个基于bolt 支持 hashmapzet 数据结构的简单嵌入式数据库。

使用例子:

Go: youdb 示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main

import (
	"fmt"
	"github.com/ego008/youdb"
)

func main() {
	db, err := youdb.Open("my.db")
	if err != nil {
		fmt.Println("open err", err)
		return
	}
	defer db.Close()

	// hash

	db.Hset("mytest", "key1", []byte("value1"))

	rs := db.Hget("mytest", "key1")
	if rs.State == "ok" {
		fmt.Println(rs.Data[0], rs.String())
	} else {
		fmt.Println("key not found")
	}

	dataMap := map[string][]byte{}
	dataMap["k1"] = []byte("12987887762987")
	dataMap["k2"] = []byte("abc")
	dataMap["k3"] = []byte("qwertyui")
	dataMap["k4"] = []byte("aaaa")
	dataMap["k5"] = []byte("aaaa555")
	dataMap["k6"] = []byte("aaaa556")
	dataMap["k7"] = []byte("77777")
	dataMap["k8"] = []byte("88888")

	db.Hmset("myhmset", dataMap)

	rs = db.Hmget("myhmset", []string{"k1", "k2", "k3", "k8"})
	if rs.State == "ok" {
		for _, v := range rs.Data {
			fmt.Println(v.Key, v.ValStr())
		}
	}

	fmt.Println(db.Hincr("num", "k1", 2))

	// zet

	db.Zset("mytest", "key1", 100)

	rs2 := db.Zget("mytest", "key1")
	if rs2.State == "ok" {
		fmt.Println(rs2.Int64())
	}

	fmt.Println(db.Zincr("num", "k1", 2))
}

本文网址: https://golangnote.com/topic/191.html 转摘请注明来源

Related articles

Write a Comment to "golang 基于bolt 做一个支持hash、zet 数据结构"

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.17 Processed in 1ms