Skip to content

Instantly share code, notes, and snippets.

@jarifibrahim
Created April 23, 2019 09:51
Show Gist options
  • Save jarifibrahim/4b1e4c111f98c7922068571548395d9f to your computer and use it in GitHub Desktop.
Save jarifibrahim/4b1e4c111f98c7922068571548395d9f to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"log"
"github.com/dgraph-io/badger"
)
func main() {
opts := badger.DefaultOptions
opts.Dir = "/tmp/dropalltestdata"
opts.ValueDir = "/tmp/dropalltestdata"
opts.SyncWrites = false
db, err := badger.Open(opts)
if err != nil {
log.Panic(err)
}
wb := db.NewWriteBatch()
for i := 0; i < 1000; i++ {
if err := wb.Set([]byte(fmt.Sprintf("key%d", i)), []byte("22"), 0); err != nil {
log.Panic(err)
}
}
if err := wb.Flush(); err != nil {
log.Panic(err)
}
err = db.View(func(txn *badger.Txn) error {
count := 0
it := txn.NewIterator(badger.DefaultIteratorOptions)
defer it.Close()
for it.Rewind(); it.Valid(); it.Next() {
count++
}
fmt.Println("Total Count before DropAll:", count)
return nil
})
if err := db.Close(); err != nil {
log.Panic(err)
}
opts.SyncWrites = true
managedDb, err := badger.OpenManaged(opts)
if err := managedDb.DropAll(); err != nil {
log.Panic(err)
}
if err := managedDb.Close(); err != nil {
log.Panic(err)
}
opts.SyncWrites = false
db, err = badger.Open(opts)
if err != nil {
log.Panic(err)
}
err = db.View(func(txn *badger.Txn) error {
count := 0
it := txn.NewIterator(badger.DefaultIteratorOptions)
defer it.Close()
for it.Rewind(); it.Valid(); it.Next() {
count++
}
fmt.Println("Total Count after DropAll:", count)
return nil
})
if err := db.Close(); err != nil {
log.Panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment