Skip to content

Commit

Permalink
Merge pull request #10959 from gyuho/adt
Browse files Browse the repository at this point in the history
pkg/adt: refactor + add more test cases
  • Loading branch information
jingyih authored Aug 1, 2019
2 parents 5ef8f27 + 6a0811a commit 456c91b
Show file tree
Hide file tree
Showing 18 changed files with 480 additions and 38 deletions.
8 changes: 4 additions & 4 deletions auth/range_perm_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ func getMergedPerms(lg *zap.Logger, tx backend.BatchTx, userName string) *unifie
return nil
}

readPerms := &adt.IntervalTree{}
writePerms := &adt.IntervalTree{}
readPerms := adt.NewIntervalTree()
writePerms := adt.NewIntervalTree()

for _, roleName := range user.Roles {
role := getRole(tx, roleName)
Expand Down Expand Up @@ -148,6 +148,6 @@ func (as *authStore) invalidateCachedPerm(userName string) {
}

type unifiedRangePermissions struct {
readPerms *adt.IntervalTree
writePerms *adt.IntervalTree
readPerms adt.IntervalTree
writePerms adt.IntervalTree
}
4 changes: 2 additions & 2 deletions auth/range_perm_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestRangePermission(t *testing.T) {
}

for i, tt := range tests {
readPerms := &adt.IntervalTree{}
readPerms := adt.NewIntervalTree()
for _, p := range tt.perms {
readPerms.Insert(p, struct{}{})
}
Expand Down Expand Up @@ -89,7 +89,7 @@ func TestKeyPermission(t *testing.T) {
}

for i, tt := range tests {
readPerms := &adt.IntervalTree{}
readPerms := adt.NewIntervalTree()
for _, p := range tt.perms {
readPerms.Insert(p, struct{}{})
}
Expand Down
2 changes: 1 addition & 1 deletion etcdserver/api/v3rpc/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func checkTxnRequest(r *pb.TxnRequest, maxTxnOps int) error {
// there is an overlap, returns an error. If no overlap, return put and delete
// sets for recursive evaluation.
func checkIntervals(reqs []*pb.RequestOp) (map[string]struct{}, adt.IntervalTree, error) {
var dels adt.IntervalTree
dels := adt.NewIntervalTree()

// collect deletes from this level; build first to check lower level overlapped puts
for _, req := range reqs {
Expand Down
1 change: 1 addition & 0 deletions mvcc/watcher_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ type watcherGroup struct {
func newWatcherGroup() watcherGroup {
return watcherGroup{
keyWatchers: make(watcherSetByKey),
ranges: adt.NewIntervalTree(),
watchers: make(watcherSet),
}
}
Expand Down
48 changes: 48 additions & 0 deletions pkg/adt/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

## Red-Black Tree

*"Introduction to Algorithms" (Cormen et al, 3rd ed.), Chapter 13*

1. Every node is either red or black.
2. The root is black.
3. Every leaf (NIL) is black.
4. If a node is red, then both its children are black.
5. For each node, all simple paths from the node to descendant leaves contain the
same number of black nodes.

For example,

```go
import (
"fmt"

"go.etcd.io/etcd/pkg/adt"
)

func main() {
ivt := adt.NewIntervalTree()
ivt.Insert(NewInt64Interval(510, 511), 0)
ivt.Insert(NewInt64Interval(82, 83), 0)
ivt.Insert(NewInt64Interval(830, 831), 0)
...
```
After inserting the values `510`, `82`, `830`, `11`, `383`, `647`, `899`, `261`, `410`, `514`, `815`, `888`, `972`, `238`, `292`, `953`.
![red-black-tree-01-insertion.png](img/red-black-tree-01-insertion.png)
Deleting the node `514` should not trigger any rebalancing:
![red-black-tree-02-delete-514.png](img/red-black-tree-02-delete-514.png)
Deleting the node `11` triggers multiple rotates for rebalancing:
![red-black-tree-03-delete-11.png](img/red-black-tree-03-delete-11.png)
![red-black-tree-04-delete-11.png](img/red-black-tree-04-delete-11.png)
![red-black-tree-05-delete-11.png](img/red-black-tree-05-delete-11.png)
![red-black-tree-06-delete-11.png](img/red-black-tree-06-delete-11.png)
![red-black-tree-07-delete-11.png](img/red-black-tree-07-delete-11.png)
![red-black-tree-08-delete-11.png](img/red-black-tree-08-delete-11.png)
![red-black-tree-09-delete-11.png](img/red-black-tree-09-delete-11.png)
Try yourself at https://www.cs.usfca.edu/~galles/visualization/RedBlack.html.
3 changes: 1 addition & 2 deletions pkg/adt/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ import (
)

func Example() {
ivt := &adt.IntervalTree{}

ivt := adt.NewIntervalTree()
ivt.Insert(adt.NewInt64Interval(1, 3), 123)
ivt.Insert(adt.NewInt64Interval(9, 13), 456)
ivt.Insert(adt.NewInt64Interval(7, 20), 789)
Expand Down
Binary file added pkg/adt/img/red-black-tree-01-insertion.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pkg/adt/img/red-black-tree-02-delete-514.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pkg/adt/img/red-black-tree-03-delete-11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pkg/adt/img/red-black-tree-04-delete-11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pkg/adt/img/red-black-tree-05-delete-11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pkg/adt/img/red-black-tree-06-delete-11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pkg/adt/img/red-black-tree-07-delete-11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pkg/adt/img/red-black-tree-08-delete-11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pkg/adt/img/red-black-tree-09-delete-11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 456c91b

Please sign in to comment.