Files
dolt/datas/commit_cache.go
2015-07-28 16:01:18 -07:00

57 lines
1.2 KiB
Go

package datas
import (
"github.com/attic-labs/noms/chunks"
"github.com/attic-labs/noms/ref"
"github.com/attic-labs/noms/types"
)
// commitCache maintains an in-memory cache of all known commits.
type commitCache struct {
source chunks.ChunkSource
refs map[ref.Ref]bool
}
func (cache *commitCache) updateFromCommit(commit Commit) {
if _, ok := cache.refs[commit.Ref()]; ok {
return
}
parents := commit.Parents()
parents.Iter(func(commit types.Value) (stop bool) {
cache.updateFromCommit(CommitFromVal(commit))
return
})
cache.refs[commit.Ref()] = true
}
func (cache *commitCache) Update(currentCommits SetOfCommit) {
if currentCommits.Len() == 0 {
return
}
commitsRef := currentCommits.Ref()
if _, ok := cache.refs[commitsRef]; ok {
return
}
commitSet := SetOfCommit{types.MustReadValue(commitsRef, cache.source).(types.Set)}
commitSet.Iter(func(commit Commit) (stop bool) {
cache.updateFromCommit(commit)
return
})
cache.refs[commitsRef] = true
}
func (cache *commitCache) Contains(candidate ref.Ref) bool {
_, ok := cache.refs[candidate]
return ok
}
func newCommitCache(source chunks.ChunkSource) *commitCache {
return &commitCache{
source,
make(map[ref.Ref]bool),
}
}