Skip to content

Commit

Permalink
Add comments and change addTables signature
Browse files Browse the repository at this point in the history
  • Loading branch information
ashish-goswami committed Nov 26, 2019
1 parent 66124f5 commit 8e2b5da
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
10 changes: 7 additions & 3 deletions level_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,12 @@ func (s *levelHandler) replaceTables(toDel, toAdd []*table.Table) error {
return decrRefs(toDel)
}

func (s *levelHandler) addTables(toAdd []*table.Table) error {
// addTable adds toAdd tables to levelHandler. Normally when we add tables to levelHandler, we sort
// tables based on table.Smallest. This is required for correctness of the system. But in some cases
// this can be avoided(such as stream writer). We can just add tables to levelHandler's table list
// and after all addTables calls, we can sort table list(check sortTable method).
// NOTE: addTables and sortTables duplicate some code from replaceTables().
func (s *levelHandler) addTables(toAdd []*table.Table) {
s.Lock()
defer s.Unlock()

Expand All @@ -150,10 +155,9 @@ func (s *levelHandler) addTables(toAdd []*table.Table) error {
t.IncrRef()
s.tables = append(s.tables, t)
}

return nil
}

// sortTables sorts tables of levelHandler based on table.Smallest.
func (s *levelHandler) sortTables() {
s.RLock()
defer s.RUnlock()
Expand Down
8 changes: 5 additions & 3 deletions stream_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,9 +454,11 @@ func (w *sortedWriter) createTable(builder *table.Builder) error {
if err := w.db.manifest.addChanges([]*pb.ManifestChange{change}); err != nil {
return err
}
if err := lhandler.addTables([]*table.Table{tbl}); err != nil {
return err
}

// We are not calling lhandler.replaceTables() here, as it sorts tables on every addition.
// We can sort all tables only once during Flush() call.
lhandler.addTables([]*table.Table{tbl})

// Release the ref held by OpenTable.
_ = tbl.DecrRef()
w.db.opt.Infof("Table created: %d at level: %d for stream: %d. Size: %s\n",
Expand Down

0 comments on commit 8e2b5da

Please sign in to comment.