Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
chirst committed Jun 23, 2024
1 parent 8f6c23e commit 0bc7355
Showing 5 changed files with 74 additions and 18 deletions.
4 changes: 2 additions & 2 deletions db.go
Original file line number Diff line number Diff line change
@@ -14,8 +14,8 @@ type db struct {
vm *vm
}

func newDb() (*db, error) {
kv, err := NewKv(false)
func newDb(useMemory bool) (*db, error) {
kv, err := NewKv(useMemory)
if err != nil {
return nil, err
}
49 changes: 48 additions & 1 deletion db_test.go
Original file line number Diff line number Diff line change
@@ -5,5 +5,52 @@ import (
)

func TestExecute(t *testing.T) {
// TODO
db, err := newDb(true)
if err != nil {
t.Fatal(err)
}
createSql := "CREATE TABLE person (id INTEGER, first_name TEXT, last_name TEXT, age INTEGER)"
createRes := db.execute(createSql)
if createRes.err != nil {
t.Fatal(err.Error())
}
selectSchemaSql := "SELECT * FROM cdb_schema"
schemaRes := db.execute(selectSchemaSql)
if schemaRes.err != nil {
t.Fatal(schemaRes.err.Error())
}
schemaSelectExpectations := []string{
"1",
"table",
"person",
"person",
"2",
"{\"columns\":[{\"name\":\"id\",\"type\":\"INTEGER\"},{\"name\":\"first_name\",\"type\":\"TEXT\"},{\"name\":\"last_name\",\"type\":\"TEXT\"},{\"name\":\"age\",\"type\":\"INTEGER\"}]}",
}
for i, s := range schemaSelectExpectations {
if c := *schemaRes.resultRows[1][i]; c != s {
t.Fatalf("expected %s got %s", s, c)
}
}
insertSql := "INSERT INTO person (first_name, last_name, age) VALUES ('John', 'Smith', 50)"
insertRes := db.execute(insertSql)
if insertRes.err != nil {
t.Fatal(err.Error())
}
selectPersonSql := "SELECT * FROM person"
selectPersonRes := db.execute(selectPersonSql)
if selectPersonRes.err != nil {
t.Fatal(err.Error())
}
selectPersonExpectations := []string{
"1",
"John",
"Smith",
"50",
}
for i, s := range selectPersonExpectations {
if c := *selectPersonRes.resultRows[1][i]; c != s {
t.Fatalf("expected %s got %s", s, c)
}
}
}
12 changes: 7 additions & 5 deletions kv_test.go
Original file line number Diff line number Diff line change
@@ -30,15 +30,17 @@ 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.
for i := 1; i < 512; i += 1 {
keyValueSize := 4
iters := PAGE_SIZE / 8
for i := 1; i <= iters; i += 1 {
kv.BeginWriteTransaction()
k := EncodeKey(uint16(i))
v := []byte{1, 0, 0, 0}
if len(k) != 4 {
t.Fatal("need k to be len 4")
if len(k) != keyValueSize {
t.Fatalf("need k to be len %d", keyValueSize)
}
if len(v) != 4 {
t.Fatal("need v to be len 4")
if len(v) != keyValueSize {
t.Fatalf("need v to be len %d", keyValueSize)
}
kv.Set(1, k, v)
if ri == i {
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ package main
import "log"

func main() {
db, err := newDb()
db, err := newDb(false)
if err != nil {
log.Fatal(err)
}
25 changes: 16 additions & 9 deletions pager.go
Original file line number Diff line number Diff line change
@@ -196,15 +196,22 @@ func (p *pager) allocatePage(pageNumber uint16, content []byte) *page {
return np
}

// page is structured as follows:
// - 2 bytes for the page type.
// - 4 bytes for the parent pointer.
// - 4 bytes for the left pointer.
// - 4 bytes for the right pointer.
// - 2 bytes for the count of tuples.
// - 4 bytes for the tuple offsets (2 bytes key 2 bytes value) multiplied by the
// amount of tuples.
// - Variable length key and value tuples filling the remaining space.
// page is structured as follows where values accumulate start to end unless
// otherwise specified:
// - 2 bytes for the page type. Which could be internal, leaf or overflow.
// - 4 bytes for the parent pointer (btree).
// - 4 bytes for the left pointer (btree).
// - 4 bytes for the right pointer (btree).
// - 2 bytes for the count of tuples stored on the page.
// - 4 bytes for the tuple offsets (2 bytes key 2 bytes value) multiplied by
// the count of tuples previously mentioned.
// - Variable length key and value tuples filling the remaining space. Which
// accumulates from the end of the page to the start.
//
// TODO could implement overflow pages in case a tuple is larger than some
// threshold (say half the page). There would be another block that would
// contain pointers to overflow pages or be empty. The overflow pages would
// contain a pointer to the next overflow page and content of the overflow.
//
// Tuple offsets are sorted and listed in order. Tuples are stored in reverse
// order starting at the end of the page. This is so the end of each tuple can

0 comments on commit 0bc7355

Please sign in to comment.