Skip to content

Instantly share code, notes, and snippets.

@xiejuncs
Last active August 6, 2022 06:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xiejuncs/f4e51e747a688671aa7f2c43bb0e4708 to your computer and use it in GitHub Desktop.
Save xiejuncs/f4e51e747a688671aa7f2c43bb0e4708 to your computer and use it in GitHub Desktop.
// https://pkg.go.dev/github.com/dgraph-io/badger#section-readme
// db, err := badger.Open(badger.DefaultOptions("/tmp/badger"))
func storeLog(val byte[]) {
seq, err := db.GetSequence("dummy-key", 1)
defer seq.Release()
var ids []uint64
for len(ids) < 1 {
num, err := seq.Next()
if err != nil {
break
}
ids = append(ids, num)
}
for _, id := range ids {
idBytes := make([]byte, 8)
// Use BigEndian instead of LittleEndian
binary.BigEndian.PutUint64(idBytes, logId)
// This key prefix should be consistent with the logic at line 33
logKeyPrefix := getLogKeyPrefix()
finalKey := append(logKeyPrefix, idBytes)
// Set finalKey and val into badger DB
}
}
func iterateLogs() {
err := db.View(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions
opts.PrefetchSize = 10
it := txn.NewIterator(opts)
defer it.Close()
// This key prefix should be consistnt with the logic at line 19
logKeyPrefix := getLogKeyPrefix()
for it.Seek([]byte(logKeyPrefix)); it.ValidForPrefix([]byte(logKeyPrefix)); it.Next() {
item := it.Item()
// This k is monotonically increasing integers
k := item.Key()
err := item.Value(func(v []byte) error {
// Do some logic
return nil
})
if err != nil {
return err
}
}
return nil
})
if err != nil {
panic("panic")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment