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
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
@@ -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"
@@ -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
}

@@ -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)
}
@@ -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{}
}
@@ -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 {
@@ -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)
}
@@ -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
}
}
10 changes: 5 additions & 5 deletions entry/entry_io.go
Original file line number Diff line number Diff line change
@@ -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"
)
@@ -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
@@ -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{
@@ -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{},
}
}

@@ -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
@@ -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()

@@ -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()

@@ -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()

@@ -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()

37 changes: 32 additions & 5 deletions entry/fetcher.go
Original file line number Diff line number Diff line change
@@ -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"
)

@@ -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 {
@@ -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),
@@ -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) {
@@ -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
@@ -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
@@ -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.
@@ -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
}

Loading