DB as a service
Fixes https://github.com/tendermint/tendermint/issues/1162
Databases as a service!
Can now access Databases as a remote service
via gRPC for performance and easy deployment.
The caveat is that each service is stateful in regards to
the DB i.e. each unique service uses only one unique DB
but nonetheless multiple clients can access it.
A full standalone example
```go
package main
import (
"bytes"
"context"
"log"
grpcdb "github.com/tendermint/tmlibs/grpcdb"
protodb "github.com/tendermint/tmlibs/proto"
)
func main() {
addr := ":8998"
go func() {
if err := grpcdb.BindRemoteDBServer(addr); err != nil {
log.Fatalf("BindRemoteDBServer: %v", err)
}
}()
client, err := grpcdb.NewClient(addr, false)
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)
}
}
```
7 years ago |
|
- syntax = "proto3";
-
- package protodb;
-
- message Entity {
- int32 id = 1;
- bytes key = 2;
- bytes value = 3;
- bool exists = 4;
- bytes start = 5;
- bytes end = 6;
- string err = 7;
- string print = 8;
- int64 time_at = 9;
- }
-
- message Nothing {
- }
-
- message DDomain {
- bytes start = 1;
- bytes end = 2;
- }
-
- message Iterator {
- DDomain domain = 1;
- bool valid = 2;
- bytes key = 3;
- bytes value = 4;
- }
-
- message Stats {
- map<string, string> data = 1;
- int64 time_at = 2;
- }
-
- message Init {
- string Type = 1;
- string Name = 2;
- string Dir = 3;
- }
-
- service DB {
- rpc init(Init) returns (Entity) {}
- rpc get(Entity) returns (Entity) {}
- rpc getStream(stream Entity) returns (stream Entity) {}
-
- rpc has(Entity) returns (Entity) {}
- rpc set(Entity) returns (Nothing) {}
- rpc setSync(Entity) returns (Nothing) {}
- rpc delete(Entity) returns (Nothing) {}
- rpc deleteSync(Entity) returns (Nothing) {}
- rpc iterator(Entity) returns (stream Iterator) {}
- rpc reverseIterator(Entity) returns (stream Iterator) {}
- // rpc print(Nothing) returns (Entity) {}
- rpc stats(Nothing) returns (Stats) {}
- }
|