@@ -23,8 +23,6 @@ import (
2323 "hash"
2424 "io"
2525 "sync"
26- // "github.com/ethereum/go-ethereum/logger"
27- // "github.com/ethereum/go-ethereum/logger/glog"
2826)
2927
3028/*
@@ -124,12 +122,13 @@ func (self *TreeChunker) Split(data io.Reader, size int64, chunkC chan *Chunk, s
124122 jobC := make (chan * hashJob , 2 * processors )
125123 wg := & sync.WaitGroup {}
126124 errC := make (chan error )
125+ quitC := make (chan bool )
127126
128127 // wwg = workers waitgroup keeps track of hashworkers spawned by this split call
129128 if wwg != nil {
130129 wwg .Add (1 )
131130 }
132- go self .hashWorker (jobC , chunkC , errC , swg , wwg )
131+ go self .hashWorker (jobC , chunkC , errC , quitC , swg , wwg )
133132
134133 depth := 0
135134 treeSize := self .chunkSize
@@ -141,19 +140,17 @@ func (self *TreeChunker) Split(data io.Reader, size int64, chunkC chan *Chunk, s
141140 }
142141
143142 key := make ([]byte , self .hashFunc ().Size ())
144- // glog.V(logger.Detail).Infof("split request received for data (%v bytes, depth: %v)", size, depth)
145143 // this waitgroup member is released after the root hash is calculated
146144 wg .Add (1 )
147145 //launch actual recursive function passing the waitgroups
148- go self .split (depth , treeSize / self .branches , key , data , size , jobC , chunkC , errC , wg , swg , wwg )
146+ go self .split (depth , treeSize / self .branches , key , data , size , jobC , chunkC , errC , quitC , wg , swg , wwg )
149147
150148 // closes internal error channel if all subprocesses in the workgroup finished
151149 go func () {
152150 // waiting for all threads to finish
153151 wg .Wait ()
154152 // if storage waitgroup is non-nil, we wait for storage to finish too
155153 if swg != nil {
156- // glog.V(logger.Detail).Infof("Waiting for storage to finish")
157154 swg .Wait ()
158155 }
159156 close (errC )
@@ -162,14 +159,15 @@ func (self *TreeChunker) Split(data io.Reader, size int64, chunkC chan *Chunk, s
162159 select {
163160 case err := <- errC :
164161 if err != nil {
162+ close (quitC )
165163 return nil , err
166164 }
167- //
165+ //TODO: add a timeout
168166 }
169167 return key , nil
170168}
171169
172- func (self * TreeChunker ) split (depth int , treeSize int64 , key Key , data io.Reader , size int64 , jobC chan * hashJob , chunkC chan * Chunk , errC chan error , parentWg , swg , wwg * sync.WaitGroup ) {
170+ func (self * TreeChunker ) split (depth int , treeSize int64 , key Key , data io.Reader , size int64 , jobC chan * hashJob , chunkC chan * Chunk , errC chan error , quitC chan bool , parentWg , swg , wwg * sync.WaitGroup ) {
173171
174172 for depth > 0 && size < treeSize {
175173 treeSize /= self .branches
@@ -180,17 +178,20 @@ func (self *TreeChunker) split(depth int, treeSize int64, key Key, data io.Reade
180178 // leaf nodes -> content chunks
181179 chunkData := make ([]byte , size + 8 )
182180 binary .LittleEndian .PutUint64 (chunkData [0 :8 ], uint64 (size ))
183- data .Read (chunkData [8 :])
181+ _ , err := data .Read (chunkData [8 :])
182+ if err != nil {
183+ errC <- err
184+ return
185+ }
184186 select {
185187 case jobC <- & hashJob {key , chunkData , size , parentWg }:
186- case <- errC :
188+ case <- quitC :
187189 }
188- // glog.V(logger.Detail).Infof("read %v", size)
189190 return
190191 }
192+ // dept > 0
191193 // intermediate chunk containing child nodes hashes
192194 branchCnt := int64 ((size + treeSize - 1 ) / treeSize )
193- // glog.V(logger.Detail).Infof("intermediate node: setting branches: %v, depth: %v, max subtree size: %v, data size: %v", branches, depth, treeSize, size)
194195
195196 var chunk []byte = make ([]byte , branchCnt * self .hashSize + 8 )
196197 var pos , i int64
@@ -210,7 +211,7 @@ func (self *TreeChunker) split(depth int, treeSize int64, key Key, data io.Reade
210211 subTreeKey := chunk [8 + i * self .hashSize : 8 + (i + 1 )* self .hashSize ]
211212
212213 childrenWg .Add (1 )
213- self .split (depth - 1 , treeSize / self .branches , subTreeKey , data , secSize , jobC , chunkC , errC , childrenWg , swg , wwg )
214+ self .split (depth - 1 , treeSize / self .branches , subTreeKey , data , secSize , jobC , chunkC , errC , quitC , childrenWg , swg , wwg )
214215
215216 i ++
216217 pos += treeSize
@@ -224,15 +225,15 @@ func (self *TreeChunker) split(depth int, treeSize int64, key Key, data io.Reade
224225 wwg .Add (1 )
225226 }
226227 self .workerCount ++
227- go self .hashWorker (jobC , chunkC , errC , swg , wwg )
228+ go self .hashWorker (jobC , chunkC , errC , quitC , swg , wwg )
228229 }
229230 select {
230231 case jobC <- & hashJob {key , chunk , size , parentWg }:
231- case <- errC :
232+ case <- quitC :
232233 }
233234}
234235
235- func (self * TreeChunker ) hashWorker (jobC chan * hashJob , chunkC chan * Chunk , errC chan error , swg , wwg * sync.WaitGroup ) {
236+ func (self * TreeChunker ) hashWorker (jobC chan * hashJob , chunkC chan * Chunk , errC chan error , quitC chan bool , swg , wwg * sync.WaitGroup ) {
236237 hasher := self .hashFunc ()
237238 if wwg != nil {
238239 defer wwg .Done ()
@@ -247,8 +248,7 @@ func (self *TreeChunker) hashWorker(jobC chan *hashJob, chunkC chan *Chunk, errC
247248 // now we got the hashes in the chunk, then hash the chunks
248249 hasher .Reset ()
249250 self .hashChunk (hasher , job , chunkC , swg )
250- // glog.V(logger.Detail).Infof("hash chunk (%v)", job.size)
251- case <- errC :
251+ case <- quitC :
252252 return
253253 }
254254 }
@@ -276,6 +276,7 @@ func (self *TreeChunker) hashChunk(hasher hash.Hash, job *hashJob, chunkC chan *
276276 }
277277 }
278278 job .parentWg .Done ()
279+
279280 if chunkC != nil {
280281 chunkC <- newChunk
281282 }
@@ -328,21 +329,17 @@ func (self *LazyChunkReader) Size(quitC chan bool) (n int64, err error) {
328329func (self * LazyChunkReader ) ReadAt (b []byte , off int64 ) (read int , err error ) {
329330 // this is correct, a swarm doc cannot be zero length, so no EOF is expected
330331 if len (b ) == 0 {
331- // glog.V(logger.Detail).Infof("Size query for %v", chunk.Key.Log())
332332 return 0 , nil
333333 }
334334 quitC := make (chan bool )
335335 size , err := self .Size (quitC )
336336 if err != nil {
337337 return 0 , err
338338 }
339- // glog.V(logger.Detail).Infof("readAt: len(b): %v, off: %v, size: %v ", len(b), off, size)
340339
341340 errC := make (chan error )
342- // glog.V(logger.Detail).Infof("readAt: reading %v into %d bytes at offset %d.", self.chunk.Key.Log(), len(b), off)
343341
344342 // }
345- // glog.V(logger.Detail).Infof("-> want: %v, off: %v size: %v ", want, off, self.size)
346343 var treeSize int64
347344 var depth int
348345 // calculate depth and max treeSize
@@ -364,22 +361,16 @@ func (self *LazyChunkReader) ReadAt(b []byte, off int64) (read int, err error) {
364361
365362 return 0 , err
366363 }
367- // glog.V(logger.Detail).Infof("ReadAt received %v", err)
368- // glog.V(logger.Detail).Infof("end: len(b): %v, off: %v, size: %v ", len(b), off, size)
369364 if off + int64 (len (b )) >= size {
370- // glog.V(logger.Detail).Infof(" len(b): %v EOF", len(b))
371365 return len (b ), io .EOF
372366 }
373- // glog.V(logger.Detail).Infof("ReadAt returning at %d: %v", read, err)
374367 return len (b ), nil
375368}
376369
377370func (self * LazyChunkReader ) join (b []byte , off int64 , eoff int64 , depth int , treeSize int64 , chunk * Chunk , parentWg * sync.WaitGroup , errC chan error , quitC chan bool ) {
378371 defer parentWg .Done ()
379372 // return NewDPA(&LocalStore{})
380- // glog.V(logger.Detail).Infof("inh len(b): %v, off: %v eoff: %v ", len(b), off, eoff)
381373
382- // glog.V(logger.Detail).Infof("depth: %v, loff: %v, eoff: %v, chunk.Size: %v, treeSize: %v", depth, off, eoff, chunk.Size, treeSize)
383374
384375 // chunk.Size = int64(binary.LittleEndian.Uint64(chunk.SData[0:8]))
385376
@@ -391,7 +382,6 @@ func (self *LazyChunkReader) join(b []byte, off int64, eoff int64, depth int, tr
391382
392383 // leaf chunk found
393384 if depth == 0 {
394- // glog.V(logger.Detail).Infof("depth: %v, len(b): %v, off: %v, eoff: %v, chunk.Size: %v %v, treeSize: %v", depth, len(b), off, eoff, chunk.Size, len(chunk.SData), treeSize)
395385 extra := 8 + eoff - int64 (len (chunk .SData ))
396386 if extra > 0 {
397387 eoff -= extra
@@ -406,7 +396,6 @@ func (self *LazyChunkReader) join(b []byte, off int64, eoff int64, depth int, tr
406396
407397 wg := & sync.WaitGroup {}
408398 defer wg .Wait ()
409- // glog.V(logger.Detail).Infof("start %v,end %v", start, end)
410399
411400 for i := start ; i < end ; i ++ {
412401 soff := i * treeSize
@@ -425,7 +414,6 @@ func (self *LazyChunkReader) join(b []byte, off int64, eoff int64, depth int, tr
425414 wg .Add (1 )
426415 go func (j int64 ) {
427416 childKey := chunk .SData [8 + j * self .hashSize : 8 + (j + 1 )* self .hashSize ]
428- // glog.V(logger.Detail).Infof("subtree ind.ex: %v -> %v", j, childKey.Log())
429417 chunk := retrieve (childKey , self .chunkC , quitC )
430418 if chunk == nil {
431419 select {
@@ -450,7 +438,6 @@ func retrieve(key Key, chunkC chan *Chunk, quitC chan bool) *Chunk {
450438 Key : key ,
451439 C : make (chan bool ), // close channel to signal data delivery
452440 }
453- // glog.V(logger.Detail).Infof("chunk data sent for %v (key interval in chunk %v-%v)", ch.Key.Log(), j*self.chunker.hashSize, (j+1)*self.chunker.hashSize)
454441 // submit chunk for retrieval
455442 select {
456443 case chunkC <- chunk : // submit retrieval request, someone should be listening on the other side (or we will time out globally)
@@ -464,7 +451,6 @@ func retrieve(key Key, chunkC chan *Chunk, quitC chan bool) *Chunk {
464451 // this is how we control process leakage (quitC is closed once join is finished (after timeout))
465452 return nil
466453 case <- chunk .C : // bells are ringing, data have been delivered
467- // glog.V(logger.Detail).Infof("chunk data received")
468454 }
469455 if len (chunk .SData ) == 0 {
470456 return nil // chunk.Size = int64(binary.LittleEndian.Uint64(chunk.SData[0:8]))
@@ -476,7 +462,6 @@ func retrieve(key Key, chunkC chan *Chunk, quitC chan bool) *Chunk {
476462// Read keeps a cursor so cannot be called simulateously, see ReadAt
477463func (self * LazyChunkReader ) Read (b []byte ) (read int , err error ) {
478464 read , err = self .ReadAt (b , self .off )
479- // glog.V(logger.Detail).Infof("read: %v, off: %v, error: %v", read, self.off, err)
480465
481466 self .off += int64 (read )
482467 return
0 commit comments