mirror of
https://github.com/dolthub/dolt.git
synced 2026-02-05 10:31:30 -06:00
Previously we had flatSet vs chunkedSet, each implementing the Set interface, and SetBuilder dealing with this Set interface. Now we have a chunkedSet interface with setValueChunk and setIndexChunk implementations, SetBuilder dealing with the chunkedSet interface, and a Set class which implements Set primitives on top of a chunkedSet. Of particular note is the introduction of the setChunkStore, an in-memory cache of setChunks keyed by their refs. This is needed right now so that we only have a single SetBuilder implementation which chunks refs, as opposed to 2 SetBuilder implementations, one which chunks refs and the other chunkedSet instances. Something like it will be needed in the longer term to implement lazily loading set chunks, however there will be issues like memory pressure to deal with as well. Follow-ups to this patch include renaming files to reflect their class names better, and doing a bit of function/member renaming to better reflect semantics. I've tried to keep the diff as small as possible.
71 lines
1.5 KiB
Go
71 lines
1.5 KiB
Go
package newset
|
|
|
|
import "github.com/attic-labs/noms/ref"
|
|
|
|
// Set is the noms set type. It's implemented by wrapping a node.
|
|
type Set struct {
|
|
root node
|
|
store *nodeStore
|
|
newChunker chunkerFactory
|
|
}
|
|
|
|
func NewSet(store *nodeStore, newChunker chunkerFactory) Set {
|
|
return Set{nil, store, newChunker}
|
|
}
|
|
|
|
// Len returns the length of this set.
|
|
func (s Set) Len() uint64 {
|
|
if s.root == nil {
|
|
return uint64(0)
|
|
}
|
|
return s.root.length()
|
|
}
|
|
|
|
// Has returns true if this set contains r, false if it doesn't.
|
|
func (s Set) Has(r ref.Ref) bool {
|
|
if s.root == nil {
|
|
return false
|
|
}
|
|
return s.root.has(r)
|
|
}
|
|
|
|
// Put returns a new Set with r added to it. The new Set may be equivalent to this if this already contains r.
|
|
func (s Set) Put(put ref.Ref) Set {
|
|
sb := NewSetBuilder(s.store, s.newChunker)
|
|
has := false
|
|
// Rebuild entire set, a very wasteful way to implement Put. Issue#475 tracks the development of newset including how to implement Put efficiently.
|
|
if s.root != nil {
|
|
s.root.iter(func(i int, r ref.Ref) {
|
|
switch {
|
|
case has:
|
|
case put == r:
|
|
has = true
|
|
case ref.Less(put, r):
|
|
sb.AddItem(put)
|
|
has = true
|
|
}
|
|
sb.AddItem(r)
|
|
})
|
|
}
|
|
if !has {
|
|
sb.AddItem(put)
|
|
}
|
|
return sb.Build()
|
|
}
|
|
|
|
// Ref returns the ref of this set, or ref.Ref{} if this set is empty.
|
|
func (s Set) Ref() ref.Ref {
|
|
if s.root == nil {
|
|
return ref.Ref{}
|
|
}
|
|
return s.root.ref()
|
|
}
|
|
|
|
// Fmt returns a nicely formatted string representation of this set for debugging.
|
|
func (s Set) Fmt() string {
|
|
if s.root == nil {
|
|
return "(empty set)"
|
|
}
|
|
return s.root.fmt(0)
|
|
}
|