Created
June 4, 2019 04:46
-
-
Save linxGnu/a99e2b3ecc5641c742faf7defceac4e0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 ( | |
"encoding/binary" | |
"fmt" | |
"time" | |
"github.com/dgraph-io/badger" | |
) | |
func main() { | |
// open db logs | |
opt := badger.DefaultOptions | |
opt.Dir = "/tmp/badger_key" | |
opt.ValueDir = "/tmp/badger_value" | |
db, err := badger.Open(opt) | |
if err != nil { | |
panic(err) | |
} | |
// writing to db | |
start := time.Now() | |
write(db) | |
fmt.Println("[PR] write done", time.Since(start).Seconds()) | |
// delete from db | |
start = time.Now() | |
delete(db) | |
fmt.Println("[PR] delete done", time.Since(start).Seconds()) | |
time.Sleep(2 * time.Minute) | |
// close db | |
db.Close() | |
} | |
func delete(db *badger.DB) { | |
defer func() { | |
fmt.Println("[PR] Running gc") | |
var err error | |
for ; err == nil; err = db.RunValueLogGC(0.5) { | |
if err != nil { | |
fmt.Println("[PR]", err) | |
} | |
} | |
}() | |
var ( | |
key [4]byte | |
) | |
batchSize := 1000 | |
for i := 0; i < 50000; i++ { | |
tx := db.NewTransaction(true) | |
for j := 0; j < batchSize; j++ { | |
k := i*batchSize + j | |
binary.BigEndian.PutUint32(key[:], uint32(k)) | |
if err := tx.Delete(key[:]); err != nil { | |
if err == badger.ErrTxnTooBig { | |
if err = tx.Commit(); err == nil { | |
// create new transaction | |
tx = db.NewTransaction(true) | |
// set again | |
if err = tx.Delete(key[:]); err != nil { | |
panic(err) | |
} | |
} else { | |
panic(err) | |
} | |
} | |
} | |
} | |
if err := tx.Commit(); err != nil { | |
panic(err) | |
} | |
} | |
} | |
func write(db *badger.DB) { | |
var ( | |
key [4]byte | |
value = make([]byte, 512) | |
) | |
batchSize := 1000 | |
for i := 0; i < 50000; i++ { | |
tx := db.NewTransaction(true) | |
for j := 0; j < batchSize; j++ { | |
k := i*batchSize + j | |
binary.BigEndian.PutUint32(key[:], uint32(k)) | |
if err := tx.Set(key[:], value); err != nil { | |
if err == badger.ErrTxnTooBig { | |
if err = tx.Commit(); err == nil { | |
// create new transaction | |
tx = db.NewTransaction(true) | |
// set again | |
if err = tx.Set(key[:], value); err != nil { | |
panic(err) | |
} | |
} else { | |
panic(err) | |
} | |
} | |
} | |
} | |
if err := tx.Commit(); err != nil { | |
panic(err) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment