2023-11-30 03:02:38 +01:00
|
|
|
package crdt
|
|
|
|
|
|
|
|
import (
|
2023-12-07 19:18:13 +01:00
|
|
|
"cmp"
|
|
|
|
|
2024-01-02 00:55:50 +01:00
|
|
|
"github.com/tim-beatham/smegmesh/pkg/lib"
|
2023-11-30 03:02:38 +01:00
|
|
|
)
|
|
|
|
|
2023-12-07 19:18:13 +01:00
|
|
|
type TwoPhaseMap[K cmp.Ordered, D any] struct {
|
2023-11-30 03:02:38 +01:00
|
|
|
addMap *GMap[K, D]
|
|
|
|
removeMap *GMap[K, bool]
|
2023-12-07 02:44:54 +01:00
|
|
|
Clock *VectorClock[K]
|
2023-11-30 03:02:38 +01:00
|
|
|
processId K
|
|
|
|
}
|
|
|
|
|
2023-12-07 19:18:13 +01:00
|
|
|
type TwoPhaseMapSnapshot[K cmp.Ordered, D any] struct {
|
2023-12-08 21:02:57 +01:00
|
|
|
Add map[uint64]Bucket[D]
|
|
|
|
Remove map[uint64]Bucket[bool]
|
2023-11-30 03:02:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Contains checks whether the value exists in the map
|
|
|
|
func (m *TwoPhaseMap[K, D]) Contains(key K) bool {
|
2023-12-08 21:02:57 +01:00
|
|
|
return m.contains(m.Clock.hashFunc(key))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Contains checks whether the value exists in the map
|
|
|
|
func (m *TwoPhaseMap[K, D]) contains(key uint64) bool {
|
|
|
|
if !m.addMap.contains(key) {
|
2023-11-30 03:02:38 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
addValue := m.addMap.get(key)
|
|
|
|
|
2023-12-08 21:02:57 +01:00
|
|
|
if !m.removeMap.contains(key) {
|
2023-11-30 03:02:38 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
removeValue := m.removeMap.get(key)
|
|
|
|
|
|
|
|
return addValue.Vector >= removeValue.Vector
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *TwoPhaseMap[K, D]) Get(key K) D {
|
|
|
|
var result D
|
|
|
|
|
|
|
|
if !m.Contains(key) {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.addMap.Get(key)
|
|
|
|
}
|
|
|
|
|
2023-12-08 21:02:57 +01:00
|
|
|
func (m *TwoPhaseMap[K, D]) get(key uint64) D {
|
|
|
|
var result D
|
|
|
|
|
|
|
|
if !m.contains(key) {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.addMap.get(key).Contents
|
|
|
|
}
|
|
|
|
|
2023-11-30 03:02:38 +01:00
|
|
|
// Put places the key K in the map
|
|
|
|
func (m *TwoPhaseMap[K, D]) Put(key K, data D) {
|
2023-12-07 02:44:54 +01:00
|
|
|
msgSequence := m.Clock.IncrementClock()
|
|
|
|
m.Clock.Put(key, msgSequence)
|
2023-11-30 03:02:38 +01:00
|
|
|
m.addMap.Put(key, data)
|
|
|
|
}
|
|
|
|
|
2023-12-06 23:45:04 +01:00
|
|
|
func (m *TwoPhaseMap[K, D]) Mark(key K) {
|
|
|
|
m.addMap.Mark(key)
|
|
|
|
}
|
|
|
|
|
2023-11-30 03:02:38 +01:00
|
|
|
// Remove removes the value from the map
|
|
|
|
func (m *TwoPhaseMap[K, D]) Remove(key K) {
|
|
|
|
m.removeMap.Put(key, true)
|
|
|
|
}
|
|
|
|
|
2023-12-08 21:02:57 +01:00
|
|
|
func (m *TwoPhaseMap[K, D]) keys() []uint64 {
|
|
|
|
keys := make([]uint64, 0)
|
2023-11-30 03:02:38 +01:00
|
|
|
|
|
|
|
addKeys := m.addMap.Keys()
|
|
|
|
|
|
|
|
for _, key := range addKeys {
|
2023-12-08 21:02:57 +01:00
|
|
|
if !m.contains(key) {
|
2023-11-30 03:02:38 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
keys = append(keys, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
return keys
|
|
|
|
}
|
|
|
|
|
2023-12-08 21:02:57 +01:00
|
|
|
func (m *TwoPhaseMap[K, D]) AsList() []D {
|
|
|
|
theList := make([]D, 0)
|
2023-11-30 03:02:38 +01:00
|
|
|
|
2023-12-08 21:02:57 +01:00
|
|
|
keys := m.keys()
|
2023-11-30 03:02:38 +01:00
|
|
|
|
|
|
|
for _, key := range keys {
|
2023-12-08 21:02:57 +01:00
|
|
|
theList = append(theList, m.get(key))
|
2023-11-30 03:02:38 +01:00
|
|
|
}
|
|
|
|
|
2023-12-08 21:02:57 +01:00
|
|
|
return theList
|
2023-11-30 03:02:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *TwoPhaseMap[K, D]) Snapshot() *TwoPhaseMapSnapshot[K, D] {
|
|
|
|
return &TwoPhaseMapSnapshot[K, D]{
|
|
|
|
Add: m.addMap.Save(),
|
|
|
|
Remove: m.removeMap.Save(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *TwoPhaseMap[K, D]) SnapShotFromState(state *TwoPhaseMapState[K]) *TwoPhaseMapSnapshot[K, D] {
|
|
|
|
addKeys := lib.MapKeys(state.AddContents)
|
|
|
|
removeKeys := lib.MapKeys(state.RemoveContents)
|
|
|
|
|
|
|
|
return &TwoPhaseMapSnapshot[K, D]{
|
|
|
|
Add: m.addMap.SaveWithKeys(addKeys),
|
|
|
|
Remove: m.removeMap.SaveWithKeys(removeKeys),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-07 19:18:13 +01:00
|
|
|
type TwoPhaseMapState[K cmp.Ordered] struct {
|
2023-12-08 21:02:57 +01:00
|
|
|
Vectors map[uint64]uint64
|
|
|
|
AddContents map[uint64]uint64
|
|
|
|
RemoveContents map[uint64]uint64
|
2023-11-30 03:02:38 +01:00
|
|
|
}
|
|
|
|
|
2023-12-06 23:45:04 +01:00
|
|
|
func (m *TwoPhaseMap[K, D]) IsMarked(key K) bool {
|
|
|
|
return m.addMap.IsMarked(key)
|
|
|
|
}
|
|
|
|
|
2023-11-30 16:58:26 +01:00
|
|
|
// GetHash: Get the hash of the current state of the map
|
|
|
|
// Sums the current values of the vectors. Provides good approximation
|
|
|
|
// of increasing numbers
|
|
|
|
func (m *TwoPhaseMap[K, D]) GetHash() uint64 {
|
2023-12-08 21:02:57 +01:00
|
|
|
return (m.addMap.GetHash() + 1) * (m.removeMap.GetHash() + 1)
|
2023-11-30 03:02:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetState: get the current vector clock of the add and remove
|
|
|
|
// map
|
|
|
|
func (m *TwoPhaseMap[K, D]) GenerateMessage() *TwoPhaseMapState[K] {
|
|
|
|
addContents := m.addMap.GetClock()
|
|
|
|
removeContents := m.removeMap.GetClock()
|
|
|
|
|
|
|
|
return &TwoPhaseMapState[K]{
|
2023-12-07 02:44:54 +01:00
|
|
|
Vectors: m.Clock.GetClock(),
|
2023-11-30 03:02:38 +01:00
|
|
|
AddContents: addContents,
|
|
|
|
RemoveContents: removeContents,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-11 12:09:02 +01:00
|
|
|
func (m *TwoPhaseMapState[K]) Difference(highestStale uint64, state *TwoPhaseMapState[K]) *TwoPhaseMapState[K] {
|
2023-11-30 03:02:38 +01:00
|
|
|
mapState := &TwoPhaseMapState[K]{
|
2023-12-08 21:02:57 +01:00
|
|
|
AddContents: make(map[uint64]uint64),
|
|
|
|
RemoveContents: make(map[uint64]uint64),
|
2023-11-30 03:02:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for key, value := range state.AddContents {
|
|
|
|
otherValue, ok := m.AddContents[key]
|
|
|
|
|
2023-12-11 12:09:02 +01:00
|
|
|
if value > highestStale && (!ok || otherValue < value) {
|
2023-11-30 03:02:38 +01:00
|
|
|
mapState.AddContents[key] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-07 19:18:13 +01:00
|
|
|
for key, value := range state.RemoveContents {
|
2023-11-30 03:02:38 +01:00
|
|
|
otherValue, ok := m.RemoveContents[key]
|
|
|
|
|
2023-12-11 12:09:02 +01:00
|
|
|
if value > highestStale && (!ok || otherValue < value) {
|
2023-11-30 03:02:38 +01:00
|
|
|
mapState.RemoveContents[key] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return mapState
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *TwoPhaseMap[K, D]) Merge(snapshot TwoPhaseMapSnapshot[K, D]) {
|
|
|
|
for key, value := range snapshot.Add {
|
2023-12-06 23:45:04 +01:00
|
|
|
// Gravestone is local only to that node.
|
|
|
|
// Discover ourselves if the node is alive
|
2023-11-30 03:02:38 +01:00
|
|
|
m.addMap.put(key, value)
|
2023-12-08 21:02:57 +01:00
|
|
|
m.Clock.put(key, value.Vector)
|
2023-11-30 03:02:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for key, value := range snapshot.Remove {
|
|
|
|
m.removeMap.put(key, value)
|
2023-12-08 21:02:57 +01:00
|
|
|
m.Clock.put(key, value.Vector)
|
2023-11-30 03:02:38 +01:00
|
|
|
}
|
2023-12-07 02:44:54 +01:00
|
|
|
}
|
2023-11-30 03:02:38 +01:00
|
|
|
|
2023-12-07 02:44:54 +01:00
|
|
|
func (m *TwoPhaseMap[K, D]) Prune() {
|
|
|
|
m.addMap.Prune()
|
|
|
|
m.removeMap.Prune()
|
|
|
|
m.Clock.Prune()
|
2023-11-30 03:02:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewTwoPhaseMap: create a new two phase map. Consists of two maps
|
|
|
|
// a grow map and a remove map. If both timestamps equal then favour keeping
|
|
|
|
// it in the map
|
2023-12-07 19:18:13 +01:00
|
|
|
func NewTwoPhaseMap[K cmp.Ordered, D any](processId K, hashKey func(K) uint64, staleTime uint64) *TwoPhaseMap[K, D] {
|
2023-11-30 03:02:38 +01:00
|
|
|
m := TwoPhaseMap[K, D]{
|
|
|
|
processId: processId,
|
2023-12-07 19:18:13 +01:00
|
|
|
Clock: NewVectorClock(processId, hashKey, staleTime),
|
2023-11-30 03:02:38 +01:00
|
|
|
}
|
|
|
|
|
2023-12-07 02:44:54 +01:00
|
|
|
m.addMap = NewGMap[K, D](m.Clock)
|
|
|
|
m.removeMap = NewGMap[K, bool](m.Clock)
|
2023-11-30 03:02:38 +01:00
|
|
|
return &m
|
|
|
|
}
|