You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

774 lines
19 KiB

  1. package arbo
  2. import (
  3. "bytes"
  4. "fmt"
  5. "math"
  6. "runtime"
  7. "sort"
  8. "sync"
  9. "github.com/iden3/go-merkletree/db"
  10. )
  11. /*
  12. AddBatch design
  13. ===============
  14. CASE A: Empty Tree --> if tree is empty (root==0)
  15. =================================================
  16. - Build the full tree from bottom to top (from all the leaf to the root)
  17. CASE B: ALMOST CASE A, Almost empty Tree --> if Tree has numLeafs < minLeafsThreshold
  18. ==============================================================================
  19. - Get the Leafs (key & value) (iterate the tree from the current root getting
  20. the leafs)
  21. - Create a new empty Tree
  22. - Do CASE A for the new Tree, giving the already existing key&values (leafs)
  23. from the original Tree + the new key&values to be added from the AddBatch call
  24. R R
  25. / \ / \
  26. A * / \
  27. / \ / \
  28. B C * *
  29. / | / \
  30. / | / \
  31. / | / \
  32. L: A B G D
  33. / \
  34. / \
  35. / \
  36. C *
  37. / \
  38. / \
  39. / \
  40. ... ... (nLeafs < minLeafsThreshold)
  41. CASE C: ALMOST CASE B --> if Tree has few Leafs (but numLeafs>=minLeafsThreshold)
  42. ==============================================================================
  43. - Use A, B, G, F as Roots of subtrees
  44. - Do CASE B for each subtree
  45. - Then go from L to the Root
  46. R
  47. / \
  48. / \
  49. / \
  50. * *
  51. / | / \
  52. / | / \
  53. / | / \
  54. L: A B G D
  55. / \
  56. / \
  57. / \
  58. C *
  59. / \
  60. / \
  61. / \
  62. ... ... (nLeafs >= minLeafsThreshold)
  63. CASE D: Already populated Tree
  64. ==============================
  65. - Use A, B, C, D as subtree
  66. - Sort the Keys in Buckets that share the initial part of the path
  67. - For each subtree add there the new leafs
  68. R
  69. / \
  70. / \
  71. / \
  72. * *
  73. / | / \
  74. / | / \
  75. / | / \
  76. L: A B C D
  77. /\ /\ / \ / \
  78. ... ... ... ... ... ...
  79. CASE E: Already populated Tree Unbalanced
  80. =========================================
  81. - Need to fill M1 and M2, and then will be able to use CASE D
  82. - Search for M1 & M2 in the inputed Keys
  83. - Add M1 & M2 to the Tree
  84. - From here can use CASE D
  85. R
  86. / \
  87. / \
  88. / \
  89. * *
  90. | \
  91. | \
  92. | \
  93. L: M1 * M2 * (where M1 and M2 are empty)
  94. / | /
  95. / | /
  96. / | /
  97. A * *
  98. / \ | \
  99. / \ | \
  100. / \ | \
  101. B * * C
  102. / \ |\
  103. ... ... | \
  104. | \
  105. D E
  106. Algorithm decision
  107. ==================
  108. - if nLeafs==0 (root==0): CASE A
  109. - if nLeafs<minLeafsThreshold: CASE B
  110. - if nLeafs>=minLeafsThreshold && (nLeafs/nBuckets) < minLeafsThreshold: CASE C
  111. - else: CASE D & CASE E
  112. - Multiple tree.Add calls: O(n log n)
  113. - Used in: cases A, B, C
  114. - Tree from bottom to top: O(log n)
  115. - Used in: cases D, E
  116. */
  117. const (
  118. minLeafsThreshold = 100 // nolint:gomnd // TMP WIP this will be autocalculated
  119. )
  120. // AddBatch adds a batch of key-values to the Tree. Returns an array containing
  121. // the indexes of the keys failed to add.
  122. func (t *Tree) AddBatch(keys, values [][]byte) ([]int, error) {
  123. // TODO: support vaules=nil
  124. t.updateAccessTime()
  125. t.Lock()
  126. defer t.Unlock()
  127. // when len(keyvalues) is not a power of 2, cut at the biggest power of
  128. // 2 under the len(keys), add those 2**n key-values using the AddBatch
  129. // approach, and then add the remaining key-values using tree.Add.
  130. kvs, err := t.keysValuesToKvs(keys, values)
  131. if err != nil {
  132. return nil, err
  133. }
  134. t.tx, err = t.db.NewTx()
  135. if err != nil {
  136. return nil, err
  137. }
  138. // if nCPU is not a power of two, cut at the highest power of two under
  139. // nCPU
  140. nCPU := highestPowerOfTwo(runtime.NumCPU())
  141. l := int(math.Log2(float64(nCPU)))
  142. var invalids []int
  143. // CASE A: if nLeafs==0 (root==0)
  144. if bytes.Equal(t.root, t.emptyHash) {
  145. invalids, err = t.caseA(nCPU, kvs)
  146. if err != nil {
  147. return nil, err
  148. }
  149. return t.finalizeAddBatch(len(keys), invalids)
  150. }
  151. // CASE B: if nLeafs<nBuckets
  152. nLeafs, err := t.GetNLeafs()
  153. if err != nil {
  154. return nil, err
  155. }
  156. if nLeafs < minLeafsThreshold { // CASE B
  157. var excedents []kv
  158. invalids, excedents, err = t.caseB(nCPU, 0, kvs)
  159. if err != nil {
  160. return nil, err
  161. }
  162. // add the excedents
  163. for i := 0; i < len(excedents); i++ {
  164. err = t.add(0, excedents[i].k, excedents[i].v)
  165. if err != nil {
  166. invalids = append(invalids, excedents[i].pos)
  167. }
  168. }
  169. return t.finalizeAddBatch(len(keys), invalids)
  170. }
  171. keysAtL, err := t.getKeysAtLevel(l + 1)
  172. if err != nil {
  173. return nil, err
  174. }
  175. // CASE C: if nLeafs>=minLeafsThreshold && (nLeafs/nBuckets) < minLeafsThreshold
  176. // available parallelization, will need to be a power of 2 (2**n)
  177. if nLeafs >= minLeafsThreshold &&
  178. (nLeafs/nCPU) < minLeafsThreshold &&
  179. len(keysAtL) == nCPU {
  180. invalids, err = t.caseC(nCPU, l, keysAtL, kvs)
  181. if err != nil {
  182. return nil, err
  183. }
  184. return t.finalizeAddBatch(len(keys), invalids)
  185. }
  186. // CASE E
  187. if len(keysAtL) != nCPU {
  188. // CASE E: add one key at each bucket, and then do CASE D
  189. buckets := splitInBuckets(kvs, nCPU)
  190. kvs = []kv{}
  191. for i := 0; i < len(buckets); i++ {
  192. // add one leaf of the bucket, if there is an error when
  193. // adding the k-v, try to add the next one of the bucket
  194. // (until one is added)
  195. var inserted int
  196. for j := 0; j < len(buckets[i]); j++ {
  197. if err := t.add(0, buckets[i][j].k, buckets[i][j].v); err == nil {
  198. inserted = j
  199. break
  200. }
  201. }
  202. // put the buckets elements except the inserted one
  203. kvs = append(kvs, buckets[i][:inserted]...)
  204. kvs = append(kvs, buckets[i][inserted+1:]...)
  205. }
  206. keysAtL, err = t.getKeysAtLevel(l + 1)
  207. if err != nil {
  208. return nil, err
  209. }
  210. }
  211. // CASE D
  212. if len(keysAtL) == nCPU { // enter in CASE D if len(keysAtL)=nCPU, if not, CASE E
  213. invalidsCaseD, err := t.caseD(nCPU, l, keysAtL, kvs)
  214. if err != nil {
  215. return nil, err
  216. }
  217. invalids = append(invalids, invalidsCaseD...)
  218. return t.finalizeAddBatch(len(keys), invalids)
  219. }
  220. return nil, fmt.Errorf("UNIMPLEMENTED")
  221. }
  222. func (t *Tree) finalizeAddBatch(nKeys int, invalids []int) ([]int, error) {
  223. // store root to db
  224. if err := t.tx.Put(dbKeyRoot, t.root); err != nil {
  225. return nil, err
  226. }
  227. // update nLeafs
  228. if err := t.incNLeafs(nKeys - len(invalids)); err != nil {
  229. return nil, err
  230. }
  231. // commit db tx
  232. if err := t.tx.Commit(); err != nil {
  233. return nil, err
  234. }
  235. return invalids, nil
  236. }
  237. func (t *Tree) caseA(nCPU int, kvs []kv) ([]int, error) {
  238. // if len(kvs) is not a power of 2, cut at the bigger power
  239. // of two under len(kvs), build the tree with that, and add
  240. // later the excedents
  241. kvsP2, kvsNonP2 := cutPowerOfTwo(kvs)
  242. invalids, err := t.buildTreeBottomUp(nCPU, kvsP2)
  243. if err != nil {
  244. return nil, err
  245. }
  246. for i := 0; i < len(kvsNonP2); i++ {
  247. if err = t.add(0, kvsNonP2[i].k, kvsNonP2[i].v); err != nil {
  248. invalids = append(invalids, kvsNonP2[i].pos)
  249. }
  250. }
  251. return invalids, nil
  252. }
  253. func (t *Tree) caseB(nCPU, l int, kvs []kv) ([]int, []kv, error) {
  254. // get already existing keys
  255. aKs, aVs, err := t.getLeafs(t.root)
  256. if err != nil {
  257. return nil, nil, err
  258. }
  259. aKvs, err := t.keysValuesToKvs(aKs, aVs)
  260. if err != nil {
  261. return nil, nil, err
  262. }
  263. // add already existing key-values to the inputted key-values
  264. // kvs = append(kvs, aKvs...)
  265. kvs, invalids := combineInKVSet(aKvs, kvs)
  266. // proceed with CASE A
  267. sortKvs(kvs)
  268. // cutPowerOfTwo, the excedent add it as normal Tree.Add
  269. kvsP2, kvsNonP2 := cutPowerOfTwo(kvs)
  270. var invalids2 []int
  271. if nCPU > 1 {
  272. invalids2, err = t.buildTreeBottomUp(nCPU, kvsP2)
  273. if err != nil {
  274. return nil, nil, err
  275. }
  276. } else {
  277. invalids2, err = t.buildTreeBottomUpSingleThread(l, kvsP2)
  278. if err != nil {
  279. return nil, nil, err
  280. }
  281. }
  282. invalids = append(invalids, invalids2...)
  283. // return the excedents which will be added at the full tree at the end
  284. return invalids, kvsNonP2, nil
  285. }
  286. func (t *Tree) caseC(nCPU, l int, keysAtL [][]byte, kvs []kv) ([]int, error) {
  287. // 1. go down until level L (L=log2(nBuckets)): keysAtL
  288. var excedents []kv
  289. buckets := splitInBuckets(kvs, nCPU)
  290. // 2. use keys at level L as roots of the subtrees under each one
  291. excedentsInBucket := make([][]kv, nCPU)
  292. subRoots := make([][]byte, nCPU)
  293. txs := make([]db.Tx, nCPU)
  294. var wg sync.WaitGroup
  295. wg.Add(nCPU)
  296. for i := 0; i < nCPU; i++ {
  297. go func(cpu int) {
  298. var err error
  299. txs[cpu], err = t.db.NewTx()
  300. if err != nil {
  301. panic(err) // TODO WIP
  302. }
  303. if err := txs[cpu].Add(t.tx); err != nil {
  304. panic(err) // TODO
  305. }
  306. bucketTree := Tree{tx: txs[cpu], db: t.db, maxLevels: t.maxLevels,
  307. hashFunction: t.hashFunction, root: keysAtL[cpu]}
  308. // 3. do CASE B (with 1 cpu) for each key at level L
  309. _, bucketExcedents, err := bucketTree.caseB(1, l, buckets[cpu])
  310. if err != nil {
  311. panic(err) // TODO WIP
  312. // return nil, err
  313. }
  314. excedentsInBucket[cpu] = bucketExcedents
  315. subRoots[cpu] = bucketTree.root
  316. wg.Done()
  317. }(i)
  318. }
  319. wg.Wait()
  320. // merge buckets txs into Tree.tx
  321. for i := 0; i < len(txs); i++ {
  322. if err := t.tx.Add(txs[i]); err != nil {
  323. return nil, err
  324. }
  325. }
  326. for i := 0; i < len(excedentsInBucket); i++ {
  327. excedents = append(excedents, excedentsInBucket[i]...)
  328. }
  329. // 4. go upFromKeys from the new roots of the subtrees
  330. newRoot, err := t.upFromKeys(subRoots)
  331. if err != nil {
  332. return nil, err
  333. }
  334. t.root = newRoot
  335. // add the key-values that have not been used yet
  336. var invalids []int
  337. for i := 0; i < len(excedents); i++ {
  338. if err = t.add(0, excedents[i].k, excedents[i].v); err != nil {
  339. invalids = append(invalids, excedents[i].pos)
  340. }
  341. }
  342. return invalids, nil
  343. }
  344. func (t *Tree) caseD(nCPU, l int, keysAtL [][]byte, kvs []kv) ([]int, error) {
  345. if nCPU == 1 { // CASE D, but with 1 cpu
  346. var invalids []int
  347. for i := 0; i < len(kvs); i++ {
  348. if err := t.add(0, kvs[i].k, kvs[i].v); err != nil {
  349. invalids = append(invalids, kvs[i].pos)
  350. }
  351. }
  352. return invalids, nil
  353. }
  354. buckets := splitInBuckets(kvs, nCPU)
  355. subRoots := make([][]byte, nCPU)
  356. invalidsInBucket := make([][]int, nCPU)
  357. txs := make([]db.Tx, nCPU)
  358. var wg sync.WaitGroup
  359. wg.Add(nCPU)
  360. for i := 0; i < nCPU; i++ {
  361. go func(cpu int) {
  362. var err error
  363. txs[cpu], err = t.db.NewTx()
  364. if err != nil {
  365. panic(err) // TODO WIP
  366. }
  367. // put already existing tx into txs[cpu], as txs[cpu]
  368. // needs the pending key-values that are not in tree.db,
  369. // but are in tree.tx
  370. if err := txs[cpu].Add(t.tx); err != nil {
  371. panic(err) // TODO WIP
  372. }
  373. bucketTree := Tree{tx: txs[cpu], db: t.db, maxLevels: t.maxLevels - l,
  374. hashFunction: t.hashFunction, root: keysAtL[cpu]}
  375. for j := 0; j < len(buckets[cpu]); j++ {
  376. if err = bucketTree.add(l, buckets[cpu][j].k, buckets[cpu][j].v); err != nil {
  377. invalidsInBucket[cpu] = append(invalidsInBucket[cpu], buckets[cpu][j].pos)
  378. }
  379. }
  380. subRoots[cpu] = bucketTree.root
  381. wg.Done()
  382. }(i)
  383. }
  384. wg.Wait()
  385. // merge buckets txs into Tree.tx
  386. for i := 0; i < len(txs); i++ {
  387. if err := t.tx.Add(txs[i]); err != nil {
  388. return nil, err
  389. }
  390. }
  391. newRoot, err := t.upFromKeys(subRoots)
  392. if err != nil {
  393. return nil, err
  394. }
  395. t.root = newRoot
  396. var invalids []int
  397. for i := 0; i < len(invalidsInBucket); i++ {
  398. invalids = append(invalids, invalidsInBucket[i]...)
  399. }
  400. return invalids, nil
  401. }
  402. func splitInBuckets(kvs []kv, nBuckets int) [][]kv {
  403. buckets := make([][]kv, nBuckets)
  404. // 1. classify the keyvalues into buckets
  405. for i := 0; i < len(kvs); i++ {
  406. pair := kvs[i]
  407. // bucketnum := keyToBucket(pair.k, nBuckets)
  408. bucketnum := keyToBucket(pair.keyPath, nBuckets)
  409. buckets[bucketnum] = append(buckets[bucketnum], pair)
  410. }
  411. return buckets
  412. }
  413. // TODO rename in a more 'real' name (calculate bucket from/for key)
  414. func keyToBucket(k []byte, nBuckets int) int {
  415. nLevels := int(math.Log2(float64(nBuckets)))
  416. b := make([]int, nBuckets)
  417. for i := 0; i < nBuckets; i++ {
  418. b[i] = i
  419. }
  420. r := b
  421. mid := len(r) / 2 //nolint:gomnd
  422. for i := 0; i < nLevels; i++ {
  423. if int(k[i/8]&(1<<(i%8))) != 0 {
  424. r = r[mid:]
  425. mid = len(r) / 2 //nolint:gomnd
  426. } else {
  427. r = r[:mid]
  428. mid = len(r) / 2 //nolint:gomnd
  429. }
  430. }
  431. return r[0]
  432. }
  433. type kv struct {
  434. pos int // original position in the array
  435. keyPath []byte
  436. k []byte
  437. v []byte
  438. }
  439. // compareBytes compares byte slices where the bytes are compared from left to
  440. // right and each byte is compared by bit from right to left
  441. func compareBytes(a, b []byte) bool {
  442. // WIP
  443. for i := 0; i < len(a); i++ {
  444. for j := 0; j < 8; j++ {
  445. aBit := a[i] & (1 << j)
  446. bBit := b[i] & (1 << j)
  447. if aBit > bBit {
  448. return false
  449. } else if aBit < bBit {
  450. return true
  451. }
  452. }
  453. }
  454. return false
  455. }
  456. // sortKvs sorts the kv by path
  457. func sortKvs(kvs []kv) {
  458. sort.Slice(kvs, func(i, j int) bool {
  459. return compareBytes(kvs[i].keyPath, kvs[j].keyPath)
  460. })
  461. }
  462. func (t *Tree) keysValuesToKvs(ks, vs [][]byte) ([]kv, error) {
  463. if len(ks) != len(vs) {
  464. return nil, fmt.Errorf("len(keys)!=len(values) (%d!=%d)",
  465. len(ks), len(vs))
  466. }
  467. kvs := make([]kv, len(ks))
  468. for i := 0; i < len(ks); i++ {
  469. keyPath := make([]byte, t.hashFunction.Len())
  470. copy(keyPath[:], ks[i])
  471. kvs[i].pos = i
  472. kvs[i].keyPath = ks[i]
  473. kvs[i].k = ks[i]
  474. kvs[i].v = vs[i]
  475. }
  476. return kvs, nil
  477. }
  478. /*
  479. func (t *Tree) kvsToKeysValues(kvs []kv) ([][]byte, [][]byte) {
  480. ks := make([][]byte, len(kvs))
  481. vs := make([][]byte, len(kvs))
  482. for i := 0; i < len(kvs); i++ {
  483. ks[i] = kvs[i].k
  484. vs[i] = kvs[i].v
  485. }
  486. return ks, vs
  487. }
  488. */
  489. // buildTreeBottomUp splits the key-values into n Buckets (where n is the number
  490. // of CPUs), in parallel builds a subtree for each bucket, once all the subtrees
  491. // are built, uses the subtrees roots as keys for a new tree, which as result
  492. // will have the complete Tree build from bottom to up, where until the
  493. // log2(nCPU) level it has been computed in parallel.
  494. func (t *Tree) buildTreeBottomUp(nCPU int, kvs []kv) ([]int, error) {
  495. l := int(math.Log2(float64(nCPU)))
  496. buckets := splitInBuckets(kvs, nCPU)
  497. subRoots := make([][]byte, nCPU)
  498. invalidsInBucket := make([][]int, nCPU)
  499. txs := make([]db.Tx, nCPU)
  500. var wg sync.WaitGroup
  501. wg.Add(nCPU)
  502. for i := 0; i < nCPU; i++ {
  503. go func(cpu int) {
  504. sortKvs(buckets[cpu])
  505. var err error
  506. txs[cpu], err = t.db.NewTx()
  507. if err != nil {
  508. panic(err) // TODO
  509. }
  510. if err := txs[cpu].Add(t.tx); err != nil {
  511. panic(err) // TODO
  512. }
  513. bucketTree := Tree{tx: txs[cpu], db: t.db, maxLevels: t.maxLevels,
  514. hashFunction: t.hashFunction, root: t.emptyHash}
  515. currInvalids, err := bucketTree.buildTreeBottomUpSingleThread(l, buckets[cpu])
  516. if err != nil {
  517. panic(err) // TODO
  518. }
  519. invalidsInBucket[cpu] = currInvalids
  520. subRoots[cpu] = bucketTree.root
  521. wg.Done()
  522. }(i)
  523. }
  524. wg.Wait()
  525. // merge buckets txs into Tree.tx
  526. for i := 0; i < len(txs); i++ {
  527. if err := t.tx.Add(txs[i]); err != nil {
  528. return nil, err
  529. }
  530. }
  531. newRoot, err := t.upFromKeys(subRoots)
  532. if err != nil {
  533. return nil, err
  534. }
  535. t.root = newRoot
  536. var invalids []int
  537. for i := 0; i < len(invalidsInBucket); i++ {
  538. invalids = append(invalids, invalidsInBucket[i]...)
  539. }
  540. return invalids, err
  541. }
  542. // buildTreeBottomUpSingleThread builds the tree with the given []kv from bottom
  543. // to the root
  544. func (t *Tree) buildTreeBottomUpSingleThread(l int, kvsRaw []kv) ([]int, error) {
  545. // TODO check that log2(len(leafs)) < t.maxLevels, if not, maxLevels
  546. // would be reached and should return error
  547. if len(kvsRaw) == 0 {
  548. return nil, nil
  549. }
  550. vt := newVT(t.maxLevels, t.hashFunction)
  551. for i := 0; i < len(kvsRaw); i++ {
  552. if err := vt.add(l, kvsRaw[i].k, kvsRaw[i].v); err != nil {
  553. return nil, err
  554. }
  555. }
  556. pairs, err := vt.computeHashes()
  557. if err != nil {
  558. return nil, err
  559. }
  560. // store pairs in db
  561. for i := 0; i < len(pairs); i++ {
  562. if err := t.tx.Put(pairs[i][0], pairs[i][1]); err != nil {
  563. return nil, err
  564. }
  565. }
  566. // set tree.root from the virtual tree root
  567. t.root = vt.root.h
  568. return nil, nil // TODO invalids
  569. }
  570. // keys & values must be sorted by path, and the array ks must be length
  571. // multiple of 2
  572. func (t *Tree) upFromKeys(ks [][]byte) ([]byte, error) {
  573. if len(ks) == 1 {
  574. return ks[0], nil
  575. }
  576. var rKs [][]byte
  577. for i := 0; i < len(ks); i += 2 {
  578. if bytes.Equal(ks[i], t.emptyHash) && bytes.Equal(ks[i+1], t.emptyHash) {
  579. // when both sub keys are empty, the key is also empty
  580. rKs = append(rKs, t.emptyHash)
  581. continue
  582. }
  583. k, v, err := newIntermediate(t.hashFunction, ks[i], ks[i+1])
  584. if err != nil {
  585. return nil, err
  586. }
  587. // store k-v to db
  588. if err = t.tx.Put(k, v); err != nil {
  589. return nil, err
  590. }
  591. rKs = append(rKs, k)
  592. }
  593. return t.upFromKeys(rKs)
  594. }
  595. func (t *Tree) getLeafs(root []byte) ([][]byte, [][]byte, error) {
  596. var ks, vs [][]byte
  597. err := t.iter(root, func(k, v []byte) {
  598. if v[0] != PrefixValueLeaf {
  599. return
  600. }
  601. leafK, leafV := ReadLeafValue(v)
  602. ks = append(ks, leafK)
  603. vs = append(vs, leafV)
  604. })
  605. return ks, vs, err
  606. }
  607. func (t *Tree) getKeysAtLevel(l int) ([][]byte, error) {
  608. var keys [][]byte
  609. err := t.iterWithStop(t.root, 0, func(currLvl int, k, v []byte) bool {
  610. if currLvl == l && !bytes.Equal(k, t.emptyHash) {
  611. keys = append(keys, k)
  612. }
  613. if currLvl >= l {
  614. return true // to stop the iter from going down
  615. }
  616. return false
  617. })
  618. return keys, err
  619. }
  620. // cutPowerOfTwo returns []kv of length that is a power of 2, and a second []kv
  621. // with the extra elements that don't fit in a power of 2 length
  622. func cutPowerOfTwo(kvs []kv) ([]kv, []kv) {
  623. x := len(kvs)
  624. if (x & (x - 1)) != 0 {
  625. p2 := highestPowerOfTwo(x)
  626. return kvs[:p2], kvs[p2:]
  627. }
  628. return kvs, nil
  629. }
  630. func highestPowerOfTwo(n int) int {
  631. res := 0
  632. for i := n; i >= 1; i-- {
  633. if (i & (i - 1)) == 0 {
  634. res = i
  635. break
  636. }
  637. }
  638. return res
  639. }
  640. // combineInKVSet combines two kv array in one single array without repeated
  641. // keys.
  642. func combineInKVSet(base, toAdd []kv) ([]kv, []int) {
  643. // TODO this is a naive version, this will be implemented in a more
  644. // efficient way or through maps, or through sorted binary search
  645. r := base
  646. var invalids []int
  647. for i := 0; i < len(toAdd); i++ {
  648. e := false
  649. // check if toAdd[i] exists in the base set
  650. for j := 0; j < len(base); j++ {
  651. if bytes.Equal(toAdd[i].k, base[j].k) {
  652. e = true
  653. }
  654. }
  655. if !e {
  656. r = append(r, toAdd[i])
  657. } else {
  658. invalids = append(invalids, toAdd[i].pos)
  659. }
  660. }
  661. return r, invalids
  662. }
  663. // func computeSimpleAddCost(nLeafs int) int {
  664. // // nLvls 2^nLvls
  665. // nLvls := int(math.Log2(float64(nLeafs)))
  666. // return nLvls * int(math.Pow(2, float64(nLvls)))
  667. // }
  668. //
  669. // func computeBottomUpAddCost(nLeafs int) int {
  670. // // 2^nLvls * 2 - 1
  671. // nLvls := int(math.Log2(float64(nLeafs)))
  672. // return (int(math.Pow(2, float64(nLvls))) * 2) - 1
  673. // }