seaweedfs/weed-fs/src/pkg/storage/compact_map.go

198 lines
4.3 KiB
Go
Raw Normal View History

package storage
import ()
type NeedleValue struct {
Key Key
Offset uint32 "Volume offset" //since aligned to 8 bytes, range is 4G*8=32G
Size uint32 "Size of the data portion"
}
const (
batch = 100000
)
type Key uint64
type CompactSection struct {
values []NeedleValue
overflow map[Key]*NeedleValue
start Key
end Key
counter int
}
func NewCompactSection(start Key) CompactSection {
return CompactSection{
values: make([]NeedleValue, batch),
overflow: make(map[Key]*NeedleValue),
start: start,
}
}
2012-11-24 01:03:27 +00:00
//return old entry size
func (cs *CompactSection) Set(key Key, offset uint32, size uint32) uint32 {
ret := uint32(0)
if key > cs.end {
cs.end = key
}
if i := cs.binarySearchValues(key); i >= 0 {
2012-11-24 01:03:27 +00:00
ret = cs.values[i].Size
//println("key", key, "old size", ret)
cs.values[i].Offset, cs.values[i].Size = offset, size
} else {
2012-11-24 01:03:27 +00:00
needOverflow := cs.counter >= batch
needOverflow = needOverflow || cs.counter > 0 && cs.values[cs.counter-1].Key > key
if needOverflow {
//println("start", cs.start, "counter", cs.counter, "key", key)
2012-11-24 01:03:27 +00:00
if oldValue := cs.overflow[key]; oldValue != nil {
ret = oldValue.Size
}
cs.overflow[key] = &NeedleValue{Key: key, Offset: offset, Size: size}
} else {
p := &cs.values[cs.counter]
p.Key, p.Offset, p.Size = key, offset, size
//println("added index", cs.counter, "key", key, cs.values[cs.counter].Key)
cs.counter++
}
}
2012-11-24 01:03:27 +00:00
return ret
}
2012-11-24 01:03:27 +00:00
//return old entry size
func (cs *CompactSection) Delete(key Key) uint32 {
ret := uint32(0)
if i := cs.binarySearchValues(key); i >= 0 {
2012-11-24 01:03:27 +00:00
if cs.values[i].Size > 0 {
ret = cs.values[i].Size
cs.values[i].Size = 0
}
}
2012-11-24 01:03:27 +00:00
if v := cs.overflow[key]; v != nil {
delete(cs.overflow, key)
ret = v.Size
}
return ret
}
func (cs *CompactSection) Get(key Key) (*NeedleValue, bool) {
if v, ok := cs.overflow[key]; ok {
return v, true
}
if i := cs.binarySearchValues(key); i >= 0 {
return &cs.values[i], true
}
return nil, false
}
func (cs *CompactSection) binarySearchValues(key Key) int {
l, h := 0, cs.counter-1
if h >= 0 && cs.values[h].Key < key {
return -2
}
//println("looking for key", key)
for l <= h {
m := (l + h) / 2
//println("mid", m, "key", cs.values[m].Key, cs.values[m].Offset, cs.values[m].Size)
if cs.values[m].Key < key {
l = m + 1
} else if key < cs.values[m].Key {
h = m - 1
} else {
//println("found", m)
return m
}
}
return -1
}
//This map assumes mostly inserting increasing keys
type CompactMap struct {
list []CompactSection
}
2013-01-12 18:56:47 +00:00
func NewCompactMap() *CompactMap {
return &CompactMap{}
}
2012-11-24 01:03:27 +00:00
func (cm *CompactMap) Set(key Key, offset uint32, size uint32) uint32 {
x := cm.binarySearchCompactSection(key)
if x < 0 {
//println(x, "creating", len(cm.list), "section1, starting", key)
cm.list = append(cm.list, NewCompactSection(key))
x = len(cm.list) - 1
}
2012-11-24 01:03:27 +00:00
return cm.list[x].Set(key, offset, size)
}
2012-11-24 01:03:27 +00:00
func (cm *CompactMap) Delete(key Key) uint32 {
x := cm.binarySearchCompactSection(key)
if x < 0 {
2012-11-24 01:03:27 +00:00
return uint32(0)
}
2012-11-24 01:03:27 +00:00
return cm.list[x].Delete(key)
}
func (cm *CompactMap) Get(key Key) (*NeedleValue, bool) {
x := cm.binarySearchCompactSection(key)
if x < 0 {
return nil, false
}
return cm.list[x].Get(key)
}
func (cm *CompactMap) binarySearchCompactSection(key Key) int {
l, h := 0, len(cm.list)-1
if h < 0 {
return -5
}
if cm.list[h].start <= key {
2012-11-24 01:03:27 +00:00
if cm.list[h].counter < batch || key <= cm.list[h].end {
return h
} else {
return -4
}
}
for l <= h {
m := (l + h) / 2
if key < cm.list[m].start {
h = m - 1
} else { // cm.list[m].start <= key
if cm.list[m+1].start <= key {
l = m + 1
} else {
return m
}
}
}
return -3
}
func (cm *CompactMap) Peek() {
for k, v := range cm.list[0].values {
if k < 100 {
println("[", v.Key, v.Offset, v.Size, "]")
}
}
2012-11-24 01:03:27 +00:00
for k, v := range cm.list[0].overflow {
if k < 100 {
println("o[", v.Key, v.Offset, v.Size, "]")
}
}
}
// iterate over the keys by calling iterate on each key till error is returned
func (cm *CompactMap) Walk(pedestrian func(*NeedleValue) error) (err error) {
var i int
for _, cs := range cm.list {
for key := cs.start; key < cs.end; key++ {
if i = cs.binarySearchValues(key); i >= 0 {
if err = pedestrian(&cs.values[i]); err != nil {
return
}
}
}
for _, val := range cs.overflow {
if err = pedestrian(val); err != nil {
return err
}
}
}
return nil
}