1
0
mirror of https://gitee.com/willfree/mlsr.git synced 2026-06-06 11:49:28 +08:00

测试buntDB基本用法

This commit is contained in:
free will
2022-04-06 17:25:01 +08:00
parent 88beb51aaf
commit e4982b3cfa
6 changed files with 259 additions and 4 deletions
View File
+4
View File
@@ -3,6 +3,10 @@ module mlsr
go 1.16
require (
github.com/smartystreets/goconvey v1.7.2 // indirect
github.com/tidwall/btree v1.2.1 // indirect
github.com/tidwall/buntdb v1.2.9
github.com/tidwall/gjson v1.14.0 // indirect
gopkg.in/ini.v1 v1.62.0
minlib v0.0.0
)
+38 -2
View File
@@ -1,10 +1,14 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/d4l3k/messagediff v1.2.1 h1:ZcAIMYsUg0EAp9X+tt8/enBE/Q8Yd5kzPynLyKptt9U=
github.com/d4l3k/messagediff v1.2.1/go.mod h1:Oozbb1TVXFac9FtSIxHBMnBCq2qeH/2KkEQxENCrlLo=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-eden/routine v0.0.2/go.mod h1:R+sklWRKa0GPYXW5Opyr1nSb9KbwpEF9AXBARb7r1XI=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/mutecomm/go-sqlcipher/v4 v4.4.2 h1:eM10bFtI4UvibIsKr10/QT7Yfz+NADfjZYh0GKrXUNc=
@@ -15,20 +19,52 @@ github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQD
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/smartystreets/assertions v1.2.0 h1:42S6lae5dvLc7BrLu/0ugRtcFVjoJNMC/N3yZFZkDFs=
github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo=
github.com/smartystreets/goconvey v1.7.2 h1:9RBaZCeXEQ3UselpuwUQHltGVXvdwm6cv1hgR6gDIPg=
github.com/smartystreets/goconvey v1.7.2/go.mod h1:Vw0tHAZW6lzCRk3xgdin6fKYcG+G3Pg9vgXWeJpQFMM=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tidwall/assert v0.1.0 h1:aWcKyRBUAdLoVebxo95N7+YZVTFF/ASTr7BN4sLP6XI=
github.com/tidwall/assert v0.1.0/go.mod h1:QLYtGyeqse53vuELQheYl9dngGCJQ+mTtlxcktb+Kj8=
github.com/tidwall/btree v1.1.0/go.mod h1:TzIRzen6yHbibdSfK6t8QimqbUnoxUSrZfeW7Uob0q4=
github.com/tidwall/btree v1.2.1 h1:57++q7DAOwfD6R0w0d3wh5yTwwett4gBYJGZD1rmyHQ=
github.com/tidwall/btree v1.2.1/go.mod h1:LGm8L/DZjPLmeWGjv5kFrY8dL4uVhMmzmmLYmsObdKE=
github.com/tidwall/buntdb v1.2.9 h1:XVz684P7X6HCTrdr385yDZWB1zt/n20ZNG3M1iGyFm4=
github.com/tidwall/buntdb v1.2.9/go.mod h1:IwyGSvvDg6hnKSIhtdZ0AqhCZGH8ukdtCAzaP8fI1X4=
github.com/tidwall/gjson v1.12.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.14.0 h1:6aeJ0bzojgWLa82gDQHcx3S0Lr/O51I9bJ5nv6JFx5w=
github.com/tidwall/gjson v1.14.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/grect v0.1.4 h1:dA3oIgNgWdSspFzn1kS4S/RDpZFLrIxAZOdJKjYapOg=
github.com/tidwall/grect v0.1.4/go.mod h1:9FBsaYRaR0Tcy4UwefBX/UDcDcDy9V5jUcxHzv2jd5Q=
github.com/tidwall/lotsa v1.0.2 h1:dNVBH5MErdaQ/xd9s769R31/n2dXavsQ0Yf4TMEHHw8=
github.com/tidwall/lotsa v1.0.2/go.mod h1:X6NiU+4yHA3fE3Puvpnn1XMDrFZrE9JO2/w+UMuqgR8=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/rtred v0.1.2 h1:exmoQtOLvDoO8ud++6LwVsAMTu0KPzLTUrMln8u1yu8=
github.com/tidwall/rtred v0.1.2/go.mod h1:hd69WNXQ5RP9vHd7dqekAz+RIdtfBogmglkZSRxCHFQ=
github.com/tidwall/tinyqueue v0.1.1 h1:SpNEvEggbpyN5DIReaJ2/1ndroY8iyEGxPYxoSaymYE=
github.com/tidwall/tinyqueue v0.1.1/go.mod h1:O/QNHwrnjqr6IHItYrzoHAKYhBkLI67Q096fQP5zMYw=
github.com/tylertreat/BoomFilters v0.0.0-20210315201527-1a82519a3e43 h1:QEePdg0ty2r0t1+qwfZmQ4OOl/MB2UXIeJSpIZv56lg=
github.com/tylertreat/BoomFilters v0.0.0-20210315201527-1a82519a3e43/go.mod h1:OYRfF6eb5wY9VRFkXJH8FFBi3plw2v+giaIu7P054pM=
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
github.com/zeebo/xxh3 v1.0.1 h1:FMSRIbkrLikb/0hZxmltpg84VkqDAT5M8ufXynuhXsI=
github.com/zeebo/xxh3 v1.0.1/go.mod h1:8VHV24/3AZLn3b6Mlp/KuC33LWH687Wq6EnziEB+rsA=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/ini.v1 v1.62.0 h1:duBzk771uxoUuOlyRLkHsygud9+5lrlGjdFBb4mSKDU=
gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+4 -2
View File
@@ -47,7 +47,8 @@ func (a *AdjLsa) AddAdjacencyInfo(info *AdjLsaAdjacencyInfo) {
// @return bool
//
func (a *AdjLsa) IsEqualContent(aLsa *AdjLsa) bool {
panic("")
// todo
return false
}
//
@@ -57,7 +58,8 @@ func (a *AdjLsa) IsEqualContent(aLsa *AdjLsa) bool {
// @return string
//
func (a *AdjLsa) ToString() string {
panic("")
// todo
return ""
}
//
+15
View File
@@ -2,6 +2,7 @@ package main
import (
"fmt"
"github.com/tidwall/buntdb"
"minlib/logicface"
"mlsr/lsa"
)
@@ -13,4 +14,18 @@ func main() {
ss:=lsa.AdjLsaAdjacencyInfo{}
ss.Status=1
TestBuntDb()
}
func TestBuntDb() {
// https://github.com/tidwall/buntdb
db,err := buntdb.Open(":memory:")
if err!= nil{
fmt.Println("bunt db open err")
}
_ = db.Close()
fmt.Println("bunt db closed")
}
+198
View File
@@ -0,0 +1,198 @@
// Package tests
// @Author: Wang Feng
// @Description: buntDB-一种开源的内存键值库
// 源代码:https://github.com/tidwall/buntdb
// 使用方法:https://segmentfault.com/a/1190000022102931
// https://darjun.github.io/2020/03/21/godailylib/buntdb/
// @Version: 0.1.0
// @Date: 2022/4/6 16:00
// @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
//
package tests
import (
"fmt"
"github.com/tidwall/buntdb"
"log"
"testing"
)
//
// TestSetAndGet_In_UpdateAndView
// @Description: 创建数据库,设置数据库参数
// @param t
//
func TestSetAndGet_In_UpdateAndView(t *testing.T) {
db, err := buntdb.Open(":memory:")
if err != nil {
log.Fatal(err)
}
defer db.Close()
db.Update(func(tx *buntdb.Tx) error {
oldValue, replaced, err := tx.Set("testkey", "testvalue", nil)
if err != nil {
return err
}
fmt.Printf("old value:%q replaced:%t\n", oldValue, replaced)
return nil
})
db.View(func(tx *buntdb.Tx) error {
value, err := tx.Get("testkey")
if err != nil {
return err
}
fmt.Println("value is:", value)
return nil
})
}
//
// TestAscend
// @Description:
// Ascend()方法接收一个索引名,然后以该索引定义的顺序遍历所有键值对
// 如果未指定索引名,则根据键升序遍历
// @param t
//
func TestAscend(t *testing.T) {
db, err := buntdb.Open(":memory:")
if err != nil {
log.Fatal(err)
}
defer db.Close()
db.Update(func(tx *buntdb.Tx) error {
data := map[string]string{
"a": "apple",
"b": "banana",
"p": "pear",
"o": "orange",
}
for key, value := range data {
tx.Set(key, value, nil)
}
return nil
})
db.View(func(tx *buntdb.Tx) error {
var count int
tx.Ascend("", func(key, value string) bool {
fmt.Printf("key:%s value:%s\n", key, value)
count++
if count >= 3 {
return false
}
return true
})
return nil
})
}
//
// TestCreatIndex
// @Description: buntDB将数据以键排序存储到b-tree中
// 可以创建自定义索引,这样就可以对值进行排序了。传入索引名、键名、值大小判断方法
// @param t
//
func TestCreatIndex(t *testing.T) {
db, err := buntdb.Open(":memory:")
if err != nil {
log.Fatal(err)
}
defer db.Close()
db.CreateIndex("names", "user:*:name", buntdb.IndexString)
db.Update(func(tx *buntdb.Tx) error {
tx.Set("user:1:name", "tom", nil)
tx.Set("user:2:name", "Randi", nil)
tx.Set("user:3:name", "jane", nil)
tx.Set("user:4:name", "Janet", nil)
tx.Set("user:5:name", "Paula", nil)
tx.Set("user:6:name", "peter", nil)
tx.Set("user:7:name", "Terri", nil)
return nil
})
db.View(func(tx *buntdb.Tx) error {
tx.Ascend("names", func(key, value string) bool {
fmt.Printf("%s: %s\n", key, value)
return true
})
return nil
})
}
//
// TestJSONIndex
// @Description: 当值为json字符串时,可以按照json字符串中的键进行排序
// @param t
//
func TestJSONIndex(t *testing.T) {
db, _ := buntdb.Open(":memory:")
defer db.Close()
db.CreateIndex("first_name", "user:*", buntdb.IndexJSON("name.first"))
db.CreateIndex("age", "user:*", buntdb.IndexJSON("age"))
db.Update(func(tx *buntdb.Tx) error {
tx.Set("user:1", `{"name":{"first":"zhang","last":"san"},"age":18}`, nil)
tx.Set("user:2", `{"name":{"first":"li","last":"si"},"age":27`, nil)
tx.Set("user:3", `{"name":{"first":"wang","last":"wu"},"age":32}`, nil)
tx.Set("user:4", `{"name":{"first":"sun","last":"qi"},"age":8}`, nil)
return nil
})
db.View(func(tx *buntdb.Tx) error {
fmt.Println("Order by first name")
tx.Ascend("first_name", func(key, value string) bool {
fmt.Printf("%s: %s\n", key, value)
return true
})
fmt.Println("Order by age")
tx.Ascend("age", func(key, value string) bool {
fmt.Printf("%s: %s\n", key, value)
return true
})
fmt.Println("Order by age range 18-30")
tx.AscendRange("age", `{"age":18}`, `{"age":30}`, func(key, value string) bool {
fmt.Printf("%s: %s\n", key, value)
return true
})
return nil
})
}
//
// TestExpiresTime
// @Description: 在增加数据库条目时,设置过期时间
// @param t
//
func TestExpiresTime(t *testing.T) {
db, _ := buntdb.Open(":memory:")
defer db.Close()
db.Update(func(tx *buntdb.Tx) error {
//tx.Set("testkey", "testvalue", &buntdb.SetOptions{Expires: true, TTL: time.Second})
tx.Set("testkey", "testvalue",nil)
return nil
})
db.View(func(tx *buntdb.Tx) error {
value, _ := tx.Get("testkey")
fmt.Println("value is:", value)
return nil
})
//time.Sleep(time.Second)
db.View(func(tx *buntdb.Tx) error {
value, _ := tx.Get("testkey")
fmt.Println("after expire, value is:", value)
return nil
})
}