Skip to content

Commit 392dbbe

Browse files
committed
Pager
1 parent 97ea4db commit 392dbbe

10 files changed

Lines changed: 708 additions & 59 deletions

File tree

Readme.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,29 @@
4545
* [Loro Is Local-First State With CRDT](https://www.youtube.com/watch?v=NB7HRfyufLk)
4646
* [How Yjs works from the inside out](https://www.youtube.com/watch?v=0l5XgnQ6rB4)
4747

48-
## HyperLogLog
48+
## Probablistic Data structures
4949

5050
* [PapersWeLove : HyperLogLog](https://www.youtube.com/watch?v=y3fTaxA8PkU)
5151
* [A problem so hard even Google relies on Random Chance](https://www.youtube.com/watch?v=lJYufx0bfpw)
5252
* [The Algorithm with the Best Name - HyperLogLog Explained](https://www.youtube.com/watch?v=2PlrMCiUN_s)
5353

54+
* [Count-min Sketch](https://www.youtube.com/watch?v=Okdjn7o4q8E)
55+
56+
## Cache
57+
58+
* [TinyLFU: A Highly Efficient Cache Admission Policy](https://dgraph.io/blog/refs/TinyLFU%20-%20A%20Highly%20Efficient%20Cache%20Admission%20Policy.pdf)
59+
60+
* [Caffeine Design of a Modern Cache](https://docs.google.com/presentation/d/1NlDxyXsUG1qlVHMl4vsUUBQfAJ2c2NsFPNPr2qymIBs/edit#slide=id.p)
61+
* [Design of a Modern Cache](https://highscalability.com/design-of-a-modern-cache/)
62+
* [The State of Caching in Go](https://dgraph.io/blog/post/caching-in-go/)
63+
* [Introducing Ristretto: A High-Performance Go Cache](https://dgraph.io/blog/post/introducing-ristretto-high-perf-go-cache/)
64+
* [On Window TinyLFU](https://9vx.org/post/on-window-tinylfu/)
65+
66+
* https://en.wikipedia.org/wiki/Cache_replacement_policies#LRU
67+
68+
* https://github.com/hypermodeinc/ristretto
69+
* https://github.com/dgryski/go-tinylfu
70+
5471
## LSM-Tree
5572

5673
[#04 - Database Storage: Log-Structured Merge Trees & Tuples (CMU Intro to Database Systems)](https://www.youtube.com/watch?v=IHtVWGhG0Xg&t=1372s)

go.work.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hC
77
github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
88
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
99
github.com/davecgh/go-xdr v0.0.0-20161123171359-e6a2ba005892/go.mod h1:CTDl0pzVzE5DEzZhPfvhY/9sPFMQIxaJ9VAMs9AagrE=
10+
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
1011
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
1112
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
1213
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=

secretary/btree.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,31 @@ func (tree *BTree) SaveHeader() error {
114114
return tree.indexPager.WriteAt(0, header)
115115
}
116116

117+
func (tree *BTree) readRoot() error {
118+
rootBytes, err := tree.indexPager.ReadAt(SECRETARY_HEADER_LENGTH, int32(tree.nodeSize))
119+
if err != nil {
120+
return err
121+
}
122+
123+
var root Node
124+
err = binstruct.Deserialize(rootBytes, &root)
125+
if err != nil {
126+
return err
127+
}
128+
129+
tree.root = &root
130+
return nil
131+
}
132+
133+
func (tree *BTree) saveRoot() error {
134+
rootHeader, err := binstruct.Serialize(*tree.root)
135+
if err != nil {
136+
return err
137+
}
138+
139+
return tree.indexPager.WriteAt(SECRETARY_HEADER_LENGTH, rootHeader)
140+
}
141+
117142
func (s *Secretary) NewBTreeReadHeader(collectionName string) (*BTree, error) {
118143
temp, err := s.NewBTree(collectionName,
119144
10,
@@ -126,12 +151,12 @@ func (s *Secretary) NewBTreeReadHeader(collectionName string) (*BTree, error) {
126151
return nil, err
127152
}
128153

129-
diskData, err := temp.indexPager.ReadAt(0, SECRETARY_HEADER_LENGTH)
154+
headerData, err := temp.indexPager.ReadAt(0, SECRETARY_HEADER_LENGTH)
130155
if err != nil {
131156
return nil, err
132157
}
133158

134-
data := bytes.Trim(diskData, "-")[len(SECRETARY):]
159+
data := bytes.Trim(headerData, "-")[len(SECRETARY):]
135160
var deserializedTree BTree
136161
err = binstruct.Deserialize(data, &deserializedTree)
137162
if err != nil {

secretary/go.mod

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ require (
1111
)
1212

1313
require (
14-
github.com/stretchr/testify v1.8.4 // indirect
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
18+
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
1721
)

secretary/go.sum

Lines changed: 9 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=
@@ -14,6 +22,7 @@ github.com/rs/cors v1.11.1 h1:eU3gRzXLRK57F5rKMGMZURNdIG4EoAmX8k94r9wXWHA=
1422
github.com/rs/cors v1.11.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
1523
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
1624
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
25+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
1726
go.uber.org/automaxprocs v1.6.0 h1:O3y2/QNTOdbF+e/dpXNNW7Rx2hZ4sTIPyybbxyNqTUs=
1827
go.uber.org/automaxprocs v1.6.0/go.mod h1:ifeIMSnPZuznNm6jmdzmU3/bfk01Fe2fotchwEFJ8r8=
1928
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=

secretary/node.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"sort"
66

77
"github.com/codeharik/secretary/utils"
8-
"github.com/codeharik/secretary/utils/binstruct"
98
)
109

1110
//------------------------------------------------------------------
@@ -173,15 +172,6 @@ func (tree *BTree) createLeafNode() *Node {
173172
}
174173
}
175174

176-
func (tree *BTree) saveRoot() error {
177-
rootHeader, err := binstruct.Serialize(*tree.root)
178-
if err != nil {
179-
return err
180-
}
181-
182-
return tree.indexPager.WriteAt(SECRETARY_HEADER_LENGTH, rootHeader)
183-
}
184-
185175
//------------------------------------------------------------------
186176
// Set/Update Key
187177
//------------------------------------------------------------------

secretary/node_test.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,22 @@ func dummyTree(t *testing.T, collectionName string, order uint8) (*Secretary, *B
3030
func TestSaveRoot(t *testing.T) {
3131
_, tree := dummyTree(t, "TestSaveRoot", 10)
3232

33-
tree.root = &Node{
33+
root := Node{
3434
ParentOffset: 101,
3535
NextOffset: 102,
3636
PrevOffset: 103,
3737

3838
Keys: [][]byte{{10, 21, 32, 34, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
3939
KeyOffsets: []DataLocation{2, 3, 4, 5, 6},
4040
}
41+
tree.root = &root
4142

4243
err := tree.saveRoot()
4344
if err != nil {
4445
t.Fatal(err)
4546
}
4647

47-
rootBytes, err := tree.indexPager.ReadAt(SECRETARY_HEADER_LENGTH, int32(tree.nodeSize))
48-
if err != nil {
49-
t.Fatal(err)
50-
}
51-
52-
var root Node
53-
err = binstruct.Deserialize(rootBytes, &root)
48+
err = tree.readRoot()
5449
if err != nil {
5550
t.Fatal(err)
5651
}

0 commit comments

Comments
 (0)