Skip to content

Commit

Permalink
Changed Storage to UMap and IStorage to IMap. Added NewUMap and NewIM…
Browse files Browse the repository at this point in the history
…ap to allow easier switching of underlying implementation.
  • Loading branch information
rasteric committed May 17, 2020
1 parent 377bd40 commit 801bf13
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions concstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@ import (
"sync"
)

// Storage is a concurrent key-value in-memory storage. Keys are uin64 and returned when an object is added.
// You create a new storage by using &Storage{}.
type Storage struct {
// UMap is a concurrent key-value in-memory storage. Keys are uin64 and returned when an object is added.
type UMap struct {
c uint64
data sync.Map
mutex sync.Mutex
}

// NewUMap() returns a new *UMap.
func NewUMap() *UMap {
return &UMap{}
}

// Add obj to the storage, return the object's key as uint64.
func (s *Storage) Add(datum interface{}) uint64 {
func (s *UMap) Add(datum interface{}) uint64 {
s.mutex.Lock()
defer s.mutex.Unlock()
s.c++
Expand All @@ -22,21 +26,21 @@ func (s *Storage) Add(datum interface{}) uint64 {
}

// Get the object by key.
func (s *Storage) Get(key uint64) (interface{}, bool) {
func (s *UMap) Get(key uint64) (interface{}, bool) {
return s.data.Load(key)
}

func (s *Storage) Set(key uint64, value interface{}) {
func (s *UMap) Set(key uint64, value interface{}) {
s.data.Store(key, value)
}

// Remove the object with given key.
func (s *Storage) Delete(key uint64) {
func (s *UMap) Delete(key uint64) {
s.data.Delete(key)
}

// Free the memory for the storage.
func (s *Storage) Free() {
func (s *UMap) Free() {
s.mutex.Lock()
defer s.mutex.Unlock()
s.data.Range(func(k, v interface{}) bool {
Expand All @@ -46,21 +50,26 @@ func (s *Storage) Free() {
}

// Range over all values until the function returns false.
func (s *Storage) Range(f func(k uint64, v interface{}) bool) {
func (s *UMap) Range(f func(k uint64, v interface{}) bool) {
s.data.Range(func(k, v interface{}) bool {
return f(k.(uint64), v)
})
}

// IntStorage is an int - interface{} key - value store.
type IStorage struct {
// IMap is a concurrent int - interface{} key - value store.
type IMap struct {
c int
data sync.Map
mutex sync.Mutex
}

// NewIMap returns a new *IMap.
func NewIMap() *IMap {
return &IMap{}
}

// Add obj to the storage, return the object's key as int.
func (s *IStorage) Add(datum interface{}) int {
func (s *IMap) Add(datum interface{}) int {
s.mutex.Lock()
defer s.mutex.Unlock()
s.c++
Expand All @@ -69,21 +78,21 @@ func (s *IStorage) Add(datum interface{}) int {
}

// Get the object by key.
func (s *IStorage) Get(key int) (interface{}, bool) {
func (s *IMap) Get(key int) (interface{}, bool) {
return s.data.Load(key)
}

func (s *IStorage) Set(key int, value interface{}) {
func (s *IMap) Set(key int, value interface{}) {
s.data.Store(key, value)
}

// Remove the object with given key.
func (s *IStorage) Delete(key int) {
func (s *IMap) Delete(key int) {
s.data.Delete(key)
}

// Free the memory for the storage.
func (s *IStorage) Free() {
func (s *IMap) Free() {
s.mutex.Lock()
defer s.mutex.Unlock()
s.data.Range(func(k, v interface{}) bool {
Expand All @@ -93,7 +102,7 @@ func (s *IStorage) Free() {
}

// Range over all values until the function returns false.
func (s *IStorage) Range(f func(k int, v interface{}) bool) {
func (s *IMap) Range(f func(k int, v interface{}) bool) {
s.data.Range(func(k, v interface{}) bool {
return f(k.(int), v)
})
Expand Down

0 comments on commit 801bf13

Please sign in to comment.