From 91041935852ab094e0202472d4f18af57a8c9cc9 Mon Sep 17 00:00:00 2001 From: Fabio Barone Date: Sun, 3 Feb 2019 19:19:22 -0500 Subject: [PATCH] swarm/storage: GetAllReferences returns all chunk references --- swarm/storage/filestore.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/swarm/storage/filestore.go b/swarm/storage/filestore.go index 2d8d82d95a..4894a846cf 100644 --- a/swarm/storage/filestore.go +++ b/swarm/storage/filestore.go @@ -96,3 +96,41 @@ func (f *FileStore) Store(ctx context.Context, data io.Reader, size int64, toEnc func (f *FileStore) HashSize() int { return f.hashFunc().Size() } + +// Public API. This endpoint returns all chunk hashes (only) for a given file +func (f *FileStore) GetAllReferences(ctx context.Context, data io.Reader) (addrs []Address, err error) { + var addrs = make([]Address, 0) + // create a special kind of putter, which only will store the references + putter := &HashExplorer{ + hasherStore: NewHasherStore(f.ChunkStore, f.hashFunc, false), + References: make([]Reference, 0), + } + // do the actual splitting anyway, no way around it + _, _, err := PyramidSplit(ctx, data, putter, putter) + if err != nil { + return nil, err + } + // collect all references + for _, ref := range putter.References { + addrs = append(addrs, ref) + } + return addrs, nil +} + +// HashExplorer is a special kind of putter which will only store chunk references +type HashExplorer struct { + *hasherStore + References []Reference +} + +// HashExplorer's Put will add just the chunk hashes to its `References` +func (he *HashExplorer) Put(ctx context.Context, chunkData ChunkData) (Reference, error) { + // Need to do the actual Put, which returns the references + ref, err := he.hasherStore.Put(ctx, chunkData) + if err != nil { + return nil, err + } + // internally store the reference + he.References = append(he.References, ref) + return ref, nil +}