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

do not write L0 table to disk #37

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
32 changes: 16 additions & 16 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,20 @@ func writeLevel0Table(s *skl.Skiplist, f *os.File) error {
return writeToFile(f, b.Finish())
}

// WriteLevel0Table flushes memtable. It drops deleteValues.
func writeLevel0TableToMem(s *skl.Skiplist) ([]byte, error) {
iter := s.NewIterator()
defer iter.Close()
b := table.NewTableBuilder(s.MemSize())
defer b.Close()
for iter.SeekToFirst(); iter.Valid(); iter.Next() {
if err := b.Add(iter.Key(), iter.Value()); err != nil {
return nil, err
}
}
return b.Finish(), nil
}

func writeToFile(f *os.File, buf []byte) error {
const step = 512 * 1024
up := step
Expand Down Expand Up @@ -809,26 +823,12 @@ func (db *DB) flushMemtable(lc *y.Closer) error {
if err != nil {
return y.Wrap(err)
}

// Don't block just to sync the directory entry.
dirSyncCh := make(chan error)
go func() { dirSyncCh <- syncDir(db.opt.Dir) }()

err = writeLevel0Table(ft.mt, fd)
dirSyncErr := <-dirSyncCh

data, err := writeLevel0TableToMem(ft.mt)
if err != nil {
log.Errorf("ERROR while writing to level 0: %v", err)
return err
}
if dirSyncErr != nil {
log.Errorf("ERROR while syncing level directory: %v", dirSyncErr)
return err
}

tbl, err := table.OpenTable(fd, db.opt.TableLoadingMode)
tbl, err := table.OpenInMemoryTable(fd, data)
if err != nil {
log.Infof("ERROR while opening table: %v", err)
return err
}
// We own a ref on tbl.
Expand Down
25 changes: 24 additions & 1 deletion table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,30 @@ func OpenTable(fd *os.File, loadingMode options.FileLoadingMode) (*Table, error)
}
}

t.init()
return t, nil
}

func OpenInMemoryTable(fd *os.File, data []byte) (*Table, error) {
filename := fd.Name()
id, ok := ParseFileID(filename)
if !ok {
_ = fd.Close()
return nil, errors.Errorf("Invalid filename: %s", filename)
}
t := &Table{
fd: fd,
ref: 1, // Caller is given one reference.
id: id,
loadingMode: options.LoadToRAM,
mmap: data,
tableSize: len(data),
}
t.init()
return t, nil
}

func (t *Table) init() {
t.readIndex()

it := t.NewIterator(false)
Expand All @@ -150,7 +174,6 @@ func OpenTable(fd *os.File, loadingMode options.FileLoadingMode) (*Table, error)
if it2.Valid() {
t.biggest = it2.Key()
}
return t, nil
}

// Close closes the open table. (Releases resources back to the OS.)
Expand Down