Skip to content

Commit

Permalink
fix: use more strict type
Browse files Browse the repository at this point in the history
Signed-off-by: gfanton <[email protected]>
  • Loading branch information
gfanton committed Jun 28, 2023
1 parent 139f3ad commit 05134ca
Show file tree
Hide file tree
Showing 12 changed files with 203 additions and 113 deletions.
2 changes: 1 addition & 1 deletion entry/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ func (e *Entry) Equals(b iface.IPFSLogEntry) bool {

func (e *Entry) IsParent(b iface.IPFSLogEntry) bool {
for _, next := range b.GetNext() {
if next.String() == e.Hash.String() {
if next == e.Hash {
return true
}
}
Expand Down
6 changes: 3 additions & 3 deletions entry/entry_io.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"

"github.com/ipfs/go-cid"
format "github.com/ipfs/go-ipld-format"
ipld "github.com/ipfs/go-ipld-format"

"berty.tech/go-ipfs-log/iface"
)
Expand All @@ -13,13 +13,13 @@ type FetchOptions = iface.FetchOptions

// FetchParallel has the same comportement than FetchAll, we keep it for retrop
// compatibility purpose
func FetchParallel(ctx context.Context, dag format.DAGService, hashes []cid.Cid, options *FetchOptions) []iface.IPFSLogEntry {
func FetchParallel(ctx context.Context, dag ipld.DAGService, hashes []cid.Cid, options *FetchOptions) []iface.IPFSLogEntry {
fetcher := NewFetcher(dag, options)
return fetcher.Fetch(ctx, hashes)
}

// FetchAll gets entries from their CIDs.
func FetchAll(ctx context.Context, dag format.DAGService, hashes []cid.Cid, options *FetchOptions) []iface.IPFSLogEntry {
func FetchAll(ctx context.Context, dag ipld.DAGService, hashes []cid.Cid, options *FetchOptions) []iface.IPFSLogEntry {
fetcher := NewFetcher(dag, options)
return fetcher.Fetch(ctx, hashes)
}
21 changes: 11 additions & 10 deletions entry/entry_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,26 @@ import (
"sync"

"berty.tech/go-ipfs-log/iface"
"github.com/ipfs/go-cid"
)

// OrderedMap is an ordered map of entries.
type OrderedMap struct {
lock sync.RWMutex
keys []string
values map[string]iface.IPFSLogEntry
keys []cid.Cid
values map[cid.Cid]iface.IPFSLogEntry
}

func (o *OrderedMap) Copy() iface.IPFSLogOrderedEntries {
o.lock.RLock()
defer o.lock.RUnlock()

values := map[string]iface.IPFSLogEntry{}
values := map[cid.Cid]iface.IPFSLogEntry{}
for k, v := range o.values {
values[k] = v
}

keys := make([]string, len(o.keys))
keys := make([]cid.Cid, len(o.keys))
copy(keys, o.keys)

return &OrderedMap{
Expand All @@ -47,7 +48,7 @@ func (o *OrderedMap) Reverse() iface.IPFSLogOrderedEntries {
func NewOrderedMap() iface.IPFSLogOrderedEntries {
return &OrderedMap{
lock: sync.RWMutex{},
values: map[string]iface.IPFSLogEntry{},
values: map[cid.Cid]iface.IPFSLogEntry{},
}
}

Expand All @@ -60,7 +61,7 @@ func NewOrderedMapFromEntries(entries []iface.IPFSLogEntry) iface.IPFSLogOrdered
continue
}

orderedMap.Set(e.GetHash().String(), e)
orderedMap.Set(e.GetHash(), e)
}

return orderedMap
Expand All @@ -84,7 +85,7 @@ func (o *OrderedMap) Merge(other iface.IPFSLogOrderedEntries) iface.IPFSLogOrder
}

// Get retrieves an Entry using its key.
func (o *OrderedMap) Get(key string) (iface.IPFSLogEntry, bool) {
func (o *OrderedMap) Get(key cid.Cid) (iface.IPFSLogEntry, bool) {
o.lock.RLock()
defer o.lock.RUnlock()

Expand All @@ -93,7 +94,7 @@ func (o *OrderedMap) Get(key string) (iface.IPFSLogEntry, bool) {
}

// UnsafeGet retrieves an Entry using its key, returns nil if not found.
func (o *OrderedMap) UnsafeGet(key string) iface.IPFSLogEntry {
func (o *OrderedMap) UnsafeGet(key cid.Cid) iface.IPFSLogEntry {
o.lock.RLock()
defer o.lock.RUnlock()

Expand All @@ -103,7 +104,7 @@ func (o *OrderedMap) UnsafeGet(key string) iface.IPFSLogEntry {
}

// Set defines an Entry in the map for a given key.
func (o *OrderedMap) Set(key string, value iface.IPFSLogEntry) {
func (o *OrderedMap) Set(key cid.Cid, value iface.IPFSLogEntry) {
o.lock.Lock()
defer o.lock.Unlock()

Expand All @@ -130,7 +131,7 @@ func (o *OrderedMap) Slice() []iface.IPFSLogEntry {
}

// Keys retrieves the ordered list of keys in the map.
func (o *OrderedMap) Keys() []string {
func (o *OrderedMap) Keys() []cid.Cid {
o.lock.RLock()
defer o.lock.RUnlock()

Expand Down
40 changes: 20 additions & 20 deletions entry/sorting/sorting.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,24 +105,24 @@ func Compare(a, b iface.IPFSLogEntry) (int, error) {
return a.GetClock().Compare(b.GetClock()), nil
}

func Sort(compFunc func(a, b iface.IPFSLogEntry) (int, error), values []iface.IPFSLogEntry, reverse bool) {
if reverse {
sort.SliceStable(values, func(i, j int) bool {
ret, err := compFunc(values[i], values[j])
if err != nil {
fmt.Printf("error while comparing: %v\n", err)
return false
}
return ret > 0
})
} else {
sort.SliceStable(values, func(i, j int) bool {
ret, err := compFunc(values[i], values[j])
if err != nil {
fmt.Printf("error while comparing: %v\n", err)
return false
}
return ret < 0
})
}
func Sort(compFunc iface.EntrySortFn, values []iface.IPFSLogEntry) {
sort.SliceStable(values, func(i, j int) bool {
ret, err := compFunc(values[i], values[j])
if err != nil {
fmt.Printf("error while comparing: %v\n", err)
return false
}
return ret < 0
})
}

func SortReverse(compFunc iface.EntrySortFn, values []iface.IPFSLogEntry) {
sort.SliceStable(values, func(i, j int) bool {
ret, err := compFunc(values[i], values[j])
if err != nil {
fmt.Printf("error while comparing: %v\n", err)
return false
}
return ret > 0
})
}
7 changes: 4 additions & 3 deletions entry/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"sort"

"berty.tech/go-ipfs-log/iface"
"github.com/ipfs/go-cid"
)

// Difference gets the list of values not present in both entries sets.
Expand Down Expand Up @@ -96,18 +97,18 @@ func FindHeads(entries iface.IPFSLogOrderedEntries) []iface.IPFSLogEntry {
}

var result []iface.IPFSLogEntry
items := map[string]string{}
items := map[cid.Cid]cid.Cid{}

for _, k := range entries.Keys() {
e := entries.UnsafeGet(k)
for _, n := range e.GetNext() {
items[n.String()] = e.GetHash().String()
items[n] = e.GetHash()
}
}

for _, h := range entries.Keys() {
e, ok := items[h]
if ok || e != "" {
if ok || e != cid.Undef {
continue
}

Expand Down
8 changes: 4 additions & 4 deletions iface/iface.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,19 @@ type IPFSLogOrderedEntries interface {
Copy() IPFSLogOrderedEntries

// Get retrieves an Entry using its key.
Get(key string) (IPFSLogEntry, bool)
Get(key cid.Cid) (IPFSLogEntry, bool)

// UnsafeGet retrieves an Entry using its key, returns nil if not found.
UnsafeGet(key string) IPFSLogEntry
UnsafeGet(key cid.Cid) IPFSLogEntry

// Set defines an Entry in the map for a given key.
Set(key string, value IPFSLogEntry)
Set(key cid.Cid, value IPFSLogEntry)

// Slice returns an ordered slice of the values existing in the map.
Slice() []IPFSLogEntry

// Keys retrieves the ordered list of keys in the map.
Keys() []string
Keys() []cid.Cid

// Len gets the length of the map.
Len() int
Expand Down
Loading

0 comments on commit 05134ca

Please sign in to comment.