adding visit function for the NeedleMap

This commit is contained in:
Chris Lu 2013-01-20 11:27:32 -08:00
parent 866d15023d
commit f35b958d90
2 changed files with 23 additions and 15 deletions

View file

@ -16,7 +16,7 @@ type Key uint64
type CompactSection struct { type CompactSection struct {
values []NeedleValue values []NeedleValue
overflow map[Key]*NeedleValue overflow map[Key]NeedleValue
start Key start Key
end Key end Key
counter int counter int
@ -25,7 +25,7 @@ type CompactSection struct {
func NewCompactSection(start Key) CompactSection { func NewCompactSection(start Key) CompactSection {
return CompactSection{ return CompactSection{
values: make([]NeedleValue, batch), values: make([]NeedleValue, batch),
overflow: make(map[Key]*NeedleValue), overflow: make(map[Key]NeedleValue),
start: start, start: start,
} }
} }
@ -45,10 +45,10 @@ func (cs *CompactSection) Set(key Key, offset uint32, size uint32) uint32 {
needOverflow = needOverflow || cs.counter > 0 && cs.values[cs.counter-1].Key > key needOverflow = needOverflow || cs.counter > 0 && cs.values[cs.counter-1].Key > key
if needOverflow { if needOverflow {
//println("start", cs.start, "counter", cs.counter, "key", key) //println("start", cs.start, "counter", cs.counter, "key", key)
if oldValue := cs.overflow[key]; oldValue != nil { if oldValue, found := cs.overflow[key]; found {
ret = oldValue.Size ret = oldValue.Size
} }
cs.overflow[key] = &NeedleValue{Key: key, Offset: offset, Size: size} cs.overflow[key] = NeedleValue{Key: key, Offset: offset, Size: size}
} else { } else {
p := &cs.values[cs.counter] p := &cs.values[cs.counter]
p.Key, p.Offset, p.Size = key, offset, size p.Key, p.Offset, p.Size = key, offset, size
@ -68,7 +68,7 @@ func (cs *CompactSection) Delete(key Key) uint32 {
cs.values[i].Size = 0 cs.values[i].Size = 0
} }
} }
if v := cs.overflow[key]; v != nil { if v, found := cs.overflow[key]; found {
delete(cs.overflow, key) delete(cs.overflow, key)
ret = v.Size ret = v.Size
} }
@ -76,7 +76,7 @@ func (cs *CompactSection) Delete(key Key) uint32 {
} }
func (cs *CompactSection) Get(key Key) (*NeedleValue, bool) { func (cs *CompactSection) Get(key Key) (*NeedleValue, bool) {
if v, ok := cs.overflow[key]; ok { if v, ok := cs.overflow[key]; ok {
return v, true return &v, true
} }
if i := cs.binarySearchValues(key); i >= 0 { if i := cs.binarySearchValues(key); i >= 0 {
return &cs.values[i], true return &cs.values[i], true
@ -163,15 +163,20 @@ func (cm *CompactMap) binarySearchCompactSection(key Key) int {
return -3 return -3
} }
func (cm *CompactMap) Peek() { func (cm *CompactMap) Visit(visit func(NeedleValue) error) error {
for k, v := range cm.list[0].values { for _, cs := range cm.list {
if k < 100 { for _, v := range cs.overflow {
println("[", v.Key, v.Offset, v.Size, "]") if err := visit(v); err != nil {
} return err
} }
for k, v := range cm.list[0].overflow { }
if k < 100 { for _, v := range cs.values {
println("o[", v.Key, v.Offset, v.Size, "]") if _, found := cs.overflow[v.Key]; !found {
if err := visit(v); err != nil {
return err
}
}
} }
} }
return nil
} }

View file

@ -98,3 +98,6 @@ func (nm *NeedleMap) Close() {
func (nm *NeedleMap) ContentSize() uint64 { func (nm *NeedleMap) ContentSize() uint64 {
return nm.fileByteCounter return nm.fileByteCounter
} }
func (nm *NeedleMap) Visit(visit func(NeedleValue) error) (err error) {
return nm.m.Visit(visit)
}