Skip to content

Commit

Permalink
chore: minor refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
rudrOwO committed Nov 27, 2024
1 parent 3217984 commit 07d2e8d
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 22 deletions.
2 changes: 1 addition & 1 deletion cmd/sqlite/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func main() {
switch userCommand {
case ".dbinfo":
fmt.Printf("database page size: %v\n", btree.PAGE_SIZE)
fmt.Printf("number of tables: %v", api.CountRows(sql.GetRootPageOFFSET("sqlite_schema")))
fmt.Printf("number of tables: %v", sql.ExecuteSelect("SELECT COUNT(*) FROM sqlite_schema"))
case ".tables":
fmt.Print(sql.ExecuteSelect("SELECT tbl_name FROM sqlite_schema WHERE tbl_name != 'sqlite_sequence'"))
default:
Expand Down
2 changes: 1 addition & 1 deletion internal/api/countrows.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

func CountRows(rootPageOffset int64) uint16 {
leafPagesChannel := make(chan btree.LeafTablePage, 100)
leafPagesChannel := make(chan btree.LeafTablePage, BTREE_BUFFER_SIZE)
go btree.LoadAllLeafTablePages(rootPageOffset, dbFile, leafPagesChannel, true)

cellsCount := uint16(0)
Expand Down
19 changes: 0 additions & 19 deletions internal/api/dotcommands.go

This file was deleted.

14 changes: 14 additions & 0 deletions internal/api/init.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"encoding/binary"
"log"
"os"

Expand All @@ -9,6 +10,8 @@ import (

var dbFile *os.File

const BTREE_BUFFER_SIZE = 100

func Init(fileName string) *os.File {
var err error
dbFile, err = os.Open(fileName)
Expand All @@ -19,3 +22,14 @@ func Init(fileName string) *os.File {
btree.PAGE_SIZE = int64(readPageSize())
return dbFile
}

func readPageSize() uint16 {
dbHeader := make([]byte, btree.SQLITE3_HEADER_SIZE)
_, err := dbFile.Read(dbHeader)
if err != nil {
log.Fatal(err)
}

pageSize := binary.BigEndian.Uint16(dbHeader[16:18])
return pageSize
}
2 changes: 1 addition & 1 deletion internal/api/scantable.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func ScanTable(columnIndicesToSerialize []int, rowLength int, rootPageOffset int64, filter func(row []any) bool, rowsChannel chan<- []any) {
leafPagesChannel := make(chan btree.LeafTablePage, 100)
leafPagesChannel := make(chan btree.LeafTablePage, BTREE_BUFFER_SIZE)
go btree.LoadAllLeafTablePages(rootPageOffset, dbFile, leafPagesChannel, true)

for page := range leafPagesChannel {
Expand Down

0 comments on commit 07d2e8d

Please sign in to comment.