From cb1103db8bfd76b9063efd51a682c2bff2c12a6c Mon Sep 17 00:00:00 2001 From: ashish Date: Thu, 28 Nov 2019 13:11:44 +0530 Subject: [PATCH] Address review comments --- level_handler.go | 22 ++++++++++------------ stream_writer.go | 2 +- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/level_handler.go b/level_handler.go index 30d889ff8..dbc2532ba 100644 --- a/level_handler.go +++ b/level_handler.go @@ -140,24 +140,22 @@ func (s *levelHandler) replaceTables(toDel, toAdd []*table.Table) error { return decrRefs(toDel) } -// 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) { +// addTable adds toAdd table 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 case of +// stream writer this can be avoided. We can just add tables to levelHandler's table list +// and after all addTable calls, we can sort table list(check sortTable method). +// NOTE: levelHandler.sortTables() should be called after call addTable calls are done. +func (s *levelHandler) addTable(t *table.Table) { s.Lock() defer s.Unlock() - // Increase totalSize first. - for _, t := range toAdd { - s.totalSize += t.Size() - t.IncrRef() - s.tables = append(s.tables, t) - } + s.totalSize += t.Size() // Increase totalSize first. + t.IncrRef() + s.tables = append(s.tables, t) } // sortTables sorts tables of levelHandler based on table.Smallest. +// Normally it should be called after all addTable calls. func (s *levelHandler) sortTables() { s.RLock() defer s.RUnlock() diff --git a/stream_writer.go b/stream_writer.go index ea812ce7b..487468290 100644 --- a/stream_writer.go +++ b/stream_writer.go @@ -457,7 +457,7 @@ func (w *sortedWriter) createTable(builder *table.Builder) error { // 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}) + lhandler.addTable(tbl) // Release the ref held by OpenTable. _ = tbl.DecrRef()