Skip to content

Commit

Permalink
OpenReadWriteFile: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelMure committed Aug 1, 2022
1 parent ed56551 commit c7cee98
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 7 deletions.
11 changes: 8 additions & 3 deletions v2/blockstore/readonly.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ import (
"github.com/ipfs/go-cid"
blockstore "github.com/ipfs/go-ipfs-blockstore"
format "github.com/ipfs/go-ipld-format"
"github.com/multiformats/go-multihash"
"github.com/multiformats/go-varint"
"golang.org/x/exp/mmap"

carv2 "github.com/ipld/go-car/v2"
"github.com/ipld/go-car/v2/index"
"github.com/ipld/go-car/v2/internal/carv1"
"github.com/ipld/go-car/v2/internal/carv1/util"
internalio "github.com/ipld/go-car/v2/internal/io"
"github.com/multiformats/go-multihash"
"github.com/multiformats/go-varint"
"golang.org/x/exp/mmap"
)

var _ blockstore.Blockstore = (*ReadOnly)(nil)
Expand Down Expand Up @@ -534,6 +535,10 @@ func (b *ReadOnly) Roots() ([]cid.Cid, error) {
return header.Roots, nil
}

func (b *ReadOnly) Index() index.Index {
return b.idx
}

// Close closes the underlying reader if it was opened by OpenReadOnly.
// After this call, the blockstore can no longer be used.
//
Expand Down
12 changes: 12 additions & 0 deletions v2/blockstore/readwrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"errors"
"fmt"
"io"
"log"
"os"
"time"

blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
Expand Down Expand Up @@ -329,6 +331,11 @@ func (b *ReadWrite) unfinalize() error {

// Put puts a given block to the underlying datastore
func (b *ReadWrite) Put(ctx context.Context, blk blocks.Block) error {
start := time.Now()
defer func() {
log.Println("Put", time.Since(start))
}()

// PutMany already checks b.ronly.closed.
return b.PutMany(ctx, []blocks.Block{blk})
}
Expand All @@ -339,6 +346,11 @@ func (b *ReadWrite) PutMany(ctx context.Context, blks []blocks.Block) error {
b.ronly.mu.Lock()
defer b.ronly.mu.Unlock()

start := time.Now()
defer func() {
log.Println("PutMany", len(blks), time.Since(start))
}()

if b.ronly.closed {
return errClosed
}
Expand Down
44 changes: 40 additions & 4 deletions v2/blockstore/readwrite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ import (
cbor "github.com/ipfs/go-ipld-cbor"
format "github.com/ipfs/go-ipld-format"
"github.com/ipfs/go-merkledag"
carv2 "github.com/ipld/go-car/v2"
"github.com/ipld/go-car/v2/blockstore"
"github.com/ipld/go-car/v2/index"
"github.com/ipld/go-car/v2/internal/carv1"
"github.com/multiformats/go-multicodec"
"github.com/multiformats/go-multihash"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

carv2 "github.com/ipld/go-car/v2"
"github.com/ipld/go-car/v2/blockstore"
"github.com/ipld/go-car/v2/index"
"github.com/ipld/go-car/v2/internal/carv1"
)

var (
Expand Down Expand Up @@ -945,6 +946,41 @@ func TestReadWrite_ReWritingCARv1WithIdentityCidIsIdenticalToOriginalWithOptions
require.Equal(t, wantSum, gotSum)
}

func TestReadWriteOpenFile(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

dir := t.TempDir() // auto cleanup
f, err := ioutil.TempFile(dir, "")
require.NoError(t, err)

root := blocks.NewBlock([]byte("foo"))

bs, err := blockstore.OpenReadWriteFile(f, []cid.Cid{root.Cid()})
require.NoError(t, err)

err = bs.Put(ctx, root)
require.NoError(t, err)

roots, err := bs.Roots()
require.NoError(t, err)
_, err = bs.Has(ctx, roots[0])
require.NoError(t, err)
_, err = bs.Get(ctx, roots[0])
require.NoError(t, err)
_, err = bs.GetSize(ctx, roots[0])
require.NoError(t, err)

err = bs.Finalize()
require.NoError(t, err)

_, err = f.Seek(0, 0)
require.NoError(t, err) // file should not be closed, let the caller do it

err = f.Close()
require.NoError(t, err)
}

func TestBlockstore_IdentityCidWithEmptyDataIsIndexed(t *testing.T) {
p := path.Join(t.TempDir(), "car-id-cid-empty.carv2")
var noData []byte
Expand Down
7 changes: 7 additions & 0 deletions v2/index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ func GetFirst(idx Index, key cid.Cid) (uint64, error) {
return firstOffset, err
}

// func Has(idx Index, key cid.Cid) (bool, error) {
// has := false
// err := idx.GetAll(key, func(_ uint64) bool {
//
// })
// }

// New constructs a new index corresponding to the given CAR index codec.
func New(codec multicodec.Code) (Index, error) {
switch codec {
Expand Down

0 comments on commit c7cee98

Please sign in to comment.