mirror of
https://github.com/ipfs/kubo.git
synced 2025-07-01 19:24:14 +08:00
@ -31,11 +31,7 @@ type Blockstore interface {
|
|||||||
Get(u.Key) (*blocks.Block, error)
|
Get(u.Key) (*blocks.Block, error)
|
||||||
Put(*blocks.Block) error
|
Put(*blocks.Block) error
|
||||||
|
|
||||||
AllKeys(ctx context.Context) ([]u.Key, error)
|
|
||||||
AllKeysChan(ctx context.Context) (<-chan u.Key, error)
|
AllKeysChan(ctx context.Context) (<-chan u.Key, error)
|
||||||
|
|
||||||
AllKeysRange(ctx context.Context, offset int, limit int) ([]u.Key, error)
|
|
||||||
AllKeysRangeChan(ctx context.Context, offset int, limit int) (<-chan u.Key, error)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewBlockstore(d ds.ThreadSafeDatastore) Blockstore {
|
func NewBlockstore(d ds.ThreadSafeDatastore) Blockstore {
|
||||||
@ -85,42 +81,14 @@ func (s *blockstore) DeleteBlock(k u.Key) error {
|
|||||||
return s.datastore.Delete(k.DsKey())
|
return s.datastore.Delete(k.DsKey())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bs *blockstore) AllKeys(ctx context.Context) ([]u.Key, error) {
|
// AllKeysChan runs a query for keys from the blockstore.
|
||||||
return bs.AllKeysRange(ctx, 0, 0)
|
// this is very simplistic, in the future, take dsq.Query as a param?
|
||||||
}
|
//
|
||||||
|
// AllKeysChan respects context
|
||||||
func (bs *blockstore) AllKeysChan(ctx context.Context) (<-chan u.Key, error) {
|
func (bs *blockstore) AllKeysChan(ctx context.Context) (<-chan u.Key, error) {
|
||||||
return bs.AllKeysRangeChan(ctx, 0, 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
// AllKeysRange runs a query for keys from the blockstore.
|
|
||||||
// this is very simplistic, in the future, take dsq.Query as a param?
|
|
||||||
// if offset and limit are 0, they are ignored.
|
|
||||||
//
|
|
||||||
// AllKeysRange respects context
|
|
||||||
func (bs *blockstore) AllKeysRange(ctx context.Context, offset int, limit int) ([]u.Key, error) {
|
|
||||||
|
|
||||||
ch, err := bs.AllKeysRangeChan(ctx, offset, limit)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var keys []u.Key
|
|
||||||
for k := range ch {
|
|
||||||
keys = append(keys, k)
|
|
||||||
}
|
|
||||||
return keys, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// AllKeysRangeChan runs a query for keys from the blockstore.
|
|
||||||
// this is very simplistic, in the future, take dsq.Query as a param?
|
|
||||||
// if offset and limit are 0, they are ignored.
|
|
||||||
//
|
|
||||||
// AllKeysRangeChan respects context
|
|
||||||
func (bs *blockstore) AllKeysRangeChan(ctx context.Context, offset int, limit int) (<-chan u.Key, error) {
|
|
||||||
|
|
||||||
// KeysOnly, because that would be _a lot_ of data.
|
// KeysOnly, because that would be _a lot_ of data.
|
||||||
q := dsq.Query{KeysOnly: true, Offset: offset, Limit: limit}
|
q := dsq.Query{KeysOnly: true}
|
||||||
res, err := bs.datastore.Query(q)
|
res, err := bs.datastore.Query(q)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -63,14 +63,24 @@ func newBlockStoreWithKeys(t *testing.T, d ds.Datastore, N int) (Blockstore, []u
|
|||||||
return bs, keys
|
return bs, keys
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func collect(ch <-chan u.Key) []u.Key {
|
||||||
|
var keys []u.Key
|
||||||
|
for k := range ch {
|
||||||
|
keys = append(keys, k)
|
||||||
|
}
|
||||||
|
return keys
|
||||||
|
}
|
||||||
|
|
||||||
func TestAllKeysSimple(t *testing.T) {
|
func TestAllKeysSimple(t *testing.T) {
|
||||||
bs, keys := newBlockStoreWithKeys(t, nil, 100)
|
bs, keys := newBlockStoreWithKeys(t, nil, 100)
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
keys2, err := bs.AllKeys(ctx)
|
ch, err := bs.AllKeysChan(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
keys2 := collect(ch)
|
||||||
|
|
||||||
// for _, k2 := range keys2 {
|
// for _, k2 := range keys2 {
|
||||||
// t.Log("found ", k2.Pretty())
|
// t.Log("found ", k2.Pretty())
|
||||||
// }
|
// }
|
||||||
@ -78,23 +88,6 @@ func TestAllKeysSimple(t *testing.T) {
|
|||||||
expectMatches(t, keys, keys2)
|
expectMatches(t, keys, keys2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAllKeysOffsetAndLimit(t *testing.T) {
|
|
||||||
N := 30
|
|
||||||
bs, _ := newBlockStoreWithKeys(t, nil, N)
|
|
||||||
|
|
||||||
ctx := context.Background()
|
|
||||||
keys3, err := bs.AllKeysRange(ctx, N/3, N/3)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
for _, k3 := range keys3 {
|
|
||||||
t.Log("found ", k3.Pretty())
|
|
||||||
}
|
|
||||||
if len(keys3) != N/3 {
|
|
||||||
t.Errorf("keys3 should be: %d != %d", N/3, len(keys3))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAllKeysRespectsContext(t *testing.T) {
|
func TestAllKeysRespectsContext(t *testing.T) {
|
||||||
N := 100
|
N := 100
|
||||||
|
|
||||||
@ -107,10 +100,11 @@ func TestAllKeysRespectsContext(t *testing.T) {
|
|||||||
|
|
||||||
getKeys := func(ctx context.Context) {
|
getKeys := func(ctx context.Context) {
|
||||||
started <- struct{}{}
|
started <- struct{}{}
|
||||||
_, err := bs.AllKeys(ctx) // once without cancelling
|
ch, err := bs.AllKeysChan(ctx) // once without cancelling
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errors <- err
|
errors <- err
|
||||||
}
|
}
|
||||||
|
_ = collect(ch)
|
||||||
done <- struct{}{}
|
done <- struct{}{}
|
||||||
errors <- nil // a nil one to signal break
|
errors <- nil // a nil one to signal break
|
||||||
}
|
}
|
||||||
|
@ -45,18 +45,6 @@ func (w *writecache) Put(b *blocks.Block) error {
|
|||||||
return w.blockstore.Put(b)
|
return w.blockstore.Put(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *writecache) AllKeys(ctx context.Context) ([]u.Key, error) {
|
|
||||||
return w.blockstore.AllKeysRange(ctx, 0, 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w *writecache) AllKeysChan(ctx context.Context) (<-chan u.Key, error) {
|
func (w *writecache) AllKeysChan(ctx context.Context) (<-chan u.Key, error) {
|
||||||
return w.blockstore.AllKeysRangeChan(ctx, 0, 0)
|
return w.blockstore.AllKeysChan(ctx)
|
||||||
}
|
|
||||||
|
|
||||||
func (w *writecache) AllKeysRange(ctx context.Context, offset int, limit int) ([]u.Key, error) {
|
|
||||||
return w.blockstore.AllKeysRange(ctx, offset, limit)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w *writecache) AllKeysRangeChan(ctx context.Context, offset int, limit int) (<-chan u.Key, error) {
|
|
||||||
return w.blockstore.AllKeysRangeChan(ctx, offset, limit)
|
|
||||||
}
|
}
|
||||||
|
13
repo/repo.go
13
repo/repo.go
@ -5,7 +5,6 @@ import (
|
|||||||
|
|
||||||
datastore "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
|
datastore "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
|
||||||
config "github.com/jbenet/go-ipfs/repo/config"
|
config "github.com/jbenet/go-ipfs/repo/config"
|
||||||
util "github.com/jbenet/go-ipfs/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Repo interface {
|
type Repo interface {
|
||||||
@ -19,15 +18,3 @@ type Repo interface {
|
|||||||
|
|
||||||
io.Closer
|
io.Closer
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsInitialized returns true if the path is home to an initialized IPFS
|
|
||||||
// repository.
|
|
||||||
func IsInitialized(path string) bool {
|
|
||||||
if !util.FileExists(path) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
// TODO add logging check
|
|
||||||
// TODO add datastore check
|
|
||||||
// TODO add config file check
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
Reference in New Issue
Block a user