1 Star 0 Fork 0

xingyp / cn-infra

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
main.go 1.58 KB
一键复制 编辑 原始数据 按行查看 历史
package main
import (
"context"
"log"
"github.com/ligato/cn-infra/agent"
"github.com/ligato/cn-infra/db/keyval"
"github.com/ligato/cn-infra/db/keyval/bolt"
"github.com/ligato/cn-infra/logging"
)
const pluginName = "bolt-example"
func main() {
example := &BoltExample{
Log: logging.ForPlugin(pluginName),
DB: &bolt.DefaultPlugin,
finished: make(chan struct{}),
}
a := agent.NewAgent(
agent.AllPlugins(example),
agent.QuitOnClose(example.finished),
)
if err := a.Run(); err != nil {
log.Fatal(err)
}
}
// BoltExample demonstrates the usage of Bolt plugin.
type BoltExample struct {
Log logging.PluginLogger
DB keyval.KvProtoPlugin
finished chan struct{}
}
// Init demonstrates using Bolt plugin.
func (p *BoltExample) Init() (err error) {
db := p.DB.NewBroker(keyval.Root)
// Store some data
txn := db.NewTxn()
txn.Put("/agent/config/interface/iface0", nil)
txn.Put("/agent/config/interface/iface1", nil)
txn.Commit(context.Background())
// List keys
const listPrefix = "/agent/config/interface/"
p.Log.Infof("List BoltDB keys: %s", listPrefix)
keys, err := db.ListKeys(listPrefix)
if err != nil {
p.Log.Fatal(err)
}
for {
key, val, all := keys.GetNext()
if all == true {
break
}
p.Log.Infof("Key: %q Val: %v", key, val)
}
return nil
}
// AfterInit closes the example.
func (p *BoltExample) AfterInit() (err error) {
close(p.finished)
return nil
}
// Close frees plugin resources.
func (p *BoltExample) Close() error {
return nil
}
// String returns name of plugin.
func (p *BoltExample) String() string {
return pluginName
}
1
https://gitee.com/xingyp/cn-infra.git
git@gitee.com:xingyp/cn-infra.git
xingyp
cn-infra
cn-infra
v2.2.0

搜索帮助