Skip to content

Commit

Permalink
namecache: prefetch option
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Łukasz Magiera <[email protected]>
  • Loading branch information
Walter Beegle committed Jun 8, 2020
1 parent f656fe2 commit 9680b26
Show file tree
Hide file tree
Showing 5 changed files with 550 additions and 8 deletions.
4 changes: 4 additions & 0 deletions core/node/libp2p/peerstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ package libp2p
import (
"context"

<<<<<<< HEAD
"github.com/libp2p/go-libp2p-core/peerstore"
=======
"github.com/libp2p/go-libp2p-peerstore"
>>>>>>> fix: close peerstore on stop
"github.com/libp2p/go-libp2p-peerstore/pstoremem"
"go.uber.org/fx"
)
Expand Down
212 changes: 212 additions & 0 deletions dagutils/diff.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
package dagutils

import (
"context"
"fmt"
"path"

coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"

"gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
ipld "gx/ipfs/QmRL22E4paat7ky7vx9MLpR97JHHbFPrg3ytFQw6qp1y1s/go-ipld-format"
dag "gx/ipfs/Qmb2UEG2TAeVrEJSjqsZF7Y2he7wRDkrdt6c3bECxwZf4k/go-merkledag"
)

// These constants define the changes that can be applied to a DAG.
const (
Add = iota
Remove
Mod
)

// Change represents a change to a DAG and contains a reference to the old and
// new CIDs.
type Change struct {
Type coreiface.ChangeType
Path string
Before cid.Cid
After cid.Cid
}

// String prints a human-friendly line about a change.
func (c *Change) String() string {
switch c.Type {
case Add:
return fmt.Sprintf("Added %s at %s", c.After.String(), c.Path)
case Remove:
return fmt.Sprintf("Removed %s from %s", c.Before.String(), c.Path)
case Mod:
return fmt.Sprintf("Changed %s to %s at %s", c.Before.String(), c.After.String(), c.Path)
default:
panic("nope")
}
}

// ApplyChange applies the requested changes to the given node in the given dag.
func ApplyChange(ctx context.Context, ds ipld.DAGService, nd *dag.ProtoNode, cs []*Change) (*dag.ProtoNode, error) {
e := NewDagEditor(nd, ds)
for _, c := range cs {
switch c.Type {
case Add:
child, err := ds.Get(ctx, c.After)
if err != nil {
return nil, err
}

childpb, ok := child.(*dag.ProtoNode)
if !ok {
return nil, dag.ErrNotProtobuf
}

err = e.InsertNodeAtPath(ctx, c.Path, childpb, nil)
if err != nil {
return nil, err
}

case Remove:
err := e.RmLink(ctx, c.Path)
if err != nil {
return nil, err
}

case Mod:
err := e.RmLink(ctx, c.Path)
if err != nil {
return nil, err
}
child, err := ds.Get(ctx, c.After)
if err != nil {
return nil, err
}

childpb, ok := child.(*dag.ProtoNode)
if !ok {
return nil, dag.ErrNotProtobuf
}

err = e.InsertNodeAtPath(ctx, c.Path, childpb, nil)
if err != nil {
return nil, err
}
}
}

return e.Finalize(ctx, ds)
}

// Diff returns a set of changes that transform node 'a' into node 'b'.
// It only traverses links in the following cases:
// 1. two node's links number are greater than 0.
// 2. both of two nodes are ProtoNode.
// Otherwise, it compares the cid and emits a Mod change object.
func Diff(ctx context.Context, ds ipld.NodeGetter, a, b ipld.Node) ([]*Change, error) {
// Base case where both nodes are leaves, just compare
// their CIDs.
if len(a.Links()) == 0 && len(b.Links()) == 0 {
return getChange(a, b)
}

var out []*Change
cleanA, okA := a.Copy().(*dag.ProtoNode)
cleanB, okB := b.Copy().(*dag.ProtoNode)
if !okA || !okB {
return getChange(a, b)
}

// strip out unchanged stuff
for _, lnk := range a.Links() {
l, _, err := b.ResolveLink([]string{lnk.Name})
if err == nil {
if l.Cid.Equals(lnk.Cid) {
// no change... ignore it
} else {
anode, err := lnk.GetNode(ctx, ds)
if err != nil {
return nil, err
}

bnode, err := l.GetNode(ctx, ds)
if err != nil {
return nil, err
}

sub, err := Diff(ctx, ds, anode, bnode)
if err != nil {
return nil, err
}

for _, subc := range sub {
subc.Path = path.Join(lnk.Name, subc.Path)
out = append(out, subc)
}
}
cleanA.RemoveNodeLink(l.Name)
cleanB.RemoveNodeLink(l.Name)
}
}

for _, lnk := range cleanA.Links() {
out = append(out, &Change{
Type: Remove,
Path: lnk.Name,
Before: lnk.Cid,
})
}
for _, lnk := range cleanB.Links() {
out = append(out, &Change{
Type: Add,
Path: lnk.Name,
After: lnk.Cid,
})
}

return out, nil
}

// Conflict represents two incompatible changes and is returned by MergeDiffs().
type Conflict struct {
A *Change
B *Change
}

// MergeDiffs takes two slice of changes and adds them to a single slice.
// When a Change from b happens to the same path of an existing change in a,
// a conflict is created and b is not added to the merged slice.
// A slice of Conflicts is returned and contains pointers to the
// Changes involved (which share the same path).
func MergeDiffs(a, b []*Change) ([]*Change, []Conflict) {
var out []*Change
var conflicts []Conflict
paths := make(map[string]*Change)
for _, c := range a {
paths[c.Path] = c
}

for _, c := range b {
if ca, ok := paths[c.Path]; ok {
conflicts = append(conflicts, Conflict{
A: ca,
B: c,
})
} else {
out = append(out, c)
}
}
for _, c := range paths {
out = append(out, c)
}
return out, conflicts
}

func getChange(a, b ipld.Node) ([]*Change, error) {
if a.Cid().Equals(b.Cid()) {
return []*Change{}, nil
}
return []*Change{
{
Type: Mod,
Before: a.Cid(),
After: b.Cid(),
},
}, nil
}
21 changes: 21 additions & 0 deletions test/dependencies/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.13
require (
github.com/Kubuxu/gocovmerge v0.0.0-20161216165753-7ecaa51963cd
github.com/golangci/golangci-lint v1.18.0
<<<<<<< HEAD
github.com/ipfs/go-blockservice v0.1.2
github.com/ipfs/go-cid v0.0.5
github.com/ipfs/go-cidutil v0.0.2
Expand All @@ -28,3 +29,23 @@ require (
github.com/multiformats/go-multihash v0.0.13
gotest.tools/gotestsum v0.3.5
)
=======
github.com/ipfs/go-cidutil v0.0.2
github.com/ipfs/go-log v0.0.1
github.com/ipfs/hang-fds v0.0.1
github.com/ipfs/iptb v1.4.0
github.com/ipfs/iptb-plugins v0.2.0
github.com/jbenet/go-random v0.0.0-20190219211222-123a90aedc0c
github.com/jbenet/go-random-files v0.0.0-20190219210431-31b3f20ebded
github.com/multiformats/go-multiaddr v0.0.4
github.com/multiformats/go-multiaddr-net v0.0.1
github.com/multiformats/go-multihash v0.0.7
gotest.tools/gotestsum v0.3.5
)

// golangci-lint likes replace directives.
replace (
github.com/ultraware/funlen => github.com/golangci/funlen v0.0.0-20190909161642-5e59b9546114
golang.org/x/tools => github.com/golangci/tools v0.0.0-20190910062050-3540c026601b
)
>>>>>>> make: move all test deps to a separate module
Loading

0 comments on commit 9680b26

Please sign in to comment.