Skip to content

Commit d2c9d3a

Browse files
committed
chore: extract DeferredCarWriter to go-car
Ref: ipld/go-car#493
1 parent 1129833 commit d2c9d3a

File tree

9 files changed

+57
-402
lines changed

9 files changed

+57
-402
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)

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ require (
2222
github.com/ipfs/go-ipld-format v0.6.0
2323
github.com/ipfs/go-log/v2 v2.5.1
2424
github.com/ipfs/go-unixfsnode v1.8.0
25-
github.com/ipld/go-car/v2 v2.11.0
25+
github.com/ipld/go-car/v2 v2.12.1-0.20230904004620-0621cff7a758
2626
github.com/ipld/go-codec-dagpb v1.6.0
2727
github.com/ipld/go-ipld-prime v0.21.0
2828
github.com/ipld/go-trustless-utils v0.0.0
@@ -90,7 +90,7 @@ require (
9090
github.com/ipfs/go-ipfs-ds-help v1.1.0 // indirect
9191
github.com/ipfs/go-ipfs-pq v0.0.3 // indirect
9292
github.com/ipfs/go-ipfs-util v0.0.2 // indirect
93-
github.com/ipfs/go-ipld-cbor v0.0.6 // indirect
93+
github.com/ipfs/go-ipld-cbor v0.1.0 // indirect
9494
github.com/ipfs/go-log v1.0.5 // indirect
9595
github.com/ipfs/go-metrics-interface v0.0.1 // indirect
9696
github.com/ipfs/go-peertaskqueue v0.8.1 // indirect
@@ -149,7 +149,7 @@ require (
149149
github.com/spaolacci/murmur3 v1.1.0 // indirect
150150
github.com/warpfork/go-testmark v0.12.1 // indirect
151151
github.com/whyrusleeping/cbor v0.0.0-20171005072247-63513f603b11 // indirect
152-
github.com/whyrusleeping/cbor-gen v0.0.0-20230126041949-52956bd4c9aa // indirect
152+
github.com/whyrusleeping/cbor-gen v0.0.0-20230818171029-f91ae536ca25 // indirect
153153
github.com/whyrusleeping/chunker v0.0.0-20181014151217-fe64bd25879f // indirect
154154
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
155155
go.opentelemetry.io/otel/sdk v1.14.0 // indirect

go.sum

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,9 @@ github.com/ipfs/go-ipld-cbor v0.0.3/go.mod h1:wTBtrQZA3SoFKMVkp6cn6HMRteIB1VsmHA
288288
github.com/ipfs/go-ipld-cbor v0.0.4/go.mod h1:BkCduEx3XBCO6t2Sfo5BaHzuok7hbhdMm9Oh8B2Ftq4=
289289
github.com/ipfs/go-ipld-cbor v0.0.5/go.mod h1:BkCduEx3XBCO6t2Sfo5BaHzuok7hbhdMm9Oh8B2Ftq4=
290290
github.com/ipfs/go-ipld-cbor v0.0.6-0.20211211231443-5d9b9e1f6fa8/go.mod h1:ssdxxaLJPXH7OjF5V4NSjBbcfh+evoR4ukuru0oPXMA=
291-
github.com/ipfs/go-ipld-cbor v0.0.6 h1:pYuWHyvSpIsOOLw4Jy7NbBkCyzLDcl64Bf/LZW7eBQ0=
292291
github.com/ipfs/go-ipld-cbor v0.0.6/go.mod h1:ssdxxaLJPXH7OjF5V4NSjBbcfh+evoR4ukuru0oPXMA=
292+
github.com/ipfs/go-ipld-cbor v0.1.0 h1:dx0nS0kILVivGhfWuB6dUpMa/LAwElHPw1yOGYopoYs=
293+
github.com/ipfs/go-ipld-cbor v0.1.0/go.mod h1:U2aYlmVrJr2wsUBU67K4KgepApSZddGRDWBYR0H4sCk=
293294
github.com/ipfs/go-ipld-format v0.0.1/go.mod h1:kyJtbkDALmFHv3QR6et67i35QzO3S0dCDnkOJhcZkms=
294295
github.com/ipfs/go-ipld-format v0.0.2/go.mod h1:4B6+FM2u9OJ9zCV+kSbgFAZlOrv1Hqbf0INGQgiKf9k=
295296
github.com/ipfs/go-ipld-format v0.6.0 h1:VEJlA2kQ3LqFSIm5Vu6eIlSxD/Ze90xtc4Meten1F5U=
@@ -317,8 +318,8 @@ github.com/ipfs/go-unixfs v0.4.5 h1:wj8JhxvV1G6CD7swACwSKYa+NgtdWC1RUit+gFnymDU=
317318
github.com/ipfs/go-unixfsnode v1.8.0 h1:yCkakzuE365glu+YkgzZt6p38CSVEBPgngL9ZkfnyQU=
318319
github.com/ipfs/go-unixfsnode v1.8.0/go.mod h1:HxRu9HYHOjK6HUqFBAi++7DVoWAHn0o4v/nZ/VA+0g8=
319320
github.com/ipfs/go-verifcid v0.0.2 h1:XPnUv0XmdH+ZIhLGKg6U2vaPaRDXb9urMyNVCE7uvTs=
320-
github.com/ipld/go-car/v2 v2.11.0 h1:lkAPwbbTFqbdfawgm+bfmFc8PjGC7D12VcaLXPCLNfM=
321-
github.com/ipld/go-car/v2 v2.11.0/go.mod h1:aDszqev0zjtU8l96g4lwXHaU9bzArj56Y7eEN0q/xqA=
321+
github.com/ipld/go-car/v2 v2.12.1-0.20230904004620-0621cff7a758 h1:VIPmhpq7uRkM4loVAfF3p3lyIBxUMC6AkW/jTf45axM=
322+
github.com/ipld/go-car/v2 v2.12.1-0.20230904004620-0621cff7a758/go.mod h1:QkdjjFNGit2GIkpQ953KBwowuoukoM75nP/JI1iDJdo=
322323
github.com/ipld/go-codec-dagpb v1.6.0 h1:9nYazfyu9B1p3NAgfVdpRco3Fs2nFC72DqVsMj6rOcc=
323324
github.com/ipld/go-codec-dagpb v1.6.0/go.mod h1:ANzFhfP2uMJxRBr8CE+WQWs5UsNa0pYtmKZ+agnUw9s=
324325
github.com/ipld/go-ipld-prime v0.21.0 h1:n4JmcpOlPDIxBcY037SVfpd1G+Sj1nKZah0m6QH9C2E=
@@ -608,8 +609,8 @@ github.com/whyrusleeping/cbor-gen v0.0.0-20200812213548-958ddffe352c/go.mod h1:f
608609
github.com/whyrusleeping/cbor-gen v0.0.0-20200826160007-0b9f6c5fb163/go.mod h1:fgkXqYy7bV2cFeIEOkVTZS/WjXARfBqSH6Q2qHL33hQ=
609610
github.com/whyrusleeping/cbor-gen v0.0.0-20210118024343-169e9d70c0c2/go.mod h1:fgkXqYy7bV2cFeIEOkVTZS/WjXARfBqSH6Q2qHL33hQ=
610611
github.com/whyrusleeping/cbor-gen v0.0.0-20210303213153-67a261a1d291/go.mod h1:fgkXqYy7bV2cFeIEOkVTZS/WjXARfBqSH6Q2qHL33hQ=
611-
github.com/whyrusleeping/cbor-gen v0.0.0-20230126041949-52956bd4c9aa h1:EyA027ZAkuaCLoxVX4r1TZMPy1d31fM6hbfQ4OU4I5o=
612-
github.com/whyrusleeping/cbor-gen v0.0.0-20230126041949-52956bd4c9aa/go.mod h1:fgkXqYy7bV2cFeIEOkVTZS/WjXARfBqSH6Q2qHL33hQ=
612+
github.com/whyrusleeping/cbor-gen v0.0.0-20230818171029-f91ae536ca25 h1:yVYDLoN2gmB3OdBXFW8e1UwgVbmCvNlnAKhvHPaNARI=
613+
github.com/whyrusleeping/cbor-gen v0.0.0-20230818171029-f91ae536ca25/go.mod h1:fgkXqYy7bV2cFeIEOkVTZS/WjXARfBqSH6Q2qHL33hQ=
613614
github.com/whyrusleeping/chunker v0.0.0-20181014151217-fe64bd25879f h1:jQa4QT2UP9WYv2nzyawpKMOCl+Z/jW7djv2/J50lj9E=
614615
github.com/whyrusleeping/chunker v0.0.0-20181014151217-fe64bd25879f/go.mod h1:p9UJB6dDgdPgMJZs7UjUOdulKyRr9fqkS+6JKAInPy8=
615616
github.com/whyrusleeping/go-logging v0.0.0-20170515211332-0457bb6b88fc/go.mod h1:bopw91TMyo8J3tvftk8xmU2kPmlrt4nScJQZU2hE5EM=

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)