Merge pull request #24 from vocdoni/feature/addbatch-invalids-error-struct Update AddBatch to return invalids index + error
arnau 17317030+arnaucube@users.noreply.github.com
Mon, 11 Oct 2021 15:45:49 +0200
M
tree.go
→
tree.go
@@ -165,10 +165,17 @@ func (t *Tree) editable() bool {
return t.snapshotRoot == nil } +// Invalid is used when a key-value can not be added trough AddBatch, and +// contains the index of the key-value and the error. +type Invalid struct { + Index int + Error error +} + // AddBatch adds a batch of key-values to the Tree. Returns an array containing // the indexes of the keys failed to add. Supports empty values as input // parameters, which is equivalent to 0 valued byte array. -func (t *Tree) AddBatch(keys, values [][]byte) ([]int, error) { +func (t *Tree) AddBatch(keys, values [][]byte) ([]Invalid, error) { wTx := t.db.WriteTx() defer wTx.Discard()@@ -182,7 +189,7 @@
// AddBatchWithTx does the same than the AddBatch method, but allowing to pass // the db.WriteTx that is used. The db.WriteTx will not be committed inside // this method. -func (t *Tree) AddBatchWithTx(wTx db.WriteTx, keys, values [][]byte) ([]int, error) { +func (t *Tree) AddBatchWithTx(wTx db.WriteTx, keys, values [][]byte) ([]Invalid, error) { t.Lock() defer t.Unlock()
M
vt.go
→
vt.go
@@ -37,20 +37,17 @@ k []byte
v []byte } -func (p *params) keysValuesToKvs(ks, vs [][]byte) ([]kv, []int, error) { +func (p *params) keysValuesToKvs(ks, vs [][]byte) ([]kv, []Invalid, error) { if len(ks) != len(vs) { return nil, nil, fmt.Errorf("len(keys)!=len(values) (%d!=%d)", len(ks), len(vs)) } - var invalids []int + var invalids []Invalid var kvs []kv for i := 0; i < len(ks); i++ { keyPath, err := keyPathFromKey(p.maxLevels, ks[i]) if err != nil { - // TODO in a future iteration, invalids will contain - // the reason of the error of why each index is - // invalid. - invalids = append(invalids, i) + invalids = append(invalids, Invalid{i, err}) continue }@@ -90,13 +87,13 @@ // containing the indexes of the keys failed to add. Does not include the
// computation of hashes of the nodes neither the storage of the key-values of // the tree into the db. After addBatch, vt.computeHashes should be called to // compute the hashes of all the nodes of the tree. -func (t *vt) addBatch(ks, vs [][]byte) ([]int, error) { +func (t *vt) addBatch(ks, vs [][]byte) ([]Invalid, error) { nCPU := flp2(runtime.NumCPU()) if nCPU == 1 || len(ks) < nCPU { - var invalids []int + var invalids []Invalid for i := 0; i < len(ks); i++ { if err := t.add(0, ks[i], vs[i]); err != nil { - invalids = append(invalids, i) + invalids = append(invalids, Invalid{i, err}) } } return invalids, nil@@ -177,7 +174,7 @@ " https://github.com/vocdoni/arbo/issues/new", len(nodesAtL), nCPU)
} subRoots := make([]*node, nCPU) - invalidsInBucket := make([][]int, nCPU) + invalidsInBucket := make([][]Invalid, nCPU) var wg sync.WaitGroup wg.Add(nCPU)@@ -186,8 +183,10 @@ go func(cpu int) {
bucketVT := newVT(t.params.maxLevels, t.params.hashFunction) bucketVT.root = nodesAtL[cpu] for j := 0; j < len(buckets[cpu]); j++ { - if err := bucketVT.add(l, buckets[cpu][j].k, buckets[cpu][j].v); err != nil { - invalidsInBucket[cpu] = append(invalidsInBucket[cpu], buckets[cpu][j].pos) + if err := bucketVT.add(l, buckets[cpu][j].k, + buckets[cpu][j].v); err != nil { + invalidsInBucket[cpu] = append(invalidsInBucket[cpu], + Invalid{buckets[cpu][j].pos, err}) } } subRoots[cpu] = bucketVT.root