Skip to content

Instantly share code, notes, and snippets.

@mperham
Created December 19, 2017 17:50
Show Gist options
  • Save mperham/d1b57b7a78ae06983076729af4f4a258 to your computer and use it in GitHub Desktop.
Save mperham/d1b57b7a78ae06983076729af4f4a258 to your computer and use it in GitHub Desktop.
package storage
import (
"crypto/rand"
"encoding/base64"
"fmt"
"os"
"testing"
"time"
"github.com/dgraph-io/badger"
"github.com/stretchr/testify/assert"
)
func randomJid() string {
bytes := make([]byte, 12)
rand.Read(bytes)
return base64.RawURLEncoding.EncodeToString(bytes)
}
func thens(tim time.Time) string {
return tim.UTC().Format(time.RFC3339Nano)
}
func TestBulkDelete(t *testing.T) {
t.Parallel()
defer os.RemoveAll("/tmp/bulk")
path := "/tmp/bulk"
opts := badger.DefaultOptions
opts.Dir = path
opts.ValueDir = path
db, err := badger.Open(opts)
assert.NoError(t, err)
ns := "r~"
// Insert
start := time.Now()
for i := 0; i < 200; i++ {
jid := randomJid()
ts := thens(start.Add(time.Duration(10*i) * time.Second))
payload := []byte("data")
key := []byte(fmt.Sprintf("%s%s|%s", ns, ts, jid))
err := db.Update(func(tx *badger.Txn) error {
return tx.Set(key, payload)
})
assert.NoError(t, err)
}
// Each
count := 0
err = db.View(func(tx *badger.Txn) error {
b := []byte(ns)
initOpts := badger.DefaultIteratorOptions
initOpts.PrefetchValues = false
it := tx.NewIterator(initOpts)
defer it.Close()
it.Seek(b)
for ; it.ValidForPrefix(b); it.Next() {
count++
}
return nil
})
assert.NoError(t, err)
assert.Equal(t, 200, count)
// Clear
count = 0
err = db.Update(func(tx *badger.Txn) error {
b := []byte(ns)
it := tx.NewIterator(badger.DefaultIteratorOptions)
defer it.Close()
it.Seek(b)
for ; it.ValidForPrefix(b); it.Next() {
count++
err := tx.Delete(it.Item().Key())
assert.NoError(t, err)
}
return nil
})
assert.NoError(t, err)
assert.Equal(t, 200, count)
// Each
count = 0
err = db.View(func(tx *badger.Txn) error {
b := []byte(ns)
initOpts := badger.DefaultIteratorOptions
initOpts.PrefetchValues = false
it := tx.NewIterator(initOpts)
defer it.Close()
it.Seek(b)
for ; it.ValidForPrefix(b); it.Next() {
count++
}
return nil
})
assert.NoError(t, err)
assert.Equal(t, 0, count)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment