-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
- Loading branch information
1 parent
3f2c774
commit 1de040d
Showing
12 changed files
with
212 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package merkledag | ||
|
||
import ( | ||
"context" | ||
|
||
cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid" | ||
ipld "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format" | ||
) | ||
|
||
// ErrorService implements ipld.DAGService, returning 'Err' for every call. | ||
type ErrorService struct { | ||
Err error | ||
} | ||
|
||
var _ ipld.DAGService = (*ErrorService)(nil) | ||
|
||
func (cs *ErrorService) Add(ctx context.Context, nd ipld.Node) error { | ||
return cs.Err | ||
} | ||
|
||
func (cs *ErrorService) AddMany(ctx context.Context, nds []ipld.Node) error { | ||
return cs.Err | ||
} | ||
|
||
func (cs *ErrorService) Get(ctx context.Context, c *cid.Cid) (ipld.Node, error) { | ||
return nil, cs.Err | ||
} | ||
|
||
func (cs *ErrorService) GetMany(ctx context.Context, cids []*cid.Cid) <-chan *ipld.NodeOption { | ||
ch := make(chan *ipld.NodeOption) | ||
close(ch) | ||
return ch | ||
} | ||
|
||
func (cs *ErrorService) Remove(ctx context.Context, c *cid.Cid) error { | ||
return cs.Err | ||
} | ||
|
||
func (cs *ErrorService) RemoveMany(ctx context.Context, cids []*cid.Cid) error { | ||
return cs.Err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package merkledag | ||
|
||
import ( | ||
"fmt" | ||
|
||
ipld "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format" | ||
) | ||
|
||
var ErrReadOnly = fmt.Errorf("cannot write to readonly DAGService") | ||
|
||
func NewReadOnlyDagService(ng ipld.NodeGetter) ipld.DAGService { | ||
return &ComboService{ | ||
Read: ng, | ||
Write: &ErrorService{ErrReadOnly}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package merkledag_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
. "github.com/ipfs/go-ipfs/merkledag" | ||
dstest "github.com/ipfs/go-ipfs/merkledag/test" | ||
|
||
cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid" | ||
ipld "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format" | ||
) | ||
|
||
func TestReadonlyProperties(t *testing.T) { | ||
ds := dstest.Mock() | ||
ro := NewReadOnlyDagService(ds) | ||
|
||
ctx := context.Background() | ||
nds := []ipld.Node{ | ||
NewRawNode([]byte("foo1")), | ||
NewRawNode([]byte("foo2")), | ||
NewRawNode([]byte("foo3")), | ||
NewRawNode([]byte("foo4")), | ||
} | ||
cids := []*cid.Cid{ | ||
nds[0].Cid(), | ||
nds[1].Cid(), | ||
nds[2].Cid(), | ||
nds[3].Cid(), | ||
} | ||
|
||
// add to the actual underlying datastore | ||
if err := ds.Add(ctx, nds[2]); err != nil { | ||
t.Fatal(err) | ||
} | ||
if err := ds.Add(ctx, nds[3]); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if err := ro.Add(ctx, nds[0]); err != ErrReadOnly { | ||
t.Fatal("expected ErrReadOnly") | ||
} | ||
if err := ro.Add(ctx, nds[2]); err != ErrReadOnly { | ||
t.Fatal("expected ErrReadOnly") | ||
} | ||
|
||
if err := ro.AddMany(ctx, nds[0:1]); err != ErrReadOnly { | ||
t.Fatal("expected ErrReadOnly") | ||
} | ||
|
||
if err := ro.Remove(ctx, cids[3]); err != ErrReadOnly { | ||
t.Fatal("expected ErrReadOnly") | ||
} | ||
if err := ro.RemoveMany(ctx, cids[1:2]); err != ErrReadOnly { | ||
t.Fatal("expected ErrReadOnly") | ||
} | ||
|
||
if _, err := ro.Get(ctx, cids[0]); err != ipld.ErrNotFound { | ||
t.Fatal("expected ErrNotFound") | ||
} | ||
if _, err := ro.Get(ctx, cids[3]); err != nil { | ||
t.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package merkledag | ||
|
||
import ( | ||
"context" | ||
|
||
cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid" | ||
ipld "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format" | ||
) | ||
|
||
// ComboService implements ipld.DAGService, using 'Read' for all fetch methods, | ||
// and 'Write' for all methods that add new objects. | ||
type ComboService struct { | ||
Read ipld.NodeGetter | ||
Write ipld.DAGService | ||
} | ||
|
||
var _ ipld.DAGService = (*ComboService)(nil) | ||
|
||
func (cs *ComboService) Add(ctx context.Context, nd ipld.Node) error { | ||
return cs.Write.Add(ctx, nd) | ||
} | ||
|
||
func (cs *ComboService) AddMany(ctx context.Context, nds []ipld.Node) error { | ||
return cs.Write.AddMany(ctx, nds) | ||
} | ||
|
||
func (cs *ComboService) Get(ctx context.Context, c *cid.Cid) (ipld.Node, error) { | ||
return cs.Read.Get(ctx, c) | ||
} | ||
|
||
func (cs *ComboService) GetMany(ctx context.Context, cids []*cid.Cid) <-chan *ipld.NodeOption { | ||
return cs.Read.GetMany(ctx, cids) | ||
} | ||
|
||
func (cs *ComboService) Remove(ctx context.Context, c *cid.Cid) error { | ||
return cs.Write.Remove(ctx, c) | ||
} | ||
|
||
func (cs *ComboService) RemoveMany(ctx context.Context, cids []*cid.Cid) error { | ||
return cs.Write.RemoveMany(ctx, cids) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package merkledag | ||
|
||
import ( | ||
"context" | ||
|
||
ipld "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format" | ||
) | ||
|
||
type SessionMaker interface { | ||
Session(context.Context) ipld.NodeGetter | ||
} | ||
|
||
func NewSession(ctx context.Context, g ipld.NodeGetter) ipld.NodeGetter { | ||
if sm, ok := g.(SessionMaker); ok { | ||
return sm.Session(ctx) | ||
} | ||
return g | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters