You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.1 KiB

  1. package grpcdb_test
  2. import (
  3. "bytes"
  4. "context"
  5. "log"
  6. grpcdb "github.com/tendermint/tmlibs/grpcdb"
  7. protodb "github.com/tendermint/tmlibs/proto"
  8. )
  9. func Example() {
  10. addr := ":8998"
  11. go func() {
  12. if err := grpcdb.ListenAndServe(addr); err != nil {
  13. log.Fatalf("BindServer: %v", err)
  14. }
  15. }()
  16. client, err := grpcdb.NewClient(addr, grpcdb.Insecure)
  17. if err != nil {
  18. log.Fatalf("Failed to create grpcDB client: %v", err)
  19. }
  20. ctx := context.Background()
  21. // 1. Initialize the DB
  22. in := &protodb.Init{
  23. Type: "leveldb",
  24. Name: "grpc-uno-test",
  25. Dir: ".",
  26. }
  27. if _, err := client.Init(ctx, in); err != nil {
  28. log.Fatalf("Init error: %v", err)
  29. }
  30. // 2. Now it can be used!
  31. query1 := &protodb.Entity{Key: []byte("Project"), Value: []byte("Tmlibs-on-gRPC")}
  32. if _, err := client.SetSync(ctx, query1); err != nil {
  33. log.Fatalf("SetSync err: %v", err)
  34. }
  35. query2 := &protodb.Entity{Key: []byte("Project")}
  36. read, err := client.Get(ctx, query2)
  37. if err != nil {
  38. log.Fatalf("Get err: %v", err)
  39. }
  40. if g, w := read.Value, []byte("Tmlibs-on-gRPC"); !bytes.Equal(g, w) {
  41. log.Fatalf("got= (%q ==> % X)\nwant=(%q ==> % X)", g, g, w, w)
  42. }
  43. }