seaweedfs/weed/storage/needle_map/compact_map.go

325 lines
9.4 KiB
Go
Raw Permalink Normal View History

2019-04-19 04:43:36 +00:00
package needle_map
2017-05-27 05:51:25 +00:00
import (
2018-12-09 09:27:11 +00:00
"sort"
2018-07-22 00:39:10 +00:00
"sync"
. "github.com/seaweedfs/seaweedfs/weed/storage/types"
2017-05-27 05:51:25 +00:00
)
2018-12-09 08:12:37 +00:00
const (
batch = 10000
2018-12-09 08:12:37 +00:00
)
2018-12-15 13:55:56 +00:00
type SectionalNeedleId uint32
const SectionalNeedleIdLimit = 1<<32 - 1
type SectionalNeedleValue struct {
Key SectionalNeedleId
OffsetLower OffsetLower `comment:"Volume offset"` //since aligned to 8 bytes, range is 4G*8=32G
2020-08-30 05:28:33 +00:00
Size Size `comment:"Size of the data portion"`
}
type SectionalNeedleValueExtra struct {
OffsetHigher OffsetHigher
2018-12-15 13:55:56 +00:00
}
2017-05-27 05:51:25 +00:00
type CompactSection struct {
sync.RWMutex
values []SectionalNeedleValue
valuesExtra []SectionalNeedleValueExtra
overflow Overflow
overflowExtra OverflowExtra
start NeedleId
end NeedleId
counter int
2017-05-27 05:51:25 +00:00
}
2018-12-15 13:55:56 +00:00
type Overflow []SectionalNeedleValue
type OverflowExtra []SectionalNeedleValueExtra
2018-12-09 09:27:11 +00:00
func NewCompactSection(start NeedleId) *CompactSection {
2017-05-27 05:51:25 +00:00
return &CompactSection{
values: make([]SectionalNeedleValue, batch),
valuesExtra: make([]SectionalNeedleValueExtra, batch),
overflow: Overflow(make([]SectionalNeedleValue, 0)),
overflowExtra: OverflowExtra(make([]SectionalNeedleValueExtra, 0)),
start: start,
2017-05-27 05:51:25 +00:00
}
}
2022-09-15 06:06:44 +00:00
// return old entry size
func (cs *CompactSection) Set(key NeedleId, offset Offset, size Size) (oldOffset Offset, oldSize Size) {
2017-05-27 05:51:25 +00:00
cs.Lock()
if key > cs.end {
cs.end = key
}
2018-12-15 13:55:56 +00:00
skey := SectionalNeedleId(key - cs.start)
if i := cs.binarySearchValues(skey); i >= 0 {
oldOffset.OffsetHigher, oldOffset.OffsetLower, oldSize = cs.valuesExtra[i].OffsetHigher, cs.values[i].OffsetLower, cs.values[i].Size
2017-05-27 05:51:25 +00:00
//println("key", key, "old size", ret)
cs.valuesExtra[i].OffsetHigher, cs.values[i].OffsetLower, cs.values[i].Size = offset.OffsetHigher, offset.OffsetLower, size
2017-05-27 05:51:25 +00:00
} else {
needOverflow := cs.counter >= batch
2018-12-15 13:55:56 +00:00
needOverflow = needOverflow || cs.counter > 0 && cs.values[cs.counter-1].Key > skey
2017-05-27 05:51:25 +00:00
if needOverflow {
lookBackIndex := cs.counter - 128
if lookBackIndex < 0 {
lookBackIndex = 0
}
if cs.counter < batch && cs.values[lookBackIndex].Key < skey {
// still has capacity and only partially out of order
p := &cs.values[cs.counter]
p.Key, cs.valuesExtra[cs.counter].OffsetHigher, p.OffsetLower, p.Size = skey, offset.OffsetHigher, offset.OffsetLower, size
//println("added index", cs.counter, "key", key, cs.values[cs.counter].Key)
for x := cs.counter - 1; x >= lookBackIndex; x-- {
if cs.values[x].Key > cs.values[x+1].Key {
cs.values[x], cs.values[x+1] = cs.values[x+1], cs.values[x]
cs.valuesExtra[x], cs.valuesExtra[x+1] = cs.valuesExtra[x+1], cs.valuesExtra[x]
} else {
break
}
}
cs.counter++
} else {
//println("start", cs.start, "counter", cs.counter, "key", key)
if oldValueExtra, oldValue, found := cs.findOverflowEntry(skey); found {
oldOffset.OffsetHigher, oldOffset.OffsetLower, oldSize = oldValueExtra.OffsetHigher, oldValue.OffsetLower, oldValue.Size
}
cs.setOverflowEntry(skey, offset, size)
2017-05-27 05:51:25 +00:00
}
} else {
p := &cs.values[cs.counter]
p.Key, cs.valuesExtra[cs.counter].OffsetHigher, p.OffsetLower, p.Size = skey, offset.OffsetHigher, offset.OffsetLower, size
2017-05-27 05:51:25 +00:00
//println("added index", cs.counter, "key", key, cs.values[cs.counter].Key)
cs.counter++
}
}
cs.Unlock()
return
}
func (cs *CompactSection) setOverflowEntry(skey SectionalNeedleId, offset Offset, size Size) {
needleValue := SectionalNeedleValue{Key: skey, OffsetLower: offset.OffsetLower, Size: size}
needleValueExtra := SectionalNeedleValueExtra{OffsetHigher: offset.OffsetHigher}
insertCandidate := sort.Search(len(cs.overflow), func(i int) bool {
return cs.overflow[i].Key >= needleValue.Key
})
if insertCandidate != len(cs.overflow) && cs.overflow[insertCandidate].Key == needleValue.Key {
cs.overflow[insertCandidate] = needleValue
} else {
cs.overflow = append(cs.overflow, needleValue)
cs.overflowExtra = append(cs.overflowExtra, needleValueExtra)
for i := len(cs.overflow) - 1; i > insertCandidate; i-- {
cs.overflow[i] = cs.overflow[i-1]
cs.overflowExtra[i] = cs.overflowExtra[i-1]
}
cs.overflow[insertCandidate] = needleValue
cs.overflowExtra[insertCandidate] = needleValueExtra
}
}
func (cs *CompactSection) findOverflowEntry(key SectionalNeedleId) (nve SectionalNeedleValueExtra, nv SectionalNeedleValue, found bool) {
foundCandidate := sort.Search(len(cs.overflow), func(i int) bool {
return cs.overflow[i].Key >= key
})
if foundCandidate != len(cs.overflow) && cs.overflow[foundCandidate].Key == key {
return cs.overflowExtra[foundCandidate], cs.overflow[foundCandidate], true
}
return nve, nv, false
}
func (cs *CompactSection) deleteOverflowEntry(key SectionalNeedleId) {
length := len(cs.overflow)
deleteCandidate := sort.Search(length, func(i int) bool {
return cs.overflow[i].Key >= key
})
if deleteCandidate != length && cs.overflow[deleteCandidate].Key == key {
2020-08-19 02:22:16 +00:00
if cs.overflow[deleteCandidate].Size.IsValid() {
2020-08-30 05:28:33 +00:00
cs.overflow[deleteCandidate].Size = -cs.overflow[deleteCandidate].Size
}
}
}
2022-09-15 06:06:44 +00:00
// return old entry size
func (cs *CompactSection) Delete(key NeedleId) Size {
2017-05-27 05:51:25 +00:00
cs.Lock()
defer cs.Unlock()
ret := Size(0)
if key > cs.end {
return ret
}
skey := SectionalNeedleId(key - cs.start)
2018-12-15 13:55:56 +00:00
if i := cs.binarySearchValues(skey); i >= 0 {
2020-08-19 01:01:37 +00:00
if cs.values[i].Size > 0 && cs.values[i].Size.IsValid() {
2017-05-27 05:51:25 +00:00
ret = cs.values[i].Size
2020-08-19 02:22:16 +00:00
cs.values[i].Size = -cs.values[i].Size
2017-05-27 05:51:25 +00:00
}
}
if _, v, found := cs.findOverflowEntry(skey); found {
cs.deleteOverflowEntry(skey)
2017-05-27 05:51:25 +00:00
ret = v.Size
}
return ret
}
func (cs *CompactSection) Get(key NeedleId) (*NeedleValue, bool) {
2017-05-27 05:51:25 +00:00
cs.RLock()
defer cs.RUnlock()
if key > cs.end {
return nil, false
}
2018-12-15 13:55:56 +00:00
skey := SectionalNeedleId(key - cs.start)
if ve, v, ok := cs.findOverflowEntry(skey); ok {
nv := toNeedleValue(ve, v, cs)
2018-12-15 13:55:56 +00:00
return &nv, true
2017-05-27 05:51:25 +00:00
}
2018-12-15 13:55:56 +00:00
if i := cs.binarySearchValues(skey); i >= 0 {
nv := toNeedleValue(cs.valuesExtra[i], cs.values[i], cs)
2018-12-15 13:55:56 +00:00
return &nv, true
2017-05-27 05:51:25 +00:00
}
return nil, false
}
2018-12-15 13:55:56 +00:00
func (cs *CompactSection) binarySearchValues(key SectionalNeedleId) int {
2018-12-22 19:05:29 +00:00
x := sort.Search(cs.counter, func(i int) bool {
return cs.values[i].Key >= key
})
if x == cs.counter {
return -1
2017-05-27 05:51:25 +00:00
}
2018-12-22 19:05:29 +00:00
if cs.values[x].Key > key {
return -2
2017-05-27 05:51:25 +00:00
}
2018-12-22 19:05:29 +00:00
return x
2017-05-27 05:51:25 +00:00
}
2022-09-15 06:06:44 +00:00
// This map assumes mostly inserting increasing keys
// This map assumes mostly inserting increasing keys
2017-05-27 05:51:25 +00:00
type CompactMap struct {
list []*CompactSection
}
func NewCompactMap() *CompactMap {
return &CompactMap{}
}
func (cm *CompactMap) Set(key NeedleId, offset Offset, size Size) (oldOffset Offset, oldSize Size) {
2017-05-27 05:51:25 +00:00
x := cm.binarySearchCompactSection(key)
2018-12-15 13:55:56 +00:00
if x < 0 || (key-cm.list[x].start) > SectionalNeedleIdLimit {
2018-12-22 19:05:29 +00:00
// println(x, "adding to existing", len(cm.list), "sections, starting", key)
2018-12-09 05:45:14 +00:00
cs := NewCompactSection(key)
cm.list = append(cm.list, cs)
2017-05-27 05:51:25 +00:00
x = len(cm.list) - 1
//keep compact section sorted by start
2018-12-22 19:05:29 +00:00
for x >= 0 {
if x > 0 && cm.list[x-1].start > key {
2018-12-09 05:45:14 +00:00
cm.list[x] = cm.list[x-1]
2018-12-22 19:05:29 +00:00
// println("shift", x, "start", cs.start, "to", x-1)
2017-05-27 05:51:25 +00:00
x = x - 1
} else {
2018-12-09 05:45:14 +00:00
cm.list[x] = cs
2018-12-22 19:05:29 +00:00
// println("cs", x, "start", cs.start)
2017-05-27 05:51:25 +00:00
break
}
}
}
2018-12-22 19:05:29 +00:00
// println(key, "set to section[", x, "].start", cm.list[x].start)
2017-05-27 05:51:25 +00:00
return cm.list[x].Set(key, offset, size)
}
func (cm *CompactMap) Delete(key NeedleId) Size {
2017-05-27 05:51:25 +00:00
x := cm.binarySearchCompactSection(key)
if x < 0 {
return Size(0)
2017-05-27 05:51:25 +00:00
}
return cm.list[x].Delete(key)
}
func (cm *CompactMap) Get(key NeedleId) (*NeedleValue, bool) {
2017-05-27 05:51:25 +00:00
x := cm.binarySearchCompactSection(key)
if x < 0 {
return nil, false
}
return cm.list[x].Get(key)
}
func (cm *CompactMap) binarySearchCompactSection(key NeedleId) int {
2018-12-22 19:52:58 +00:00
l, h := 0, len(cm.list)-1
if h < 0 {
return -5
2017-05-27 05:51:25 +00:00
}
2018-12-22 19:52:58 +00:00
if cm.list[h].start <= key {
if cm.list[h].counter < batch || key <= cm.list[h].end {
return h
}
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
}
}
2018-12-22 19:05:29 +00:00
}
2018-12-22 19:52:58 +00:00
return -3
2017-05-27 05:51:25 +00:00
}
// Visit visits all entries or stop if any error when visiting
func (cm *CompactMap) AscendingVisit(visit func(NeedleValue) error) error {
2017-05-27 05:51:25 +00:00
for _, cs := range cm.list {
cs.RLock()
var i, j int
2019-05-29 04:29:07 +00:00
for i, j = 0, 0; i < len(cs.overflow) && j < len(cs.values) && j < cs.counter; {
if cs.overflow[i].Key < cs.values[j].Key {
if err := visit(toNeedleValue(cs.overflowExtra[i], cs.overflow[i], cs)); err != nil {
cs.RUnlock()
return err
}
i++
2019-05-29 04:29:07 +00:00
} else if cs.overflow[i].Key == cs.values[j].Key {
j++
2019-05-29 04:29:07 +00:00
} else {
if err := visit(toNeedleValue(cs.valuesExtra[j], cs.values[j], cs)); err != nil {
cs.RUnlock()
return err
}
j++
}
}
2019-05-29 04:29:07 +00:00
for ; i < len(cs.overflow); i++ {
if err := visit(toNeedleValue(cs.overflowExtra[i], cs.overflow[i], cs)); err != nil {
2017-05-27 05:51:25 +00:00
cs.RUnlock()
return err
}
}
2019-05-29 04:29:07 +00:00
for ; j < len(cs.values) && j < cs.counter; j++ {
if err := visit(toNeedleValue(cs.valuesExtra[j], cs.values[j], cs)); err != nil {
cs.RUnlock()
return err
2017-05-27 05:51:25 +00:00
}
}
cs.RUnlock()
}
return nil
}
2018-12-09 09:27:11 +00:00
func toNeedleValue(snve SectionalNeedleValueExtra, snv SectionalNeedleValue, cs *CompactSection) NeedleValue {
offset := Offset{
OffsetHigher: snve.OffsetHigher,
OffsetLower: snv.OffsetLower,
2018-12-09 09:27:11 +00:00
}
return NeedleValue{Key: NeedleId(snv.Key) + cs.start, Offset: offset, Size: snv.Size}
2018-12-09 09:27:11 +00:00
}
func (nv NeedleValue) toSectionalNeedleValue(cs *CompactSection) (SectionalNeedleValue, SectionalNeedleValueExtra) {
return SectionalNeedleValue{
2019-05-29 04:29:07 +00:00
SectionalNeedleId(nv.Key - cs.start),
nv.Offset.OffsetLower,
nv.Size,
}, SectionalNeedleValueExtra{
nv.Offset.OffsetHigher,
}
2018-12-15 13:55:56 +00:00
}