···77 "net/http"
88 "strings"
991010+ "github.com/bluesky-social/indigo/repo"
1111+ "github.com/bluesky-social/indigo/util"
1012 "github.com/ipfs/go-cid"
1113 "github.com/ipld/go-car"
1214 carutil "github.com/ipld/go-car/util"
···273275 return
274276 }
275277276276- // Get the record CID and raw bytes
277277- recordCID, _, err := h.pds.GetCrewMember(r.Context(), rkey)
278278+ // Get the current repo head
279279+ repoHead, err := h.pds.carstore.GetUserRepoHead(r.Context(), h.pds.uid)
280280+ if err != nil {
281281+ http.Error(w, fmt.Sprintf("failed to get repo head: %v", err), http.StatusInternalServerError)
282282+ return
283283+ }
284284+285285+ // Create a new delta session with logging blockstore
286286+ tempSession, err := h.pds.carstore.NewDeltaSession(r.Context(), h.pds.uid, nil)
287287+ if err != nil {
288288+ http.Error(w, fmt.Sprintf("failed to create temp session: %v", err), http.StatusInternalServerError)
289289+ return
290290+ }
291291+292292+ // Wrap the session's blockstore with a logging blockstore
293293+ loggingBS := util.NewLoggingBstore(tempSession)
294294+295295+ // Open the repo with the logging blockstore
296296+ tempRepo, err := repo.OpenRepo(r.Context(), loggingBS, repoHead)
278297 if err != nil {
279279- http.Error(w, fmt.Sprintf("failed to get record: %v", err), http.StatusNotFound)
298298+ http.Error(w, fmt.Sprintf("failed to open repo: %v", err), http.StatusInternalServerError)
280299 return
281300 }
282301283283- // Get the raw block from the blockstore
284284- blk, err := h.pds.repo.Blockstore().Get(r.Context(), recordCID)
302302+ // Get the record path
303303+ path := fmt.Sprintf("%s/%s", collection, rkey)
304304+305305+ // Get the record (this will log all accessed blocks in the MST path)
306306+ recordCID, _, err := tempRepo.GetRecordBytes(r.Context(), path)
285307 if err != nil {
286286- http.Error(w, fmt.Sprintf("failed to get record block: %v", err), http.StatusInternalServerError)
308308+ http.Error(w, fmt.Sprintf("failed to get record: %v", err), http.StatusNotFound)
287309 return
288310 }
289311290290- // Write CAR file with the single record
312312+ // Get all blocks that were accessed during record retrieval
313313+ blocks := loggingBS.GetLoggedBlocks()
314314+315315+ // Log block count for debugging
316316+ fmt.Printf("sync.getRecord: Retrieved %d blocks for record at %s/%s\n", len(blocks), collection, rkey)
317317+ for i, blk := range blocks {
318318+ fmt.Printf(" Block %d: CID=%s, size=%d bytes\n", i+1, blk.Cid().String(), len(blk.RawData()))
319319+ }
320320+321321+ // Write CAR file with all accessed blocks
291322 w.Header().Set("Content-Type", "application/vnd.ipld.car")
292323293324 // Create a buffer to write the CAR data
···305336 return
306337 }
307338308308- // Write the block using LdWrite
309309- if err := carutil.LdWrite(&buf, recordCID.Bytes(), blk.RawData()); err != nil {
310310- http.Error(w, fmt.Sprintf("failed to write block to CAR: %v", err), http.StatusInternalServerError)
311311- return
339339+ // Write all logged blocks to the CAR file
340340+ for _, blk := range blocks {
341341+ if err := carutil.LdWrite(&buf, blk.Cid().Bytes(), blk.RawData()); err != nil {
342342+ http.Error(w, fmt.Sprintf("failed to write block to CAR: %v", err), http.StatusInternalServerError)
343343+ return
344344+ }
312345 }
313346314347 // Write the CAR data to the response