Skip to content

Commit

Permalink
refactor: metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
kj455 committed Nov 4, 2024
1 parent a4406a6 commit 1c1fae8
Show file tree
Hide file tree
Showing 19 changed files with 539 additions and 427 deletions.
58 changes: 33 additions & 25 deletions pkg/metadata/index_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,28 @@ import (
)

type IndexInfoImpl struct {
idxname string
fldname string
idxName string
fldName string
tx tx.Transaction
tblSchema record.Schema
idxLayout record.Layout
si StatInfo
}

// NewIndexInfo creates an IndexInfoImpl object for the specified index.
func NewIndexInfo(idxname, fldname string, tblSchema record.Schema, tx tx.Transaction, si StatInfo) (IndexInfo, error) {
func NewIndexInfo(idxName, fldName string, tblSchema record.Schema, tx tx.Transaction, si StatInfo) (*IndexInfoImpl, error) {
ii := &IndexInfoImpl{
idxname: idxname,
fldname: fldname,
idxName: idxName,
fldName: fldName,
tx: tx,
tblSchema: tblSchema,
si: si,
tblSchema: tblSchema,
}
l, err := ii.createIdxLayout()
layout, err := ii.createIdxLayout()
if err != nil {
return nil, fmt.Errorf("index info: %w", err)
return nil, err
}
ii.idxLayout = l
ii.idxLayout = layout
return ii, nil
}

Expand All @@ -41,7 +41,7 @@ func NewIndexInfo(idxname, fldname string, tblSchema record.Schema, tx tx.Transa
// }

func (ii *IndexInfoImpl) IndexName() string {
return ii.idxname
return ii.idxName
}

func (ii *IndexInfoImpl) IdxLayout() record.Layout {
Expand All @@ -56,45 +56,53 @@ func (ii *IndexInfoImpl) Si() StatInfo {
return ii.si
}

// TODO:
// BlocksAccessed estimates the number of block accesses required to find all index records.
// func (ii *IndexInfoImpl) BlocksAccessed() int {
// rpb := ii.tx.BlockSize() / ii.idxLayout.SlotSize()
// numblocks := ii.si.RecordsOutput() / rpb
// return HashIndexSearchCost(numblocks, rpb)
// // return BTreeIndexSearchCost(numblocks, rpb)
// }
// FIXME
func (ii *IndexInfoImpl) BlocksAccessed() int {
rpb := ii.tx.BlockSize() / ii.idxLayout.SlotSize()
numblocks := ii.si.RecordsOutput() / rpb
return numblocks
// return HashIndexSearchCost(numblocks, rpb)
// return BTreeIndexSearchCost(numblocks, rpb)
}

// RecordsOutput returns the estimated number of records having a search key.
func (ii *IndexInfoImpl) RecordsOutput() int {
return ii.si.RecordsOutput() / ii.si.DistinctValues(ii.fldname)
return ii.si.RecordsOutput() / ii.si.DistinctValues(ii.fldName)
}

// DistinctValues returns the distinct values for a specified field or 1 for the indexed field.
func (ii *IndexInfoImpl) DistinctValues(fname string) int {
if ii.fldname == fname {
if ii.fldName == fname {
return 1
}
return ii.si.DistinctValues(ii.fldname)
return ii.si.DistinctValues(ii.fldName)
}

// createIdxLayout returns the layout of the index records.
func (ii *IndexInfoImpl) createIdxLayout() (record.Layout, error) {
sch := record.NewSchema()
sch.AddIntField("block")
sch.AddIntField("id")
schType, err := ii.tblSchema.Type(ii.fldname)

schType, err := ii.tblSchema.Type(ii.fldName)
if err != nil {
return nil, err
return nil, fmt.Errorf("metadata: failed to get field type: %v", err)
}

if schType == record.SCHEMA_TYPE_INTEGER {
sch.AddIntField("dataval")
} else {
fldlen, err := ii.tblSchema.Length(ii.fldname)
fldlen, err := ii.tblSchema.Length(ii.fldName)
if err != nil {
return nil, err
return nil, fmt.Errorf("metadata: failed to get field length: %v", err)
}
sch.AddStringField("dataval", fldlen)
}
return record.NewLayoutFromSchema(sch)

layout, err := record.NewLayoutFromSchema(sch)
if err != nil {
return nil, fmt.Errorf("metadata: failed to create layout: %v", err)
}
return layout, nil
}
105 changes: 0 additions & 105 deletions pkg/metadata/metadata_mgr_test.go

This file was deleted.

3 changes: 1 addition & 2 deletions pkg/metadata/stat_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ func (si *StatInfoImpl) RecordsOutput() int {

// DistinctValues returns the estimated number of distinct values
// for the specified field.
// This estimate is a complete guess, because doing something
// reasonable is beyond the scope of this system.
// FIXME: This is a fake value.
func (si *StatInfoImpl) DistinctValues(fldname string) int {
return 1 + (si.numRecords / 3)
}
2 changes: 1 addition & 1 deletion pkg/metadata/view_mgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestViewMgr__(t *testing.T) {
func TestViewMgr(t *testing.T) {
const (
logFileName = "test_view_mgr_log"
blockSize = 1024
Expand Down
2 changes: 1 addition & 1 deletion pkg/parse/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (p *Parser) Constant() (*constant.Const, error) {
}

// Expression parses and returns an expression.
func (p *Parser) Expression() (*query.ExpressionImpl, error) {
func (p *Parser) Expression() (query.Expression, error) {
if p.lex.MatchId() {
f, err := p.Field()
if err != nil {
Expand Down
61 changes: 0 additions & 61 deletions pkg/query/expression.go

This file was deleted.

39 changes: 39 additions & 0 deletions pkg/query/expression_const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package query

import (
"github.com/kj455/db/pkg/constant"
"github.com/kj455/db/pkg/record"
)

type ConstantExpression struct {
val *constant.Const
}

func NewConstantExpression(val *constant.Const) *ConstantExpression {
return &ConstantExpression{val: val}
}

func (c *ConstantExpression) Evaluate(s Scan) (*constant.Const, error) {
return c.val, nil
}

func (c *ConstantExpression) IsFieldName() bool {
return false
}

func (c *ConstantExpression) AsConstant() *constant.Const {
return c.val
}

func (c *ConstantExpression) AsFieldName() string {
return ""
}

// CanApply determines if all of the fields mentioned in this expression are contained in the specified schema.
func (c *ConstantExpression) CanApply(sch record.Schema) bool {
return true
}

func (c *ConstantExpression) ToString() string {
return c.val.ToString()
}
38 changes: 38 additions & 0 deletions pkg/query/expression_field.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package query

import (
"github.com/kj455/db/pkg/constant"
"github.com/kj455/db/pkg/record"
)

type FieldExpression struct {
field string
}

func NewFieldExpression(field string) *FieldExpression {
return &FieldExpression{field: field}
}

func (f *FieldExpression) Evaluate(s Scan) (*constant.Const, error) {
return s.GetVal(f.field)
}

func (f *FieldExpression) IsFieldName() bool {
return true
}

func (f *FieldExpression) AsConstant() *constant.Const {
return nil
}

func (f *FieldExpression) AsFieldName() string {
return f.field
}

func (f *FieldExpression) CanApply(sch record.Schema) bool {
return sch.HasField(f.field)
}

func (f *FieldExpression) ToString() string {
return f.field
}
Loading

0 comments on commit 1c1fae8

Please sign in to comment.