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

Implement Mem() and SetMemoryFootprintChangeHook() for PipelinedMemDB #1233

Merged
merged 1 commit into from
Mar 18, 2024
Merged
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: 24 additions & 8 deletions internal/unionstore/pipelined_memdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type PipelinedMemDB struct {
// Some([...]) -> put
// Some([]) -> delete
batchGetCache map[string]util.Option[[]byte]
memChangeHook func(uint64)
}

const (
Expand Down Expand Up @@ -237,28 +238,36 @@ func (p *PipelinedMemDB) UpdateFlags(k []byte, ops ...kv.FlagsOp) {
func (p *PipelinedMemDB) Set(key, value []byte) error {
p.Lock()
defer p.Unlock()
return p.memDB.Set(key, value)
err := p.memDB.Set(key, value)
p.onMemChange()
return err
}

// SetWithFlags sets the value for key k in the MemBuffer with flags.
func (p *PipelinedMemDB) SetWithFlags(key, value []byte, ops ...kv.FlagsOp) error {
p.Lock()
defer p.Unlock()
return p.memDB.SetWithFlags(key, value, ops...)
err := p.memDB.SetWithFlags(key, value, ops...)
p.onMemChange()
return err
}

// Delete deletes the key k in the MemBuffer.
func (p *PipelinedMemDB) Delete(key []byte) error {
p.Lock()
defer p.Unlock()
return p.memDB.Delete(key)
err := p.memDB.Delete(key)
p.onMemChange()
return err
}

// DeleteWithFlags deletes the key k in the MemBuffer with flags.
func (p *PipelinedMemDB) DeleteWithFlags(key []byte, ops ...kv.FlagsOp) error {
p.Lock()
defer p.Unlock()
return p.memDB.DeleteWithFlags(key, ops...)
err := p.memDB.DeleteWithFlags(key, ops...)
p.onMemChange()
return err
}

// Flush is called during execution of a transaction, it does flush when there are enough keys and the ongoing flushingMemDB is done.
Expand Down Expand Up @@ -303,6 +312,7 @@ func (p *PipelinedMemDB) Flush(force bool) (bool, error) {
// this guarantees the onFlushing.Store(true) in another goroutine's Flush happens after onFlushing.Store(false) in this function.
p.errCh <- err
}(p.generation)
p.onMemChange()
return true, nil
}

Expand Down Expand Up @@ -383,13 +393,19 @@ func (p *PipelinedMemDB) OnFlushing() bool {
}

// SetMemoryFootprintChangeHook sets the hook for memory footprint change.
// TODO: implement this.
func (p *PipelinedMemDB) SetMemoryFootprintChangeHook(hook func(uint64)) {}
func (p *PipelinedMemDB) SetMemoryFootprintChangeHook(hook func(uint64)) {
p.memChangeHook = hook
}

func (p *PipelinedMemDB) onMemChange() {
if p.memChangeHook != nil {
p.memChangeHook(p.Mem())
}
}

// Mem returns the memory usage of MemBuffer.
// TODO: implement this.
func (p *PipelinedMemDB) Mem() uint64 {
return 0
return p.memDB.Mem() + p.flushingMemDB.Mem()
}

type errIterator struct {
Expand Down
Loading