chunker and root key fix

- chunker panics if root key buffer is not allocated or have proper length
- rename HashSize->KeySize and add it to Chunker interface
This commit is contained in:
zelig 2015-02-07 07:09:43 +01:00
parent 3f328c20b2
commit 22249ac2b9
2 changed files with 15 additions and 9 deletions

View file

@ -26,7 +26,7 @@ func main() {
sr := io.NewSectionReader(f, 0, stat.Size()) sr := io.NewSectionReader(f, 0, stat.Size())
chunker := &bzz.TreeChunker{} chunker := &bzz.TreeChunker{}
chunker.Init() chunker.Init()
hash := make([]byte, chunker.HashSize()) hash := make([]byte, chunker.KeySize())
errC := chunker.Split(hash, sr, nil) errC := chunker.Split(hash, sr, nil)
err, ok := <-errC err, ok := <-errC
if err != nil { if err != nil {

View file

@ -72,6 +72,9 @@ type Chunker interface {
Lifecycle of the reader can be modified with SetTimeout() Lifecycle of the reader can be modified with SetTimeout()
*/ */
Join(key Key, chunkC chan *Chunk) (LazySectionReader, chan error) Join(key Key, chunkC chan *Chunk) (LazySectionReader, chan error)
// returns the key length
KeySize() int64
} }
/* /*
@ -111,7 +114,7 @@ func (self *TreeChunker) Init() {
} }
func (self *TreeChunker) HashSize() int64 { func (self *TreeChunker) KeySize() int64 {
return self.hashSize return self.hashSize
} }
@ -143,13 +146,20 @@ func (self *TreeChunker) Hash(size int64, input SectionReader) []byte {
} }
func (self *TreeChunker) Split(key Key, data SectionReader, chunkC chan *Chunk) (errC chan error) { func (self *TreeChunker) Split(key Key, data SectionReader, chunkC chan *Chunk) (errC chan error) {
if self.chunkSize <= 0 {
panic("chunker must be initialised")
}
if int64(len(key)) != self.hashSize {
panic(fmt.Sprintf("root key buffer must be allocated byte slice of length %d", self.hashSize))
}
wg := &sync.WaitGroup{} wg := &sync.WaitGroup{}
errC = make(chan error) errC = make(chan error)
rerrC := make(chan error) rerrC := make(chan error)
timeout := time.After(self.SplitTimeout) timeout := time.After(self.SplitTimeout)
if key == nil {
key = make([]byte, self.hashSize)
}
wg.Add(1) wg.Add(1)
go func() { go func() {
@ -159,10 +169,6 @@ func (self *TreeChunker) Split(key Key, data SectionReader, chunkC chan *Chunk)
// takes lowest depth such that chunksize*HashCount^(depth+1) > size // takes lowest depth such that chunksize*HashCount^(depth+1) > size
// power series, will find the order of magnitude of the data size in base hashCount or numbers of levels of branching in the resulting tree. // power series, will find the order of magnitude of the data size in base hashCount or numbers of levels of branching in the resulting tree.
if self.Branches <= 1 || self.chunkSize <= 0 {
panic("chunker must be initialised")
}
for ; treeSize < size; treeSize *= self.Branches { for ; treeSize < size; treeSize *= self.Branches {
depth++ depth++
} }