Compare commits
3
Commits
dev
..
abc07646c7
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
abc07646c7 | ||
|
|
8ea0978d5f | ||
|
|
28acfd8d7e |
No files matched your search
@@ -1,23 +1,3 @@
|
||||
module godb
|
||||
|
||||
go 1.26.3
|
||||
|
||||
require charm.land/lipgloss/v2 v2.0.6
|
||||
|
||||
require (
|
||||
github.com/charmbracelet/colorprofile v0.4.3 // indirect
|
||||
github.com/charmbracelet/ultraviolet v0.0.0-20260811164956-006e29f97886 // indirect
|
||||
github.com/charmbracelet/x/ansi v0.11.8 // indirect
|
||||
github.com/charmbracelet/x/term v0.2.2 // indirect
|
||||
github.com/charmbracelet/x/termios v0.1.1 // indirect
|
||||
github.com/charmbracelet/x/windows v0.2.2 // indirect
|
||||
github.com/clipperhouse/displaywidth v0.11.0 // indirect
|
||||
github.com/clipperhouse/uax29/v2 v2.7.0 // indirect
|
||||
github.com/lucasb-eyer/go-colorful v1.4.1 // indirect
|
||||
github.com/mattn/go-runewidth v0.0.24 // indirect
|
||||
github.com/muesli/cancelreader v0.2.2 // indirect
|
||||
github.com/rivo/uniseg v0.4.7 // indirect
|
||||
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
|
||||
golang.org/x/sync v0.22.0 // indirect
|
||||
golang.org/x/sys v0.47.0 // indirect
|
||||
)
|
||||
@@ -1,38 +0,0 @@
|
||||
charm.land/lipgloss/v2 v2.0.6 h1:EaGKeuA8FvF+v2BT5VmZd2LoYLaMZJXA5n34th8nCIQ=
|
||||
charm.land/lipgloss/v2 v2.0.6/go.mod h1:ipDDJNSGa1hlwDtSfW1s2/xR8Vdhbut4PXh2zEKZd0Q=
|
||||
github.com/aymanbagabas/go-udiff v0.4.1 h1:OEIrQ8maEeDBXQDoGCbbTTXYJMYRCRO1fnodZ12Gv5o=
|
||||
github.com/aymanbagabas/go-udiff v0.4.1/go.mod h1:0L9PGwj20lrtmEMeyw4WKJ/TMyDtvAoK9bf2u/mNo3w=
|
||||
github.com/charmbracelet/colorprofile v0.4.3 h1:QPa1IWkYI+AOB+fE+mg/5/4HRMZcaXex9t5KX76i20Q=
|
||||
github.com/charmbracelet/colorprofile v0.4.3/go.mod h1:/zT4BhpD5aGFpqQQqw7a+VtHCzu+zrQtt1zhMt9mR4Q=
|
||||
github.com/charmbracelet/ultraviolet v0.0.0-20260811164956-006e29f97886 h1:rdnVWKgJpTVXKuKuJyxDJ+NFJdUaUqGvyGy61OcvlbA=
|
||||
github.com/charmbracelet/ultraviolet v0.0.0-20260811164956-006e29f97886/go.mod h1:nAw0d9PhFp1qdzi2xhQU5YOu5sVpDIHWlaW2Uz/bCro=
|
||||
github.com/charmbracelet/x/ansi v0.11.8 h1:JMFwp0CgDC2+jcOB162HH5k7I3FVbgFSMMYg7dSPBQQ=
|
||||
github.com/charmbracelet/x/ansi v0.11.8/go.mod h1:ZNN+3mXny/516oTQPLMPIBeSINvNJJQ8uQXDgbeJxY0=
|
||||
github.com/charmbracelet/x/exp/golden v0.0.0-20250806222409-83e3a29d542f h1:pk6gmGpCE7F3FcjaOEKYriCvpmIN4+6OS/RD0vm4uIA=
|
||||
github.com/charmbracelet/x/exp/golden v0.0.0-20250806222409-83e3a29d542f/go.mod h1:IfZAMTHB6XkZSeXUqriemErjAWCCzT0LwjKFYCZyw0I=
|
||||
github.com/charmbracelet/x/term v0.2.2 h1:xVRT/S2ZcKdhhOuSP4t5cLi5o+JxklsoEObBSgfgZRk=
|
||||
github.com/charmbracelet/x/term v0.2.2/go.mod h1:kF8CY5RddLWrsgVwpw4kAa6TESp6EB5y3uxGLeCqzAI=
|
||||
github.com/charmbracelet/x/termios v0.1.1 h1:o3Q2bT8eqzGnGPOYheoYS8eEleT5ZVNYNy8JawjaNZY=
|
||||
github.com/charmbracelet/x/termios v0.1.1/go.mod h1:rB7fnv1TgOPOyyKRJ9o+AsTU/vK5WHJ2ivHeut/Pcwo=
|
||||
github.com/charmbracelet/x/windows v0.2.2 h1:IofanmuvaxnKHuV04sC0eBy/smG6kIKrWG2/jYn2GuM=
|
||||
github.com/charmbracelet/x/windows v0.2.2/go.mod h1:/8XtdKZzedat74NQFn0NGlGL4soHB0YQZrETF96h75k=
|
||||
github.com/clipperhouse/displaywidth v0.11.0 h1:lBc6kY44VFw+TDx4I8opi/EtL9m20WSEFgwIwO+UVM8=
|
||||
github.com/clipperhouse/displaywidth v0.11.0/go.mod h1:bkrFNkf81G8HyVqmKGxsPufD3JhNl3dSqnGhOoSD/o0=
|
||||
github.com/clipperhouse/uax29/v2 v2.7.0 h1:+gs4oBZ2gPfVrKPthwbMzWZDaAFPGYK72F0NJv2v7Vk=
|
||||
github.com/clipperhouse/uax29/v2 v2.7.0/go.mod h1:EFJ2TJMRUaplDxHKj1qAEhCtQPW2tJSwu5BF98AuoVM=
|
||||
github.com/lucasb-eyer/go-colorful v1.4.1 h1:1EO+WB73+EH8EVbzlrG3KLAfEypQWVHIBqlTf+2hNss=
|
||||
github.com/lucasb-eyer/go-colorful v1.4.1/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
|
||||
github.com/mattn/go-runewidth v0.0.24 h1:cpokDiIn0MGnhdHwuWnJBITySJ20QyNGnY2kR/ay2DU=
|
||||
github.com/mattn/go-runewidth v0.0.24/go.mod h1:XBkDxAl56ILZc9knddidhrOlY5R/pDhgLpndooCuJAs=
|
||||
github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=
|
||||
github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
|
||||
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
|
||||
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
|
||||
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
|
||||
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
|
||||
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
|
||||
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
|
||||
golang.org/x/sync v0.22.0 h1:SZjpbeLmrCk4xhRSZFNZW5gFUeCeFgjekvI/+gfScek=
|
||||
golang.org/x/sync v0.22.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
|
||||
golang.org/x/sys v0.47.0 h1:o7XGOvZQCADBQQ4Y7VNq2dRWQR7JmOUW8Kxx4ZsNgWs=
|
||||
golang.org/x/sys v0.47.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
||||
@@ -37,10 +37,6 @@ func Run() {
|
||||
fmt.Println("Syntax error, could not parse statement")
|
||||
case PrepareUnrecognised:
|
||||
fmt.Printf("Unrecognised keyword at the start of '%s'.\n", inputBuffer.Buffer)
|
||||
case PrepareTooManyArgs:
|
||||
fmt.Printf("Too many arguments in the insert statement '%v'\n", inputBuffer.Buffer)
|
||||
case PrepareValueTooLong:
|
||||
fmt.Printf("Values for either username or email are too long\n")
|
||||
}
|
||||
|
||||
switch statement.ExecuteStatement(table) {
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
package repl
|
||||
|
||||
// TODO
|
||||
|
||||
// in trying to implement unit test for Run we would kinda endup implementing a test which would be somewhat similar to
|
||||
// e2e test which we already have in place under ../tests directory
|
||||
// PrintConsole simply prints godb > to the console, so not sure if we would need a test for that
|
||||
// Skipping unit test for this, while marking it as a TODO
|
||||
+14
-73
@@ -4,14 +4,8 @@ import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"os"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
// additional
|
||||
"charm.land/lipgloss/v2"
|
||||
"charm.land/lipgloss/v2/table"
|
||||
|
||||
. "godb/internals/buffer"
|
||||
)
|
||||
|
||||
@@ -27,8 +21,6 @@ type PrepareResults int
|
||||
const (
|
||||
PrepareSuccess PrepareResults = iota
|
||||
PrepareUnrecognised
|
||||
PrepareTooManyArgs
|
||||
PrepareValueTooLong
|
||||
PrepareSyntaxError
|
||||
)
|
||||
|
||||
@@ -78,7 +70,6 @@ type Statement struct {
|
||||
type Table struct {
|
||||
numOfRows uint
|
||||
pages [maxPages]any
|
||||
tableRep *table.Table
|
||||
}
|
||||
|
||||
func NewStatement() *Statement {
|
||||
@@ -86,29 +77,8 @@ func NewStatement() *Statement {
|
||||
return statement
|
||||
}
|
||||
|
||||
var (
|
||||
purple = lipgloss.Color("99")
|
||||
|
||||
headerStyle = lipgloss.NewStyle().Foreground(purple).Bold(true).Padding(0, 1)
|
||||
cellStyle = lipgloss.NewStyle().Padding(0, 1)
|
||||
rowStyle = cellStyle.Foreground(lipgloss.White)
|
||||
)
|
||||
|
||||
func NewTable() *Table {
|
||||
tbl := table.New().
|
||||
Border(lipgloss.ASCIIBorder()).
|
||||
BorderStyle(lipgloss.NewStyle().Foreground(purple)).
|
||||
StyleFunc(func(row, col int) lipgloss.Style {
|
||||
switch {
|
||||
case row == table.HeaderRow:
|
||||
return headerStyle
|
||||
default:
|
||||
return rowStyle
|
||||
}
|
||||
}).
|
||||
Headers("ID", "NAME", "EMAIL")
|
||||
|
||||
newTable := Table{numOfRows: 0, tableRep: tbl}
|
||||
newTable := Table{numOfRows: 0}
|
||||
for i := range maxPages {
|
||||
newTable.pages[i] = nil
|
||||
}
|
||||
@@ -148,8 +118,8 @@ func (row *row) seralizeRow(page []byte) {
|
||||
|
||||
func (row *row) deSeralizeRow(page []byte) {
|
||||
row.id = uint(binary.NativeEndian.Uint64(page[idOffset:idSize]))
|
||||
row.username = strings.TrimRight(string(page[usernameOffset:usernameOffset+usernameSize]), "\x00")
|
||||
row.email = strings.TrimRight(string(page[emailOffset:emailOffset+emailSize]), "\x00")
|
||||
row.username = string(page[usernameOffset : usernameOffset+usernameSize])
|
||||
row.email = string(page[emailOffset : emailOffset+emailSize])
|
||||
}
|
||||
|
||||
func (statement *Statement) execInsert(table *Table) ExecuteResult {
|
||||
@@ -163,13 +133,10 @@ func (statement *Statement) execInsert(table *Table) ExecuteResult {
|
||||
|
||||
func (statement *Statement) execSelect(table *Table) ExecuteResult {
|
||||
var row row
|
||||
t := table.tableRep
|
||||
t.ClearRows()
|
||||
for i := range table.numOfRows {
|
||||
row.deSeralizeRow(rowToUse(table, i))
|
||||
t.Row(fmt.Sprintf("%v", row.id), row.username, row.email)
|
||||
fmt.Printf("%v\n", row)
|
||||
}
|
||||
lipgloss.Println(t)
|
||||
return ExecuteSuccess
|
||||
}
|
||||
|
||||
@@ -184,45 +151,19 @@ func DoMetaCommands(inputBuffer *InputBuffer) MetaCommandResults {
|
||||
return metacommand
|
||||
}
|
||||
|
||||
func (statement *Statement) perpareInsert(inputBuffer *InputBuffer) PrepareResults {
|
||||
inputs := strings.Split(inputBuffer.Buffer, " ")[1:]
|
||||
if len(inputs) < 3 {
|
||||
return PrepareSyntaxError
|
||||
}
|
||||
if len(inputs) > 3 {
|
||||
return PrepareTooManyArgs
|
||||
}
|
||||
if slices.Contains(inputs, "null") || slices.Contains(inputs, "NULL") || slices.Contains(inputs, "Null") || slices.Contains(inputs, " ") {
|
||||
return PrepareSyntaxError
|
||||
}
|
||||
|
||||
tid, err := strconv.ParseUint(inputs[0], 10, 0)
|
||||
if err != nil {
|
||||
// fmt.Printf("supplied ID: %v isn't acceptable, please correct and retry\n", inputs[0])
|
||||
return PrepareSyntaxError
|
||||
}
|
||||
id := uint(tid)
|
||||
if len(inputs[1]) > usernameSize {
|
||||
// fmt.Printf("%v is too long for username\n", inputs[1])
|
||||
return PrepareValueTooLong
|
||||
}
|
||||
if len(inputs[2]) > emailSize {
|
||||
// fmt.Printf("%v is too long for email\n", inputs[2])
|
||||
return PrepareValueTooLong
|
||||
}
|
||||
|
||||
statement.row.id = id
|
||||
statement.row.username = inputs[1]
|
||||
statement.row.email = inputs[2]
|
||||
|
||||
statement.stype = StatementInsert
|
||||
return PrepareSuccess
|
||||
}
|
||||
|
||||
func (statement *Statement) PrepareStatements(inputBuffer *InputBuffer) PrepareResults {
|
||||
statement.stype = StatementUnrecognized
|
||||
if strings.HasPrefix(inputBuffer.Buffer, "insert") {
|
||||
return statement.perpareInsert(inputBuffer)
|
||||
argsAssigned, err := fmt.Sscanf(inputBuffer.Buffer, "insert %d %s %s", &statement.row.id, &statement.row.username, &statement.row.email)
|
||||
if err != nil {
|
||||
fmt.Println("error parsing insert statement")
|
||||
fmt.Println(err.Error())
|
||||
}
|
||||
if err != nil || argsAssigned > 3 {
|
||||
return PrepareSyntaxError
|
||||
}
|
||||
statement.stype = StatementInsert
|
||||
return PrepareSuccess
|
||||
}
|
||||
if inputBuffer.Buffer == "select" {
|
||||
statement.stype = StatementSelect
|
||||
|
||||
@@ -1,54 +0,0 @@
|
||||
package vm
|
||||
|
||||
import (
|
||||
"godb/internals/buffer"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var inputBuffer = buffer.NewInputBuffer()
|
||||
|
||||
var statement = NewStatement()
|
||||
|
||||
func TestVM(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
prepareStatusExpected PrepareResults
|
||||
statementResultExpected statementType
|
||||
}{
|
||||
{
|
||||
name: "Testing Select Statement",
|
||||
input: "select",
|
||||
prepareStatusExpected: PrepareSuccess,
|
||||
statementResultExpected: StatementSelect,
|
||||
},
|
||||
{
|
||||
name: "Testing Insert Statement",
|
||||
input: "insert 0 hello hello.com",
|
||||
prepareStatusExpected: PrepareSuccess,
|
||||
statementResultExpected: StatementInsert,
|
||||
},
|
||||
{
|
||||
name: "Testing Unrecognized Statement",
|
||||
input: "something",
|
||||
prepareStatusExpected: PrepareUnrecognised,
|
||||
statementResultExpected: StatementUnrecognized,
|
||||
},
|
||||
{
|
||||
name: "Testing Syntax Errors",
|
||||
input: "insert 0 hello",
|
||||
prepareStatusExpected: PrepareSyntaxError,
|
||||
statementResultExpected: StatementUnrecognized,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
inputBuffer.Buffer = test.input
|
||||
result := statement.PrepareStatements(inputBuffer)
|
||||
if result != test.prepareStatusExpected && statement.stype == test.statementResultExpected {
|
||||
t.Errorf("Expected prepareResult and stype to be: (%v, %v), got: (%v,%v)\n", test.prepareStatusExpected, test.statementResultExpected, result, statement.stype)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
package tests
|
||||
|
||||
// implement benchmark test for the main loop
|
||||
+17
-54
@@ -35,21 +35,15 @@ func runner(commands []string, t *testing.T) []string {
|
||||
go func() {
|
||||
defer stdin.Close()
|
||||
for _, c := range commands {
|
||||
_, err = io.WriteString(stdin, c+"\n")
|
||||
if err != nil {
|
||||
t.Errorf("Failed to execute %v because of %q\n", c, err)
|
||||
break
|
||||
}
|
||||
_, _ = io.WriteString(stdin, c+"\n")
|
||||
}
|
||||
}()
|
||||
|
||||
reader := bufio.NewReader(stdout)
|
||||
for {
|
||||
// fmt.Printf("reading command outputs...\n")
|
||||
line, err := reader.ReadString('\n')
|
||||
if line != "" {
|
||||
formattedline := fmt.Sprintf("%v", strings.Trim(strings.ReplaceAll(line, "\x00", ""), "\n"))
|
||||
// fmt.Printf("formatting and appending result: %v\n", formattedline)
|
||||
results = append(results, formattedline)
|
||||
}
|
||||
if err != nil {
|
||||
@@ -66,57 +60,26 @@ func runner(commands []string, t *testing.T) []string {
|
||||
return results
|
||||
}
|
||||
|
||||
func TestE2E(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
commands []string
|
||||
expected []string
|
||||
}{
|
||||
{
|
||||
name: "Test Main Loop",
|
||||
commands: []string{"insert 1 user1 person1@example.com", "select", ".exit"},
|
||||
expected: []string{"godb > Executed.", "godb > {1 user1 person1@example.com}", "Executed.", "godb > bye!"},
|
||||
},
|
||||
{
|
||||
name: "Test Maximum Length For Values",
|
||||
commands: []string{"insert 0 " + strings.Repeat("n", 32) + " " + strings.Repeat("n", 255), ".exit"},
|
||||
expected: []string{"godb > Executed.", "godb > bye!"},
|
||||
},
|
||||
{
|
||||
name: "Test Over Maximum Length For Values",
|
||||
commands: []string{"insert 0 " + strings.Repeat("n", 42) + " " + strings.Repeat("n", 275), ".exit"},
|
||||
expected: []string{"godb > Values for either username or email are too long", "godb > bye!"},
|
||||
},
|
||||
func TestMain(t *testing.T) {
|
||||
commands := []string{
|
||||
"insert 1 user1 person1@example.com",
|
||||
"select",
|
||||
".exit",
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
results := runner(test.commands, t)
|
||||
for i := range results {
|
||||
results[i] = strings.ReplaceAll(results[i], "\n", "")
|
||||
}
|
||||
|
||||
if !slices.Equal(test.expected, results) {
|
||||
t.Errorf("Output Mismatch\nExpected: %v\nGot: %v\n", test.expected, results)
|
||||
}
|
||||
})
|
||||
expected := []string{
|
||||
"godb > Executed.",
|
||||
"godb > {1 user1 person1@example.com}",
|
||||
"Executed.",
|
||||
"godb > bye!",
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestTableFull(t *testing.T) {
|
||||
maxRows := 1400
|
||||
commands := make([]string, 0)
|
||||
|
||||
for i := range maxRows {
|
||||
c := fmt.Sprintf("insert %d user%d person%d@example.com", i, i, i)
|
||||
commands = append(commands, c)
|
||||
}
|
||||
commands = append(commands, ".exit")
|
||||
|
||||
results := runner(commands, t)
|
||||
|
||||
if results[1300] != "godb > Error: Table is full" {
|
||||
t.Errorf("Our Tables max capacity is 1300 rows, but we're able to insert %v\n", len(results))
|
||||
for i := range results {
|
||||
results[i] = strings.ReplaceAll(results[i], "\n", "")
|
||||
}
|
||||
|
||||
if !slices.Equal(expected, results) {
|
||||
t.Errorf("Output Mismatch\nExpected: %v\nGot: %v\n", expected, results)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user