2012-09-29 23:07:24 +00:00
|
|
|
package storage
|
|
|
|
|
2016-05-27 15:13:43 +00:00
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
)
|
2015-05-26 06:50:19 +00:00
|
|
|
|
2012-09-29 23:07:24 +00:00
|
|
|
type NeedleValue struct {
|
|
|
|
Key Key
|
2013-02-10 17:44:44 +00:00
|
|
|
Offset uint32 `comment:"Volume offset"` //since aligned to 8 bytes, range is 4G*8=32G
|
|
|
|
Size uint32 `comment:"Size of the data portion"`
|
2012-09-29 23:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
batch = 100000
|
|
|
|
)
|
|
|
|
|
|
|
|
type Key uint64
|
|
|
|
|
2015-05-26 06:50:19 +00:00
|
|
|
func (k Key) String() string {
|
|
|
|
return strconv.FormatUint(uint64(k), 10)
|
|
|
|
}
|
|
|
|
|
2012-09-29 23:07:24 +00:00
|
|
|
type CompactSection struct {
|
2016-05-27 15:13:43 +00:00
|
|
|
sync.RWMutex
|
2012-09-29 23:07:24 +00:00
|
|
|
values []NeedleValue
|
2013-01-20 19:27:32 +00:00
|
|
|
overflow map[Key]NeedleValue
|
2012-09-29 23:07:24 +00:00
|
|
|
start Key
|
|
|
|
end Key
|
|
|
|
counter int
|
|
|
|
}
|
|
|
|
|
2013-10-31 19:57:06 +00:00
|
|
|
func NewCompactSection(start Key) *CompactSection {
|
|
|
|
return &CompactSection{
|
2012-09-29 23:07:24 +00:00
|
|
|
values: make([]NeedleValue, batch),
|
2013-01-20 19:27:32 +00:00
|
|
|
overflow: make(map[Key]NeedleValue),
|
2012-09-29 23:07:24 +00:00
|
|
|
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)
|
2016-09-08 16:50:27 +00:00
|
|
|
cs.Lock()
|
2012-09-29 23:07:24 +00:00
|
|
|
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
|
2013-10-31 19:57:06 +00:00
|
|
|
//println("key", key, "old size", ret)
|
2012-09-29 23:07:24 +00:00
|
|
|
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
|
2012-09-29 23:07:24 +00:00
|
|
|
if needOverflow {
|
2013-10-31 19:57:06 +00:00
|
|
|
//println("start", cs.start, "counter", cs.counter, "key", key)
|
2013-01-20 19:27:32 +00:00
|
|
|
if oldValue, found := cs.overflow[key]; found {
|
2012-11-24 01:03:27 +00:00
|
|
|
ret = oldValue.Size
|
|
|
|
}
|
2013-01-20 19:27:32 +00:00
|
|
|
cs.overflow[key] = NeedleValue{Key: key, Offset: offset, Size: size}
|
2012-09-29 23:07:24 +00:00
|
|
|
} else {
|
|
|
|
p := &cs.values[cs.counter]
|
|
|
|
p.Key, p.Offset, p.Size = key, offset, size
|
2013-10-31 19:57:06 +00:00
|
|
|
//println("added index", cs.counter, "key", key, cs.values[cs.counter].Key)
|
2012-09-29 23:07:24 +00:00
|
|
|
cs.counter++
|
|
|
|
}
|
|
|
|
}
|
2016-05-27 15:13:43 +00:00
|
|
|
cs.Unlock()
|
2012-11-24 01:03:27 +00:00
|
|
|
return ret
|
2012-09-29 23:07:24 +00:00
|
|
|
}
|
2012-11-24 01:03:27 +00:00
|
|
|
|
|
|
|
//return old entry size
|
|
|
|
func (cs *CompactSection) Delete(key Key) uint32 {
|
2016-05-27 15:13:43 +00:00
|
|
|
cs.Lock()
|
2012-11-24 01:03:27 +00:00
|
|
|
ret := uint32(0)
|
2012-09-29 23:07:24 +00:00
|
|
|
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-09-29 23:07:24 +00:00
|
|
|
}
|
2013-01-20 19:27:32 +00:00
|
|
|
if v, found := cs.overflow[key]; found {
|
2012-11-24 01:03:27 +00:00
|
|
|
delete(cs.overflow, key)
|
|
|
|
ret = v.Size
|
|
|
|
}
|
2016-05-27 15:13:43 +00:00
|
|
|
cs.Unlock()
|
2012-11-24 01:03:27 +00:00
|
|
|
return ret
|
2012-09-29 23:07:24 +00:00
|
|
|
}
|
|
|
|
func (cs *CompactSection) Get(key Key) (*NeedleValue, bool) {
|
2016-05-27 15:13:43 +00:00
|
|
|
cs.RLock()
|
2012-09-29 23:07:24 +00:00
|
|
|
if v, ok := cs.overflow[key]; ok {
|
2016-05-27 15:13:43 +00:00
|
|
|
cs.RUnlock()
|
2013-01-20 19:27:32 +00:00
|
|
|
return &v, true
|
2012-09-29 23:07:24 +00:00
|
|
|
}
|
|
|
|
if i := cs.binarySearchValues(key); i >= 0 {
|
2016-05-27 15:13:43 +00:00
|
|
|
cs.RUnlock()
|
2012-09-29 23:07:24 +00:00
|
|
|
return &cs.values[i], true
|
|
|
|
}
|
2016-05-27 15:13:43 +00:00
|
|
|
cs.RUnlock()
|
2012-09-29 23:07:24 +00:00
|
|
|
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
|
|
|
|
}
|
2013-10-31 19:57:06 +00:00
|
|
|
//println("looking for key", key)
|
2012-09-29 23:07:24 +00:00
|
|
|
for l <= h {
|
|
|
|
m := (l + h) / 2
|
2013-10-31 19:57:06 +00:00
|
|
|
//println("mid", m, "key", cs.values[m].Key, cs.values[m].Offset, cs.values[m].Size)
|
2012-09-29 23:07:24 +00:00
|
|
|
if cs.values[m].Key < key {
|
|
|
|
l = m + 1
|
|
|
|
} else if key < cs.values[m].Key {
|
|
|
|
h = m - 1
|
|
|
|
} else {
|
2013-10-31 19:57:06 +00:00
|
|
|
//println("found", m)
|
2012-09-29 23:07:24 +00:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
//This map assumes mostly inserting increasing keys
|
|
|
|
type CompactMap struct {
|
2013-10-31 19:57:06 +00:00
|
|
|
list []*CompactSection
|
2012-09-29 23:07:24 +00:00
|
|
|
}
|
|
|
|
|
2013-01-17 08:15:01 +00:00
|
|
|
func NewCompactMap() CompactMap {
|
|
|
|
return CompactMap{}
|
2012-09-29 23:07:24 +00:00
|
|
|
}
|
|
|
|
|
2012-11-24 01:03:27 +00:00
|
|
|
func (cm *CompactMap) Set(key Key, offset uint32, size uint32) uint32 {
|
2012-09-29 23:07:24 +00:00
|
|
|
x := cm.binarySearchCompactSection(key)
|
|
|
|
if x < 0 {
|
2013-10-31 19:57:06 +00:00
|
|
|
//println(x, "creating", len(cm.list), "section, starting", key)
|
2012-09-29 23:07:24 +00:00
|
|
|
cm.list = append(cm.list, NewCompactSection(key))
|
|
|
|
x = len(cm.list) - 1
|
2013-10-31 19:57:06 +00:00
|
|
|
//keep compact section sorted by start
|
|
|
|
for x > 0 {
|
|
|
|
if cm.list[x-1].start > cm.list[x].start {
|
|
|
|
cm.list[x-1], cm.list[x] = cm.list[x], cm.list[x-1]
|
|
|
|
x = x - 1
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2012-09-29 23:07:24 +00:00
|
|
|
}
|
2012-11-24 01:03:27 +00:00
|
|
|
return cm.list[x].Set(key, offset, size)
|
2012-09-29 23:07:24 +00:00
|
|
|
}
|
2012-11-24 01:03:27 +00:00
|
|
|
func (cm *CompactMap) Delete(key Key) uint32 {
|
2012-09-29 23:07:24 +00:00
|
|
|
x := cm.binarySearchCompactSection(key)
|
|
|
|
if x < 0 {
|
2012-11-24 01:03:27 +00:00
|
|
|
return uint32(0)
|
2012-09-29 23:07:24 +00:00
|
|
|
}
|
2012-11-24 01:03:27 +00:00
|
|
|
return cm.list[x].Delete(key)
|
2012-09-29 23:07:24 +00:00
|
|
|
}
|
|
|
|
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 {
|
2012-09-29 23:07:24 +00:00
|
|
|
return h
|
|
|
|
}
|
2015-03-10 07:20:31 +00:00
|
|
|
return -4
|
2012-09-29 23:07:24 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-05-23 17:13:26 +00:00
|
|
|
// Visit visits all entries or stop if any error when visiting
|
2013-01-20 19:27:32 +00:00
|
|
|
func (cm *CompactMap) Visit(visit func(NeedleValue) error) error {
|
|
|
|
for _, cs := range cm.list {
|
2016-06-23 16:10:25 +00:00
|
|
|
cs.RLock()
|
2013-01-20 19:27:32 +00:00
|
|
|
for _, v := range cs.overflow {
|
|
|
|
if err := visit(v); err != nil {
|
2016-06-23 16:10:25 +00:00
|
|
|
cs.RUnlock()
|
2013-01-20 19:27:32 +00:00
|
|
|
return err
|
|
|
|
}
|
2012-09-29 23:07:24 +00:00
|
|
|
}
|
2013-01-20 19:27:32 +00:00
|
|
|
for _, v := range cs.values {
|
|
|
|
if _, found := cs.overflow[v.Key]; !found {
|
|
|
|
if err := visit(v); err != nil {
|
2016-06-23 16:10:25 +00:00
|
|
|
cs.RUnlock()
|
2013-01-20 19:27:32 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2012-11-24 01:03:27 +00:00
|
|
|
}
|
2016-06-23 16:10:25 +00:00
|
|
|
cs.RUnlock()
|
2012-11-24 01:03:27 +00:00
|
|
|
}
|
2013-01-20 19:27:32 +00:00
|
|
|
return nil
|
2012-09-29 23:07:24 +00:00
|
|
|
}
|