Skip to content

Commit

Permalink
Reduce memory consumption
Browse files Browse the repository at this point in the history
  • Loading branch information
firelizzard18 committed Jun 25, 2024
1 parent 180f3f8 commit c4adbfd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
19 changes: 10 additions & 9 deletions pkg/database/keyvalue/block/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ var poolBuffer = binary.NewPointerPool[bytes.Buffer]()

type Database struct {
config
commitMu sync.Mutex
records *recordFileSet
indexFiles *indexFileTree
index recordIndex
commitMu sync.Mutex
records *recordFileSet
indexFiles *indexFileTree
blockIndex vmap[blockID, int]
recordLocation vmap[[32]byte, *recordLocation]
}

type config struct {
Expand Down Expand Up @@ -88,8 +89,8 @@ func Open(path string, options ...Option) (_ *Database, err error) {
return nil, err
}

db.index.records.fn.forEach = db.indexFiles.ForEach
db.index.records.fn.commit = db.indexFiles.Commit
db.recordLocation.fn.forEach = db.indexFiles.ForEach
db.recordLocation.fn.commit = db.indexFiles.Commit

// Determine the next block number
if len(db.records.files) > 0 {
Expand All @@ -109,7 +110,7 @@ func Open(path string, options ...Option) (_ *Database, err error) {
}

// Index blocks
err = db.index.indexBlocks(db.records)
err = db.indexBlocks()
if err != nil {
return nil, err
}
Expand All @@ -135,8 +136,8 @@ func (d *Database) Begin(prefix *record.Key, writable bool) keyvalue.ChangeSet {
view := &databaseView{
recordFiles: d.records,
indexFiles: d.indexFiles,
blocks: d.index.blocks.View(),
records: d.index.records.View(),
blocks: d.blockIndex.View(),
records: d.recordLocation.View(),
}

get := func(key *record.Key) ([]byte, error) {
Expand Down
23 changes: 14 additions & 9 deletions pkg/database/keyvalue/block/database_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,16 @@ import (
"golang.org/x/exp/slog"
)

type recordIndex struct {
blocks vmap[blockID, int]
records vmap[[32]byte, *recordLocation]
}

type databaseView struct {
recordFiles *recordFileSet
indexFiles *indexFileTree
blocks *vmapView[blockID, int]
records *vmapView[[32]byte, *recordLocation]
}

func (r *recordIndex) indexBlocks(records *recordFileSet) error {
blocks := r.blocks.View()
it := records.entries(func(typ entryType) bool {
func (db *Database) indexBlocks() error {
blocks := db.blockIndex.View()
it := db.records.entries(func(typ entryType) bool {
return typ == entryTypeStartBlock
})
var prev *recordFile
Expand Down Expand Up @@ -62,7 +57,7 @@ func (db *Database) indexRecords() error {
return nil
}

records := db.index.records.View()
records := db.recordLocation.View()
it := db.records.entries(nil)

var prev *recordFile
Expand All @@ -81,6 +76,7 @@ func (db *Database) indexRecords() error {
return false
}
block = &e.blockID
slog.Info("Indexing block", "file", filepath.Base(item.File.file.Name()), "block", block, "module", "database")

case *endBlockEntry:
if block == nil {
Expand All @@ -89,6 +85,15 @@ func (db *Database) indexRecords() error {
}
block = nil

// Commit at the end of each block to keep the memory usage under
// control for large databases
err := records.Commit()
if err != nil {
it.err = err
return false
}
records = db.recordLocation.View()

case *recordEntry:
if block == nil {
it.err = fmt.Errorf("%v is corrupted", filepath.Base(item.File.file.Name()))
Expand Down

0 comments on commit c4adbfd

Please sign in to comment.