Skip to content

Commit

Permalink
Add isRemoved flag in RHTNode
Browse files Browse the repository at this point in the history
  • Loading branch information
justiceHui committed Jan 8, 2024
1 parent 3d22070 commit 080822a
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions pkg/document/crdt/rht.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type RHTNode struct {
val string
updatedAt *time.Ticket
removedAt *time.Ticket
isRemoved bool
}

func newRHTNode(key, val string, updatedAt *time.Ticket) *RHTNode {
Expand All @@ -44,11 +45,13 @@ func newRHTNode(key, val string, updatedAt *time.Ticket) *RHTNode {
func (n *RHTNode) Remove(removedAt *time.Ticket) {
if n.removedAt == nil || removedAt.After(n.removedAt) {
n.removedAt = removedAt
n.isRemoved = true
}
}

func (n *RHTNode) isRemoved() bool {
return n.removedAt != nil && n.updatedAt != nil && n.removedAt.Compare(n.updatedAt) == 0
// IsRemoved returns the node has been removed.
func (n *RHTNode) IsRemoved() bool {
return n.removedAt != nil && n.isRemoved
}

// Key returns the key of this node.
Expand All @@ -73,6 +76,7 @@ func (n *RHTNode) RemovedAt() *time.Ticket {

// 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
Expand All @@ -89,7 +93,7 @@ func NewRHT() *RHT {
// 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() {
if node.IsRemoved() {
return ""
}
return node.val
Expand All @@ -101,7 +105,7 @@ func (rht *RHT) Get(key string) string {
// 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 node != nil && !node.IsRemoved()
}

return false
Expand All @@ -110,7 +114,7 @@ func (rht *RHT) Has(key string) bool {
// 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 || node.updatedAt == nil || executedAt.After(node.updatedAt) {
if node != nil && node.isRemoved() {
if node != nil && node.IsRemoved() {
rht.numberOfRemovedElement--
}
newNode := newRHTNode(k, v, executedAt)
Expand All @@ -121,7 +125,7 @@ func (rht *RHT) Set(k, v string, executedAt *time.Ticket) {
// 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 && (node.updatedAt == nil || executedAt.After(node.updatedAt)) {
alreadyRemoved := node.isRemoved()
alreadyRemoved := node.IsRemoved()
if !alreadyRemoved {
rht.numberOfRemovedElement++
}
Expand All @@ -144,7 +148,7 @@ func (rht *RHT) Remove(k string, executedAt *time.Ticket) string {
func (rht *RHT) Elements() map[string]string {
members := make(map[string]string)
for _, node := range rht.nodeMapByKey {
if !node.isRemoved() {
if !node.IsRemoved() {
members[node.key] = node.val
}
}
Expand All @@ -157,7 +161,7 @@ func (rht *RHT) Elements() map[string]string {
func (rht *RHT) Nodes() []*RHTNode {
var nodes []*RHTNode
for _, node := range rht.nodeMapByKey {
if !node.isRemoved() {
if !node.IsRemoved() {
nodes = append(nodes, node)
}
}
Expand Down

1 comment on commit 080822a

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Go Benchmark

Benchmark suite Current: 080822a Previous: 50b3c50 Ratio
BenchmarkDocument/constructor_test - ns/op 1390 ns/op 1542 ns/op 0.90
BenchmarkDocument/constructor_test - B/op 1208 B/op 1208 B/op 1
BenchmarkDocument/constructor_test - allocs/op 20 allocs/op 20 allocs/op 1
BenchmarkDocument/status_test - ns/op 775.5 ns/op 780 ns/op 0.99
BenchmarkDocument/status_test - B/op 1176 B/op 1176 B/op 1
BenchmarkDocument/status_test - allocs/op 18 allocs/op 18 allocs/op 1
BenchmarkDocument/equals_test - ns/op 7093 ns/op 7138 ns/op 0.99
BenchmarkDocument/equals_test - B/op 6913 B/op 6913 B/op 1
BenchmarkDocument/equals_test - allocs/op 120 allocs/op 120 allocs/op 1
BenchmarkDocument/nested_update_test - ns/op 15989 ns/op 16248 ns/op 0.98
BenchmarkDocument/nested_update_test - B/op 11963 B/op 11963 B/op 1
BenchmarkDocument/nested_update_test - allocs/op 254 allocs/op 254 allocs/op 1
BenchmarkDocument/delete_test - ns/op 21708 ns/op 22078 ns/op 0.98
BenchmarkDocument/delete_test - B/op 15188 B/op 15188 B/op 1
BenchmarkDocument/delete_test - allocs/op 333 allocs/op 333 allocs/op 1
BenchmarkDocument/object_test - ns/op 8273 ns/op 8387 ns/op 0.99
BenchmarkDocument/object_test - B/op 6721 B/op 6721 B/op 1
BenchmarkDocument/object_test - allocs/op 116 allocs/op 116 allocs/op 1
BenchmarkDocument/array_test - ns/op 28478 ns/op 28691 ns/op 0.99
BenchmarkDocument/array_test - B/op 11819 B/op 11819 B/op 1
BenchmarkDocument/array_test - allocs/op 270 allocs/op 270 allocs/op 1
BenchmarkDocument/text_test - ns/op 34994 ns/op 31234 ns/op 1.12
BenchmarkDocument/text_test - B/op 14884 B/op 15412 B/op 0.97
BenchmarkDocument/text_test - allocs/op 468 allocs/op 479 allocs/op 0.98
BenchmarkDocument/text_composition_test - ns/op 28771 ns/op 28838 ns/op 1.00
BenchmarkDocument/text_composition_test - B/op 18398 B/op 18590 B/op 0.99
BenchmarkDocument/text_composition_test - allocs/op 477 allocs/op 481 allocs/op 0.99
BenchmarkDocument/rich_text_test - ns/op 79584 ns/op 84725 ns/op 0.94
BenchmarkDocument/rich_text_test - B/op 38805 B/op 47959 B/op 0.81
BenchmarkDocument/rich_text_test - allocs/op 1147 allocs/op 1216 allocs/op 0.94
BenchmarkDocument/counter_test - ns/op 16477 ns/op 16633 ns/op 0.99
BenchmarkDocument/counter_test - B/op 10210 B/op 10210 B/op 1
BenchmarkDocument/counter_test - allocs/op 236 allocs/op 236 allocs/op 1
BenchmarkDocument/text_edit_gc_100 - ns/op 2879043 ns/op 2896824 ns/op 0.99
BenchmarkDocument/text_edit_gc_100 - B/op 1658348 B/op 1658595 B/op 1.00
BenchmarkDocument/text_edit_gc_100 - allocs/op 17091 allocs/op 17092 allocs/op 1.00
BenchmarkDocument/text_edit_gc_1000 - ns/op 228160830 ns/op 232593749 ns/op 0.98
BenchmarkDocument/text_edit_gc_1000 - B/op 144391065 B/op 144379059 B/op 1.00
BenchmarkDocument/text_edit_gc_1000 - allocs/op 200965 allocs/op 200906 allocs/op 1.00
BenchmarkDocument/text_split_gc_100 - ns/op 3375378 ns/op 3420377 ns/op 0.99
BenchmarkDocument/text_split_gc_100 - B/op 2316767 B/op 2316994 B/op 1.00
BenchmarkDocument/text_split_gc_100 - allocs/op 16194 allocs/op 16196 allocs/op 1.00
BenchmarkDocument/text_split_gc_1000 - ns/op 289244434 ns/op 291333302 ns/op 0.99
BenchmarkDocument/text_split_gc_1000 - B/op 228916560 B/op 228919328 B/op 1.00
BenchmarkDocument/text_split_gc_1000 - allocs/op 203955 allocs/op 203934 allocs/op 1.00
BenchmarkDocument/text_delete_all_10000 - ns/op 10897520 ns/op 11818492 ns/op 0.92
BenchmarkDocument/text_delete_all_10000 - B/op 5810949 B/op 5810678 B/op 1.00
BenchmarkDocument/text_delete_all_10000 - allocs/op 40676 allocs/op 40675 allocs/op 1.00
BenchmarkDocument/text_delete_all_100000 - ns/op 181631004 ns/op 201368223 ns/op 0.90
BenchmarkDocument/text_delete_all_100000 - B/op 81904693 B/op 81900570 B/op 1.00
BenchmarkDocument/text_delete_all_100000 - allocs/op 411620 allocs/op 411626 allocs/op 1.00
BenchmarkDocument/text_100 - ns/op 227292 ns/op 220270 ns/op 1.03
BenchmarkDocument/text_100 - B/op 120104 B/op 120107 B/op 1.00
BenchmarkDocument/text_100 - allocs/op 5080 allocs/op 5080 allocs/op 1
BenchmarkDocument/text_1000 - ns/op 2447171 ns/op 2429840 ns/op 1.01
BenchmarkDocument/text_1000 - B/op 1169095 B/op 1169078 B/op 1.00
BenchmarkDocument/text_1000 - allocs/op 50084 allocs/op 50084 allocs/op 1
BenchmarkDocument/array_1000 - ns/op 1229139 ns/op 1191138 ns/op 1.03
BenchmarkDocument/array_1000 - B/op 1091276 B/op 1091341 B/op 1.00
BenchmarkDocument/array_1000 - allocs/op 11826 allocs/op 11826 allocs/op 1
BenchmarkDocument/array_10000 - ns/op 12953955 ns/op 13214012 ns/op 0.98
BenchmarkDocument/array_10000 - B/op 9798365 B/op 9800667 B/op 1.00
BenchmarkDocument/array_10000 - allocs/op 120284 allocs/op 120294 allocs/op 1.00
BenchmarkDocument/array_gc_100 - ns/op 151515 ns/op 144166 ns/op 1.05
BenchmarkDocument/array_gc_100 - B/op 132502 B/op 132489 B/op 1.00
BenchmarkDocument/array_gc_100 - allocs/op 1249 allocs/op 1248 allocs/op 1.00
BenchmarkDocument/array_gc_1000 - ns/op 1418085 ns/op 1375392 ns/op 1.03
BenchmarkDocument/array_gc_1000 - B/op 1158863 B/op 1158914 B/op 1.00
BenchmarkDocument/array_gc_1000 - allocs/op 12864 allocs/op 12864 allocs/op 1
BenchmarkDocument/counter_1000 - ns/op 205295 ns/op 198354 ns/op 1.03
BenchmarkDocument/counter_1000 - B/op 192852 B/op 192853 B/op 1.00
BenchmarkDocument/counter_1000 - allocs/op 5765 allocs/op 5765 allocs/op 1
BenchmarkDocument/counter_10000 - ns/op 2152950 ns/op 2157794 ns/op 1.00
BenchmarkDocument/counter_10000 - B/op 2087768 B/op 2087766 B/op 1.00
BenchmarkDocument/counter_10000 - allocs/op 59772 allocs/op 59772 allocs/op 1
BenchmarkDocument/object_1000 - ns/op 1370571 ns/op 1354096 ns/op 1.01
BenchmarkDocument/object_1000 - B/op 1427951 B/op 1428036 B/op 1.00
BenchmarkDocument/object_1000 - allocs/op 9845 allocs/op 9845 allocs/op 1
BenchmarkDocument/object_10000 - ns/op 14618726 ns/op 15028111 ns/op 0.97
BenchmarkDocument/object_10000 - B/op 12167156 B/op 12166744 B/op 1.00
BenchmarkDocument/object_10000 - allocs/op 100560 allocs/op 100559 allocs/op 1.00
BenchmarkDocument/tree_100 - ns/op 1077471 ns/op 1049112 ns/op 1.03
BenchmarkDocument/tree_100 - B/op 943678 B/op 943678 B/op 1
BenchmarkDocument/tree_100 - allocs/op 6099 allocs/op 6099 allocs/op 1
BenchmarkDocument/tree_1000 - ns/op 75763298 ns/op 79782715 ns/op 0.95
BenchmarkDocument/tree_1000 - B/op 86460480 B/op 86460627 B/op 1.00
BenchmarkDocument/tree_1000 - allocs/op 60113 allocs/op 60114 allocs/op 1.00
BenchmarkDocument/tree_10000 - ns/op 9276805943 ns/op 9957562898 ns/op 0.93
BenchmarkDocument/tree_10000 - B/op 8580974168 B/op 8580990424 B/op 1.00
BenchmarkDocument/tree_10000 - allocs/op 600239 allocs/op 600232 allocs/op 1.00
BenchmarkDocument/tree_delete_all_1000 - ns/op 76190589 ns/op 77379744 ns/op 0.98
BenchmarkDocument/tree_delete_all_1000 - B/op 86990588 B/op 86990891 B/op 1.00
BenchmarkDocument/tree_delete_all_1000 - allocs/op 67750 allocs/op 67751 allocs/op 1.00
BenchmarkDocument/tree_edit_gc_100 - ns/op 3770898 ns/op 3743356 ns/op 1.01
BenchmarkDocument/tree_edit_gc_100 - B/op 4121028 B/op 4120983 B/op 1.00
BenchmarkDocument/tree_edit_gc_100 - allocs/op 14356 allocs/op 14356 allocs/op 1
BenchmarkDocument/tree_edit_gc_1000 - ns/op 312610446 ns/op 311312288 ns/op 1.00
BenchmarkDocument/tree_edit_gc_1000 - B/op 383466624 B/op 383465558 B/op 1.00
BenchmarkDocument/tree_edit_gc_1000 - allocs/op 145409 allocs/op 145406 allocs/op 1.00
BenchmarkDocument/tree_split_gc_100 - ns/op 2564814 ns/op 2564917 ns/op 1.00
BenchmarkDocument/tree_split_gc_100 - B/op 2386864 B/op 2386900 B/op 1.00
BenchmarkDocument/tree_split_gc_100 - allocs/op 10341 allocs/op 10341 allocs/op 1
BenchmarkDocument/tree_split_gc_1000 - ns/op 187280111 ns/op 194397785 ns/op 0.96
BenchmarkDocument/tree_split_gc_1000 - B/op 221990834 B/op 221990026 B/op 1.00
BenchmarkDocument/tree_split_gc_1000 - allocs/op 112253 allocs/op 112248 allocs/op 1.00
BenchmarkRPC/client_to_server - ns/op 354427400 ns/op 364396329 ns/op 0.97
BenchmarkRPC/client_to_server - B/op 17481037 B/op 18067424 B/op 0.97
BenchmarkRPC/client_to_server - allocs/op 166836 allocs/op 165863 allocs/op 1.01
BenchmarkRPC/client_to_client_via_server - ns/op 604992964 ns/op 625660123 ns/op 0.97
BenchmarkRPC/client_to_client_via_server - B/op 33305984 B/op 32815272 B/op 1.01
BenchmarkRPC/client_to_client_via_server - allocs/op 312502 allocs/op 310207 allocs/op 1.01
BenchmarkRPC/attach_large_document - ns/op 1340732837 ns/op 1355198048 ns/op 0.99
BenchmarkRPC/attach_large_document - B/op 1879529896 B/op 1868233624 B/op 1.01
BenchmarkRPC/attach_large_document - allocs/op 7547 allocs/op 7466 allocs/op 1.01
BenchmarkRPC/adminCli_to_server - ns/op 534419731 ns/op 549909416 ns/op 0.97
BenchmarkRPC/adminCli_to_server - B/op 35977076 B/op 36780108 B/op 0.98
BenchmarkRPC/adminCli_to_server - allocs/op 289645 allocs/op 288662 allocs/op 1.00
BenchmarkLocker - ns/op 63.94 ns/op 65.86 ns/op 0.97
BenchmarkLocker - B/op 16 B/op 16 B/op 1
BenchmarkLocker - allocs/op 1 allocs/op 1 allocs/op 1
BenchmarkLockerParallel - ns/op 38.38 ns/op 38.59 ns/op 0.99
BenchmarkLockerParallel - B/op 0 B/op 0 B/op NaN
BenchmarkLockerParallel - allocs/op 0 allocs/op 0 allocs/op NaN
BenchmarkLockerMoreKeys - ns/op 148.1 ns/op 148.9 ns/op 0.99
BenchmarkLockerMoreKeys - B/op 15 B/op 15 B/op 1
BenchmarkLockerMoreKeys - allocs/op 0 allocs/op 0 allocs/op NaN
BenchmarkChange/Push_10_Changes - ns/op 3771466 ns/op 3838436 ns/op 0.98
BenchmarkChange/Push_10_Changes - B/op 126242 B/op 125677 B/op 1.00
BenchmarkChange/Push_10_Changes - allocs/op 1248 allocs/op 1254 allocs/op 1.00
BenchmarkChange/Push_100_Changes - ns/op 14032620 ns/op 14440415 ns/op 0.97
BenchmarkChange/Push_100_Changes - B/op 639311 B/op 647975 B/op 0.99
BenchmarkChange/Push_100_Changes - allocs/op 6531 allocs/op 6540 allocs/op 1.00
BenchmarkChange/Push_1000_Changes - ns/op 112808578 ns/op 115497921 ns/op 0.98
BenchmarkChange/Push_1000_Changes - B/op 6149759 B/op 6151829 B/op 1.00
BenchmarkChange/Push_1000_Changes - allocs/op 62157 allocs/op 62159 allocs/op 1.00
BenchmarkChange/Pull_10_Changes - ns/op 2836368 ns/op 2909758 ns/op 0.97
BenchmarkChange/Pull_10_Changes - B/op 100799 B/op 100076 B/op 1.01
BenchmarkChange/Pull_10_Changes - allocs/op 951 allocs/op 952 allocs/op 1.00
BenchmarkChange/Pull_100_Changes - ns/op 4270242 ns/op 4414877 ns/op 0.97
BenchmarkChange/Pull_100_Changes - B/op 258169 B/op 256121 B/op 1.01
BenchmarkChange/Pull_100_Changes - allocs/op 3154 allocs/op 3154 allocs/op 1
BenchmarkChange/Pull_1000_Changes - ns/op 8131854 ns/op 8566513 ns/op 0.95
BenchmarkChange/Pull_1000_Changes - B/op 1395249 B/op 1392837 B/op 1.00
BenchmarkChange/Pull_1000_Changes - allocs/op 26874 allocs/op 26865 allocs/op 1.00
BenchmarkSnapshot/Push_3KB_snapshot - ns/op 16652817 ns/op 17384863 ns/op 0.96
BenchmarkSnapshot/Push_3KB_snapshot - B/op 812659 B/op 802328 B/op 1.01
BenchmarkSnapshot/Push_3KB_snapshot - allocs/op 6544 allocs/op 6549 allocs/op 1.00
BenchmarkSnapshot/Push_30KB_snapshot - ns/op 117200153 ns/op 118971166 ns/op 0.99
BenchmarkSnapshot/Push_30KB_snapshot - B/op 6140972 B/op 6156721 B/op 1.00
BenchmarkSnapshot/Push_30KB_snapshot - allocs/op 62162 allocs/op 62158 allocs/op 1.00
BenchmarkSnapshot/Pull_3KB_snapshot - ns/op 6449074 ns/op 6602183 ns/op 0.98
BenchmarkSnapshot/Pull_3KB_snapshot - B/op 905319 B/op 902235 B/op 1.00
BenchmarkSnapshot/Pull_3KB_snapshot - allocs/op 14876 allocs/op 14878 allocs/op 1.00
BenchmarkSnapshot/Pull_30KB_snapshot - ns/op 14565850 ns/op 14754933 ns/op 0.99
BenchmarkSnapshot/Pull_30KB_snapshot - B/op 6979163 B/op 6981411 B/op 1.00
BenchmarkSnapshot/Pull_30KB_snapshot - allocs/op 144140 allocs/op 144131 allocs/op 1.00
BenchmarkSync/memory_sync_10_test - ns/op 6773 ns/op 6984 ns/op 0.97
BenchmarkSync/memory_sync_10_test - B/op 1286 B/op 1286 B/op 1
BenchmarkSync/memory_sync_10_test - allocs/op 38 allocs/op 38 allocs/op 1
BenchmarkSync/memory_sync_100_test - ns/op 51222 ns/op 51752 ns/op 0.99
BenchmarkSync/memory_sync_100_test - B/op 8654 B/op 8655 B/op 1.00
BenchmarkSync/memory_sync_100_test - allocs/op 274 allocs/op 274 allocs/op 1
BenchmarkSync/memory_sync_1000_test - ns/op 583329 ns/op 593943 ns/op 0.98
BenchmarkSync/memory_sync_1000_test - B/op 74788 B/op 74410 B/op 1.01
BenchmarkSync/memory_sync_1000_test - allocs/op 2133 allocs/op 2113 allocs/op 1.01
BenchmarkSync/memory_sync_10000_test - ns/op 7105816 ns/op 7699568 ns/op 0.92
BenchmarkSync/memory_sync_10000_test - B/op 755359 B/op 761656 B/op 0.99
BenchmarkSync/memory_sync_10000_test - allocs/op 20443 allocs/op 20495 allocs/op 1.00
BenchmarkTextEditing - ns/op 18761780035 ns/op 19296651559 ns/op 0.97
BenchmarkTextEditing - B/op 9042031432 B/op 9041594456 B/op 1.00
BenchmarkTextEditing - allocs/op 19922814 allocs/op 19920775 allocs/op 1.00

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.