/
root.go
203 lines (169 loc) · 5.54 KB
/
root.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
* Copyright 2020 The Yorkie Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Package crdt provides the implementation of the CRDT data structure.
// The CRDT data structure is a data structure that can be replicated and
// shared among multiple replicas.
package crdt
import (
"github.com/yorkie-team/yorkie/pkg/document/time"
)
// ElementPair represents pair that has a parent element and child element.
type ElementPair struct {
parent Container
elem Element
}
// Root is a structure represents the root of JSON. It has a hash table of
// all JSON elements to find a specific element when applying remote changes
// received from server.
//
// Every element has a unique time ticket at creation, which allows us to find
// a particular element.
type Root struct {
object *Object
elementMapByCreatedAt map[string]Element
removedElementPairMapByCreatedAt map[string]ElementPair
elementHasRemovedNodesSetByCreatedAt map[string]GCElement
}
// NewRoot creates a new instance of Root.
func NewRoot(root *Object) *Root {
r := &Root{
elementMapByCreatedAt: make(map[string]Element),
removedElementPairMapByCreatedAt: make(map[string]ElementPair),
elementHasRemovedNodesSetByCreatedAt: make(map[string]GCElement),
}
r.object = root
r.RegisterElement(root)
root.Descendants(func(elem Element, parent Container) bool {
if elem.RemovedAt() != nil {
r.RegisterRemovedElementPair(parent, elem)
}
// TODO(hackerwins): Register text elements with garbage
return false
})
return r
}
// Object returns the root object of the JSON.
func (r *Root) Object() *Object {
return r.object
}
// FindByCreatedAt returns the element of given creation time.
func (r *Root) FindByCreatedAt(createdAt *time.Ticket) Element {
return r.elementMapByCreatedAt[createdAt.Key()]
}
// RegisterElement registers the given element to hash table.
func (r *Root) RegisterElement(element Element) {
r.elementMapByCreatedAt[element.CreatedAt().Key()] = element
switch element := element.(type) {
case Container:
{
element.Descendants(func(elem Element, parent Container) bool {
r.elementMapByCreatedAt[elem.CreatedAt().Key()] = elem
return false
})
}
}
}
// deregisterElement deregister the given element from hash tables.
func (r *Root) deregisterElement(element Element) int {
count := 0
deregisterElementInternal := func(elem Element) {
createdAt := elem.CreatedAt().Key()
delete(r.elementMapByCreatedAt, createdAt)
delete(r.removedElementPairMapByCreatedAt, createdAt)
count++
}
deregisterElementInternal(element)
switch element := element.(type) {
case Container:
{
element.Descendants(func(elem Element, parent Container) bool {
deregisterElementInternal(elem)
return false
})
}
}
return count
}
// RegisterRemovedElementPair register the given element pair to hash table.
func (r *Root) RegisterRemovedElementPair(parent Container, elem Element) {
r.removedElementPairMapByCreatedAt[elem.CreatedAt().Key()] = ElementPair{
parent,
elem,
}
}
// RegisterElementHasRemovedNodes register the given element with garbage to hash table.
func (r *Root) RegisterElementHasRemovedNodes(element GCElement) {
r.elementHasRemovedNodesSetByCreatedAt[element.CreatedAt().Key()] = element
}
// DeepCopy copies itself deeply.
func (r *Root) DeepCopy() (*Root, error) {
copiedObject, err := r.object.DeepCopy()
if err != nil {
return nil, err
}
return NewRoot(copiedObject.(*Object)), nil
}
// GarbageCollect purge elements that were removed before the given time.
func (r *Root) GarbageCollect(ticket *time.Ticket) (int, error) {
count := 0
for _, pair := range r.removedElementPairMapByCreatedAt {
if pair.elem.RemovedAt() != nil && ticket.Compare(pair.elem.RemovedAt()) >= 0 {
if err := pair.parent.Purge(pair.elem); err != nil {
return 0, err
}
count += r.deregisterElement(pair.elem)
}
}
for _, node := range r.elementHasRemovedNodesSetByCreatedAt {
purgedNodes, err := node.purgeRemovedNodesBefore(ticket)
if err != nil {
return 0, err
}
if node.removedNodesLen() == 0 {
delete(r.elementHasRemovedNodesSetByCreatedAt, node.CreatedAt().Key())
}
count += purgedNodes
}
return count, nil
}
// ElementMapLen returns the size of element map.
func (r *Root) ElementMapLen() int {
return len(r.elementMapByCreatedAt)
}
// RemovedElementLen returns the size of removed element map.
func (r *Root) RemovedElementLen() int {
return len(r.removedElementPairMapByCreatedAt)
}
// GarbageLen returns the count of removed elements.
func (r *Root) GarbageLen() int {
count := 0
seen := make(map[string]bool)
for _, pair := range r.removedElementPairMapByCreatedAt {
seen[pair.elem.CreatedAt().Key()] = true
switch elem := pair.elem.(type) {
case Container:
elem.Descendants(func(elem Element, parent Container) bool {
seen[elem.CreatedAt().Key()] = true
return false
})
}
}
count += len(seen)
for _, element := range r.elementHasRemovedNodesSetByCreatedAt {
count += element.removedNodesLen()
}
return count
}