-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
convert DAGService to an interface #191
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,7 +62,7 @@ func MakeLink(n *Node) (*Link, error) { | |
}, nil | ||
} | ||
|
||
func (l *Link) GetNode(serv *DAGService) (*Node, error) { | ||
func (l *Link) GetNode(serv DAGService) (*Node, error) { | ||
if l.Node != nil { | ||
return l.Node, nil | ||
} | ||
|
@@ -151,20 +151,32 @@ func (n *Node) Key() (u.Key, error) { | |
} | ||
|
||
// DAGService is an IPFS Merkle DAG service. | ||
type DAGService interface { | ||
Add(*Node) (u.Key, error) | ||
AddRecursive(*Node) error | ||
Get(u.Key) (*Node, error) | ||
Remove(*Node) error | ||
} | ||
|
||
func NewDAGService(bs *bserv.BlockService) DAGService { | ||
return &dagService{bs} | ||
} | ||
|
||
// dagService is an IPFS Merkle DAG service. | ||
// - the root is virtual (like a forest) | ||
// - stores nodes' data in a BlockService | ||
// TODO: should cache Nodes that are in memory, and be | ||
// able to free some of them when vm pressure is high | ||
type DAGService struct { | ||
type dagService struct { | ||
Blocks *bserv.BlockService | ||
} | ||
|
||
// Add adds a node to the DAGService, storing the block in the BlockService | ||
func (n *DAGService) Add(nd *Node) (u.Key, error) { | ||
// Add adds a node to the dagService, storing the block in the BlockService | ||
func (n *dagService) Add(nd *Node) (u.Key, error) { | ||
k, _ := nd.Key() | ||
log.Debug("DagService Add [%s]", k) | ||
if n == nil { | ||
return "", fmt.Errorf("DAGService is nil") | ||
return "", fmt.Errorf("dagService is nil") | ||
} | ||
|
||
d, err := nd.Encoded(false) | ||
|
@@ -182,7 +194,7 @@ func (n *DAGService) Add(nd *Node) (u.Key, error) { | |
return n.Blocks.AddBlock(b) | ||
} | ||
|
||
func (n *DAGService) AddRecursive(nd *Node) error { | ||
func (n *dagService) AddRecursive(nd *Node) error { | ||
_, err := n.Add(nd) | ||
if err != nil { | ||
log.Info("AddRecursive Error: %s\n", err) | ||
|
@@ -201,10 +213,10 @@ func (n *DAGService) AddRecursive(nd *Node) error { | |
return nil | ||
} | ||
|
||
// Get retrieves a node from the DAGService, fetching the block in the BlockService | ||
func (n *DAGService) Get(k u.Key) (*Node, error) { | ||
// Get retrieves a node from the dagService, fetching the block in the BlockService | ||
func (n *dagService) Get(k u.Key) (*Node, error) { | ||
if n == nil { | ||
return nil, fmt.Errorf("DAGService is nil") | ||
return nil, fmt.Errorf("dagService is nil") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder why this nil check is here... I dont like it... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @whyrusleeping i've seen a lot of the go stdlib do it too. I think it's because you might have nil structs that still work. e.g. http://play.golang.org/p/zLaycf4H-E (this isn't best example because that's not a ptr, but shrug). Not sure what's good idiomatic practice. thoughts @cryptix ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jbenet: I guess this check is there to protect against a runtime panic that could occur otherwise when looking up There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, i feel like this is a bad check the have, what we should ensure is that we never end up in a scenario where this could occur, like, if the dagservice is nil, other bad things will happen.. |
||
} | ||
|
||
ctx, _ := context.WithTimeout(context.TODO(), time.Second*5) | ||
|
@@ -216,7 +228,7 @@ func (n *DAGService) Get(k u.Key) (*Node, error) { | |
return Decoded(b.Data) | ||
} | ||
|
||
func (n *DAGService) Remove(nd *Node) error { | ||
func (n *dagService) Remove(nd *Node) error { | ||
for _, l := range nd.Links { | ||
if l.Node != nil { | ||
n.Remove(l.Node) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ import ( | |
|
||
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore" | ||
bs "github.com/jbenet/go-ipfs/blockservice" | ||
"github.com/jbenet/go-ipfs/importer" | ||
chunk "github.com/jbenet/go-ipfs/importer/chunk" | ||
mdag "github.com/jbenet/go-ipfs/merkledag" | ||
) | ||
|
@@ -53,7 +54,7 @@ func TestDagWriter(t *testing.T) { | |
if err != nil { | ||
t.Fatal(err) | ||
} | ||
dag := &mdag.DAGService{Blocks: bserv} | ||
dag := mdag.NewDAGService(bserv) | ||
dw := NewDagWriter(dag, &chunk.SizeSplitter{Size: 4096}) | ||
|
||
nbytes := int64(1024 * 1024 * 2) | ||
|
@@ -87,7 +88,7 @@ func TestMassiveWrite(t *testing.T) { | |
if err != nil { | ||
t.Fatal(err) | ||
} | ||
dag := &mdag.DAGService{Blocks: bserv} | ||
dag := mdag.NewDAGService(bserv) | ||
dw := NewDagWriter(dag, &chunk.SizeSplitter{Size: 4096}) | ||
|
||
nbytes := int64(1024 * 1024 * 1024 * 16) | ||
|
@@ -107,7 +108,7 @@ func BenchmarkDagWriter(b *testing.B) { | |
if err != nil { | ||
b.Fatal(err) | ||
} | ||
dag := &mdag.DAGService{Blocks: bserv} | ||
dag := mdag.NewDAGService(bserv) | ||
|
||
b.ResetTimer() | ||
nbytes := int64(100000) | ||
|
@@ -125,3 +126,45 @@ func BenchmarkDagWriter(b *testing.B) { | |
} | ||
|
||
} | ||
|
||
func TestAgainstImporter(t *testing.T) { | ||
dstore := ds.NewMapDatastore() | ||
bserv, err := bs.NewBlockService(dstore, nil) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i approve of this test 👍 |
||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
dag := mdag.NewDAGService(bserv) | ||
|
||
nbytes := int64(1024 * 1024 * 2) | ||
|
||
// DagWriter | ||
dw := NewDagWriter(dag, &chunk.SizeSplitter{4096}) | ||
n, err := io.CopyN(dw, &datasource{}, nbytes) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if n != nbytes { | ||
t.Fatal("Copied incorrect amount of bytes!") | ||
} | ||
|
||
dw.Close() | ||
dwNode := dw.GetNode() | ||
dwKey, err := dwNode.Key() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// DagFromFile | ||
rl := &io.LimitedReader{&datasource{}, nbytes} | ||
|
||
dffNode, err := importer.NewDagFromReaderWithSplitter(rl, &chunk.SizeSplitter{4096}) | ||
dffKey, err := dffNode.Key() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if dwKey.String() != dffKey.String() { | ||
t.Errorf("\nDagWriter produced %s\n"+ | ||
"DagFromReader produced %s", | ||
dwKey, dffKey) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
been wanting to do this forever...