github.com/attic-labs/noms@v0.0.0-20210827224422-e5fa29d95e8b/go/chunks/put_cache.go (about) 1 // Copyright 2016 Attic Labs, Inc. All rights reserved. 2 // Licensed under the Apache License, version 2.0: 3 // http://www.apache.org/licenses/LICENSE-2.0 4 5 package chunks 6 7 import ( 8 "sync" 9 10 "github.com/attic-labs/noms/go/hash" 11 ) 12 13 func newUnwrittenPutCache() *unwrittenPutCache { 14 return &unwrittenPutCache{map[hash.Hash]Chunk{}, &sync.Mutex{}} 15 } 16 17 type unwrittenPutCache struct { 18 unwrittenPuts map[hash.Hash]Chunk 19 mu *sync.Mutex 20 } 21 22 func (p *unwrittenPutCache) Add(c Chunk) bool { 23 p.mu.Lock() 24 defer p.mu.Unlock() 25 if _, ok := p.unwrittenPuts[c.Hash()]; !ok { 26 p.unwrittenPuts[c.Hash()] = c 27 return true 28 } 29 30 return false 31 } 32 33 func (p *unwrittenPutCache) Has(c Chunk) (has bool) { 34 p.mu.Lock() 35 defer p.mu.Unlock() 36 _, has = p.unwrittenPuts[c.Hash()] 37 return 38 } 39 40 func (p *unwrittenPutCache) Get(r hash.Hash) Chunk { 41 p.mu.Lock() 42 defer p.mu.Unlock() 43 if c, ok := p.unwrittenPuts[r]; ok { 44 return c 45 } 46 return EmptyChunk 47 } 48 49 func (p *unwrittenPutCache) Clear(chunks []Chunk) { 50 p.mu.Lock() 51 defer p.mu.Unlock() 52 for _, c := range chunks { 53 delete(p.unwrittenPuts, c.Hash()) 54 } 55 }