-
-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove priority queue from RHTPQMap (#462)
Co-authored-by: Youngteac Hong <susukang98@gmail.com>
- Loading branch information
1 parent
61e6953
commit 3d9fe9d
Showing
17 changed files
with
265 additions
and
560 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,208 @@ | ||
/* | ||
* Copyright 2023 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" | ||
) | ||
|
||
// ElementRHTNode is a node of ElementRHT. | ||
type ElementRHTNode struct { | ||
key string | ||
elem Element | ||
} | ||
|
||
func newElementRHTNode(key string, elem Element) *ElementRHTNode { | ||
return &ElementRHTNode{ | ||
key: key, | ||
elem: elem, | ||
} | ||
} | ||
|
||
// Remove removes this node. It only marks the deleted time (tombstone). | ||
func (n *ElementRHTNode) Remove(removedAt *time.Ticket) bool { | ||
if removedAt != nil && removedAt.After(n.elem.CreatedAt()) { | ||
if n.elem.RemovedAt() == nil || removedAt.After(n.elem.RemovedAt()) { | ||
return n.elem.Remove(removedAt) | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func (n *ElementRHTNode) isRemoved() bool { | ||
return n.elem.RemovedAt() != nil | ||
} | ||
|
||
// Key returns the key of this node. | ||
func (n *ElementRHTNode) Key() string { | ||
return n.key | ||
} | ||
|
||
// Element returns the element of this node. | ||
func (n *ElementRHTNode) Element() Element { | ||
return n.elem | ||
} | ||
|
||
// ElementRHT is a hashtable with logical clock(Replicated hashtable). | ||
type ElementRHT struct { | ||
// nodeMapByKey is a map with values of nodes by key. | ||
nodeMapByKey map[string]*ElementRHTNode | ||
// nodeMapByCreatedAt is a map with values of nodes by creation time. | ||
// Even if an element is removed by `set` or `delete`, it remains in | ||
// nodeMapByCreatedAt and will be deleted physically by GC. | ||
nodeMapByCreatedAt map[string]*ElementRHTNode | ||
} | ||
|
||
// NewElementRHT creates a new instance of ElementRHT. | ||
func NewElementRHT() *ElementRHT { | ||
return &ElementRHT{ | ||
nodeMapByKey: make(map[string]*ElementRHTNode), | ||
nodeMapByCreatedAt: make(map[string]*ElementRHTNode), | ||
} | ||
} | ||
|
||
// Get returns the value of the given key. | ||
func (rht *ElementRHT) Get(key string) Element { | ||
if node, ok := rht.nodeMapByKey[key]; ok { | ||
if node.isRemoved() { | ||
return nil | ||
} | ||
return node.elem | ||
} | ||
return nil | ||
} | ||
|
||
// Has returns whether the element exists of the given key or not. | ||
func (rht *ElementRHT) 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. If there is an existing value, it is removed. | ||
func (rht *ElementRHT) Set(k string, v Element) Element { | ||
node, ok := rht.nodeMapByKey[k] | ||
var removed Element | ||
if ok && node.Remove(v.CreatedAt()) { | ||
removed = node.elem | ||
} | ||
newNode := newElementRHTNode(k, v) | ||
rht.nodeMapByCreatedAt[v.CreatedAt().Key()] = newNode | ||
if !ok || v.CreatedAt().After(node.elem.CreatedAt()) { | ||
rht.nodeMapByKey[k] = newNode | ||
} | ||
|
||
return removed | ||
} | ||
|
||
// Delete deletes the Element of the given key. | ||
func (rht *ElementRHT) Delete(k string, deletedAt *time.Ticket) Element { | ||
node, ok := rht.nodeMapByKey[k] | ||
if !ok { | ||
return nil | ||
} | ||
|
||
if !node.Remove(deletedAt) { | ||
return nil | ||
} | ||
|
||
return node.elem | ||
} | ||
|
||
// DeleteByCreatedAt deletes the Element of the given creation time. | ||
func (rht *ElementRHT) DeleteByCreatedAt(createdAt *time.Ticket, deletedAt *time.Ticket) Element { | ||
node, ok := rht.nodeMapByCreatedAt[createdAt.Key()] | ||
if !ok { | ||
return nil | ||
} | ||
|
||
if !node.Remove(deletedAt) { | ||
return nil | ||
} | ||
|
||
return node.elem | ||
} | ||
|
||
// 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 *ElementRHT) Elements() map[string]Element { | ||
members := make(map[string]Element) | ||
for _, node := range rht.nodeMapByKey { | ||
if !node.isRemoved() { | ||
members[node.key] = node.elem | ||
} | ||
} | ||
|
||
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 *ElementRHT) Nodes() []*ElementRHTNode { | ||
var nodes []*ElementRHTNode | ||
for _, node := range rht.nodeMapByKey { | ||
nodes = append(nodes, node) | ||
} | ||
|
||
return nodes | ||
} | ||
|
||
// purge physically purge child element. | ||
func (rht *ElementRHT) purge(elem Element) { | ||
node, ok := rht.nodeMapByCreatedAt[elem.CreatedAt().Key()] | ||
if !ok { | ||
panic("fail to find: " + elem.CreatedAt().Key()) | ||
} | ||
delete(rht.nodeMapByCreatedAt, node.elem.CreatedAt().Key()) | ||
|
||
nodeByKey, ok := rht.nodeMapByKey[node.key] | ||
if ok && node == nodeByKey { | ||
delete(rht.nodeMapByKey, nodeByKey.key) | ||
} | ||
} | ||
|
||
// Marshal returns the JSON encoding of this map. | ||
func (rht *ElementRHT) 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), value.Marshal())) | ||
} | ||
sb.WriteString("}") | ||
|
||
return sb.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.