-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.go
52 lines (40 loc) · 882 Bytes
/
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
package tempdb
type BucketID uint64
type State struct {
Buckets []Bucket
next BucketID
nextTX int
}
const RootBucketID = BucketID(0)
func (s *State) Add(bkt Bucket) BucketID {
s.next++
bkt.ID = s.next
s.Buckets = append(s.Buckets, bkt)
return bkt.ID
}
// perform a deep copy.
func (s *State) Copy() *State {
// create a new state.
cpy := &State{
next: s.next,
nextTX: s.nextTX,
}
for _, bkt := range s.Buckets {
// create a new bucket, shallow-copying most values.
cbkt := Bucket{
ID: bkt.ID,
Parent: bkt.Parent,
Key: bkt.Key,
sequence: bkt.sequence,
}
// create a new map for storing the value.
cbkt.Value = make(map[string][]byte)
// deep copy the map values.
for k, v := range bkt.Value {
cbkt.Value[k] = v
}
// add the bucket to the new state.
cpy.Buckets = append(cpy.Buckets, cbkt)
}
return cpy
}