-
-
Save jarifibrahim/cd8f64de07d5a690cc3bbab66777b57d to your computer and use it in GitHub Desktop.
Badger: Subscription working code
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" | |
"time" | |
badger "github.com/dgraph-io/badger/v2" | |
"github.com/dgraph-io/badger/v2/pb" | |
) | |
func main() { | |
log.Println("starting") | |
db, err := badger.Open(badger.DefaultOptions("./p")) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer db.Close() | |
// Your code here… | |
ctx := context.Background() | |
handleSubscription := func(kvl *pb.KVList) error { | |
panic("AAAAAAAHHH") | |
for _, kv := range kvl.GetKv() { | |
fmt.Println(string(kv.GetKey()), string(kv.GetValue())) | |
} | |
return nil | |
} | |
go func() { | |
// It is important that the prefix is not nil. | |
if err := db.Subscribe(ctx, handleSubscription, []byte("f")); err != nil { | |
fmt.Println(err) | |
} | |
}() | |
log.Println("opened?") | |
// Insert some data | |
txn := db.NewTransaction(true) | |
txn.Set([]byte("foo"), []byte("bar")) | |
if err := txn.Commit(); err != nil { | |
panic(err) | |
} | |
log.Println("commit done") | |
// Wait for a second. | |
time.Sleep(time.Second * 1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment