Chris Lu 9 years ago
parent
commit
46a89a7d61
  1. 14
      go/storage/compact_map.go

14
go/storage/compact_map.go

@ -1,6 +1,9 @@
package storage package storage
import "strconv"
import (
"strconv"
"sync"
)
type NeedleValue struct { type NeedleValue struct {
Key Key Key Key
@ -19,6 +22,7 @@ func (k Key) String() string {
} }
type CompactSection struct { type CompactSection struct {
sync.RWMutex
values []NeedleValue values []NeedleValue
overflow map[Key]NeedleValue overflow map[Key]NeedleValue
start Key start Key
@ -40,6 +44,7 @@ func (cs *CompactSection) Set(key Key, offset uint32, size uint32) uint32 {
if key > cs.end { if key > cs.end {
cs.end = key cs.end = key
} }
cs.Lock()
if i := cs.binarySearchValues(key); i >= 0 { if i := cs.binarySearchValues(key); i >= 0 {
ret = cs.values[i].Size ret = cs.values[i].Size
//println("key", key, "old size", ret) //println("key", key, "old size", ret)
@ -60,11 +65,13 @@ func (cs *CompactSection) Set(key Key, offset uint32, size uint32) uint32 {
cs.counter++ cs.counter++
} }
} }
cs.Unlock()
return ret return ret
} }
//return old entry size //return old entry size
func (cs *CompactSection) Delete(key Key) uint32 { func (cs *CompactSection) Delete(key Key) uint32 {
cs.Lock()
ret := uint32(0) ret := uint32(0)
if i := cs.binarySearchValues(key); i >= 0 { if i := cs.binarySearchValues(key); i >= 0 {
if cs.values[i].Size > 0 { if cs.values[i].Size > 0 {
@ -76,15 +83,20 @@ func (cs *CompactSection) Delete(key Key) uint32 {
delete(cs.overflow, key) delete(cs.overflow, key)
ret = v.Size ret = v.Size
} }
cs.Unlock()
return ret return ret
} }
func (cs *CompactSection) Get(key Key) (*NeedleValue, bool) { func (cs *CompactSection) Get(key Key) (*NeedleValue, bool) {
cs.RLock()
if v, ok := cs.overflow[key]; ok { if v, ok := cs.overflow[key]; ok {
cs.RUnlock()
return &v, true return &v, true
} }
if i := cs.binarySearchValues(key); i >= 0 { if i := cs.binarySearchValues(key); i >= 0 {
cs.RUnlock()
return &cs.values[i], true return &cs.values[i], true
} }
cs.RUnlock()
return nil, false return nil, false
} }
func (cs *CompactSection) binarySearchValues(key Key) int { func (cs *CompactSection) binarySearchValues(key Key) int {

Loading…
Cancel
Save