rbtree provides a map-like data structure rbtree.Map[K, V] that can rollback to any points in time.
It allocates internal nodes with bump allocation, and they point each other with indices, which minimizes the impact on GC.
This implementation is roughly based on red-black tree from Purely Functional Data Structures by Chris Okasaki.
go get github.com/ichiban/rbtreepackage main
import (
"fmt"
"github.com/ichiban/rbtree"
)
func main() {
var m rbtree.Map[string, int]
m.Set("a", 1) // {a: 1}
m.Set("b", 2) // {a: 1, b: 2}
snapshot := m
m.Set("c", 3)
m.Set("a", 4) // Overwrites "a".
m.Delete("b") // Deletes "b".
for k, v := range rbtree.All(m) {
fmt.Printf("%s: %v\n", k, v)
}
fmt.Println("rollback to snapshot")
m = snapshot
for k, v := range rbtree.All(m) {
fmt.Printf("%s: %v\n", k, v)
}
}a: 4
c: 3
rollback to snapshot
a: 1
b: 2
Distributed under the MIT license. See LICENSE for more information.
- Fork it (https://github.com/ichiban/rbtree/fork)
- Create your feature branch (git checkout -b feature/fooBar)
- Commit your changes (git commit -am 'Add some fooBar')
- Push to the branch (git push origin feature/fooBar)
- Create a new Pull Request