Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BREAKING CHANGE: remove kubo deps #90

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 17 additions & 19 deletions entry/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"sort"

"github.com/ipfs/go-cid"
core_iface "github.com/ipfs/interface-go-ipfs-core"
ipld "github.com/ipfs/go-ipld-format"
"github.com/multiformats/go-multibase"

"berty.tech/go-ipfs-log/errmsg"
Expand Down Expand Up @@ -134,18 +134,18 @@ func (e *Entry) SetAdditionalDataValue(key string, value string) {
e.AdditionalData[key] = value
}

func CreateEntry(ctx context.Context, ipfsInstance core_iface.CoreAPI, identity *identityprovider.Identity, data *Entry, opts *iface.CreateEntryOptions) (iface.IPFSLogEntry, error) {
func CreateEntry(ctx context.Context, dag ipld.DAGService, identity *identityprovider.Identity, data *Entry, opts *iface.CreateEntryOptions) (iface.IPFSLogEntry, error) {
io, err := cbor.IO(&Entry{}, &LamportClock{})
if err != nil {
return nil, err
}

return CreateEntryWithIO(ctx, ipfsInstance, identity, data, opts, io)
return CreateEntryWithIO(ctx, dag, identity, data, opts, io)
}

// CreateEntryWithIO creates an Entry.
func CreateEntryWithIO(ctx context.Context, ipfsInstance core_iface.CoreAPI, identity *identityprovider.Identity, data iface.IPFSLogEntry, opts *iface.CreateEntryOptions, io iface.IO) (iface.IPFSLogEntry, error) {
if ipfsInstance == nil {
func CreateEntryWithIO(ctx context.Context, dag ipld.DAGService, identity *identityprovider.Identity, data iface.IPFSLogEntry, opts *iface.CreateEntryOptions, io iface.IO) (iface.IPFSLogEntry, error) {
if dag == nil {
return nil, errmsg.ErrIPFSNotDefined
}

Expand Down Expand Up @@ -201,7 +201,7 @@ func CreateEntryWithIO(ctx context.Context, ipfsInstance core_iface.CoreAPI, ide

data.SetIdentity(identity.Filtered())

h, err := ToMultihashWithIO(ctx, data, ipfsInstance, opts, io)
h, err := ToMultihashWithIO(ctx, data, dag, opts, io)
if err != nil {
return nil, errmsg.ErrIPFSOperationFailed.Wrap(err)
}
Expand Down Expand Up @@ -394,17 +394,17 @@ func (e *Entry) Verify(identity identityprovider.Interface, io iface.IO) error {
}

// ToMultihash gets the multihash of an Entry.
func (e *Entry) ToMultihash(ctx context.Context, ipfsInstance core_iface.CoreAPI, opts *iface.CreateEntryOptions) (cid.Cid, error) {
func (e *Entry) ToMultihash(ctx context.Context, dag ipld.NodeAdder, opts *iface.CreateEntryOptions) (cid.Cid, error) {
io, err := cbor.IO(&Entry{}, &LamportClock{})
if err != nil {
return cid.Undef, err
}

return ToMultihashWithIO(ctx, e, ipfsInstance, opts, io)
return ToMultihashWithIO(ctx, e, dag, opts, io)
}

// ToMultihashWithIO gets the multihash of an Entry.
func ToMultihashWithIO(ctx context.Context, e iface.IPFSLogEntry, ipfsInstance core_iface.CoreAPI, opts *iface.CreateEntryOptions, io iface.IO) (cid.Cid, error) {
func ToMultihashWithIO(ctx context.Context, e iface.IPFSLogEntry, adder ipld.NodeAdder, opts *iface.CreateEntryOptions, io iface.IO) (cid.Cid, error) {
if opts == nil {
opts = &iface.CreateEntryOptions{}
}
Expand All @@ -413,17 +413,15 @@ func ToMultihashWithIO(ctx context.Context, e iface.IPFSLogEntry, ipfsInstance c
return cid.Undef, errmsg.ErrEntryNotDefined
}

if ipfsInstance == nil {
if adder == nil {
return cid.Undef, errmsg.ErrIPFSNotDefined
}

data := Normalize(e, &normalizeEntryOpts{
preSigned: opts.PreSigned,
})

return io.Write(ctx, ipfsInstance, data, &iface.WriteOpts{
Pin: opts.Pin,
})
return io.Write(ctx, adder, &iface.WriteOpts{Pin: opts.Pin}, data)
}

type normalizeEntryOpts struct {
Expand Down Expand Up @@ -468,22 +466,22 @@ func Normalize(e iface.IPFSLogEntry, opts *normalizeEntryOpts) *Entry {
}

// FromMultihash creates an Entry from a hash.
func FromMultihash(ctx context.Context, ipfs core_iface.CoreAPI, hash cid.Cid, provider identityprovider.Interface) (iface.IPFSLogEntry, error) {
func FromMultihash(ctx context.Context, getter ipld.NodeGetter, hash cid.Cid, provider identityprovider.Interface) (iface.IPFSLogEntry, error) {
io, err := cbor.IO(&Entry{}, &LamportClock{})
if err != nil {
return nil, err
}

return FromMultihashWithIO(ctx, ipfs, hash, provider, io)
return FromMultihashWithIO(ctx, getter, hash, provider, io)
}

// FromMultihashWithIO creates an Entry from a hash.
func FromMultihashWithIO(ctx context.Context, ipfs core_iface.CoreAPI, hash cid.Cid, provider identityprovider.Interface, io iface.IO) (iface.IPFSLogEntry, error) {
if ipfs == nil {
func FromMultihashWithIO(ctx context.Context, getter ipld.NodeGetter, hash cid.Cid, provider identityprovider.Interface, io iface.IO) (iface.IPFSLogEntry, error) {
if getter == nil {
return nil, errmsg.ErrIPFSNotDefined
}

result, err := io.Read(ctx, ipfs, hash)
result, err := io.Read(ctx, getter, hash)
if err != nil {
return nil, errmsg.ErrIPFSReadFailed.Wrap(err)
}
Expand All @@ -503,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
10 changes: 5 additions & 5 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"
core_iface "github.com/ipfs/interface-go-ipfs-core"
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, ipfs core_iface.CoreAPI, hashes []cid.Cid, options *FetchOptions) []iface.IPFSLogEntry {
fetcher := NewFetcher(ipfs, options)
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, ipfs core_iface.CoreAPI, hashes []cid.Cid, options *FetchOptions) []iface.IPFSLogEntry {
fetcher := NewFetcher(ipfs, options)
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
37 changes: 32 additions & 5 deletions entry/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"berty.tech/go-ipfs-log/iface"
"berty.tech/go-ipfs-log/io/cbor"
"github.com/ipfs/go-cid"
core_iface "github.com/ipfs/interface-go-ipfs-core"
ipld "github.com/ipfs/go-ipld-format"
"golang.org/x/sync/semaphore"
)

Expand Down Expand Up @@ -39,11 +39,11 @@ type Fetcher struct {
condProcess *sync.Cond
muProcess *sync.RWMutex
sem *semaphore.Weighted
ipfs core_iface.CoreAPI
dag ipld.DAGService
progressChan chan iface.IPFSLogEntry
}

func NewFetcher(ipfs core_iface.CoreAPI, options *FetchOptions) *Fetcher {
func NewFetcher(dag ipld.DAGService, options *FetchOptions) *Fetcher {
// set default
length := -1
if options.Length != nil {
Expand Down Expand Up @@ -76,7 +76,7 @@ func NewFetcher(ipfs core_iface.CoreAPI, options *FetchOptions) *Fetcher {
timeout: options.Timeout,
shouldExclude: options.ShouldExclude,
sem: semaphore.NewWeighted(int64(options.Concurrency)),
ipfs: ipfs,
dag: dag,
progressChan: options.ProgressChan,
muProcess: &muProcess,
condProcess: sync.NewCond(&muProcess),
Expand Down Expand Up @@ -249,7 +249,7 @@ func (f *Fetcher) addNextEntry(ctx context.Context, queue processQueue, entry if

func (f *Fetcher) fetchEntry(ctx context.Context, hash cid.Cid) (entry iface.IPFSLogEntry, err error) {
// Load the entry
return FromMultihashWithIO(ctx, f.ipfs, hash, f.provider, f.io)
return FromMultihashWithIO(ctx, f.dag, hash, f.provider, f.io)
}

func (f *Fetcher) addHashesToQueue(queue processQueue, hashes ...cid.Cid) (added int) {
Expand Down Expand Up @@ -280,3 +280,30 @@ func (f *Fetcher) processDone() {
// signal that a process slot is available
f.sem.Release(1)
}

// func (f *Fetcher) Fetch(ctx context.Context, hashes []cid.Cid) <-chan iface.IPFSLogEntry {
// cnode := f.dag.GetMany(ctx, hashes)
// centry := make(chan iface.IPFSLogEntry)
// go func() {
// defer close(centry)
// for opt := range cnode {
// if opt.Err != nil {

// //TODO: log this
// continue
// }

// node := opt.Node
// decoded, err := f.io.DecodeRawEntry(node, node.Cid(), f.provider)
// if err != nil {
// // log this
// // errmsg.ErrIPFSReadUnmarshalFailed.Wrap(err)
// }

// navnode := ipld.NewNavigableIPLDNode(node, f.dag)
// navnode.ChildTotal()
// }
// }()

// return centry
// }
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
Loading