Skip to content

Commit 3f741ba

Browse files
committed
Refactor
1 parent 1d7c8b4 commit 3f741ba

8 files changed

Lines changed: 85 additions & 78 deletions

File tree

secretary/btree_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
func TestBTreeSerialization(t *testing.T) {
12-
s, originalTree := dummySecretary(t, "TestBTreeSerialization", 10)
12+
s, originalTree := dummySecretary(t, 10)
1313

1414
serializedData, err := binstruct.Serialize(originalTree)
1515
if err != nil {
@@ -71,7 +71,7 @@ func TestBtreeInvalid(t *testing.T) {
7171
}
7272

7373
func TestBtreeSaveReadHeader(t *testing.T) {
74-
s, tree := dummySecretary(t, "TestBtreeSaveReadHeader", 10)
74+
s, tree := dummySecretary(t, 10)
7575

7676
err := tree.SaveHeader()
7777
if err != nil {
@@ -98,7 +98,7 @@ func TestBtreeSaveReadHeader(t *testing.T) {
9898
}
9999

100100
func TestBTreeHeight(t *testing.T) {
101-
s, tree := dummySecretary(t, "TestBTreeHeight", 4)
101+
s, tree := dummySecretary(t, 4)
102102

103103
var keySeq uint64 = 0
104104

@@ -129,7 +129,7 @@ func TestBTreeHeight(t *testing.T) {
129129
}
130130

131131
func TestBTreeClose(t *testing.T) {
132-
s, _ := dummySecretary(t, "TestBTreeClose", 4)
132+
s, _ := dummySecretary(t, 4)
133133

134134
err := s.PagerShutdown()
135135
if err != nil {

secretary/example/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func main() {
3535
utils.Log(userErr, imagesErr)
3636
}
3737

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

4141
for _, r := range sortedRecords {

secretary/helper.go

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

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

15-
for r := 0; r < 64; r++ {
15+
for r := 0; r < numkeys; r++ {
1616
key := []byte(utils.GenerateSeqString(&keySeq, 16, 5))
1717
sortedKeys = append(sortedKeys, key)
1818

secretary/node.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -358,14 +358,20 @@ func (tree *BTree) SortedRecordSet(sortedRecords []*Record) error {
358358
return ErrorRecordsNotSorted
359359
}
360360

361-
leafNodes := tree.buildSortedLeafNodes(sortedRecords)
361+
o := len(sortedRecords) % (int(tree.Order-1) * int(tree.Order-1))
362+
leafNodes := tree.buildSortedLeafNodes(sortedRecords[:len(sortedRecords)-o])
362363

363364
tree.root = tree.buildInternalNodes(leafNodes)
364365

365-
lastIndex := len(leafNodes) - 1
366-
if lastIndex > 0 {
367-
tree.handleUnderflow(leafNodes[lastIndex])
368-
}
366+
// for _, r := range sortedRecords[len(sortedRecords)-o:] {
367+
// err := tree.Set(r.Key, r.Value)
368+
// if err != nil {
369+
// log.Fatal(err, r.Key)
370+
// }
371+
// }
372+
373+
// leafNodes := tree.buildSortedLeafNodes(sortedRecords)
374+
// tree.root = tree.buildInternalNodes(leafNodes)
369375

370376
return nil
371377
}

secretary/node_test.go

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ import (
1010
"github.com/codeharik/secretary/utils/binstruct"
1111
)
1212

13-
func dummySecretary(t *testing.T, collectionName string, order uint8) (*Secretary, *BTree) {
13+
func dummySecretary(t *testing.T, order uint8) (*Secretary, *BTree) {
1414
s, serr := New()
15+
1516
tree, err := s.NewBTree(
16-
collectionName,
17+
utils.GenerateRandomString(16),
1718
order,
1819
32,
1920
1024,
@@ -35,7 +36,7 @@ func dummySecretary(t *testing.T, collectionName string, order uint8) (*Secretar
3536
}
3637

3738
func TestNodeSaveRoot(t *testing.T) {
38-
s, tree := dummySecretary(t, "TestNodeSaveRoot", 10)
39+
s, tree := dummySecretary(t, 10)
3940

4041
root := Node{
4142
ParentIndex: 101,
@@ -237,7 +238,7 @@ func TestNodeGetLeafNode(t *testing.T) {
237238
}
238239

239240
func TestNodeSet(t *testing.T) {
240-
s, tree := dummySecretary(t, "TestNodeSet", 10)
241+
s, tree := dummySecretary(t, 10)
241242

242243
r, err := tree.Get([]byte(utils.GenerateRandomString(16)))
243244
if err == nil || r != nil {
@@ -277,7 +278,7 @@ func TestNodeSet(t *testing.T) {
277278
}
278279

279280
func TestNodeUpdate(t *testing.T) {
280-
s, tree := dummySecretary(t, "TestNodeUpdate", 10)
281+
s, tree := dummySecretary(t, 10)
281282

282283
key := []byte(utils.GenerateRandomString(16))
283284
value := []byte("Hello world!")
@@ -382,9 +383,9 @@ func TestNodeRangeScan(t *testing.T) {
382383

383384
// func TestNodeSortedRecordSet(t *testing.T) {
384385
// // {
385-
// // _, sortedRecords := SampleSortedKeyRecords()
386+
// // _, sortedRecords := SampleSortedKeyRecords(64)
386387

387-
// // s, tree := dummySecretary(t, "TestNodeDelete", 8)
388+
// // s, tree := dummySecretary(t, 8)
388389

389390
// // tree.SortedRecordSet(sortedRecords)
390391

@@ -396,55 +397,40 @@ func TestNodeRangeScan(t *testing.T) {
396397
// // }
397398

398399
// {
399-
// var keySeq uint64 = 0
400-
401400
// numKeys := make([]int32, 10)
402401
// for i := range numKeys {
403402
// numKeys[i] = 10 + rand.Int32N(500)
404403
// }
405404

406405
// for _, numKey := range numKeys {
407-
// var sortedRecords []*Record
408-
// var sortedValues []string
409-
// for r := int32(0); r < numKey; r++ {
410-
// sortedRecords = append(sortedRecords, &Record{
411-
// Key: []byte(utils.GenerateSeqString(&keySeq, 16, 5)),
412-
// Value: []byte(fmt.Sprint(r)),
413-
// })
414-
415-
// sortedValues = append(sortedValues, fmt.Sprint(r))
416-
// }
406+
// _, sortedRecords := SampleSortedKeyRecords(int(numKey))
417407

418-
// s, tree := dummySecretary(t, "TestNodeSortedRecordSet", 8)
408+
// _, tree := dummySecretary(t, 8)
419409

420410
// err := tree.SortedRecordSet(sortedRecords)
421411
// if err != nil {
422412
// t.Fatal(err)
423413
// }
424414

425-
// if errs := tree.TreeVerify(); len(errs) != 0 {
426-
// t.Fatal(errs)
427-
// }
428-
429-
// startKey := sortedRecords[0].Key
430-
// endKey := sortedRecords[len(sortedRecords)-1].Key
415+
// // if errs := tree.TreeVerify(); len(errs) != 0 {
416+
// // t.Fatal(errs)
417+
// // }
431418

432-
// rangeScan := tree.RangeScan([]byte(startKey), []byte(endKey))
433-
// if len(sortedValues) != len(rangeScan) {
434-
// t.Fatal("Range should be equal", len(sortedValues), len(rangeScan))
435-
// }
419+
// // startKey := sortedRecords[0].Key
420+
// // endKey := sortedRecords[len(sortedRecords)-1].Key
436421

437-
// for i, s := range sortedRecords {
438-
// if string(rangeScan[i].Value) != string(s.Value) || string(rangeScan[i].Key) != string(s.Key) {
439-
// t.Fatal("Record should be equal")
440-
// }
441-
// }
422+
// // rangeScan := tree.RangeScan([]byte(startKey), []byte(endKey))
423+
// // if len(sortedValues) != len(rangeScan) {
424+
// // t.Fatal("Range should be equal", len(sortedValues), len(rangeScan))
425+
// // }
442426

443-
// // if errs := tree.TreeVerify(); len(errs) != 0 {
444-
// // utils.Log(t, errs)
427+
// // for i, s := range sortedRecords {
428+
// // if string(rangeScan[i].Value) != string(s.Value) || string(rangeScan[i].Key) != string(s.Key) {
429+
// // t.Fatal("Record should be equal")
430+
// // }
445431
// // }
446432

447-
// s.PagerShutdown()
433+
// // s.PagerShutdown()
448434
// }
449435
// }
450436
// }
@@ -510,7 +496,7 @@ func TestNodeRangeScan(t *testing.T) {
510496
// }
511497

512498
func TestNodeSplitInternal(t *testing.T) {
513-
s, tree := dummySecretary(t, "TestNodeSplitInternal", 4)
499+
s, tree := dummySecretary(t, 4)
514500

515501
var keySeq uint64 = 0
516502
var sortedRecords []*Record

secretary/pager.go

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"fmt"
55
"math"
66
"os"
7-
8-
"github.com/dgraph-io/ristretto/v2"
97
)
108

119
/*
@@ -108,21 +106,26 @@ func NewPager[T PageItem[T]](tree *BTree, fileType string, level uint8) (*Pager[
108106
dirtyPages: map[int64]bool{},
109107
}
110108

111-
// Initialize Ristretto Cache
112-
cache, err := ristretto.NewCache(
113-
&ristretto.Config[int64, *Page[T]]{
114-
NumCounters: 10000, // Track frequency of ~10,000 items
115-
MaxCost: 1 << 24, // 16MB total cache size
116-
BufferItems: 64, // Batch writes for performance
117-
OnEvict: func(item *ristretto.Item[*Page[T]]) {
118-
delete(pager.dirtyPages, item.Value.Index) // Mark page as clean
119-
},
120-
})
121-
if err != nil {
122-
return nil, err
123-
}
109+
//-------
110+
//-------
111+
// // Initialize Ristretto Cache
112+
// cache, err := ristretto.NewCache(
113+
// &ristretto.Config[int64, *Page[T]]{
114+
// NumCounters: 10000, // Track frequency of ~10,000 items
115+
// MaxCost: 1 << 24, // 16MB total cache size
116+
// BufferItems: 64, // Batch writes for performance
117+
// OnEvict: func(item *ristretto.Item[*Page[T]]) {
118+
// delete(pager.dirtyPages, item.Value.Index) // Mark page as clean
119+
// },
120+
// })
121+
// if err != nil {
122+
// return nil, err
123+
// }
124124

125-
pager.cache = cache
125+
// pager.cache = cache
126+
127+
//-------
128+
//-------
126129

127130
return pager, nil
128131
}
@@ -242,9 +245,15 @@ func (store *Pager[T]) ReadPage(index int64) (*Page[T], error) {
242245
store.mu.Lock()
243246

244247
// Check if page exists in Ristretto cache
245-
if cachedPage, found := store.cache.Get(index); found {
246-
return cachedPage, nil
247-
}
248+
249+
// --------
250+
// --------
251+
// if cachedPage, found := store.cache.Get(index); found {
252+
// return cachedPage, nil
253+
// }
254+
// --------
255+
// --------
256+
// --------
248257

249258
var item T
250259
page := item.NewPage(index) // Calls the NewPage method of PageItem[T]
@@ -253,8 +262,12 @@ func (store *Pager[T]) ReadPage(index int64) (*Page[T], error) {
253262
// Index: index,
254263
// }
255264
// Store in Ristretto cache
256-
store.cache.Set(index, page, store.itemSize)
257-
store.cache.Wait()
265+
// --------
266+
// --------
267+
// store.cache.Set(index, page, store.itemSize)
268+
// store.cache.Wait()
269+
// --------
270+
// --------
258271

259272
store.mu.Unlock()
260273

@@ -333,7 +346,11 @@ func (store *Pager[T]) Close() error {
333346
return err
334347
}
335348

336-
store.cache.Close()
349+
//--------
350+
//--------
351+
// store.cache.Close()
352+
//--------
353+
//--------
337354

338355
return store.file.Close()
339356
}

secretary/pager_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
)
77

88
func TestPagerAllocateBatch(t *testing.T) {
9-
s, tree := dummySecretary(t, "TestPagerAllocateBatch", 10)
9+
s, tree := dummySecretary(t, 10)
1010

1111
fileInfo, err := tree.nodePager.file.Stat()
1212
if err != nil {
@@ -46,7 +46,7 @@ func TestPagerAllocateBatch(t *testing.T) {
4646
}
4747

4848
func TestPagerWriteAndReadAtOffset(t *testing.T) {
49-
s, tree := dummySecretary(t, "TestPagerWriteAndReadAtOffset", 10)
49+
s, tree := dummySecretary(t, 10)
5050

5151
{ // Test: Write small data within the first batch
5252
data := []byte("Hello, B+ Tree!")

secretary/types.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import (
55
"net/http"
66
"os"
77
"sync"
8-
9-
"github.com/dgraph-io/ristretto/v2"
108
)
119

1210
const (
@@ -150,7 +148,7 @@ type Pager[T PageItem[T]] struct {
150148
headerSize int64
151149
itemSize int64 // Maximum batch size = 4GB
152150

153-
cache *ristretto.Cache[int64, *Page[T]] // In-memory cache
151+
// cache *ristretto.Cache[int64, *Page[T]] // In-memory cache
154152
dirtyPages map[int64]bool
155153

156154
mu sync.Mutex

0 commit comments

Comments
 (0)