aboutsummaryrefslogtreecommitdiffstats
path: root/swarm/storage/filestore.go
diff options
context:
space:
mode:
authorholisticode <holistic.computing@gmail.com>2019-02-06 19:16:43 +0800
committerRafael Matias <rafael@skyle.net>2019-02-19 19:57:53 +0800
commit034f65e9e8976f3ca7d9622e4c362ad9c26ad5f7 (patch)
treec7a16205a52a88e27b625b952bb3c0a4772248f6 /swarm/storage/filestore.go
parent607a1968e6cc28194565242cc7d1faa7b2c8a44e (diff)
downloaddexon-034f65e9e8976f3ca7d9622e4c362ad9c26ad5f7.tar
dexon-034f65e9e8976f3ca7d9622e4c362ad9c26ad5f7.tar.gz
dexon-034f65e9e8976f3ca7d9622e4c362ad9c26ad5f7.tar.bz2
dexon-034f65e9e8976f3ca7d9622e4c362ad9c26ad5f7.tar.lz
dexon-034f65e9e8976f3ca7d9622e4c362ad9c26ad5f7.tar.xz
dexon-034f65e9e8976f3ca7d9622e4c362ad9c26ad5f7.tar.zst
dexon-034f65e9e8976f3ca7d9622e4c362ad9c26ad5f7.zip
swarm/storage: Get all chunk references for a given file (#19002)
(cherry picked from commit 3eff652a7b606f25d43bef6ccb998b8e306f8a75)
Diffstat (limited to 'swarm/storage/filestore.go')
-rw-r--r--swarm/storage/filestore.go40
1 files changed, 40 insertions, 0 deletions
diff --git a/swarm/storage/filestore.go b/swarm/storage/filestore.go
index 2d8d82d95..aebe03c1e 100644
--- a/swarm/storage/filestore.go
+++ b/swarm/storage/filestore.go
@@ -19,6 +19,7 @@ package storage
import (
"context"
"io"
+ "sort"
)
/*
@@ -96,3 +97,42 @@ 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, toEncrypt bool) (addrs AddressCollection, err error) {
+ // create a special kind of putter, which only will store the references
+ putter := &HashExplorer{
+ hasherStore: NewHasherStore(f.ChunkStore, f.hashFunc, toEncrypt),
+ 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
+ addrs = NewAddressCollection(0)
+ for _, ref := range putter.References {
+ addrs = append(addrs, Address(ref))
+ }
+ sort.Sort(addrs)
+ 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
+}