Files
dolt/chunks/put_cache.go
Chris Masone 40efa266bd Factor Put-cache out of HTTPStore and DynamoStore
There was some copypasta. Now it's extracted into its own struct
that provides the same API and semantics.
2016-02-24 13:51:53 -08:00

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())
}
}