Skip to content

Commit 2b80913

Browse files
committed
chore: extract DeferredCarWriter to go-car
Ref: ipld/go-car#493
1 parent 9ad1559 commit 2b80913

File tree

7 files changed

+48
-394
lines changed

7 files changed

+48
-394
lines changed

cmd/lassie/fetch.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"github.com/filecoin-project/lassie/pkg/storage"
1414
"github.com/filecoin-project/lassie/pkg/types"
1515
"github.com/ipfs/go-cid"
16+
"github.com/ipld/go-car/v2"
17+
"github.com/ipld/go-car/v2/storage/deferred"
1618
"github.com/ipld/go-ipld-prime/datamodel"
1719
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
1820
trustlessutils "github.com/ipld/go-trustless-utils"
@@ -253,14 +255,14 @@ func defaultFetchRun(
253255
lassie.RegisterSubscriber(pp.subscriber)
254256
}
255257

256-
var carWriter *storage.DeferredCarWriter
258+
var carWriter *deferred.DeferredCarWriter
257259
if outfile == stdoutFileString {
258260
// we need the onlyWriter because stdout is presented as an os.File, and
259261
// therefore pretend to support seeks, so feature-checking in go-car
260262
// will make bad assumptions about capabilities unless we hide it
261-
carWriter = storage.NewDeferredCarWriterForStream(rootCid, &onlyWriter{dataWriter})
263+
carWriter = deferred.NewDeferredCarWriterForStream(&onlyWriter{dataWriter}, []cid.Cid{rootCid})
262264
} else {
263-
carWriter = storage.NewDeferredCarWriterForPath(rootCid, outfile)
265+
carWriter = deferred.NewDeferredCarWriterForPath(outfile, []cid.Cid{rootCid}, car.WriteAsCarV1(true))
264266
}
265267

266268
tempStore := storage.NewDeferredStorageCar(tempDir, rootCid)

pkg/server/http/ipfs.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/filecoin-project/lassie/pkg/types"
1414
"github.com/ipfs/go-cid"
1515
"github.com/ipfs/go-unixfsnode"
16+
"github.com/ipld/go-car/v2/storage/deferred"
1617
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
1718
trustlessutils "github.com/ipld/go-trustless-utils"
1819
trustlesshttp "github.com/ipld/go-trustless-utils/http"
@@ -50,9 +51,9 @@ func IpfsHandler(fetcher types.Fetcher, cfg HttpServerConfig) func(http.Response
5051
tempStore := storage.NewDeferredStorageCar(cfg.TempDir, request.Root)
5152
var carWriter storage.DeferredWriter
5253
if request.Duplicates {
53-
carWriter = storage.NewDuplicateAdderCarForStream(req.Context(), request.Root, request.Path, request.Scope, request.Bytes, tempStore, res)
54+
carWriter = storage.NewDuplicateAdderCarForStream(req.Context(), res, request.Root, request.Path, request.Scope, request.Bytes, tempStore)
5455
} else {
55-
carWriter = storage.NewDeferredCarWriterForStream(request.Root, res)
56+
carWriter = deferred.NewDeferredCarWriterForStream(res, []cid.Cid{request.Root})
5657
}
5758
carStore := storage.NewCachingTempStore(carWriter.BlockWriteOpener(), tempStore)
5859
defer func() {

pkg/storage/cachingtempstore_test.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,21 @@ import (
44
"bytes"
55
"context"
66
"io"
7+
"math/rand"
8+
"sync"
79
"testing"
810
"time"
911

1012
"github.com/ipfs/go-cid"
1113
carv2 "github.com/ipld/go-car/v2"
14+
"github.com/ipld/go-car/v2/storage/deferred"
15+
mh "github.com/multiformats/go-multihash"
1216
"github.com/stretchr/testify/require"
1317
)
1418

19+
var rng = rand.New(rand.NewSource(3333))
20+
var rngLk sync.Mutex
21+
1522
func TestDeferredCarWriterWritesCARv1(t *testing.T) {
1623
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
1724
defer cancel()
@@ -48,7 +55,7 @@ func TestDeferredCarWriterWritesCARv1(t *testing.T) {
4855
testCid2, testData2 := randBlock()
4956

5057
var buf bytes.Buffer
51-
cw := NewDeferredCarWriterForStream(testCid1, &buf)
58+
cw := deferred.NewDeferredCarWriterForStream(&buf, []cid.Cid{testCid1})
5259
ss := NewCachingTempStore(cw.BlockWriteOpener(), NewDeferredStorageCar("", testCid1))
5360
t.Cleanup(func() { ss.Close() })
5461

@@ -155,3 +162,20 @@ func TestDeferredCarWriterWritesCARv1(t *testing.T) {
155162
})
156163
}
157164
}
165+
166+
func randBlock() (cid.Cid, []byte) {
167+
data := make([]byte, 1024)
168+
rngLk.Lock()
169+
rng.Read(data)
170+
rngLk.Unlock()
171+
h, err := mh.Sum(data, mh.SHA2_512, -1)
172+
if err != nil {
173+
panic(err)
174+
}
175+
return cid.NewCidV1(cid.Raw, h), data
176+
}
177+
178+
func randCid() cid.Cid {
179+
c, _ := randBlock()
180+
return c
181+
}

pkg/storage/deferredcarwriter.go

Lines changed: 0 additions & 160 deletions
This file was deleted.

0 commit comments

Comments
 (0)