Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Snapshotable binary tree #3

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions build/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.22

require (
github.com/outofforest/build/v2 v2.2.0
github.com/outofforest/tools v1.0.2
github.com/outofforest/tools v1.0.4
)

require (
Expand All @@ -19,5 +19,5 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/text v0.17.0 // indirect
golang.org/x/text v0.18.0 // indirect
)
8 changes: 4 additions & 4 deletions build/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ github.com/outofforest/parallel v0.2.3 h1:DRIgHr7XTL4LLgsTqrj041kulv4ajtbCkRbkOG
github.com/outofforest/parallel v0.2.3/go.mod h1:cu210xIjJtOMXR2ERzEcNA2kr0Z0xfZjSKw2jTxAQ2E=
github.com/outofforest/run v0.6.0 h1:t/3vAodvU5L5vJ3BB0qRgfviX+T3JJmLgPN6G2WQs3U=
github.com/outofforest/run v0.6.0/go.mod h1:l7TAtA/zG+7JEH017Qrfm7gzmMyM0gjd+CereJdJSpI=
github.com/outofforest/tools v1.0.2 h1:EE+rasbu44NEX0ML7HtyNaeKdBT3CwEEURKMxGgJgoM=
github.com/outofforest/tools v1.0.2/go.mod h1:fn8F7Z4F4SdyWCSIQIf2NvHbN08fDCum+aDVEbxSPUs=
github.com/outofforest/tools v1.0.4 h1:PdZVOeEbHDwQoVMifrOFz0CzTGDxDNBvv295Z2A5mps=
github.com/outofforest/tools v1.0.4/go.mod h1:fn8F7Z4F4SdyWCSIQIf2NvHbN08fDCum+aDVEbxSPUs=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down Expand Up @@ -73,8 +73,8 @@ golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
Expand Down
13 changes: 13 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
module github.com/outofforest/quantum

go 1.22

require (
github.com/cespare/xxhash v1.1.0
github.com/outofforest/photon v0.5.0
github.com/stretchr/testify v1.8.4
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
20 changes: 20 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
github.com/OneOfOne/xxhash v1.2.2 h1:KMrpdQIwFcEqXDklaen+P1axHaj9BSKzvpUUfnHldSE=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/outofforest/photon v0.5.0 h1:7Mq92+Dwj7TPOIZzbwOYBe05OLOP0d7GtRFvOGaU000=
github.com/outofforest/photon v0.5.0/go.mod h1:4qOhLdJ3jiXj7umpt57hCGs5T+p3LX9QdpkipX4YDy4=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72 h1:qLC7fQah7D6K1B0ujays3HV9gkFtllcxhzImRR7ArPQ=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
213 changes: 213 additions & 0 deletions quantum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
package quantum

import (
"unsafe"

"github.com/cespare/xxhash"

"github.com/outofforest/photon"
)

// FIXME (wojciech): avoid individual heap allocations for nodes.

// New creates new quantum store.
func New[K comparable, V any]() Snapshot[K, V] {
return Snapshot[K, V]{
root: new(node[K, V]),
rootSet: new(bool),
defaultValue: *new(V),
}
}

// Snapshot represents the state at particular point in time.
type Snapshot[K comparable, V any] struct {
version uint64
root *node[K, V]
rootSet *bool
defaultValue V
hasher hasher[K]
hashMod uint64
}

// Next transitions to the next snapshot of the state.
func (s Snapshot[K, V]) Next() Snapshot[K, V] {
s.version++

r := *s.root
s.root = &r

rs := *s.rootSet
s.rootSet = &rs

return s
}

// Get gets the value of the key.
func (s Snapshot[K, V]) Get(key K) (value V, exists bool) {
h := s.hasher.Hash(key)
n := s.root
for {
if n == nil {
return s.defaultValue, false
}
if n.Hash == h {
if n.Key == key {
return n.Value, true
}

// conflict
if s.hasher.bytes == nil {
s.hashMod++
n.hasher = newHasher[K](s.hashMod)
}
h = n.hasher.Hash(key)
}

bit := h & 0x01
h >>= 1

switch bit {
case 0x00:
n = n.Left
default:
n = n.Right
}
}
}

// Set sets the value for the key.
func (s Snapshot[K, V]) Set(key K, value V) {
const (
leftChild int = iota
rightChild
)

h := s.hasher.Hash(key)

if !*s.rootSet {
*s.root = node[K, V]{
Value: value,
Key: key,
Version: s.version,
Hash: h,
}
*s.rootSet = true
return
}

var parentNode *node[K, V]
var child int
n := s.root
for {
if n == nil {
n = &node[K, V]{
Value: value,
Key: key,
Version: s.version,
Hash: h,
}

if child == leftChild {
parentNode.Left = n
} else {
parentNode.Right = n
}
return
}
if n.Version < s.version {
n2 := *n
n2.Version = s.version

switch {
case parentNode == nil:
*s.root = n2
n = s.root
case child == leftChild:
n = &n2
parentNode.Left = n
default:
n = &n2
parentNode.Right = n
}
}
if n.Hash == h {
if n.Key == key {
n.Value = value
return
}

// conflict
if s.hasher.bytes == nil {
s.hashMod++
n.hasher = newHasher[K](s.hashMod)
}
h = n.hasher.Hash(key)
}

bit := h & 0x01
h >>= 1
parentNode = n

switch bit {
case 0x00:
n = n.Left
child = leftChild
case 0x01:
n = n.Right
child = rightChild
}
}
}

type node[K comparable, V any] struct {
Key K
Value V

Version uint64
Hash uint64
Left *node[K, V]
Right *node[K, V]

hasher hasher[K]
}

const uint64Length = 8

func newHasher[K comparable](mod uint64) hasher[K] {
var k K
var bytes []byte
var data []byte
if mod > 0 {
bytes = make([]byte, uint64Length+unsafe.Sizeof(k))
copy(bytes, photon.NewFromValue(&mod).B)
data = bytes[uint64Length:]
}

return hasher[K]{
bytes: bytes,
data: data,
}
}

type hasher[K comparable] struct {
bytes []byte
data []byte
}

func (h hasher[K]) Hash(key K) uint64 {
var hash uint64
if h.bytes == nil {
hash = xxhash.Sum64(photon.NewFromValue[K](&key).B)
} else {
copy(h.data, photon.NewFromValue[K](&key).B)
hash = xxhash.Sum64(h.data)
}
if isTesting {
hash = testHash(hash)
}
return hash
}

func testHash(hash uint64) uint64 {
return hash & 0x7fffffff
}
Loading