Files
dolt/types/future.go
Erik Arvidsson 1d13a878c4 Fix types.Ref and RefKind objects
Ref Values now have a TargetRef() method that returns the ref.Ref of
the target the Value is referencing.

Note: This is a breaking change. In old code the Ref() of the Value was
the Ref of the underlying target.

Fixes #464
2015-10-26 11:18:02 -04:00

71 lines
1.6 KiB
Go

package types
import (
"github.com/attic-labs/noms/chunks"
"github.com/attic-labs/noms/d"
"github.com/attic-labs/noms/ref"
)
// Future encapsulates a Value which may or may not be available yet.
type Future interface {
// Returns the Ref of the value without fetching it.
Ref() ref.Ref
// Returns the Value if we already have it, nil otherwise.
Val() Value
// Fetch the Future value if necessary, then return it. Multiple calls to deref only result in one fetch.
Deref(cs chunks.ChunkSource) Value
// BUG 141. The lazy loading api is clearly not working.
Release()
}
func futuresEqual(f1, f2 Future) bool {
// If we already have both values, then use their Equals() methods since for primitives it is faster than computing a reference.
if f1.Val() != nil && f2.Val() != nil {
return f1.Val().Equals(f2.Val())
} else {
return f1.Ref() == f2.Ref()
}
}
func futureEqualsValue(f Future, v Value) bool {
d.Chk.NotNil(v)
if f.Val() != nil {
return f.Val().Equals(v)
} else {
return f.Ref() == v.Ref()
}
}
func futureFromValue(v Value) Future {
return resolvedFuture{v}
}
type targetRef interface {
TargetRef() ref.Ref
}
func appendChunks(chunks []Future, f Future) []Future {
if uf, ok := f.(*unresolvedFuture); ok {
chunks = append(chunks, uf)
} else if f != nil {
v := f.Val()
if v != nil {
if v.TypeRef().Kind() == RefKind {
chunks = append(chunks, futureFromRef(v.(targetRef).TargetRef()))
}
}
}
return chunks
}
func appendValueToChunks(chunks []Future, v Value) []Future {
if v.TypeRef().Kind() == RefKind {
chunks = append(chunks, futureFromRef(v.(targetRef).TargetRef()))
}
return chunks
}