Skip to content

Instantly share code, notes, and snippets.

@ehfeng
Created April 20, 2024 02:20
Show Gist options
  • Save ehfeng/b40930acb1d90d76140f65d4eb950218 to your computer and use it in GitHub Desktop.
Save ehfeng/b40930acb1d90d76140f65d4eb950218 to your computer and use it in GitHub Desktop.
badgerdb sequences
package main
import (
"encoding/binary"
"fmt"
"sync"
"sync/atomic"
"github.com/dgraph-io/badger/v4"
)
const parallelism = 10
// can you use reverse iterators to create a "sequence"?
func main() {
opts := badger.DefaultOptions("/tmp/badger")
opts.Logger = nil
db, err := badger.Open(opts)
if err != nil {
panic(err)
}
defer db.Close()
if err := db.DropAll(); err != nil {
panic(err)
}
iteratorOps := badger.DefaultIteratorOptions
iteratorOps.PrefetchValues = false
iteratorOps.Reverse = true
var wg sync.WaitGroup
// badgerdb attempt
for i := 0; i < parallelism; i++ {
wg.Add(1)
go func() {
txn := db.NewTransaction(true)
defer txn.Discard()
var idx uint64
func() {
it := txn.NewIterator(iteratorOps)
defer it.Close()
it.Rewind()
if it.Valid() {
k := it.Item().Key()
idx := binary.BigEndian.Uint64(k)
idx++
}
}()
byteSlice := make([]byte, 8)
binary.BigEndian.PutUint64(byteSlice, idx)
if err := txn.Set(byteSlice, []byte("0")); err != nil {
panic(err)
}
if err := txn.Commit(); err != nil {
panic(err)
}
wg.Done()
}()
}
wg.Wait()
func() {
txn := db.NewTransaction(false)
it := txn.NewIterator(iteratorOps)
it.Rewind()
defer it.Close()
var k []byte
defer it.Close()
for it.Valid() {
k = it.Item().Key()
fmt.Println("badgerdb", len(k), binary.BigEndian.Uint64(k))
it.Next()
}
txn.Commit()
}()
if err := db.DropAll(); err != nil {
panic(err)
}
// sync/atomic attempt
var op atomic.Uint64
op.Store(5)
for i := 0; i < parallelism; i++ {
wg.Add(1)
go func() {
txn := db.NewTransaction(true)
defer txn.Discard()
byteSlice := make([]byte, 8)
binary.BigEndian.PutUint64(byteSlice, op.Add(1))
if err := txn.Set(byteSlice, []byte("0")); err != nil {
panic(err)
}
if err := txn.Commit(); err != nil {
panic(err)
}
wg.Done()
}()
}
wg.Wait()
func() {
var k []byte
txn := db.NewTransaction(false)
defer txn.Discard()
it := txn.NewIterator(iteratorOps)
it.Rewind()
defer it.Close()
for it.Valid() {
k = it.Item().Key()
fmt.Println("atomic", k, binary.BigEndian.Uint64(k))
it.Next()
}
txn.Commit()
}()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment