@@ -20,6 +20,7 @@ import (
20
20
"context"
21
21
"io"
22
22
"sort"
23
+ "sync"
23
24
)
24
25
25
26
/*
@@ -101,38 +102,45 @@ func (f *FileStore) HashSize() int {
101
102
// GetAllReferences is a public API. This endpoint returns all chunk hashes (only) for a given file
102
103
func (f * FileStore ) GetAllReferences (ctx context.Context , data io.Reader , toEncrypt bool ) (addrs AddressCollection , err error ) {
103
104
// create a special kind of putter, which only will store the references
104
- putter := & HashExplorer {
105
+ putter := & hashExplorer {
105
106
hasherStore : NewHasherStore (f .ChunkStore , f .hashFunc , toEncrypt ),
106
- References : make ([]Reference , 0 ),
107
107
}
108
108
// do the actual splitting anyway, no way around it
109
- _ , _ , err = PyramidSplit (ctx , data , putter , putter )
109
+ _ , wait , err := PyramidSplit (ctx , data , putter , putter )
110
+ if err != nil {
111
+ return nil , err
112
+ }
113
+ // wait for splitting to be complete and all chunks processed
114
+ err = wait (ctx )
110
115
if err != nil {
111
116
return nil , err
112
117
}
113
118
// collect all references
114
119
addrs = NewAddressCollection (0 )
115
- for _ , ref := range putter .References {
120
+ for _ , ref := range putter .references {
116
121
addrs = append (addrs , Address (ref ))
117
122
}
118
123
sort .Sort (addrs )
119
124
return addrs , nil
120
125
}
121
126
122
- // HashExplorer is a special kind of putter which will only store chunk references
123
- type HashExplorer struct {
127
+ // hashExplorer is a special kind of putter which will only store chunk references
128
+ type hashExplorer struct {
124
129
* hasherStore
125
- References []Reference
130
+ references []Reference
131
+ lock sync.Mutex
126
132
}
127
133
128
134
// HashExplorer's Put will add just the chunk hashes to its `References`
129
- func (he * HashExplorer ) Put (ctx context.Context , chunkData ChunkData ) (Reference , error ) {
135
+ func (he * hashExplorer ) Put (ctx context.Context , chunkData ChunkData ) (Reference , error ) {
130
136
// Need to do the actual Put, which returns the references
131
137
ref , err := he .hasherStore .Put (ctx , chunkData )
132
138
if err != nil {
133
139
return nil , err
134
140
}
135
141
// internally store the reference
136
- he .References = append (he .References , ref )
142
+ he .lock .Lock ()
143
+ he .references = append (he .references , ref )
144
+ he .lock .Unlock ()
137
145
return ref , nil
138
146
}
0 commit comments