diff --git a/addbatch.go b/addbatch.go index bba5a5d..974e0b4 100644 --- a/addbatch.go +++ b/addbatch.go @@ -172,10 +172,21 @@ func (t *Tree) AddBatchOpt(keys, values [][]byte) ([]int, error) { // CASE A: if nLeafs==0 (root==0) if bytes.Equal(t.root, t.emptyHash) { - // TODO if len(kvs) is not a power of 2, cut at the bigger power + // if len(kvs) is not a power of 2, cut at the bigger power // of two under len(kvs), build the tree with that, and add // later the excedents - return t.buildTreeBottomUp(nCPU, kvs) + kvsP2, kvsNonP2 := cutPowerOfTwo(kvs) + invalids, err := t.buildTreeBottomUp(nCPU, kvsP2) + if err != nil { + return nil, err + } + for i := 0; i < len(kvsNonP2); i++ { + err = t.add(0, kvsNonP2[i].k, kvsNonP2[i].v) + if err != nil { + invalids = append(invalids, kvsNonP2[i].pos) + } + } + return invalids, nil } // CASE B: if nLeafs