@@ -19,6 +19,7 @@ package storage
19
19
import (
20
20
"context"
21
21
"io"
22
+ "sort"
22
23
)
23
24
24
25
/*
@@ -96,3 +97,42 @@ func (f *FileStore) Store(ctx context.Context, data io.Reader, size int64, toEnc
96
97
func (f * FileStore ) HashSize () int {
97
98
return f .hashFunc ().Size ()
98
99
}
100
+
101
+ // Public API. This endpoint returns all chunk hashes (only) for a given file
102
+ func (f * FileStore ) GetAllReferences (ctx context.Context , data io.Reader , toEncrypt bool ) (addrs AddressCollection , err error ) {
103
+ // create a special kind of putter, which only will store the references
104
+ putter := & HashExplorer {
105
+ hasherStore : NewHasherStore (f .ChunkStore , f .hashFunc , toEncrypt ),
106
+ References : make ([]Reference , 0 ),
107
+ }
108
+ // do the actual splitting anyway, no way around it
109
+ _ , _ , err = PyramidSplit (ctx , data , putter , putter )
110
+ if err != nil {
111
+ return nil , err
112
+ }
113
+ // collect all references
114
+ addrs = NewAddressCollection (0 )
115
+ for _ , ref := range putter .References {
116
+ addrs = append (addrs , Address (ref ))
117
+ }
118
+ sort .Sort (addrs )
119
+ return addrs , nil
120
+ }
121
+
122
+ // HashExplorer is a special kind of putter which will only store chunk references
123
+ type HashExplorer struct {
124
+ * hasherStore
125
+ References []Reference
126
+ }
127
+
128
+ // HashExplorer's Put will add just the chunk hashes to its `References`
129
+ func (he * HashExplorer ) Put (ctx context.Context , chunkData ChunkData ) (Reference , error ) {
130
+ // Need to do the actual Put, which returns the references
131
+ ref , err := he .hasherStore .Put (ctx , chunkData )
132
+ if err != nil {
133
+ return nil , err
134
+ }
135
+ // internally store the reference
136
+ he .References = append (he .References , ref )
137
+ return ref , nil
138
+ }
0 commit comments