fileservice.HasCid

This commit is contained in:
Sergey Cherepanov 2023-01-11 15:39:32 +03:00 committed by Mikhail Iudin
parent 302a65bedb
commit a20f0771f1
No known key found for this signature in database
GPG Key ID: FAAAA8BAABDFF1C0

View File

@ -33,10 +33,12 @@ type FileService interface {
AddFile(ctx context.Context, r io.Reader) (ipld.Node, error) AddFile(ctx context.Context, r io.Reader) (ipld.Node, error)
// DAGService returns ipld.DAGService object // DAGService returns ipld.DAGService object
DAGService() ipld.DAGService DAGService() ipld.DAGService
// HasCid checks is CID exists
HasCid(ctx context.Context, c cid.Cid) (exists bool, err error)
app.Component app.Component
} }
type fileService struct { type fileService struct {
bs fileblockstore.BlockStoreLocal
merkledag ipld.DAGService merkledag ipld.DAGService
prefix cid.Prefix prefix cid.Prefix
} }
@ -52,8 +54,8 @@ func (fs *fileService) Init(a *app.App) (err error) {
} }
prefix.MhType = hashFunCode prefix.MhType = hashFunCode
prefix.MhLength = -1 prefix.MhLength = -1
bs := newBlockService(a.MustComponent(fileblockstore.CName).(fileblockstore.BlockStore)) fs.bs = a.MustComponent(fileblockstore.CName).(fileblockstore.BlockStoreLocal)
fs.merkledag = merkledag.NewDAGService(bs) fs.merkledag = merkledag.NewDAGService(newBlockService(fs.bs))
fs.prefix = prefix fs.prefix = prefix
return return
} }
@ -92,3 +94,11 @@ func (fs *fileService) GetFile(ctx context.Context, c cid.Cid) (ufsio.ReadSeekCl
} }
return ufsio.NewDagReader(ctx, n, fs.merkledag) return ufsio.NewDagReader(ctx, n, fs.merkledag)
} }
func (fs *fileService) HasCid(ctx context.Context, c cid.Cid) (exists bool, err error) {
res, err := fs.bs.ExistsCids(ctx, []cid.Cid{c})
if err != nil {
return
}
return len(res) > 0, nil
}