Skip to content

Commit

Permalink
refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
chirst committed Jun 28, 2024
1 parent a487c34 commit e3a934b
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 131 deletions.
24 changes: 11 additions & 13 deletions kv/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"github.com/chirst/cdb/pager"
)

var errorReservedPage = errors.New("specified a reserved page number")

type KV struct {
pager *pager.Pager
catalog *catalog
Expand Down Expand Up @@ -42,8 +40,8 @@ func (kv *KV) GetCatalog() *catalog {
// the key was found. The pageNumber has to do with the root page of the
// corresponding table. The system catalog uses the page number 1.
func (kv *KV) Get(pageNumber int, key []byte) ([]byte, bool, error) {
if pageNumber == pager.EMPTY_PARENT_PAGE_NUMBER {
return nil, false, errorReservedPage
if pageNumber == 0 {
panic("pageNumber cannot be 0")
}
// Step 1. Need a source page to start from. Will start from 1 if there is
// no source page specified. This source page has to do with a table. 1 has
Expand All @@ -52,7 +50,7 @@ func (kv *KV) Get(pageNumber int, key []byte) ([]byte, bool, error) {
page := kv.pager.GetPage(pageNumber)
// Step 2. Decide whether the page is an internal node or a leaf node.
//This can be determined by asking what the page type is.
if page.GetType() == pager.PAGE_TYPE_LEAF {
if page.IsLeaf() {
// 4. Find the value for the key and return.
b1, b2 := page.GetValue(key)
return b1, b2, nil
Expand All @@ -72,8 +70,8 @@ func (kv *KV) Get(pageNumber int, key []byte) ([]byte, bool, error) {
// page number 1.
func (kv *KV) Set(pageNumber int, key, value []byte) error {
// TODO set doesn't differentiate between insert and update
if pageNumber == pager.EMPTY_PARENT_PAGE_NUMBER {
return errorReservedPage
if pageNumber == 0 {
panic("pageNumber cannot be 0")
}
leafPage := kv.getLeafPage(pageNumber, key)
if leafPage.CanInsertTuple(key, value) {
Expand All @@ -88,7 +86,7 @@ func (kv *KV) Set(pageNumber int, key, value []byte) error {
kv.parentInsert(parentPage, leftPage, rightPage)
return nil
}
leafPage.SetType(pager.PAGE_TYPE_INTERNAL)
leafPage.SetTypeInternal()
leafPage.SetEntries([]pager.PageTuple{
{
Key: leftPage.GetEntries()[0].Key,
Expand Down Expand Up @@ -123,7 +121,7 @@ func insertIntoOne(key, value []byte, lp, rp *pager.Page) {

func (kv *KV) getLeafPage(nextPageNumber int, key []byte) *pager.Page {
p := kv.pager.GetPage(nextPageNumber)
for p.GetType() != pager.PAGE_TYPE_LEAF {
for !p.IsLeaf() {
nextPage, found := p.GetValue(key)
if !found {
return nil
Expand Down Expand Up @@ -196,7 +194,7 @@ func (kv *KV) parentInsert(p, l, r *pager.Page) {
// The root node needs to be split. It is wise to keep the root node the
// same page number so the table catalog doesn't need to be updated every
// time a root node splits.
p.SetType(pager.PAGE_TYPE_INTERNAL)
p.SetTypeInternal()
p.SetEntries([]pager.PageTuple{
{
Key: leftPage.GetEntries()[0].Key,
Expand Down Expand Up @@ -241,7 +239,7 @@ func (kv *KV) NewRowID(rootPageNumber int) (int, error) {
if len(candidate.GetEntries()) == 0 {
return 1, nil
}
for candidate.GetType() != pager.PAGE_TYPE_LEAF {
for !candidate.IsLeaf() {
pagePointers := candidate.GetEntries()
descendingPageNum := pagePointers[len(pagePointers)-1].Value
descendingPageNum16 := binary.LittleEndian.Uint16(descendingPageNum)
Expand Down Expand Up @@ -311,7 +309,7 @@ func (c *Cursor) GotoFirstRecord() bool {
if len(candidatePage.GetEntries()) == 0 {
return false
}
for candidatePage.GetType() != pager.PAGE_TYPE_LEAF {
for !candidatePage.IsLeaf() {
pagePointers := candidatePage.GetEntries()
ascendingPageNum := pagePointers[0].Value
ascendingPageNum16 := binary.LittleEndian.Uint16(ascendingPageNum)
Expand All @@ -329,7 +327,7 @@ func (c *Cursor) GotoLastRecord() bool {
if len(candidatePage.GetEntries()) == 0 {
return false
}
for candidatePage.GetType() != pager.PAGE_TYPE_LEAF {
for !candidatePage.IsLeaf() {
pagePointers := candidatePage.GetEntries()
descendingPageNum := pagePointers[len(pagePointers)-1].Value
descendingPageNum16 := binary.LittleEndian.Uint16(descendingPageNum)
Expand Down
4 changes: 1 addition & 3 deletions kv/kv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package kv
import (
"bytes"
"testing"

"github.com/chirst/cdb/pager"
)

func TestKv(t *testing.T) {
Expand Down Expand Up @@ -32,7 +30,7 @@ func TestKv(t *testing.T) {
ri := 178
// For a page 4096 a split is more than guaranteed here because
// 512*8=4096 not including the header of each page.
iters := pager.PAGE_SIZE / 8
iters := 4096 / 8
for i := 1; i <= iters; i += 1 {
kv.BeginWriteTransaction()
k, err := EncodeKey(i)
Expand Down
Loading

0 comments on commit e3a934b

Please sign in to comment.