Last active
August 11, 2020 11:53
-
-
Save poketulhu/0bffb65bfdea850f167ce1cdcc077ce2 to your computer and use it in GitHub Desktop.
based on https://github.com/dgraph-io/badger/blob/f50343ff404d8198df6dc83755ec2eab863d5ff2/db_test.go#L1939
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"log" | |
"sync" | |
badger "github.com/dgraph-io/badger/v2" | |
"github.com/dgraph-io/badger/v2/pb" | |
"github.com/dgraph-io/dgo/v2/protos/api" | |
) | |
func main() { | |
// open local database | |
db, err := badger.Open(badger.DefaultOptions("/tmp/dgraph/p")) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer db.Close() | |
ctx, cancel := context.WithCancel(context.Background()) | |
defer cancel() | |
handleSubscription := func(kvl *pb.KVList) error { | |
log.Println("subscription handled") | |
for _, kv := range kvl.GetKv() { | |
fmt.Println(string(kv.GetKey()), string(kv.GetValue())) | |
} | |
return nil | |
} | |
var wg sync.WaitGroup | |
wg.Add(1) | |
go func() { | |
defer wg.Done() | |
if err := db.Subscribe(ctx, handleSubscription, []byte("status")); err != nil { | |
fmt.Println(err) | |
} | |
}() | |
mu := &api.Mutation{ | |
SetNquads: []byte(` _:foo <status> "pending" .`), | |
} | |
req := &api.Request{CommitNow: true, Mutations: []*api.Mutation{mu}} | |
res, err := txn.Do(ctx, req) | |
if err != nil { | |
log.Fatal(err) | |
} | |
cancel() | |
wg.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment