Skip to content

Commit

Permalink
refactor dir
Browse files Browse the repository at this point in the history
  • Loading branch information
sjcsjc123 committed Aug 19, 2023
1 parent d825835 commit 04a53ff
Show file tree
Hide file tree
Showing 13 changed files with 211 additions and 319 deletions.
11 changes: 11 additions & 0 deletions config/column_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package config

type ColumnOptions struct {
Option Options
LogNum uint32
FileSize int64
SaveTime int64
MemSize int64
TotalMemSize int64
ColumnName string
}
11 changes: 11 additions & 0 deletions config/db_memory_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package config

type DbMemoryOptions struct {
Option Options
LogNum uint32
FileSize int64
SaveTime int64
MemSize int64
TotalMemSize int64
ColumnName string
}
8 changes: 8 additions & 0 deletions config/wal_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package config

type WalConfig struct {
DirPath string
FileSize int64
SaveTime int64
LogNum uint32
}
66 changes: 45 additions & 21 deletions db/column/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,13 @@ import (
"fmt"
"github.com/ByteStorage/FlyDB/config"
"github.com/ByteStorage/FlyDB/db/memory"
"github.com/ByteStorage/FlyDB/lib/wal"
"io/ioutil"
"os"
"strings"
"sync"
)

type Options struct {
Option config.Options
LogNum uint32
FileSize int64
SaveTime int64
MemSize int64
TotalMemSize int64
ColumnName string
Wal *memory.Wal
}

// Column is a column family
type Column interface {
// CreateColumnFamily create column family
Expand All @@ -41,9 +31,15 @@ type Column interface {
}

// NewColumn create a column family
func NewColumn(option Options) (Column, error) {
func NewColumn(option config.ColumnOptions) (Column, error) {
// create wal, all column family share a wal
wal, err := memory.NewWal(option)
walConf := config.WalConfig{
DirPath: option.Option.DirPath,
LogNum: option.LogNum,
SaveTime: option.SaveTime,
FileSize: option.FileSize,
}
w, err := wal.NewWal(walConf)
if err != nil {
return nil, err
}
Expand All @@ -64,7 +60,7 @@ func NewColumn(option Options) (Column, error) {
option: option,
mux: sync.RWMutex{},
columnFamily: columnFamily,
wal: wal,
wal: w,
}, nil
}

Expand All @@ -73,8 +69,18 @@ func NewColumn(option Options) (Column, error) {
option.ColumnName = "default"
}

options := config.DbMemoryOptions{
Option: option.Option,
LogNum: option.LogNum,
FileSize: option.FileSize,
SaveTime: option.SaveTime,
MemSize: option.MemSize,
TotalMemSize: option.TotalMemSize,
ColumnName: option.ColumnName,
}

// create a new db
db, err := memory.NewDB(option)
db, err := memory.NewDB(options)
if err != nil {
return nil, err
}
Expand All @@ -84,15 +90,15 @@ func NewColumn(option Options) (Column, error) {
columnFamily: map[string]*memory.Db{
option.ColumnName: db,
},
wal: wal,
wal: w,
}, nil
}

type column struct {
mux sync.RWMutex
wal *memory.Wal
wal *wal.Wal
columnFamily map[string]*memory.Db
option Options
option config.ColumnOptions
}

func (c *column) CreateColumnFamily(name string) error {
Expand All @@ -101,7 +107,16 @@ func (c *column) CreateColumnFamily(name string) error {
if _, ok := c.columnFamily[name]; ok {
return errors.New("column family already exists")
}
db, err := memory.NewDB(c.option)
options := config.DbMemoryOptions{
Option: c.option.Option,
LogNum: c.option.LogNum,
FileSize: c.option.FileSize,
SaveTime: c.option.SaveTime,
MemSize: c.option.MemSize,
TotalMemSize: c.option.TotalMemSize,
ColumnName: c.option.ColumnName,
}
db, err := memory.NewDB(options)
if err != nil {
return err
}
Expand Down Expand Up @@ -145,7 +160,7 @@ func (c *column) Keys(cf string) ([][]byte, error) {
return c.columnFamily[cf].Keys()
}

func loadColumn(option Options) (map[string]*memory.Db, error) {
func loadColumn(option config.ColumnOptions) (map[string]*memory.Db, error) {
base := option.Option.DirPath
base = strings.Trim(base, "/")
// Check if the base path exists
Expand All @@ -163,7 +178,16 @@ func loadColumn(option Options) (map[string]*memory.Db, error) {
colName := dir.Name()
dirPath := base + "/" + colName
option.Option.DirPath = dirPath
db, err := memory.NewDB(option)
options := config.DbMemoryOptions{
Option: option.Option,
LogNum: option.LogNum,
FileSize: option.FileSize,
SaveTime: option.SaveTime,
MemSize: option.MemSize,
TotalMemSize: option.TotalMemSize,
ColumnName: option.ColumnName,
}
db, err := memory.NewDB(options)
if err != nil {
return nil, err
}
Expand Down
21 changes: 16 additions & 5 deletions db/memory/db.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,51 @@
package memory

import (
"github.com/ByteStorage/FlyDB/db/column"
"github.com/ByteStorage/FlyDB/config"
"github.com/ByteStorage/FlyDB/db/engine"
"github.com/ByteStorage/FlyDB/lib/wal"
"sync"
)

type Db struct {
option column.Options
option config.DbMemoryOptions
db *engine.DB
mem *MemTable
oldList []*MemTable
wal *Wal
wal *wal.Wal
oldListChan chan *MemTable
totalSize int64
activeSize int64
pool *sync.Pool
errMsgCh chan []byte
}

func NewDB(option column.Options) (*Db, error) {
func NewDB(option config.DbMemoryOptions) (*Db, error) {
mem := NewMemTable()
option.Option.DirPath = option.Option.DirPath + "/" + option.ColumnName
db, err := engine.NewDB(option.Option)
if err != nil {
return nil, err
}
walConfig := config.WalConfig{
DirPath: option.Option.DirPath,
LogNum: option.LogNum,
FileSize: option.FileSize,
SaveTime: option.SaveTime,
}
newWal, err := wal.NewWal(walConfig)
if err != nil {
return nil, err
}
d := &Db{
mem: mem,
db: db,
wal: option.Wal,
option: option,
oldList: make([]*MemTable, 0),
oldListChan: make(chan *MemTable, 1000000),
activeSize: 0,
totalSize: 0,
wal: newWal,
pool: &sync.Pool{New: func() interface{} { return make([]byte, 0, 1024) }},
}
go d.async()
Expand Down
19 changes: 6 additions & 13 deletions db/memory/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package memory
import (
"fmt"
"github.com/ByteStorage/FlyDB/config"
"github.com/ByteStorage/FlyDB/db/column"
"github.com/ByteStorage/FlyDB/lib/randkv"
"github.com/stretchr/testify/assert"
"os"
Expand All @@ -16,19 +15,13 @@ func TestPutAndGet(t *testing.T) {
dir, _ := os.MkdirTemp("", "flydb-benchmark")
opts.DirPath = dir
opts.DataFileSize = 64 * 1024 * 1024

options := column.Options{
Option: opts,
LogNum: 100,
SaveTime: 100 * 1000,
FileSize: 100 * 1024 * 1024,
MemSize: 2 * 1024 * 1024 * 1024,
TotalMemSize: 10 * 1024 * 1024 * 1024,
memoryOptions := config.DbMemoryOptions{
Option: opts,
LogNum: 100,
FileSize: 100 * 1024 * 1024,
SaveTime: 100 * 1000,
}
wal, err := NewWal(options)
assert.Nil(t, err)
options.Wal = wal
db, err := NewDB(options)
db, err := NewDB(memoryOptions)
defer db.Clean()
assert.Nil(t, err)
assert.NotNil(t, db)
Expand Down
113 changes: 0 additions & 113 deletions db/memory/wal.go

This file was deleted.

Loading

0 comments on commit 04a53ff

Please sign in to comment.