-
Notifications
You must be signed in to change notification settings - Fork 0
/
prefixed_state.go
96 lines (78 loc) · 2.22 KB
/
prefixed_state.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package state
import (
"github.com/lasthyphen/dijetsnodesgo/cache"
"github.com/lasthyphen/dijetsnodesgo/ids"
"github.com/lasthyphen/dijetsnodesgo/snow/choices"
"github.com/lasthyphen/dijetsnodesgo/snow/engine/dijets/vertex"
)
const (
vtxID uint64 = iota
vtxStatusID
edgeID
)
var uniqueEdgeID = ids.Empty.Prefix(edgeID)
type prefixedState struct {
state *state
vtx, status cache.Cacher
uniqueVtx cache.Deduplicator
}
func newPrefixedState(state *state, idCacheSizes int) *prefixedState {
return &prefixedState{
state: state,
vtx: &cache.LRU{Size: idCacheSizes},
status: &cache.LRU{Size: idCacheSizes},
uniqueVtx: &cache.EvictableLRU{Size: idCacheSizes},
}
}
func (s *prefixedState) UniqueVertex(vtx *uniqueVertex) *uniqueVertex {
return s.uniqueVtx.Deduplicate(vtx).(*uniqueVertex)
}
func (s *prefixedState) Vertex(id ids.ID) vertex.StatelessVertex {
var vID ids.ID
if cachedVtxIDIntf, found := s.vtx.Get(id); found {
vID = cachedVtxIDIntf.(ids.ID)
} else {
vID = id.Prefix(vtxID)
s.vtx.Put(id, vID)
}
return s.state.Vertex(vID)
}
func (s *prefixedState) SetVertex(vtx vertex.StatelessVertex) error {
rawVertexID := vtx.ID()
var vID ids.ID
if cachedVtxIDIntf, found := s.vtx.Get(rawVertexID); found {
vID = cachedVtxIDIntf.(ids.ID)
} else {
vID = rawVertexID.Prefix(vtxID)
s.vtx.Put(rawVertexID, vID)
}
return s.state.SetVertex(vID, vtx)
}
func (s *prefixedState) Status(id ids.ID) choices.Status {
var sID ids.ID
if cachedStatusIDIntf, found := s.status.Get(id); found {
sID = cachedStatusIDIntf.(ids.ID)
} else {
sID = id.Prefix(vtxStatusID)
s.status.Put(id, sID)
}
return s.state.Status(sID)
}
func (s *prefixedState) SetStatus(id ids.ID, status choices.Status) error {
var sID ids.ID
if cachedStatusIDIntf, found := s.status.Get(id); found {
sID = cachedStatusIDIntf.(ids.ID)
} else {
sID = id.Prefix(vtxStatusID)
s.status.Put(id, sID)
}
return s.state.SetStatus(sID, status)
}
func (s *prefixedState) Edge() []ids.ID {
return s.state.Edge(uniqueEdgeID)
}
func (s *prefixedState) SetEdge(frontier []ids.ID) error {
return s.state.SetEdge(uniqueEdgeID, frontier)
}