Skip to content
This repository has been archived by the owner on Jun 19, 2023. It is now read-only.

gx update and fix code to use new Cid type #15

Merged
merged 2 commits into from
Sep 12, 2018
Merged
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
2 changes: 1 addition & 1 deletion .gx/lastpubver
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.20: Qmeg56ecxRnVv7VWViMrDeEMoBHaNFMs4vQnyQrJ79Zz7i
0.1.0: QmeMussyD8s3fQ3pM19ZsfbxvomEqPV9FvczLMWyBDYSnS
24 changes: 12 additions & 12 deletions arc_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func newARCCachedBS(ctx context.Context, bs Blockstore, lruSize int) (*arccache,
return c, nil
}

func (b *arccache) DeleteBlock(k *cid.Cid) error {
func (b *arccache) DeleteBlock(k cid.Cid) error {
if has, _, ok := b.hasCached(k); ok && !has {
return ErrNotFound
}
Expand All @@ -53,10 +53,10 @@ func (b *arccache) DeleteBlock(k *cid.Cid) error {

// if ok == false has is inconclusive
// if ok == true then has respons to question: is it contained
func (b *arccache) hasCached(k *cid.Cid) (has bool, size int, ok bool) {
func (b *arccache) hasCached(k cid.Cid) (has bool, size int, ok bool) {
b.total.Inc()
if k == nil {
log.Error("nil cid in arccache")
if !k.Defined() {
log.Error("undefined cid in arccache")
// Return cache invalid so the call to blockstore happens
// in case of invalid key and correct error is created.
return false, -1, false
Expand All @@ -75,7 +75,7 @@ func (b *arccache) hasCached(k *cid.Cid) (has bool, size int, ok bool) {
return false, -1, false
}

func (b *arccache) Has(k *cid.Cid) (bool, error) {
func (b *arccache) Has(k cid.Cid) (bool, error) {
if has, _, ok := b.hasCached(k); ok {
return has, nil
}
Expand All @@ -87,7 +87,7 @@ func (b *arccache) Has(k *cid.Cid) (bool, error) {
return has, nil
}

func (b *arccache) GetSize(k *cid.Cid) (int, error) {
func (b *arccache) GetSize(k cid.Cid) (int, error) {
if _, blockSize, ok := b.hasCached(k); ok {
return blockSize, nil
}
Expand All @@ -100,9 +100,9 @@ func (b *arccache) GetSize(k *cid.Cid) (int, error) {
return blockSize, err
}

func (b *arccache) Get(k *cid.Cid) (blocks.Block, error) {
if k == nil {
log.Error("nil cid in arc cache")
func (b *arccache) Get(k cid.Cid) (blocks.Block, error) {
if !k.Defined() {
log.Error("undefined cid in arc cache")
return nil, ErrNotFound
}

Expand Down Expand Up @@ -154,15 +154,15 @@ func (b *arccache) HashOnRead(enabled bool) {
b.blockstore.HashOnRead(enabled)
}

func (b *arccache) cacheHave(c *cid.Cid, have bool) {
func (b *arccache) cacheHave(c cid.Cid, have bool) {
b.arc.Add(c.KeyString(), cacheHave(have))
}

func (b *arccache) cacheSize(c *cid.Cid, blockSize int) {
func (b *arccache) cacheSize(c cid.Cid, blockSize int) {
b.arc.Add(c.KeyString(), cacheSize(blockSize))
}

func (b *arccache) AllKeysChan(ctx context.Context) (<-chan *cid.Cid, error) {
func (b *arccache) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {
return b.blockstore.AllKeysChan(ctx)
}

Expand Down
2 changes: 1 addition & 1 deletion arc_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func TestArcCreationFailure(t *testing.T) {
func TestInvalidKey(t *testing.T) {
arc, _, _ := createStores(t)

bl, err := arc.Get(nil)
bl, err := arc.Get(cid.Cid{})

if bl != nil {
t.Fatal("blocks should be nil")
Expand Down
26 changes: 13 additions & 13 deletions blockstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ var ErrNotFound = errors.New("blockstore: block not found")
// Blockstore wraps a Datastore block-centered methods and provides a layer
// of abstraction which allows to add different caching strategies.
type Blockstore interface {
DeleteBlock(*cid.Cid) error
Has(*cid.Cid) (bool, error)
Get(*cid.Cid) (blocks.Block, error)
DeleteBlock(cid.Cid) error
Has(cid.Cid) (bool, error)
Get(cid.Cid) (blocks.Block, error)

// GetSize returns the CIDs mapped BlockSize
GetSize(*cid.Cid) (int, error)
GetSize(cid.Cid) (int, error)

// Put puts a given block to the underlying datastore
Put(blocks.Block) error
Expand All @@ -49,7 +49,7 @@ type Blockstore interface {
// AllKeysChan returns a channel from which
// the CIDs in the Blockstore can be read. It should respect
// the given context, closing the channel if it becomes Done.
AllKeysChan(ctx context.Context) (<-chan *cid.Cid, error)
AllKeysChan(ctx context.Context) (<-chan cid.Cid, error)

// HashOnRead specifies if every read block should be
// rehashed to make sure it matches its CID.
Expand Down Expand Up @@ -114,9 +114,9 @@ func (bs *blockstore) HashOnRead(enabled bool) {
bs.rehash = enabled
}

func (bs *blockstore) Get(k *cid.Cid) (blocks.Block, error) {
if k == nil {
log.Error("nil cid in blockstore")
func (bs *blockstore) Get(k cid.Cid) (blocks.Block, error) {
if !k.Defined() {
log.Error("undefined cid in blockstore")
return nil, ErrNotFound
}

Expand Down Expand Up @@ -173,11 +173,11 @@ func (bs *blockstore) PutMany(blocks []blocks.Block) error {
return t.Commit()
}

func (bs *blockstore) Has(k *cid.Cid) (bool, error) {
func (bs *blockstore) Has(k cid.Cid) (bool, error) {
return bs.datastore.Has(dshelp.CidToDsKey(k))
}

func (bs *blockstore) GetSize(k *cid.Cid) (int, error) {
func (bs *blockstore) GetSize(k cid.Cid) (int, error) {
bdata, err := bs.datastore.Get(dshelp.CidToDsKey(k))
if err == ds.ErrNotFound {
return -1, ErrNotFound
Expand All @@ -188,7 +188,7 @@ func (bs *blockstore) GetSize(k *cid.Cid) (int, error) {
return len(bdata), nil
}

func (bs *blockstore) DeleteBlock(k *cid.Cid) error {
func (bs *blockstore) DeleteBlock(k cid.Cid) error {
err := bs.datastore.Delete(dshelp.CidToDsKey(k))
if err == ds.ErrNotFound {
return ErrNotFound
Expand All @@ -200,7 +200,7 @@ func (bs *blockstore) DeleteBlock(k *cid.Cid) error {
// this is very simplistic, in the future, take dsq.Query as a param?
//
// AllKeysChan respects context.
func (bs *blockstore) AllKeysChan(ctx context.Context) (<-chan *cid.Cid, error) {
func (bs *blockstore) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {

// KeysOnly, because that would be _a lot_ of data.
q := dsq.Query{KeysOnly: true}
Expand All @@ -209,7 +209,7 @@ func (bs *blockstore) AllKeysChan(ctx context.Context) (<-chan *cid.Cid, error)
return nil, err
}

output := make(chan *cid.Cid, dsq.KeysOnlyBufSize)
output := make(chan cid.Cid, dsq.KeysOnlyBufSize)
go func() {
defer func() {
res.Close() // ensure exit (signals early exit, too)
Expand Down
12 changes: 6 additions & 6 deletions blockstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestGetWhenKeyNotPresent(t *testing.T) {

func TestGetWhenKeyIsNil(t *testing.T) {
bs := NewBlockstore(ds_sync.MutexWrap(ds.NewMapDatastore()))
_, err := bs.Get(nil)
_, err := bs.Get(cid.Cid{})
if err != ErrNotFound {
t.Fail()
}
Expand Down Expand Up @@ -113,13 +113,13 @@ func TestHashOnRead(t *testing.T) {
}
}

func newBlockStoreWithKeys(t *testing.T, d ds.Datastore, N int) (Blockstore, []*cid.Cid) {
func newBlockStoreWithKeys(t *testing.T, d ds.Datastore, N int) (Blockstore, []cid.Cid) {
if d == nil {
d = ds.NewMapDatastore()
}
bs := NewBlockstore(ds_sync.MutexWrap(d))

keys := make([]*cid.Cid, N)
keys := make([]cid.Cid, N)
for i := 0; i < N; i++ {
block := blocks.NewBlock([]byte(fmt.Sprintf("some data %d", i)))
err := bs.Put(block)
Expand All @@ -131,8 +131,8 @@ func newBlockStoreWithKeys(t *testing.T, d ds.Datastore, N int) (Blockstore, []*
return bs, keys
}

func collect(ch <-chan *cid.Cid) []*cid.Cid {
var keys []*cid.Cid
func collect(ch <-chan cid.Cid) []cid.Cid {
var keys []cid.Cid
for k := range ch {
keys = append(keys, k)
}
Expand Down Expand Up @@ -217,7 +217,7 @@ func TestAllKeysRespectsContext(t *testing.T) {

}

func expectMatches(t *testing.T, expect, actual []*cid.Cid) {
func expectMatches(t *testing.T, expect, actual []cid.Cid) {

if len(expect) != len(actual) {
t.Errorf("expect and actual differ: %d != %d", len(expect), len(actual))
Expand Down
16 changes: 8 additions & 8 deletions bloom_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (b *bloomcache) Rebuild(ctx context.Context) {
atomic.StoreInt32(&b.active, 1)
}

func (b *bloomcache) DeleteBlock(k *cid.Cid) error {
func (b *bloomcache) DeleteBlock(k cid.Cid) error {
if has, ok := b.hasCached(k); ok && !has {
return ErrNotFound
}
Expand All @@ -107,10 +107,10 @@ func (b *bloomcache) DeleteBlock(k *cid.Cid) error {

// if ok == false has is inconclusive
// if ok == true then has respons to question: is it contained
func (b *bloomcache) hasCached(k *cid.Cid) (has bool, ok bool) {
func (b *bloomcache) hasCached(k cid.Cid) (has bool, ok bool) {
b.total.Inc()
if k == nil {
log.Error("nil cid in bloom cache")
if !k.Defined() {
log.Error("undefined in bloom cache")
// Return cache invalid so call to blockstore
// in case of invalid key is forwarded deeper
return false, false
Expand All @@ -125,19 +125,19 @@ func (b *bloomcache) hasCached(k *cid.Cid) (has bool, ok bool) {
return false, false
}

func (b *bloomcache) Has(k *cid.Cid) (bool, error) {
func (b *bloomcache) Has(k cid.Cid) (bool, error) {
if has, ok := b.hasCached(k); ok {
return has, nil
}

return b.blockstore.Has(k)
}

func (b *bloomcache) GetSize(k *cid.Cid) (int, error) {
func (b *bloomcache) GetSize(k cid.Cid) (int, error) {
return b.blockstore.GetSize(k)
}

func (b *bloomcache) Get(k *cid.Cid) (blocks.Block, error) {
func (b *bloomcache) Get(k cid.Cid) (blocks.Block, error) {
if has, ok := b.hasCached(k); ok && !has {
return nil, ErrNotFound
}
Expand Down Expand Up @@ -173,7 +173,7 @@ func (b *bloomcache) HashOnRead(enabled bool) {
b.blockstore.HashOnRead(enabled)
}

func (b *bloomcache) AllKeysChan(ctx context.Context) (<-chan *cid.Cid, error) {
func (b *bloomcache) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {
return b.blockstore.AllKeysChan(ctx)
}

Expand Down
12 changes: 6 additions & 6 deletions idstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,39 @@ func NewIdStore(bs Blockstore) Blockstore {
return &idstore{bs}
}

func extractContents(k *cid.Cid) (bool, []byte) {
func extractContents(k cid.Cid) (bool, []byte) {
dmh, err := mh.Decode(k.Hash())
if err != nil || dmh.Code != mh.ID {
return false, nil
}
return true, dmh.Digest
}

func (b *idstore) DeleteBlock(k *cid.Cid) error {
func (b *idstore) DeleteBlock(k cid.Cid) error {
isId, _ := extractContents(k)
if isId {
return nil
}
return b.bs.DeleteBlock(k)
}

func (b *idstore) Has(k *cid.Cid) (bool, error) {
func (b *idstore) Has(k cid.Cid) (bool, error) {
isId, _ := extractContents(k)
if isId {
return true, nil
}
return b.bs.Has(k)
}

func (b *idstore) GetSize(k *cid.Cid) (int, error) {
func (b *idstore) GetSize(k cid.Cid) (int, error) {
isId, bdata := extractContents(k)
if isId {
return len(bdata), nil
}
return b.bs.GetSize(k)
}

func (b *idstore) Get(k *cid.Cid) (blocks.Block, error) {
func (b *idstore) Get(k cid.Cid) (blocks.Block, error) {
isId, bdata := extractContents(k)
if isId {
return blocks.NewBlockWithCid(bdata, k)
Expand Down Expand Up @@ -81,6 +81,6 @@ func (b *idstore) HashOnRead(enabled bool) {
b.bs.HashOnRead(enabled)
}

func (b *idstore) AllKeysChan(ctx context.Context) (<-chan *cid.Cid, error) {
func (b *idstore) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {
return b.bs.AllKeysChan(ctx)
}
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
"gxDependencies": [
{
"author": "stebalien",
"hash": "QmWAzSEoqZ6xU6pu8yL8e5WaMb7wtbfbhhN4p1DknUPtr3",
"hash": "QmRcHuYzAyswytBuMF78rj3LTChYszomRFXNg4685ZN1WM",
"name": "go-block-format",
"version": "0.1.11"
"version": "0.2.0"
},
{
"author": "jbenet",
Expand All @@ -33,15 +33,15 @@
},
{
"author": "whyrusleeping",
"hash": "QmZFbDTY9jfSBms2MchvYM9oYRbAF19K7Pby47yDBfpPrb",
"hash": "QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7",
"name": "go-cid",
"version": "0.8.0"
"version": "0.9.0"
},
{
"author": "hector",
"hash": "Qmf1xGr3SyBpiPp3ZDuKkYMh4gnRk9K4QnbL17kstnf35h",
"hash": "QmXejiSr776HgKLEGSs7unW7GT82AgfMbQX5crfSybGU8b",
"name": "go-ipfs-ds-help",
"version": "0.0.10"
"version": "0.1.0"
},
{
"author": "kubuxu",
Expand All @@ -60,6 +60,6 @@
"license": "MIT",
"name": "go-ipfs-blockstore",
"releaseCmd": "git commit -a -m \"gx publish $VERSION\"",
"version": "0.0.20"
"version": "0.1.0"
}