From 8f492f775299e3dfd7875b88dbde4690c596b403 Mon Sep 17 00:00:00 2001 From: whyrusleeping Date: Mon, 2 Dec 2024 19:13:22 -0800 Subject: [PATCH] return written blocks in closing deltasession --- carstore/bs.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/carstore/bs.go b/carstore/bs.go index 3acfaccc0..dbce4f098 100644 --- a/carstore/bs.go +++ b/carstore/bs.go @@ -607,7 +607,11 @@ func (ds *DeltaSession) CloseWithRoot(ctx context.Context, root cid.Cid, rev str case *FileCarStore: return ocs.writeNewShard(ctx, root, rev, ds.user, ds.seq, ds.blks, ds.rmcids) case *NonArchivalCarstore: - return nil, ocs.updateLastCommit(ctx, ds.user, rev, root) + slice, err := blocksToCar(ctx, root, rev, ds.blks) + if err != nil { + return nil, err + } + return slice, ocs.updateLastCommit(ctx, ds.user, rev, root) default: return nil, fmt.Errorf("unsupported carstore type") } @@ -631,6 +635,23 @@ func WriteCarHeader(w io.Writer, root cid.Cid) (int64, error) { return hnw, nil } +func blocksToCar(ctx context.Context, root cid.Cid, rev string, blks map[cid.Cid]blockformat.Block) ([]byte, error) { + buf := new(bytes.Buffer) + _, err := WriteCarHeader(buf, root) + if err != nil { + return nil, fmt.Errorf("failed to write car header: %w", err) + } + + for k, blk := range blks { + _, err := LdWrite(buf, k.Bytes(), blk.RawData()) + if err != nil { + return nil, fmt.Errorf("failed to write block: %w", err) + } + } + + return buf.Bytes(), nil +} + func (cs *FileCarStore) writeNewShard(ctx context.Context, root cid.Cid, rev string, user models.Uid, seq int, blks map[cid.Cid]blockformat.Block, rmcids map[cid.Cid]bool) ([]byte, error) { buf := new(bytes.Buffer)