Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: plan #10

Merged
merged 1 commit into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/copilot-instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
コードスタイル
- エラー文言はパッケージ名を先頭に含める(ex. `fmt.Errorf("pkg: error message")`)
-
8 changes: 4 additions & 4 deletions pkg/buffer/buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestBuffer_WriteContents(t *testing.T) {
lsn = 2
)
dir, _, cleanup := testutil.SetupFile(logFileName)
defer cleanup()
t.Cleanup(cleanup)
fileMgr := file.NewFileMgr(dir, blockSize)
logMgr, err := log.NewLogMgr(fileMgr, logFileName)
assert.NoError(t, err)
Expand All @@ -43,7 +43,7 @@ func TestBuffer_Flush(t *testing.T) {
t.Parallel()
const logFileName = "test_buffer_flush_skip"
dir, _, cleanup := testutil.SetupFile(logFileName)
defer cleanup()
t.Cleanup(cleanup)
fileMgr := file.NewFileMgr(dir, blockSize)
logMgr, err := log.NewLogMgr(fileMgr, logFileName)
assert.NoError(t, err)
Expand All @@ -58,7 +58,7 @@ func TestBuffer_Flush(t *testing.T) {
t.Parallel()
const logFileName = "test_buffer_flush"
dir, _, cleanup := testutil.SetupFile(logFileName)
defer cleanup()
t.Cleanup(cleanup)
fileMgr := file.NewFileMgr(dir, blockSize)
logMgr, err := log.NewLogMgr(fileMgr, logFileName)
assert.NoError(t, err)
Expand Down Expand Up @@ -92,7 +92,7 @@ func TestBuffer_AssignToBlock(t *testing.T) {
logFileName = "test_buffer_assign_to_block"
)
dir, _, cleanup := testutil.SetupFile(logFileName)
defer cleanup()
t.Cleanup(cleanup)
fileMgr := file.NewFileMgr(dir, blockSize)
logMgr, err := log.NewLogMgr(fileMgr, logFileName)
assert.NoError(t, err)
Expand Down
10 changes: 5 additions & 5 deletions pkg/buffer_mgr/buffer_mgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestBufferMgr_Pin(t *testing.T) {
logFileName = "test_buffer_mgr_pin_no_buffer_assigned"
)
dir, _, cleanup := testutil.SetupFile(logFileName)
defer cleanup()
t.Cleanup(cleanup)
fileMgr := file.NewFileMgr(dir, blockSize)
logMgr, err := log.NewLogMgr(fileMgr, logFileName)
assert.NoError(t, err)
Expand All @@ -45,7 +45,7 @@ func TestBufferMgr_Pin(t *testing.T) {
buffNum = 1
)
dir, _, cleanup := testutil.SetupFile(logFileName)
defer cleanup()
t.Cleanup(cleanup)
fileMgr := file.NewFileMgr(dir, blockSize)
logMgr, err := log.NewLogMgr(fileMgr, logFileName)
assert.NoError(t, err)
Expand Down Expand Up @@ -73,7 +73,7 @@ func TestBufferMgr_Pin(t *testing.T) {
buffNum = 1
)
dir, _, cleanup := testutil.SetupFile(logFileName)
defer cleanup()
t.Cleanup(cleanup)
fileMgr := file.NewFileMgr(dir, blockSize)
logMgr, err := log.NewLogMgr(fileMgr, logFileName)
assert.NoError(t, err)
Expand Down Expand Up @@ -103,7 +103,7 @@ func TestBufferMgrImpl_Unpin(t *testing.T) {
logFileName = "test_buffer_mgr_unpin_available_increment"
)
dir, _, cleanup := testutil.SetupFile(logFileName)
defer cleanup()
t.Cleanup(cleanup)
fileMgr := file.NewFileMgr(dir, blockSize)
logMgr, err := log.NewLogMgr(fileMgr, logFileName)
assert.NoError(t, err)
Expand Down Expand Up @@ -137,7 +137,7 @@ func TestBufferMgrImpl_FlushAll(t *testing.T) {
txNum = 1
)
dir, _, cleanup := testutil.SetupFile(logFileName)
defer cleanup()
t.Cleanup(cleanup)
fileMgr := file.NewFileMgr(dir, blockSize)
logMgr, err := log.NewLogMgr(fileMgr, logFileName)
assert.NoError(t, err)
Expand Down
6 changes: 3 additions & 3 deletions pkg/file/file_mgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestFileMgr_Read(t *testing.T) {
t.Parallel()
const fileName = "read_test"
f, cleanup := setupFile(fileName)
defer cleanup()
t.Cleanup(cleanup)
bytes := []byte("hello world!!!!")
_, err := f.Write(bytes)
assert.NoError(t, err)
Expand All @@ -73,7 +73,7 @@ func TestFileMgr_Read(t *testing.T) {
blockSize = 4096
)
_, cleanup := setupFile(fileName)
defer cleanup()
t.Cleanup(cleanup)
page := NewPage(blockSize)
page.SetString(0, "hello world!!!!")
id := NewBlockId(fileName, 0)
Expand All @@ -89,7 +89,7 @@ func TestFileMgr_Read(t *testing.T) {
t.Parallel()
const fileName = "append_test"
_, cleanup := setupFile(fileName)
defer cleanup()
t.Cleanup(cleanup)

id, err := mgr.Append(fileName)

Expand Down
10 changes: 5 additions & 5 deletions pkg/log/log_iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestNewLogIterator(t *testing.T) {
blockSize = 8
)
dir, _, cleanup := testutil.SetupFile(fileName)
defer cleanup()
t.Cleanup(cleanup)
fileMgr := file.NewFileMgr(dir, blockSize)
writePage := file.NewPage(blockSize)
writePage.SetInt(0, blockSize)
Expand All @@ -36,7 +36,7 @@ func TestLogIterator_HasNext(t *testing.T) {
filename = "test_log_iterator_has_next"
)
dir, _, cleanup := testutil.SetupFile(filename)
defer cleanup()
t.Cleanup(cleanup)
fileMgr := file.NewFileMgr(dir, blockSize)
block := file.NewBlockId(filename, 0)

Expand All @@ -55,7 +55,7 @@ func TestLogIterator_HasNext(t *testing.T) {
filename = "test_log_iterator_has_next"
)
dir, _, cleanup := testutil.SetupFile(filename)
defer cleanup()
t.Cleanup(cleanup)
fileMgr := file.NewFileMgr(dir, blockSize)
block := file.NewBlockId(filename, 1)

Expand All @@ -76,7 +76,7 @@ func TestLogIterator_Next(t *testing.T) {
filename = "test_log_iterator_next_not_finished"
)
dir, _, cleanup := testutil.SetupFile(filename)
defer cleanup()
t.Cleanup(cleanup)
fileMgr := file.NewFileMgr(dir, blockSize)
block := file.NewBlockId(filename, 0)
page := file.NewPage(blockSize)
Expand All @@ -102,7 +102,7 @@ func TestLogIterator_Next(t *testing.T) {
filename = "test_log_iterator_next_block_finished"
)
dir, _, cleanup := testutil.SetupFile(filename)
defer cleanup()
t.Cleanup(cleanup)
fileMgr := file.NewFileMgr(dir, blockSize)
block0 := file.NewBlockId(filename, 0)
page0 := file.NewPage(blockSize)
Expand Down
12 changes: 6 additions & 6 deletions pkg/log/log_mgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestNewLogMgr(t *testing.T) {
blockSize = 4096
)
dir, _, cleanup := testutil.SetupFile(testFileName)
defer cleanup()
t.Cleanup(cleanup)
fileMgr := file.NewFileMgr(dir, blockSize)

lm, err := NewLogMgr(fileMgr, testFileName)
Expand All @@ -33,7 +33,7 @@ func TestNewLogMgr(t *testing.T) {
blockSize = 5
)
dir, f, cleanup := testutil.SetupFile(testFileName)
defer cleanup()
t.Cleanup(cleanup)
record := []byte("hello world!!")
_, err := f.Write(record)
f.Close()
Expand All @@ -58,7 +58,7 @@ func TestLogMgr_Append(t *testing.T) {
blockIdx = 0
)
dir, _, cleanup := testutil.SetupFile(testFileName)
defer cleanup()
t.Cleanup(cleanup)
page := file.NewPage(blockSize)
page.SetInt(0, blockSize)
fileMgr := file.NewFileMgr(dir, blockSize)
Expand All @@ -82,7 +82,7 @@ func TestLogMgr_Append(t *testing.T) {
blockIdx = 0
)
dir, _, cleanup := testutil.SetupFile(testFileName)
defer cleanup()
t.Cleanup(cleanup)
page := file.NewPage(blockSize)
page.SetInt(0, blockSize)
fileMgr := file.NewFileMgr(dir, blockSize)
Expand Down Expand Up @@ -110,7 +110,7 @@ func TestLogMgr_Flush(t *testing.T) {
blockSize = 10
)
dir, _, cleanup := testutil.SetupFile(testFileName)
defer cleanup()
t.Cleanup(cleanup)
fileMgr := file.NewFileMgr(dir, blockSize)
lm, err := NewLogMgr(fileMgr, testFileName)
assert.NoError(t, err)
Expand All @@ -131,7 +131,7 @@ func TestLogMgr_Flush(t *testing.T) {
blockSize = 10
)
dir, _, cleanup := testutil.SetupFile(testFileName)
defer cleanup()
t.Cleanup(cleanup)
fileMgr := file.NewFileMgr(dir, blockSize)
lm, err := NewLogMgr(fileMgr, testFileName)
assert.NoError(t, err)
Expand Down
2 changes: 2 additions & 0 deletions pkg/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ type TableMgr interface {
CreateTable(table string, schema record.Schema, tx tx.Transaction) error
GetLayout(table string, tx tx.Transaction) (record.Layout, error)
HasTable(table string, tx tx.Transaction) (bool, error)
TableCatalog() string
FieldCatalog() string
}

type ViewMgr interface {
Expand Down
4 changes: 2 additions & 2 deletions pkg/metadata/metadata_mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ type MetadataMgrImpl struct {
idxMgr IndexMgr
}

func NewMetadataMgr(tx tx.Transaction) (MetadataMgr, error) {
tm, err := NewTableMgr(tx)
func NewMetadataMgr(tx tx.Transaction, opts ...TableMgrOption) (MetadataMgr, error) {
tm, err := NewTableMgr(tx, opts...)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/metadata/stat_mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ func (sm *StatMgrImpl) refreshStatistics(tx tx.Transaction) error {
sm.tableStats = make(map[string]StatInfo, INIT_STAT_CAP)
sm.numCalls = 0

tableCatLayout, err := sm.tableMgr.GetLayout(tableTableCatalog, tx)
tableCatLayout, err := sm.tableMgr.GetLayout(sm.tableMgr.TableCatalog(), tx)
if err != nil {
return err
}
tcat, err := record.NewTableScan(tx, tableTableCatalog, tableCatLayout)
tcat, err := record.NewTableScan(tx, sm.tableMgr.TableCatalog(), tableCatLayout)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/metadata/stat_mgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestStatMgr(t *testing.T) {
blockSize = 1024
)
dir, _, cleanup := testutil.SetupFile(logFileName)
defer cleanup()
t.Cleanup(cleanup)
fileMgr := file.NewFileMgr(dir, blockSize)
logMgr, err := log.NewLogMgr(fileMgr, logFileName)
assert.NoError(t, err)
Expand Down
56 changes: 43 additions & 13 deletions pkg/metadata/table_mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

const (
tableFieldCatalog = "fldcat"
tableTableCatalog = "tblcat"
defaultTableFieldCatalog = "fldcat"
defaultTableTableCatalog = "tblcat"

fieldTableName = "tblname"
fieldSlotSize = "slotsize"
Expand All @@ -21,17 +21,39 @@ const (
)

type TableMgrImpl struct {
tableCatalog string
fieldCatalog string
tblCatLayout record.Layout
fldCatLayout record.Layout
mu sync.Mutex
}

type TableMgrOption func(*TableMgrImpl)

func WithTableTableCatalog(name string) TableMgrOption {
return func(tm *TableMgrImpl) {
tm.tableCatalog = name
}
}

func WithTableFieldCatalog(name string) TableMgrOption {
return func(tm *TableMgrImpl) {
tm.fieldCatalog = name
}
}

// MaxName is the maximum character length a tablename or fieldname can have.
const MAX_NAME_LENGTH = 16

// NewTableMgr creates a new catalog manager for the database system.
func NewTableMgr(tx tx.Transaction) (*TableMgrImpl, error) {
tm := &TableMgrImpl{}
func NewTableMgr(tx tx.Transaction, opts ...TableMgrOption) (*TableMgrImpl, error) {
tm := &TableMgrImpl{
tableCatalog: defaultTableTableCatalog,
fieldCatalog: defaultTableFieldCatalog,
}
for _, opt := range opts {
opt(tm)
}

var err error

Expand All @@ -47,10 +69,10 @@ func NewTableMgr(tx tx.Transaction) (*TableMgrImpl, error) {
return nil, fmt.Errorf("metadata: failed to create field catalog layout from schema: %w", err)
}

if err := tm.CreateTable(tableTableCatalog, tblCatSchema, tx); err != nil {
if err := tm.CreateTable(tm.tableCatalog, tblCatSchema, tx); err != nil {
return nil, err
}
if err := tm.CreateTable(tableFieldCatalog, fldCatSchema, tx); err != nil {
if err := tm.CreateTable(defaultTableFieldCatalog, fldCatSchema, tx); err != nil {
return nil, err
}
return tm, nil
Expand Down Expand Up @@ -100,7 +122,7 @@ func (tm *TableMgrImpl) CreateTable(tblname string, sch record.Schema, tx tx.Tra
}

func (tm *TableMgrImpl) HasTable(tblname string, tx tx.Transaction) (bool, error) {
tcat, err := record.NewTableScan(tx, tableTableCatalog, tm.tblCatLayout)
tcat, err := record.NewTableScan(tx, tm.tableCatalog, tm.tblCatLayout)
if err != nil {
return false, fmt.Errorf("metadata: failed to create table scan: %w", err)
}
Expand All @@ -118,7 +140,7 @@ func (tm *TableMgrImpl) HasTable(tblname string, tx tx.Transaction) (bool, error
}

func (tm *TableMgrImpl) addToTableCatalog(tblname string, slotSize int, tx tx.Transaction) error {
tcat, err := record.NewTableScan(tx, tableTableCatalog, tm.tblCatLayout)
tcat, err := record.NewTableScan(tx, tm.tableCatalog, tm.tblCatLayout)
if err != nil {
return fmt.Errorf("metadata: failed to create table scan: %w", err)
}
Expand All @@ -140,7 +162,7 @@ func (tm *TableMgrImpl) addToFieldCatalog(tblname string, schema record.Schema,
if err != nil {
return fmt.Errorf("metadata: failed to create layout from schema: %w", err)
}
fcat, err := record.NewTableScan(tx, tableFieldCatalog, tm.fldCatLayout)
fcat, err := record.NewTableScan(tx, tm.fieldCatalog, tm.fldCatLayout)
if err != nil {
return fmt.Errorf("metadata: failed to create table scan: %w", err)
}
Expand Down Expand Up @@ -179,7 +201,7 @@ func (tm *TableMgrImpl) addToFieldCatalog(tblname string, schema record.Schema,
func (tm *TableMgrImpl) DropTable(tblname string, tx tx.Transaction) error {
tm.mu.Lock()
defer tm.mu.Unlock()
tcat, err := record.NewTableScan(tx, tableTableCatalog, tm.tblCatLayout)
tcat, err := record.NewTableScan(tx, tm.tableCatalog, tm.tblCatLayout)
if err != nil {
return fmt.Errorf("metadata: failed to create table scan: %w", err)
}
Expand All @@ -198,7 +220,7 @@ func (tm *TableMgrImpl) DropTable(tblname string, tx tx.Transaction) error {
}
tcat.Close()

fcat, err := record.NewTableScan(tx, tableFieldCatalog, tm.fldCatLayout)
fcat, err := record.NewTableScan(tx, tm.fieldCatalog, tm.fldCatLayout)
if err != nil {
return fmt.Errorf("metadata: failed to create table scan: %w", err)
}
Expand Down Expand Up @@ -233,7 +255,7 @@ func (tm *TableMgrImpl) GetLayout(tblname string, tx tx.Transaction) (record.Lay
}

func (tm *TableMgrImpl) getTableSlotSize(tblname string, tx tx.Transaction) (int, error) {
tcat, err := record.NewTableScan(tx, tableTableCatalog, tm.tblCatLayout)
tcat, err := record.NewTableScan(tx, tm.tableCatalog, tm.tblCatLayout)
if err != nil {
return 0, fmt.Errorf("metadata: failed to create table scan: %w", err)
}
Expand All @@ -254,7 +276,7 @@ func (tm *TableMgrImpl) getTableSlotSize(tblname string, tx tx.Transaction) (int
func (tm *TableMgrImpl) getTableSchemaOffset(tblName string, tx tx.Transaction) (record.Schema, map[string]int, error) {
sch := record.NewSchema()
offsets := make(map[string]int)
fcat, err := record.NewTableScan(tx, tableFieldCatalog, tm.fldCatLayout)
fcat, err := record.NewTableScan(tx, defaultTableFieldCatalog, tm.fldCatLayout)
if err != nil {
return nil, nil, fmt.Errorf("metadata: failed to create table scan: %w", err)
}
Expand Down Expand Up @@ -288,3 +310,11 @@ func (tm *TableMgrImpl) getTableSchemaOffset(tblName string, tx tx.Transaction)
}
return sch, offsets, nil
}

func (tm *TableMgrImpl) TableCatalog() string {
return tm.tableCatalog
}

func (tm *TableMgrImpl) FieldCatalog() string {
return tm.fieldCatalog
}
Loading
Loading