mirror of
https://github.com/fluencelabs/tendermint
synced 2025-04-29 08:42:14 +00:00
Simplified the abstractions to remotedb, a package that allows clients to use whatever database they can in client code without having to switch out their code e.g ```go client, err := remotedb.NewInsecure(":9888") ... // Just like they'd initialize locally in := &remotedb.Init{ Name: "test-remote-db", Type: "leveldb", Dir: "/tmp/dbs", } if err := client.InitRemote(in); err != nil { log.Fatalf("Failed to initialize the database") } v1 := client.Get(k1) client.Set(k9, dog) for itr := client.Iterator(a1, z1); itr.Valid(); itr.Next() { k, v := itr.Key(), itr.Value() dom := itr.Domain() ... } ```
51 lines
1.1 KiB
Go
51 lines
1.1 KiB
Go
package grpcdb_test
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"log"
|
|
|
|
grpcdb "github.com/tendermint/tmlibs/grpcdb"
|
|
protodb "github.com/tendermint/tmlibs/proto"
|
|
)
|
|
|
|
func Example() {
|
|
addr := ":8998"
|
|
go func() {
|
|
if err := grpcdb.ListenAndServe(addr); err != nil {
|
|
log.Fatalf("BindServer: %v", err)
|
|
}
|
|
}()
|
|
|
|
client, err := grpcdb.NewClient(addr, grpcdb.Insecure)
|
|
if err != nil {
|
|
log.Fatalf("Failed to create grpcDB client: %v", err)
|
|
}
|
|
|
|
ctx := context.Background()
|
|
// 1. Initialize the DB
|
|
in := &protodb.Init{
|
|
Type: "leveldb",
|
|
Name: "grpc-uno-test",
|
|
Dir: ".",
|
|
}
|
|
if _, err := client.Init(ctx, in); err != nil {
|
|
log.Fatalf("Init error: %v", err)
|
|
}
|
|
|
|
// 2. Now it can be used!
|
|
query1 := &protodb.Entity{Key: []byte("Project"), Value: []byte("Tmlibs-on-gRPC")}
|
|
if _, err := client.SetSync(ctx, query1); err != nil {
|
|
log.Fatalf("SetSync err: %v", err)
|
|
}
|
|
|
|
query2 := &protodb.Entity{Key: []byte("Project")}
|
|
read, err := client.Get(ctx, query2)
|
|
if err != nil {
|
|
log.Fatalf("Get err: %v", err)
|
|
}
|
|
if g, w := read.Value, []byte("Tmlibs-on-gRPC"); !bytes.Equal(g, w) {
|
|
log.Fatalf("got= (%q ==> % X)\nwant=(%q ==> % X)", g, g, w, w)
|
|
}
|
|
}
|