Monotonically increasing integers
To get unique monotonically increasing integers with strong durability, you can use the DB.GetSequence
method. This method returns a Sequence
object, which is thread-safe and can be used concurrently via various goroutines.
Badger would lease a range of integers to hand out from memory, with the bandwidth provided to DB.GetSequence
. The frequency at which disk writes are done is determined by this lease bandwidth and the frequency of Next
invocations. Setting a bandwidth too low would do more disk writes, setting it too high would result in wasted integers if Badger is closed or crashes. To avoid wasted integers, call Release
before closing Badger.
seq, err := db.GetSequence(key, 1000)
defer seq.Release()
for {
num, err := seq.Next()
}