Last active
August 6, 2022 06:11
-
-
Save xiejuncs/f4e51e747a688671aa7f2c43bb0e4708 to your computer and use it in GitHub Desktop.
This file contains 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
// 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