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

Support SSTable #1

Merged
merged 2 commits into from
Jan 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
4 changes: 2 additions & 2 deletions dbms/dbms_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package dbms

import (
filesystem "sstable/filesystem"
mockfilesystem "sstable/filesystem/mock"
"sstable/filesystem"
"sstable/storage"
"sstable/test/util/mockfilesystem"
)

func createDummyDbmsWithDirectory(rootDirectory filesystem.DirectoryOperation, metadata *DatabaseMetadata) *DatabaseManagementSystem {
Expand Down
2 changes: 1 addition & 1 deletion dbms/memtable.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func generateMemtableName() string {
func (dbms *DatabaseManagementSystem) loadMemtableFromFile(memtableFile filesystem.FileOperation) error {
memtable := memtable.NewMemoryTable(memtableFile)
if err := memtable.LoadMemoryTable(); err == nil {
dbms.memoryTable.Store(&memtable)
dbms.memoryTable.Store(memtable)
return nil
} else {
return err
Expand Down
4 changes: 2 additions & 2 deletions dbms/memtable_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dbms

import (
filesystem "sstable/filesystem/mock"
"sstable/test/util/mockfilesystem"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -22,7 +22,7 @@ func TestMemtableEmpty(t *testing.T) {

func TestMemtableLoad(t *testing.T) {
//arrange
rootDirectory := filesystem.NewDummyDirectory()
rootDirectory := mockfilesystem.NewDummyDirectory()
key, value := "k", "v"

//act
Expand Down
2 changes: 2 additions & 0 deletions filesystem/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ type FileOperation interface {
Close() error
AppendBytes(bytes []byte) error
ReadAll() ([]byte, error)
ReadAt(bytes []byte, offset int) (int, error)
Size() (int, error)
}
33 changes: 0 additions & 33 deletions filesystem/mock/file_mock.go

This file was deleted.

67 changes: 0 additions & 67 deletions filesystem/mock/filesystem_mock_test.go

This file was deleted.

30 changes: 25 additions & 5 deletions memtable/memtable.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,26 @@ import (
"strings"
)

type MemoryTableIO interface {
Read(key string) any
Write(key string, value any) error
}

type MemoryTableLowLevel interface {
GetRecords() map[string]any
IsLoaded() bool
LoadMemoryTable() error
}

type MemoryTable struct {
file filesystem.FileOperation
records map[string]any
file filesystem.FileOperation
records map[string]any
isLoaded bool
}

func NewMemoryTable(file filesystem.FileOperation) MemoryTable {
func NewMemoryTable(file filesystem.FileOperation) *MemoryTable {
records := make(map[string]any)
return MemoryTable{file: file, records: records}
return &MemoryTable{file: file, records: records}
}

func (memtable *MemoryTable) Read(key string) any {
Expand Down Expand Up @@ -55,9 +67,18 @@ func (memtable *MemoryTable) LoadMemoryTable() error {
content := string(bytes)
memtable.enrichRecordsFromContent(content)

memtable.isLoaded = true
return nil
}

func (memtable *MemoryTable) IsLoaded() bool {
return memtable.isLoaded
}

func (memtable *MemoryTable) GetRecords() map[string]any {
return memtable.records
}

func (memtable *MemoryTable) enrichRecordsFromContent(content string) {
lines := strings.Split(content, "\n")
for _, line := range lines {
Expand All @@ -67,5 +88,4 @@ func (memtable *MemoryTable) enrichRecordsFromContent(content string) {
memtable.records[keyValue.Key] = keyValue.Value
}
}

}
45 changes: 6 additions & 39 deletions memtable/memtable_rw_test.go → memtable/test/memtable_test.go
Original file line number Diff line number Diff line change
@@ -1,51 +1,18 @@
package memtable
package testmemtable

import (
"encoding/json"
"path"
filesystem "sstable/filesystem"
mockfilesystem "sstable/filesystem/mock"

"sstable/test/util/mockmemtable"
"sstable/util"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

const MEMTABLE_TEST_DATA_FOLDER = "memtable"
const BASIC_TEST_DATA = "basic_1.log"

func createDummyFile(dataFileName string) filesystem.FileOperation {
fullPath := path.Join(MEMTABLE_TEST_DATA_FOLDER, dataFileName)
var fileOperation filesystem.FileOperation = mockfilesystem.NewDummyFileFromAnotherFile(fullPath)
return fileOperation
}

func createEmptyFile() filesystem.FileOperation {
var fileOperation filesystem.FileOperation = mockfilesystem.NewDummyFile("")
return fileOperation
}

func createDummyMemoryTable(dataFileName string) MemoryTable {
dummyFile := createDummyFile(dataFileName)
memtableInstance := NewMemoryTable(dummyFile)
return memtableInstance
}

func createReadyBasicDummyMemoryTable() MemoryTable {
memoryTable := createDummyMemoryTable(BASIC_TEST_DATA)
memoryTable.LoadMemoryTable()
return memoryTable
}

func createReadyEmptyDummyMemoryTable() (MemoryTable, filesystem.FileOperation) {
fileOperation := createEmptyFile()
memoryTable := NewMemoryTable(fileOperation)
memoryTable.LoadMemoryTable()
return memoryTable, fileOperation
}

func getKeyValueJson(key string, value any) string {

keyValue := util.KeyValueObject{Key: key, Value: value}
Expand All @@ -63,7 +30,7 @@ func TestMemtableReadInt(t *testing.T) {
expectValue := 14

//act
sut := createReadyBasicDummyMemoryTable()
sut := mockmemtable.NewReadyBasicMemtable()
result := sut.Read("score#deea")

//assert
Expand All @@ -77,7 +44,7 @@ func TestMemtableReadString(t *testing.T) {
expectValue := "deeax99"

//act
sut := createReadyBasicDummyMemoryTable()
sut := mockmemtable.NewReadyBasicMemtable()
result := sut.Read("nickname#deea")

//assert
Expand All @@ -95,7 +62,7 @@ func TestMemtableReadObject(t *testing.T) {
}

//act
sut := createReadyBasicDummyMemoryTable()
sut := mockmemtable.NewReadyBasicMemtable()
result := sut.Read("profile#deea")

//assert
Expand All @@ -104,7 +71,7 @@ func TestMemtableReadObject(t *testing.T) {

func TestMemtableWrite(t *testing.T) {
//arrange
memoryTable, dummyFile := createReadyEmptyDummyMemoryTable()
memoryTable, dummyFile := mockmemtable.NewReadyEmptyMemtable()
key := "user_score#deeax99"
value := 10.0
expectedJson := getKeyValueJson(key, value)
Expand Down
Loading