From 46a89a7d61a269dbf8cfb1d113d328f138ac5361 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Fri, 27 May 2016 08:13:43 -0700 Subject: [PATCH] fix concurrent write map fix https://github.com/chrislusf/seaweedfs/issues/311 --- go/storage/compact_map.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/go/storage/compact_map.go b/go/storage/compact_map.go index ff3621910..d4438d044 100644 --- a/go/storage/compact_map.go +++ b/go/storage/compact_map.go @@ -1,6 +1,9 @@ package storage -import "strconv" +import ( + "strconv" + "sync" +) type NeedleValue struct { Key Key @@ -19,6 +22,7 @@ func (k Key) String() string { } type CompactSection struct { + sync.RWMutex values []NeedleValue overflow map[Key]NeedleValue start Key @@ -40,6 +44,7 @@ func (cs *CompactSection) Set(key Key, offset uint32, size uint32) uint32 { if key > cs.end { cs.end = key } + cs.Lock() if i := cs.binarySearchValues(key); i >= 0 { ret = cs.values[i].Size //println("key", key, "old size", ret) @@ -60,11 +65,13 @@ func (cs *CompactSection) Set(key Key, offset uint32, size uint32) uint32 { cs.counter++ } } + cs.Unlock() return ret } //return old entry size func (cs *CompactSection) Delete(key Key) uint32 { + cs.Lock() ret := uint32(0) if i := cs.binarySearchValues(key); i >= 0 { if cs.values[i].Size > 0 { @@ -76,15 +83,20 @@ func (cs *CompactSection) Delete(key Key) uint32 { delete(cs.overflow, key) ret = v.Size } + cs.Unlock() return ret } func (cs *CompactSection) Get(key Key) (*NeedleValue, bool) { + cs.RLock() if v, ok := cs.overflow[key]; ok { + cs.RUnlock() return &v, true } if i := cs.binarySearchValues(key); i >= 0 { + cs.RUnlock() return &cs.values[i], true } + cs.RUnlock() return nil, false } func (cs *CompactSection) binarySearchValues(key Key) int {