From b3007a057e1140e7391d2964e4599ec041a6daea Mon Sep 17 00:00:00 2001 From: arnaucube Date: Sat, 24 Apr 2021 18:53:59 +0200 Subject: [PATCH] AddBatchOpt return invalid key positions --- addbatch.go | 47 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 14 deletions(-) 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