Skip to content

Commit d090720

Browse files
committed
Refactor
1 parent e63b959 commit d090720

19 files changed

Lines changed: 287 additions & 248 deletions

File tree

secretary/btree.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,4 +318,5 @@ func (tree *BTree) Erase() {
318318
tree.root = nil
319319
tree.NodeSeq = 0
320320
tree.NumNodeSeq = 0
321+
tree.KeySeq = 0
321322
}

secretary/btree_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func TestBTreeHeight(t *testing.T) {
108108
// Set more keys to increase height
109109
for i := 0; i < 9; i++ {
110110
key := []byte(utils.GenerateSeqRandomString(&keySeq, 16, 5, 4))
111-
err := tree.Set(key, key)
111+
err := tree.SetKV(key, key)
112112
if err != nil {
113113
t.Fatalf("Set failed: %s", err)
114114
}
@@ -119,7 +119,7 @@ func TestBTreeHeight(t *testing.T) {
119119
}
120120

121121
key := []byte(utils.GenerateSeqRandomString(&keySeq, 16, 5, 4))
122-
err := tree.Set(key, key)
122+
err := tree.SetKV(key, key)
123123
if err != nil {
124124
t.Fatalf("Set failed: %s", err)
125125
}

secretary/example/main.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,17 @@ func main() {
3535
utils.Log(userErr, imagesErr)
3636
}
3737

38-
_, sortedRecords := secretary.SampleSortedKeyRecords(64)
38+
sortedRecords := secretary.SampleSortedKeyRecords(64)
3939
images.SortedRecordSet(sortedRecords)
4040

4141
for _, r := range sortedRecords {
42-
users.Set(r.Key, r.Value)
42+
users.SetKV(r.Key, r.Value)
4343
}
4444

45-
users.Set([]byte("0000000000000196"), []byte("Hello:196"))
46-
users.Set([]byte("0000000000000197"), []byte("Hello:197"))
47-
users.Set([]byte("0000000000000198"), []byte("Hello:198"))
48-
users.Set([]byte("0000000000000199"), []byte("Hello:199"))
45+
users.SetKV([]byte("0000000000000196"), []byte("Hello:196"))
46+
users.SetKV([]byte("0000000000000197"), []byte("Hello:197"))
47+
users.SetKV([]byte("0000000000000198"), []byte("Hello:198"))
48+
users.SetKV([]byte("0000000000000199"), []byte("Hello:199"))
4949

5050
s.Serve()
5151
}

secretary/go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ require (
1111
)
1212

1313
require (
14+
github.com/cespare/xxhash/v2 v2.3.0 // indirect
15+
github.com/dgraph-io/ristretto/v2 v2.1.0 // indirect
16+
github.com/dustin/go-humanize v1.0.1 // indirect
17+
github.com/pkg/errors v0.9.1 // indirect
1418
github.com/stretchr/testify v1.10.0 // indirect
1519
golang.org/x/sys v0.30.0 // indirect
1620
golang.org/x/text v0.14.0 // indirect

secretary/go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@ buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.5-2025030720450
22
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.5-20250307204501-0409229c3780.1/go.mod h1:eOqrCVUfhh7SLo00urDe/XhJHljj0dWMZirS0aX7cmc=
33
connectrpc.com/connect v1.18.1 h1:PAg7CjSAGvscaf6YZKUefjoih5Z/qYkyaTrBW8xvYPw=
44
connectrpc.com/connect v1.18.1/go.mod h1:0292hj1rnx8oFrStN7cB4jjVBeqs+Yx5yDIC2prWDO8=
5+
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
6+
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
57
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
68
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
9+
github.com/dgraph-io/ristretto/v2 v2.1.0 h1:59LjpOJLNDULHh8MC4UaegN52lC4JnO2dITsie/Pa8I=
10+
github.com/dgraph-io/ristretto/v2 v2.1.0/go.mod h1:uejeqfYXpUomfse0+lO+13ATz4TypQYLJZzBSAemuB4=
11+
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
12+
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
713
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
814
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
15+
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
16+
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
917
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1018
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1119
github.com/prashantv/gostub v1.1.0 h1:BTyx3RfQjRHnUWaGF9oQos79AlQ5k8WNktv7VGvVH4g=

secretary/helper.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@ import (
66
"github.com/codeharik/secretary/utils"
77
)
88

9-
func SampleSortedKeyRecords(numkeys int) (keys [][]byte, records []*Record) {
9+
func SampleSortedKeyRecords(numkeys int) (records []*Record) {
1010
var keySeq uint64 = 0
1111
var sortedRecords []*Record
12-
var sortedKeys [][]byte
1312
var sortedValues []string
1413

1514
for r := 0; r < numkeys; r++ {
16-
key := []byte(utils.GenerateSeqString(&keySeq, 16, 5))
17-
sortedKeys = append(sortedKeys, key)
15+
key := []byte(utils.GenerateSeqString(&keySeq, KEY_SIZE, KEY_INCREMENT))
1816

1917
sortedRecords = append(sortedRecords, &Record{
2018
Key: key,
@@ -23,5 +21,5 @@ func SampleSortedKeyRecords(numkeys int) (keys [][]byte, records []*Record) {
2321

2422
sortedValues = append(sortedValues, fmt.Sprint(r))
2523
}
26-
return sortedKeys, sortedRecords
24+
return sortedRecords
2725
}

secretary/node.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package secretary
22

33
import (
44
"bytes"
5+
"encoding/binary"
56
"fmt"
67
"sort"
78
"sync/atomic"
@@ -296,13 +297,22 @@ func (tree *BTree) splitInternal(node *Node) {
296297
tree.promoteKey(node, promotedKey, newRightInternal)
297298
}
298299

299-
// Set a Record key-value pair into the B+ Tree
300-
func (tree *BTree) Set(key []byte, value []byte) error {
300+
func (tree *BTree) Set(value []byte) error {
301+
buf := make([]byte, 16)
302+
binary.BigEndian.PutUint64(buf[8:], tree.KeySeq)
303+
return tree.SetKV(buf, value)
304+
}
305+
306+
// SetKV a Record key-value pair into the B+ Tree
307+
func (tree *BTree) SetKV(key []byte, value []byte) error {
301308
if len(key) != KEY_SIZE {
302309
return ErrorInvalidKey
303310
}
304311

305312
if tree.root == nil {
313+
314+
atomic.AddUint64(&tree.KeySeq, KEY_INCREMENT)
315+
306316
tree.root = tree.createLeafNode()
307317
tree.root.setLeafKV(key, value)
308318

@@ -314,6 +324,8 @@ func (tree *BTree) Set(key []byte, value []byte) error {
314324
return ErrorDuplicateKey
315325
}
316326

327+
atomic.AddUint64(&tree.KeySeq, KEY_INCREMENT)
328+
317329
leaf.setLeafKV(key, value)
318330

319331
if len(leaf.Keys) >= int(tree.Order) {
@@ -364,7 +376,7 @@ func equiDivision(len int, max int) []int {
364376
ends[i]++
365377
}
366378
}
367-
// fmt.Println("ends", ends)
379+
ServerLog("ends", ends)
368380
return ends
369381
}
370382

@@ -377,13 +389,15 @@ func (tree *BTree) SortedRecordSet(sortedRecords []*Record) error {
377389
leafNodes := tree.buildSortedLeafNodes(sortedRecords)
378390
tree.root = tree.buildInternalNodes(leafNodes)
379391

392+
atomic.AddUint64(&tree.KeySeq, KEY_INCREMENT*uint64(len(sortedRecords)))
393+
380394
return nil
381395
}
382396

383397
func (tree *BTree) buildSortedLeafNodes(sortedRecords []*Record) []*Node {
384398
leafNodes := []*Node{}
385399

386-
// fmt.Println("---", len(sortedRecords))
400+
ServerLog("---", len(sortedRecords))
387401
ends := equiDivision(len(sortedRecords), int(tree.Order-1))
388402

389403
end := 0

0 commit comments

Comments
 (0)