Files
dolt/datas/commit_cache.go
Aaron Boodman 39084cc0be Disambiguate the term "root".
datas.Root(Set) -> datas.Commit(Set)
DataStore::Roots() -> DataStore::Heads()
2015-07-22 14:43:36 -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 CommitSet) {
if currentCommits.Len() == 0 {
return
}
commitsRef := currentCommits.Ref()
if _, ok := cache.refs[commitsRef]; ok {
return
}
commitSet := CommitSet{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),
}
}