Skip to content

Commit

Permalink
prepare space feature
Browse files Browse the repository at this point in the history
  • Loading branch information
vahagz committed Feb 5, 2024
1 parent 17abb58 commit 06e6c9b
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 19 deletions.
8 changes: 7 additions & 1 deletion client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ func main() {
// for rows.Next() { }
// fmt.Printf("[create] %v\n", time.Since(t))

// t = time.Now()
// rows, err = client.Query([]byte(`PREPARE TABLE testtable ROWS 1000000;`))
// exitIfErr(errors.Wrap(err, "query failed"))
// for rows.Next() { }
// fmt.Printf("[prepare] %v\n", time.Since(t))

// t = time.Now()
// var insertId int
// setInterval(time.Second, func() {
Expand Down Expand Up @@ -224,7 +230,7 @@ func main() {
rows, err = client.Query([]byte(`
SELECT id, firstname, lastname
FROM testtable
WHERE_INDEX id id >= 1 AND id <= 1000000;
WHERE_INDEX id id >= 450000 AND id <= 460000;
`))
exitIfErr(errors.Wrap(err, "query failed"))
var (
Expand Down
2 changes: 1 addition & 1 deletion pkg/bptree
Submodule bptree updated 5 files
+9 −8 bptree.go
+1 −1 bptree_del.go
+1 −1 bptree_put.go
+1 −1 bptree_scan.go
+3 −3 meta.go
4 changes: 2 additions & 2 deletions pkg/index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func (i *Index) Options() bptree.Options {
return i.tree.Options()
}

func (i *Index) PrepareSpace(size uint32) {
i.tree.PrepareSpace(size)
func (i *Index) PrepareSpace(rows int) {
i.tree.PrepareSpace(uint32(rows * int(i.tree.HeapSize() / i.tree.Count())))
}

func (i *Index) Close() error {
Expand Down
6 changes: 3 additions & 3 deletions pkg/table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ func (t* Table) Column(name string) *column.Column {
return t.meta.ColumnsMap[name]
}

func (t* Table) PrepareSpace(size uint32) {
t.df.PrepareSpace(size)
func (t* Table) PrepareSpace(rows int) {
t.df.PrepareSpace(uint32(rows * int(t.df.HeapSize() / t.df.Count())))
for _, i := range t.indexes {
i.PrepareSpace(size)
i.PrepareSpace(rows)
}
}

Expand Down
39 changes: 39 additions & 0 deletions services/executor/dml_prepare.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package executor

import (
"fmt"
"io"

"go-dbms/services/parser/query/dml"

"github.com/pkg/errors"
)

func (es *ExecutorServiceT) dmlPrepareValidate(q *dml.QueryPrepare) error {
_, ok := es.tables[q.Table]
if !ok {
return fmt.Errorf("table not found: '%s'", q.Table)
}

if q.Rows < 0 {
return fmt.Errorf("rows must be positive integer")
}

return nil
}

func (es *ExecutorServiceT) dmlPrepare(q *dml.QueryPrepare) (io.WriterTo, error) {
if err := es.dmlPrepareValidate(q); err != nil {
return nil, errors.Wrapf(err, "validation error")
}

p := newPipe(EOS)
go func() {
es.tables[q.Table].PrepareSpace(q.Rows)
if _, err := p.Write(EOS); err != nil {
panic(err)
}
}()

return p, nil
}
13 changes: 7 additions & 6 deletions services/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,13 @@ func New(dataPath string) (*ExecutorServiceT, error) {

func (es *ExecutorServiceT) Exec(q query.Querier) (io.WriterTo, error) {
switch q.GetType() {
case query.CREATE: return es.ddlCreate(q.(create.Creater))
case query.DELETE: return es.dmlDelete(q.(*dml.QueryDelete))
case query.INSERT: return es.dmlInsert(q.(*dml.QueryInsert))
case query.SELECT: return es.dmlSelect(q.(*dml.QuerySelect))
case query.UPDATE: return es.dmlUpdate(q.(*dml.QueryUpdate))
default: panic(fmt.Errorf("invalid query type: '%s'", q.GetType()))
case query.CREATE: return es.ddlCreate(q.(create.Creater))
case query.DELETE: return es.dmlDelete(q.(*dml.QueryDelete))
case query.INSERT: return es.dmlInsert(q.(*dml.QueryInsert))
case query.SELECT: return es.dmlSelect(q.(*dml.QuerySelect))
case query.UPDATE: return es.dmlUpdate(q.(*dml.QueryUpdate))
case query.PREPARE: return es.dmlPrepare(q.(*dml.QueryPrepare))
default: panic(fmt.Errorf("invalid query type: '%s'", q.GetType()))
}
}

Expand Down
4 changes: 4 additions & 0 deletions services/parser/kwords/kwords.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ var KeyWords = map[string]struct{}{

"UPDATE": {},
"SET": {},

"PREPARE": {},
"TABLE": {},
"ROWS": {},
}

var IndexOperators = map[string]struct{}{
Expand Down
2 changes: 1 addition & 1 deletion services/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (ps *ParserServiceT) ParseQuery(data []byte) (query.Querier, error) {
switch qt {
case query.CREATE, query.DROP:
return ddl.Parse(s, qt)
case query.DELETE, query.INSERT, query.SELECT, query.UPDATE:
case query.DELETE, query.INSERT, query.SELECT, query.UPDATE, query.PREPARE:
return dml.Parse(s, qt)
}
}
Expand Down
11 changes: 6 additions & 5 deletions services/parser/query/dml/dml.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ func Parse(s *scanner.Scanner, queryType query.QueryType) (query.Querier, error)
var q query.Querier

switch queryType {
case query.DELETE: q = &QueryDelete{}
case query.INSERT: q = &QueryInsert{}
case query.SELECT: q = &QuerySelect{}
case query.UPDATE: q = &QueryUpdate{}
default: return nil, errors.New(fmt.Sprintf("unsupported query type: '%s'", queryType))
case query.DELETE: q = &QueryDelete{}
case query.INSERT: q = &QueryInsert{}
case query.SELECT: q = &QuerySelect{}
case query.UPDATE: q = &QueryUpdate{}
case query.PREPARE: q = &QueryPrepare{}
default: return nil, errors.New(fmt.Sprintf("unsupported query type: '%s'", queryType))
}

return q, q.Parse(s)
Expand Down
70 changes: 70 additions & 0 deletions services/parser/query/dml/prepare.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package dml

import (
"strconv"
"text/scanner"

"go-dbms/services/parser/errors"
"go-dbms/services/parser/kwords"
"go-dbms/services/parser/query"
)

type QueryPrepare struct {
query.Query
DB string
Table string
Rows int
}


func (qp *QueryPrepare) Parse(s *scanner.Scanner) (err error) {
defer func () {
if r := recover(); r != nil {
var ok bool
err, ok = r.(error)
if !ok {
panic(r)
}
}
}()

qp.Type = query.PREPARE

qp.parseTable(s)
qp.parseRows(s)

return nil
}

func (qp *QueryPrepare) parseTable(s *scanner.Scanner) {
s.Scan()
if s.TokenText() != "TABLE" {
panic(errors.ErrSyntax)
}

s.Scan()
qp.Table = s.TokenText()
if _, isKW := kwords.KeyWords[qp.Table]; isKW {
panic(errors.ErrSyntax)
}
}

func (qp *QueryPrepare) parseRows(s *scanner.Scanner) {
s.Scan()
if s.TokenText() != "ROWS" {
panic(errors.ErrSyntax)
}

s.Scan()
rows, err := strconv.Atoi(s.TokenText())
if err != nil {
panic(err)
}

qp.Rows = rows

s.Scan()
if s.TokenText() != ";" {
panic(errors.ErrSyntax)
}
}
1 change: 1 addition & 0 deletions services/parser/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const (
DROP QueryType = "DROP"
TRUNCATE QueryType = "TRUNCATE"
RENAME QueryType = "RENAME"
PREPARE QueryType = "PREPARE"
)

type Querier interface {
Expand Down

0 comments on commit 06e6c9b

Please sign in to comment.