Skip to content

Commit

Permalink
fix: entry map lock
Browse files Browse the repository at this point in the history
  • Loading branch information
glouvigny committed Aug 5, 2019
1 parent 6d40282 commit 7c649b6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
13 changes: 7 additions & 6 deletions entry/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ var _ = atlas.BuildEntry(Hashable{}).
AddField("Clock", atlas.StructMapEntry{SerialName: "clock"}).
Complete()

type cborEntry struct {
// CborEntry CBOR representable version of Entry
type CborEntry struct {
V uint64
LogID string
Key string
Expand All @@ -61,7 +62,7 @@ type cborEntry struct {
}

// ToEntry returns a plain Entry from a CBOR serialized version
func (c *cborEntry) ToEntry(provider identityprovider.Interface) (*Entry, error) {
func (c *CborEntry) ToEntry(provider identityprovider.Interface) (*Entry, error) {
key, err := hex.DecodeString(c.Key)
if err != nil {
return nil, err
Expand Down Expand Up @@ -95,8 +96,8 @@ func (c *cborEntry) ToEntry(provider identityprovider.Interface) (*Entry, error)
}

// ToCborEntry creates a CBOR serializable version of an entry
func (e *Entry) ToCborEntry() *cborEntry {
return &cborEntry{
func (e *Entry) ToCborEntry() *CborEntry {
return &CborEntry{
V: e.V,
LogID: e.LogID,
Key: hex.EncodeToString(e.Key),
Expand All @@ -110,7 +111,7 @@ func (e *Entry) ToCborEntry() *cborEntry {
}

func init() {
AtlasEntry := atlas.BuildEntry(cborEntry{}).
AtlasEntry := atlas.BuildEntry(CborEntry{}).
StructMap().
AddField("V", atlas.StructMapEntry{SerialName: "v"}).
AddField("LogID", atlas.StructMapEntry{SerialName: "id"}).
Expand Down Expand Up @@ -350,7 +351,7 @@ func fromMultihash(ctx context.Context, ipfs io.IpfsServices, hash cid.Cid, prov
return nil, err
}

obj := &cborEntry{}
obj := &CborEntry{}
err = cbornode.DecodeInto(result.RawData(), obj)
if err != nil {
return nil, err
Expand Down
21 changes: 20 additions & 1 deletion entry/entry_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@ package entry

import (
"github.com/iancoleman/orderedmap"
"sync"
)

// OrderedMap is an ordered map of entries.
type OrderedMap struct {
lock sync.RWMutex
orderedMap *orderedmap.OrderedMap
}

// NewOrderedMap creates a new OrderedMap of entries.
func NewOrderedMap() *OrderedMap {
return &OrderedMap{
lock: sync.RWMutex{},
orderedMap: orderedmap.New(),
}
}
Expand Down Expand Up @@ -59,25 +62,31 @@ func (o *OrderedMap) Copy() *OrderedMap {

// Get retrieves an Entry using its key.
func (o *OrderedMap) Get(key string) (*Entry, bool) {
o.lock.RLock()
val, exists := o.orderedMap.Get(key)
entry, ok := val.(*Entry)
if !ok {
exists = false
}
o.lock.RUnlock()

return entry, exists
}

// UnsafeGet retrieves an Entry using its key, returns nil if not found.
func (o *OrderedMap) UnsafeGet(key string) *Entry {
o.lock.RLock()
val, _ := o.Get(key)
o.lock.RUnlock()

return val
}

// Set defines an Entry in the map for a given key.
func (o *OrderedMap) Set(key string, value *Entry) {
o.lock.Lock()
o.orderedMap.Set(key, value)
o.lock.Unlock()
}

// Slice returns an ordered slice of the values existing in the map.
Expand All @@ -94,22 +103,32 @@ func (o *OrderedMap) Slice() []*Entry {

// Delete removes an Entry from the map for a given key.
func (o *OrderedMap) Delete(key string) {
o.lock.Lock()
o.orderedMap.Delete(key)
o.lock.Unlock()
}

// Keys retrieves the ordered list of keys in the map.
func (o *OrderedMap) Keys() []string {
return o.orderedMap.Keys()
o.lock.RLock()
keys := o.orderedMap.Keys()
o.lock.RUnlock()

return keys
}

// SortKeys orders the map keys using your sort func.
func (o *OrderedMap) SortKeys(sortFunc func(keys []string)) {
o.lock.Lock()
o.orderedMap.SortKeys(sortFunc)
o.lock.Unlock()
}

// Sort orders the map using your sort func.
func (o *OrderedMap) Sort(lessFunc func(a *orderedmap.Pair, b *orderedmap.Pair) bool) {
o.lock.Lock()
o.orderedMap.Sort(lessFunc)
o.lock.Unlock()
}

// Len gets the length of the map.
Expand Down

0 comments on commit 7c649b6

Please sign in to comment.