diff options
| author | Brian McQueen <bmcquee@l-sclX1Q0DV7-M.local> | 2014-12-14 00:13:51 -0800 |
|---|---|---|
| committer | Brian McQueen <bmcquee@l-sclX1Q0DV7-M.local> | 2014-12-14 00:13:51 -0800 |
| commit | a3583e4e7cdba69346397b963193eda9ed10c3a3 (patch) | |
| tree | 5c984294280a16779c416a90f0f19e28cb98e7f4 /go/util | |
| parent | bd664def45925d81dfae9c7edfb244d2367170ca (diff) | |
| parent | e431d4121e8da8d7fc243b29b780c2cd535a4210 (diff) | |
| download | seaweedfs-a3583e4e7cdba69346397b963193eda9ed10c3a3.tar.xz seaweedfs-a3583e4e7cdba69346397b963193eda9ed10c3a3.zip | |
Merge branch 'master' of https://github.com/chrislusf/weed-fs
Diffstat (limited to 'go/util')
| -rw-r--r-- | go/util/concurrent_read_map.go | 37 | ||||
| -rw-r--r-- | go/util/config.go | 3 | ||||
| -rw-r--r-- | go/util/constants.go | 4 | ||||
| -rw-r--r-- | go/util/file_util.go | 3 | ||||
| -rw-r--r-- | go/util/net_timeout.go | 3 |
5 files changed, 44 insertions, 6 deletions
diff --git a/go/util/concurrent_read_map.go b/go/util/concurrent_read_map.go new file mode 100644 index 000000000..d16fdbcaf --- /dev/null +++ b/go/util/concurrent_read_map.go @@ -0,0 +1,37 @@ +package util + +import "sync" + +// A mostly for read map, which can thread-safely +// initialize the map entries. +type ConcurrentReadMap struct { + rmutex sync.RWMutex + mutex sync.Mutex + Items map[string]interface{} +} + +func NewConcurrentReadMap() *ConcurrentReadMap { + return &ConcurrentReadMap{Items: make(map[string]interface{})} +} + +func (m *ConcurrentReadMap) initMapEntry(key string, newEntry func() interface{}) (value interface{}) { + m.mutex.Lock() + defer m.mutex.Unlock() + if value, ok := m.Items[key]; ok { + return value + } + value = newEntry() + m.Items[key] = value + return value +} + +func (m *ConcurrentReadMap) Get(key string, newEntry func() interface{}) interface{} { + m.rmutex.RLock() + if value, ok := m.Items[key]; ok { + m.rmutex.RUnlock() + return value + } else { + m.rmutex.RUnlock() + return m.initMapEntry(key, newEntry) + } +} diff --git a/go/util/config.go b/go/util/config.go index 050fd0e64..4cf1d7c64 100644 --- a/go/util/config.go +++ b/go/util/config.go @@ -10,9 +10,10 @@ package util import ( "bytes" - "github.com/chrislusf/weed-fs/go/glog" "encoding/json" "os" + + "github.com/chrislusf/weed-fs/go/glog" ) type Config struct { diff --git a/go/util/constants.go b/go/util/constants.go index db1ca38e5..d677ba44f 100644 --- a/go/util/constants.go +++ b/go/util/constants.go @@ -1,7 +1,5 @@ package util -import () - const ( - VERSION = "0.64" + VERSION = "0.67" ) diff --git a/go/util/file_util.go b/go/util/file_util.go index 412d98458..30e24f001 100644 --- a/go/util/file_util.go +++ b/go/util/file_util.go @@ -2,9 +2,10 @@ package util import ( "bufio" - "github.com/chrislusf/weed-fs/go/glog" "errors" "os" + + "github.com/chrislusf/weed-fs/go/glog" ) func TestFolderWritable(folder string) (err error) { diff --git a/go/util/net_timeout.go b/go/util/net_timeout.go index eb80822b5..f274e4802 100644 --- a/go/util/net_timeout.go +++ b/go/util/net_timeout.go @@ -1,9 +1,10 @@ package util import ( - "github.com/chrislusf/weed-fs/go/stats" "net" "time" + + "github.com/chrislusf/weed-fs/go/stats" ) // Listener wraps a net.Listener, and gives a place to store the timeout |
