-
-
Notifications
You must be signed in to change notification settings - Fork 147
/
rht.go
225 lines (189 loc) · 5.23 KB
/
rht.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
* 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
import (
"fmt"
"sort"
"strings"
"github.com/yorkie-team/yorkie/pkg/document/time"
)
// RHTNode is a node of RHT(Replicated Hashtable).
type RHTNode struct {
key string
val string
updatedAt *time.Ticket
isRemoved bool
}
func newRHTNode(key, val string, updatedAt *time.Ticket, isRemoved bool) *RHTNode {
return &RHTNode{
key: key,
val: val,
updatedAt: updatedAt,
isRemoved: isRemoved,
}
}
// Key returns the key of this node.
func (n *RHTNode) Key() string {
return n.key
}
// Value returns the value of this node.
func (n *RHTNode) Value() string {
return n.val
}
// UpdatedAt returns the last update time.
func (n *RHTNode) UpdatedAt() *time.Ticket {
return n.updatedAt
}
// RHT is a hashtable with logical clock(Replicated hashtable).
// For more details about RHT: http://csl.skku.edu/papers/jpdc11.pdf
// NOTE(justiceHui): RHT and ElementRHT has duplicated functions.
type RHT struct {
nodeMapByKey map[string]*RHTNode
numberOfRemovedElement int
}
// NewRHT creates a new instance of RHT.
func NewRHT() *RHT {
return &RHT{
nodeMapByKey: make(map[string]*RHTNode),
numberOfRemovedElement: 0,
}
}
// Get returns the value of the given key.
func (rht *RHT) Get(key string) string {
if node, ok := rht.nodeMapByKey[key]; ok {
if node.isRemoved {
return ""
}
return node.val
}
return ""
}
// Has returns whether the element exists of the given key or not.
func (rht *RHT) Has(key string) bool {
if node, ok := rht.nodeMapByKey[key]; ok {
return node != nil && !node.isRemoved
}
return false
}
// Set sets the value of the given key.
func (rht *RHT) Set(k, v string, executedAt *time.Ticket) {
if node, ok := rht.nodeMapByKey[k]; !ok || executedAt.After(node.updatedAt) {
if node != nil && node.isRemoved {
rht.numberOfRemovedElement--
}
newNode := newRHTNode(k, v, executedAt, false)
rht.nodeMapByKey[k] = newNode
}
}
// Remove removes the Element of the given key.
func (rht *RHT) Remove(k string, executedAt *time.Ticket) string {
if node, ok := rht.nodeMapByKey[k]; !ok || executedAt.After(node.updatedAt) {
// NOTE(justiceHui): Even if key is not existed, we must set flag `isRemoved` for concurrency
if node == nil {
rht.numberOfRemovedElement++
newNode := newRHTNode(k, ``, executedAt, true)
rht.nodeMapByKey[k] = newNode
return ""
}
alreadyRemoved := node.isRemoved
if !alreadyRemoved {
rht.numberOfRemovedElement++
}
newNode := newRHTNode(k, node.val, executedAt, true)
rht.nodeMapByKey[k] = newNode
if alreadyRemoved {
return ""
}
return node.val
}
return ""
}
// Elements returns a map of elements because the map easy to use for loop.
// TODO: If we encounter performance issues, we need to replace this with other solution.
func (rht *RHT) Elements() map[string]string {
members := make(map[string]string)
for _, node := range rht.nodeMapByKey {
if !node.isRemoved {
members[node.key] = node.val
}
}
return members
}
// Nodes returns a map of elements because the map easy to use for loop.
// TODO: If we encounter performance issues, we need to replace this with other solution.
func (rht *RHT) Nodes() []*RHTNode {
var nodes []*RHTNode
for _, node := range rht.nodeMapByKey {
if !node.isRemoved {
nodes = append(nodes, node)
}
}
return nodes
}
// Len returns the number of elements.
func (rht *RHT) Len() int {
return len(rht.nodeMapByKey) - rht.numberOfRemovedElement
}
// DeepCopy copies itself deeply.
func (rht *RHT) DeepCopy() *RHT {
instance := NewRHT()
for _, node := range rht.Nodes() {
instance.Set(node.key, node.val, node.updatedAt)
}
return instance
}
// Marshal returns the JSON encoding of this hashtable.
func (rht *RHT) Marshal() string {
members := rht.Elements()
size := len(members)
// Extract and sort the keys
keys := make([]string, 0, size)
for k := range members {
keys = append(keys, k)
}
sort.Strings(keys)
sb := strings.Builder{}
sb.WriteString("{")
for idx, k := range keys {
if idx > 0 {
sb.WriteString(",")
}
value := members[k]
sb.WriteString(fmt.Sprintf(`"%s":"%s"`, EscapeString(k), EscapeString(value)))
}
sb.WriteString("}")
return sb.String()
}
// ToXML returns the XML representation of this hashtable.
func (rht *RHT) ToXML() string {
members := rht.Elements()
size := len(members)
// Extract and sort the keys
keys := make([]string, 0, size)
for k := range members {
keys = append(keys, k)
}
sort.Strings(keys)
sb := strings.Builder{}
for idx, k := range keys {
if idx > 0 {
sb.WriteString(" ")
}
value := members[k]
sb.WriteString(fmt.Sprintf(`%s="%s"`, k, EscapeString(value)))
}
return sb.String()
}