Skip to content

Commit 655baf1

Browse files
committed
btree: remove dependency on github.com/satori/go.uuid
The only call was uuid.NewV4().Bytes(), which really means "get me 16 random bytes". uuid.NewV4 changed at some point to return an error in addition to the UUID, which made this code no longer compile. There's no need to depend on a uuid package to get 16 random bytes. Do what the old package did, namely read from crypto/rand.Reader and expect it to succeed (or else panic), but without the dependency. Fixes #187. Fixes #188.
1 parent 01f52d4 commit 655baf1

File tree

4 files changed

+21
-17
lines changed

4 files changed

+21
-17
lines changed

btree/immutable/node.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,22 @@ limitations under the License.
1919
package btree
2020

2121
import (
22+
"crypto/rand"
23+
"io"
2224
"sort"
23-
24-
"github.com/satori/go.uuid"
2525
)
2626

27+
func newID() []byte {
28+
id := make([]byte, 16)
29+
_, err := io.ReadFull(rand.Reader, id)
30+
if err != nil {
31+
// This can't happen unless the system is badly
32+
// configured, like /dev/urandom isn't readable.
33+
panic("reading random: " + err.Error())
34+
}
35+
return id
36+
}
37+
2738
// ID exists because i'm tired of writing []byte
2839
type ID []byte
2940

@@ -120,7 +131,7 @@ func (n *Node) copy() *Node {
120131
copy(cpKeys, n.ChildKeys)
121132

122133
return &Node{
123-
ID: uuid.NewV4().Bytes(),
134+
ID: newID(),
124135
IsLeaf: n.IsLeaf,
125136
ChildValues: cpValues,
126137
ChildKeys: cpKeys,
@@ -297,7 +308,7 @@ func (n *Node) deleteKeyAt(i int) {
297308
func (n *Node) splitLeafAt(i int) (interface{}, *Node) {
298309
left := newNode()
299310
left.IsLeaf = n.IsLeaf
300-
left.ID = uuid.NewV4().Bytes()
311+
left.ID = newID()
301312

302313
value := n.ChildValues[i]
303314
leftValues := make([]interface{}, i+1)
@@ -320,7 +331,7 @@ func (n *Node) splitLeafAt(i int) (interface{}, *Node) {
320331
func (n *Node) splitInternalAt(i int) (interface{}, *Node) {
321332
left := newNode()
322333
left.IsLeaf = n.IsLeaf
323-
left.ID = uuid.NewV4().Bytes()
334+
left.ID = newID()
324335
value := n.ChildValues[i]
325336
leftValues := make([]interface{}, i)
326337
copy(leftValues, n.ChildValues[:i])
@@ -421,7 +432,7 @@ func nodeFromBytes(t *Tr, data []byte) (*Node, error) {
421432
// IsLeaf is false by default.
422433
func newNode() *Node {
423434
return &Node{
424-
ID: uuid.NewV4().Bytes(),
435+
ID: newID(),
425436
}
426437
}
427438

btree/immutable/rt.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@ limitations under the License.
1818

1919
package btree
2020

21-
import (
22-
"sync"
23-
24-
"github.com/satori/go.uuid"
25-
)
21+
import "sync"
2622

2723
// context is used to keep track of the nodes in this mutable
2824
// that have been created. This is basically any node that had
@@ -149,7 +145,7 @@ func (t *Tr) Len() int {
149145
func (t *Tr) AsMutable() MutableTree {
150146
return &Tr{
151147
Count: t.Count,
152-
UUID: uuid.NewV4().Bytes(),
148+
UUID: newID(),
153149
Root: t.Root,
154150
config: t.config,
155151
cacher: t.cacher,
@@ -197,7 +193,7 @@ func treeFromBytes(p Persister, data []byte, comparator Comparator) (*Tr, error)
197193
func newTree(cfg Config) *Tr {
198194
return &Tr{
199195
config: cfg,
200-
UUID: uuid.NewV4().Bytes(),
196+
UUID: newID(),
201197
cacher: newCacher(cfg.Persister),
202198
}
203199
}

btree/immutable/rt_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"testing"
2525
"time"
2626

27-
"github.com/satori/go.uuid"
2827
"github.com/stretchr/testify/assert"
2928
"github.com/stretchr/testify/require"
3029
)
@@ -220,7 +219,7 @@ func generateRandomQuery() (interface{}, interface{}) {
220219
func newItem(value interface{}) *Item {
221220
return &Item{
222221
Value: value,
223-
Payload: uuid.NewV4().Bytes(),
222+
Payload: newID(),
224223
}
225224
}
226225

glide.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package: github.com/Workiva/go-datastructures
22
import:
3-
- package: github.com/satori/go.uuid
4-
version: ^1.1.0
53
- package: github.com/stretchr/testify
64
version: ^1.1.4
75
subpackages:

0 commit comments

Comments
 (0)