mirror of
https://github.com/dolthub/dolt.git
synced 2026-02-11 10:33:08 -06:00
There was some copypasta. Now it's extracted into its own struct that provides the same API and semantics.
45 lines
787 B
Go
45 lines
787 B
Go
package chunks
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"github.com/attic-labs/noms/ref"
|
|
)
|
|
|
|
func newUnwrittenPutCache() *unwrittenPutCache {
|
|
return &unwrittenPutCache{map[ref.Ref]Chunk{}, &sync.Mutex{}}
|
|
}
|
|
|
|
type unwrittenPutCache struct {
|
|
unwrittenPuts map[ref.Ref]Chunk
|
|
mu *sync.Mutex
|
|
}
|
|
|
|
func (p *unwrittenPutCache) Add(c Chunk) bool {
|
|
p.mu.Lock()
|
|
defer p.mu.Unlock()
|
|
if _, ok := p.unwrittenPuts[c.Ref()]; !ok {
|
|
p.unwrittenPuts[c.Ref()] = c
|
|
return true
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func (p *unwrittenPutCache) Get(r ref.Ref) Chunk {
|
|
p.mu.Lock()
|
|
defer p.mu.Unlock()
|
|
if c, ok := p.unwrittenPuts[r]; ok {
|
|
return c
|
|
}
|
|
return EmptyChunk
|
|
}
|
|
|
|
func (p *unwrittenPutCache) Clear(chunks []Chunk) {
|
|
p.mu.Lock()
|
|
defer p.mu.Unlock()
|
|
for _, c := range chunks {
|
|
delete(p.unwrittenPuts, c.Ref())
|
|
}
|
|
}
|