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.

821 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. t.updateAccessTime()
  124. t.Lock()
  125. defer t.Unlock()
  126. vt, err := t.loadVT()
  127. if err != nil {
  128. return nil, err
  129. }
  130. invalids, err := vt.addBatch(keys, values)
  131. if err != nil {
  132. return nil, err
  133. }
  134. pairs, err := vt.computeHashes()
  135. if err != nil {
  136. return nil, err
  137. }
  138. t.root = vt.root.h
  139. // store pairs in db
  140. t.tx, err = t.db.NewTx()
  141. if err != nil {
  142. return nil, err
  143. }
  144. for i := 0; i < len(pairs); i++ {
  145. if err := t.dbPut(pairs[i][0], pairs[i][1]); err != nil {
  146. return nil, err
  147. }
  148. }
  149. return t.finalizeAddBatch(len(keys), invalids)
  150. }
  151. // AddBatchOLD adds a batch of key-values to the Tree. Returns an array containing
  152. // the indexes of the keys failed to add.
  153. func (t *Tree) AddBatchOLD(keys, values [][]byte) ([]int, error) {
  154. // TODO: support vaules=nil
  155. t.updateAccessTime()
  156. t.Lock()
  157. defer t.Unlock()
  158. kvs, err := t.keysValuesToKvs(keys, values)
  159. if err != nil {
  160. return nil, err
  161. }
  162. t.tx, err = t.db.NewTx()
  163. if err != nil {
  164. return nil, err
  165. }
  166. // if nCPU is not a power of two, cut at the highest power of two under
  167. // nCPU
  168. nCPU := flp2(runtime.NumCPU())
  169. l := int(math.Log2(float64(nCPU)))
  170. var invalids []int
  171. // CASE A: if nLeafs==0 (root==0)
  172. if bytes.Equal(t.root, t.emptyHash) {
  173. invalids, err = t.caseA(nCPU, kvs)
  174. if err != nil {
  175. return nil, err
  176. }
  177. return t.finalizeAddBatch(len(keys), invalids)
  178. }
  179. // CASE B: if nLeafs<nBuckets
  180. nLeafs, err := t.GetNLeafs()
  181. if err != nil {
  182. return nil, err
  183. }
  184. if nLeafs < minLeafsThreshold { // CASE B
  185. invalids, err = t.caseB(nCPU, 0, kvs)
  186. if err != nil {
  187. return nil, err
  188. }
  189. return t.finalizeAddBatch(len(keys), invalids)
  190. }
  191. keysAtL, err := t.getKeysAtLevel(l + 1)
  192. if err != nil {
  193. return nil, err
  194. }
  195. // CASE C: if nLeafs>=minLeafsThreshold && (nLeafs/nBuckets) < minLeafsThreshold
  196. // available parallelization, will need to be a power of 2 (2**n)
  197. if nLeafs >= minLeafsThreshold &&
  198. (nLeafs/nCPU) < minLeafsThreshold &&
  199. len(keysAtL) == nCPU {
  200. invalids, err = t.caseC(nCPU, l, keysAtL, kvs)
  201. if err != nil {
  202. return nil, err
  203. }
  204. return t.finalizeAddBatch(len(keys), invalids)
  205. }
  206. // CASE E
  207. if len(keysAtL) != nCPU {
  208. // CASE E: add one key at each bucket, and then do CASE D
  209. buckets := splitInBuckets(kvs, nCPU)
  210. kvs = []kv{}
  211. for i := 0; i < len(buckets); i++ {
  212. // add one leaf of the bucket, if there is an error when
  213. // adding the k-v, try to add the next one of the bucket
  214. // (until one is added)
  215. var inserted int
  216. for j := 0; j < len(buckets[i]); j++ {
  217. if err := t.add(0, buckets[i][j].k, buckets[i][j].v); err == nil {
  218. inserted = j
  219. break
  220. }
  221. }
  222. // put the buckets elements except the inserted one
  223. kvs = append(kvs, buckets[i][:inserted]...)
  224. kvs = append(kvs, buckets[i][inserted+1:]...)
  225. }
  226. keysAtL, err = t.getKeysAtLevel(l + 1)
  227. if err != nil {
  228. return nil, err
  229. }
  230. }
  231. // CASE D
  232. if len(keysAtL) == nCPU { // enter in CASE D if len(keysAtL)=nCPU, if not, CASE E
  233. invalidsCaseD, err := t.caseD(nCPU, l, keysAtL, kvs)
  234. if err != nil {
  235. return nil, err
  236. }
  237. invalids = append(invalids, invalidsCaseD...)
  238. return t.finalizeAddBatch(len(keys), invalids)
  239. }
  240. return nil, fmt.Errorf("UNIMPLEMENTED")
  241. }
  242. func (t *Tree) finalizeAddBatch(nKeys int, invalids []int) ([]int, error) {
  243. // store root to db
  244. if err := t.dbPut(dbKeyRoot, t.root); err != nil {
  245. return nil, err
  246. }
  247. // update nLeafs
  248. if err := t.incNLeafs(nKeys - len(invalids)); err != nil {
  249. return nil, err
  250. }
  251. // commit db tx
  252. if err := t.tx.Commit(); err != nil {
  253. return nil, err
  254. }
  255. return invalids, nil
  256. }
  257. func (t *Tree) caseA(nCPU int, kvs []kv) ([]int, error) {
  258. invalids, err := t.buildTreeFromLeafs(nCPU, kvs)
  259. if err != nil {
  260. return nil, err
  261. }
  262. return invalids, nil
  263. }
  264. func (t *Tree) caseB(nCPU, l int, kvs []kv) ([]int, error) {
  265. // get already existing keys
  266. aKs, aVs, err := t.getLeafs(t.root)
  267. if err != nil {
  268. return nil, err
  269. }
  270. aKvs, err := t.keysValuesToKvs(aKs, aVs)
  271. if err != nil {
  272. return nil, err
  273. }
  274. // add already existing key-values to the inputted key-values
  275. // kvs = append(kvs, aKvs...)
  276. kvs, invalids := combineInKVSet(aKvs, kvs)
  277. // proceed with CASE A
  278. sortKvs(kvs)
  279. var invalids2 []int
  280. if nCPU > 1 {
  281. invalids2, err = t.buildTreeFromLeafs(nCPU, kvs)
  282. if err != nil {
  283. return nil, err
  284. }
  285. } else {
  286. invalids2, err = t.buildTreeFromLeafsSingleThread(l, kvs)
  287. if err != nil {
  288. return nil, err
  289. }
  290. }
  291. invalids = append(invalids, invalids2...)
  292. return invalids, nil
  293. }
  294. func (t *Tree) caseC(nCPU, l int, keysAtL [][]byte, kvs []kv) ([]int, error) {
  295. // 1. go down until level L (L=log2(nBuckets)): keysAtL
  296. var excedents []kv
  297. buckets := splitInBuckets(kvs, nCPU)
  298. // 2. use keys at level L as roots of the subtrees under each one
  299. subRoots := make([][]byte, nCPU)
  300. dbgStatsPerBucket := make([]*dbgStats, nCPU)
  301. txs := make([]db.Tx, nCPU)
  302. var wg sync.WaitGroup
  303. wg.Add(nCPU)
  304. for i := 0; i < nCPU; i++ {
  305. go func(cpu int) {
  306. var err error
  307. txs[cpu], err = t.db.NewTx()
  308. if err != nil {
  309. panic(err) // TODO WIP
  310. }
  311. if err := txs[cpu].Add(t.tx); err != nil {
  312. panic(err) // TODO
  313. }
  314. bucketTree := Tree{tx: txs[cpu], db: t.db, maxLevels: t.maxLevels,
  315. hashFunction: t.hashFunction, root: keysAtL[cpu],
  316. emptyHash: t.emptyHash, dbg: newDbgStats()}
  317. // 3. do CASE B (with 1 cpu) for each key at level L
  318. _, err = bucketTree.caseB(1, l, buckets[cpu]) // TODO handle invalids
  319. if err != nil {
  320. panic(err) // TODO WIP
  321. // return nil, err
  322. }
  323. subRoots[cpu] = bucketTree.root
  324. dbgStatsPerBucket[cpu] = bucketTree.dbg
  325. wg.Done()
  326. }(i)
  327. }
  328. wg.Wait()
  329. // merge buckets txs into Tree.tx
  330. for i := 0; i < len(txs); i++ {
  331. if err := t.tx.Add(txs[i]); err != nil {
  332. return nil, err
  333. }
  334. }
  335. // 4. go upFromKeys from the new roots of the subtrees
  336. newRoot, err := t.upFromKeys(subRoots)
  337. if err != nil {
  338. return nil, err
  339. }
  340. t.root = newRoot
  341. // add the key-values that have not been used yet
  342. var invalids []int
  343. for i := 0; i < len(excedents); i++ {
  344. if err = t.add(0, excedents[i].k, excedents[i].v); err != nil {
  345. invalids = append(invalids, excedents[i].pos)
  346. }
  347. }
  348. for i := 0; i < len(dbgStatsPerBucket); i++ {
  349. t.dbg.add(dbgStatsPerBucket[i])
  350. }
  351. return invalids, nil
  352. }
  353. func (t *Tree) caseD(nCPU, l int, keysAtL [][]byte, kvs []kv) ([]int, error) {
  354. if nCPU == 1 { // CASE D, but with 1 cpu
  355. var invalids []int
  356. for i := 0; i < len(kvs); i++ {
  357. if err := t.add(0, kvs[i].k, kvs[i].v); err != nil {
  358. invalids = append(invalids, kvs[i].pos)
  359. }
  360. }
  361. return invalids, nil
  362. }
  363. buckets := splitInBuckets(kvs, nCPU)
  364. subRoots := make([][]byte, nCPU)
  365. invalidsInBucket := make([][]int, nCPU)
  366. dbgStatsPerBucket := make([]*dbgStats, nCPU)
  367. txs := make([]db.Tx, nCPU)
  368. var wg sync.WaitGroup
  369. wg.Add(nCPU)
  370. for i := 0; i < nCPU; i++ {
  371. go func(cpu int) {
  372. var err error
  373. txs[cpu], err = t.db.NewTx()
  374. if err != nil {
  375. panic(err) // TODO WIP
  376. }
  377. // put already existing tx into txs[cpu], as txs[cpu]
  378. // needs the pending key-values that are not in tree.db,
  379. // but are in tree.tx
  380. if err := txs[cpu].Add(t.tx); err != nil {
  381. panic(err) // TODO WIP
  382. }
  383. bucketTree := Tree{tx: txs[cpu], db: t.db, maxLevels: t.maxLevels - l,
  384. hashFunction: t.hashFunction, root: keysAtL[cpu],
  385. emptyHash: t.emptyHash, dbg: newDbgStats()} // TODO bucketTree.dbg should be optional
  386. for j := 0; j < len(buckets[cpu]); j++ {
  387. if err = bucketTree.add(l, buckets[cpu][j].k, buckets[cpu][j].v); err != nil {
  388. invalidsInBucket[cpu] = append(invalidsInBucket[cpu], buckets[cpu][j].pos)
  389. }
  390. }
  391. subRoots[cpu] = bucketTree.root
  392. dbgStatsPerBucket[cpu] = bucketTree.dbg
  393. wg.Done()
  394. }(i)
  395. }
  396. wg.Wait()
  397. // merge buckets txs into Tree.tx
  398. for i := 0; i < len(txs); i++ {
  399. if err := t.tx.Add(txs[i]); err != nil {
  400. return nil, err
  401. }
  402. }
  403. newRoot, err := t.upFromKeys(subRoots)
  404. if err != nil {
  405. return nil, err
  406. }
  407. t.root = newRoot
  408. var invalids []int
  409. for i := 0; i < len(invalidsInBucket); i++ {
  410. invalids = append(invalids, invalidsInBucket[i]...)
  411. }
  412. for i := 0; i < len(dbgStatsPerBucket); i++ {
  413. t.dbg.add(dbgStatsPerBucket[i])
  414. }
  415. return invalids, nil
  416. }
  417. func splitInBuckets(kvs []kv, nBuckets int) [][]kv {
  418. buckets := make([][]kv, nBuckets)
  419. // 1. classify the keyvalues into buckets
  420. for i := 0; i < len(kvs); i++ {
  421. pair := kvs[i]
  422. // bucketnum := keyToBucket(pair.k, nBuckets)
  423. bucketnum := keyToBucket(pair.keyPath, nBuckets)
  424. buckets[bucketnum] = append(buckets[bucketnum], pair)
  425. }
  426. return buckets
  427. }
  428. // TODO rename in a more 'real' name (calculate bucket from/for key)
  429. func keyToBucket(k []byte, nBuckets int) int {
  430. nLevels := int(math.Log2(float64(nBuckets)))
  431. b := make([]int, nBuckets)
  432. for i := 0; i < nBuckets; i++ {
  433. b[i] = i
  434. }
  435. r := b
  436. mid := len(r) / 2 //nolint:gomnd
  437. for i := 0; i < nLevels; i++ {
  438. if int(k[i/8]&(1<<(i%8))) != 0 {
  439. r = r[mid:]
  440. mid = len(r) / 2 //nolint:gomnd
  441. } else {
  442. r = r[:mid]
  443. mid = len(r) / 2 //nolint:gomnd
  444. }
  445. }
  446. return r[0]
  447. }
  448. type kv struct {
  449. pos int // original position in the array
  450. keyPath []byte
  451. k []byte
  452. v []byte
  453. }
  454. // compareBytes compares byte slices where the bytes are compared from left to
  455. // right and each byte is compared by bit from right to left
  456. func compareBytes(a, b []byte) bool {
  457. // WIP
  458. for i := 0; i < len(a); i++ {
  459. for j := 0; j < 8; j++ {
  460. aBit := a[i] & (1 << j)
  461. bBit := b[i] & (1 << j)
  462. if aBit > bBit {
  463. return false
  464. } else if aBit < bBit {
  465. return true
  466. }
  467. }
  468. }
  469. return false
  470. }
  471. // sortKvs sorts the kv by path
  472. func sortKvs(kvs []kv) {
  473. sort.Slice(kvs, func(i, j int) bool {
  474. return compareBytes(kvs[i].keyPath, kvs[j].keyPath)
  475. })
  476. }
  477. func (t *Tree) keysValuesToKvs(ks, vs [][]byte) ([]kv, error) {
  478. if len(ks) != len(vs) {
  479. return nil, fmt.Errorf("len(keys)!=len(values) (%d!=%d)",
  480. len(ks), len(vs))
  481. }
  482. kvs := make([]kv, len(ks))
  483. for i := 0; i < len(ks); i++ {
  484. keyPath := make([]byte, t.hashFunction.Len())
  485. copy(keyPath[:], ks[i])
  486. kvs[i].pos = i
  487. kvs[i].keyPath = keyPath
  488. kvs[i].k = ks[i]
  489. kvs[i].v = vs[i]
  490. }
  491. return kvs, nil
  492. }
  493. /*
  494. func (t *Tree) kvsToKeysValues(kvs []kv) ([][]byte, [][]byte) {
  495. ks := make([][]byte, len(kvs))
  496. vs := make([][]byte, len(kvs))
  497. for i := 0; i < len(kvs); i++ {
  498. ks[i] = kvs[i].k
  499. vs[i] = kvs[i].v
  500. }
  501. return ks, vs
  502. }
  503. */
  504. // buildTreeFromLeafs splits the key-values into n Buckets (where n is the number
  505. // of CPUs), in parallel builds a subtree for each bucket, once all the subtrees
  506. // are built, uses the subtrees roots as keys for a new tree, which as result
  507. // will have the complete Tree build from bottom to up, where until the
  508. // log2(nCPU) level it has been computed in parallel.
  509. func (t *Tree) buildTreeFromLeafs(nCPU int, kvs []kv) ([]int, error) {
  510. l := int(math.Log2(float64(nCPU)))
  511. buckets := splitInBuckets(kvs, nCPU)
  512. subRoots := make([][]byte, nCPU)
  513. invalidsInBucket := make([][]int, nCPU)
  514. dbgStatsPerBucket := make([]*dbgStats, nCPU)
  515. txs := make([]db.Tx, nCPU)
  516. var wg sync.WaitGroup
  517. wg.Add(nCPU)
  518. for i := 0; i < nCPU; i++ {
  519. go func(cpu int) {
  520. sortKvs(buckets[cpu])
  521. var err error
  522. txs[cpu], err = t.db.NewTx()
  523. if err != nil {
  524. panic(err) // TODO
  525. }
  526. if err := txs[cpu].Add(t.tx); err != nil {
  527. panic(err) // TODO
  528. }
  529. bucketTree := Tree{tx: txs[cpu], db: t.db, maxLevels: t.maxLevels,
  530. hashFunction: t.hashFunction, root: t.emptyHash,
  531. emptyHash: t.emptyHash, dbg: newDbgStats()}
  532. currInvalids, err := bucketTree.buildTreeFromLeafsSingleThread(l, buckets[cpu])
  533. if err != nil {
  534. panic(err) // TODO
  535. }
  536. invalidsInBucket[cpu] = currInvalids
  537. subRoots[cpu] = bucketTree.root
  538. dbgStatsPerBucket[cpu] = bucketTree.dbg
  539. wg.Done()
  540. }(i)
  541. }
  542. wg.Wait()
  543. // merge buckets txs into Tree.tx
  544. for i := 0; i < len(txs); i++ {
  545. if err := t.tx.Add(txs[i]); err != nil {
  546. return nil, err
  547. }
  548. }
  549. newRoot, err := t.upFromKeys(subRoots)
  550. if err != nil {
  551. return nil, err
  552. }
  553. t.root = newRoot
  554. var invalids []int
  555. for i := 0; i < len(invalidsInBucket); i++ {
  556. invalids = append(invalids, invalidsInBucket[i]...)
  557. }
  558. for i := 0; i < len(dbgStatsPerBucket); i++ {
  559. t.dbg.add(dbgStatsPerBucket[i])
  560. }
  561. return invalids, err
  562. }
  563. // buildTreeFromLeafsSingleThread builds the tree with the given []kv from bottom
  564. // to the root
  565. func (t *Tree) buildTreeFromLeafsSingleThread(l int, kvsRaw []kv) ([]int, error) {
  566. // TODO check that log2(len(leafs)) < t.maxLevels, if not, maxLevels
  567. // would be reached and should return error
  568. if len(kvsRaw) == 0 {
  569. return nil, nil
  570. }
  571. vt := newVT(t.maxLevels, t.hashFunction)
  572. if t.dbg != nil {
  573. vt.params.dbg = newDbgStats()
  574. }
  575. for i := 0; i < len(kvsRaw); i++ {
  576. if err := vt.add(l, kvsRaw[i].k, kvsRaw[i].v); err != nil {
  577. return nil, err
  578. }
  579. }
  580. pairs, err := vt.computeHashes()
  581. if err != nil {
  582. return nil, err
  583. }
  584. // store pairs in db
  585. for i := 0; i < len(pairs); i++ {
  586. if err := t.dbPut(pairs[i][0], pairs[i][1]); err != nil {
  587. return nil, err
  588. }
  589. }
  590. t.dbg.add(vt.params.dbg)
  591. // set tree.root from the virtual tree root
  592. t.root = vt.root.h
  593. return nil, nil // TODO invalids
  594. }
  595. // keys & values must be sorted by path, and the array ks must be length
  596. // multiple of 2
  597. func (t *Tree) upFromKeys(ks [][]byte) ([]byte, error) {
  598. if len(ks) == 1 {
  599. return ks[0], nil
  600. }
  601. var rKs [][]byte
  602. for i := 0; i < len(ks); i += 2 {
  603. if bytes.Equal(ks[i], t.emptyHash) && bytes.Equal(ks[i+1], t.emptyHash) {
  604. // when both sub keys are empty, the key is also empty
  605. rKs = append(rKs, t.emptyHash)
  606. continue
  607. }
  608. k, v, err := newIntermediate(t.hashFunction, ks[i], ks[i+1])
  609. if err != nil {
  610. return nil, err
  611. }
  612. // store k-v to db
  613. if err = t.dbPut(k, v); err != nil {
  614. return nil, err
  615. }
  616. rKs = append(rKs, k)
  617. }
  618. return t.upFromKeys(rKs)
  619. }
  620. func (t *Tree) getLeafs(root []byte) ([][]byte, [][]byte, error) {
  621. var ks, vs [][]byte
  622. err := t.iter(root, func(k, v []byte) {
  623. if v[0] != PrefixValueLeaf {
  624. return
  625. }
  626. leafK, leafV := ReadLeafValue(v)
  627. ks = append(ks, leafK)
  628. vs = append(vs, leafV)
  629. })
  630. return ks, vs, err
  631. }
  632. func (t *Tree) getKeysAtLevel(l int) ([][]byte, error) {
  633. var keys [][]byte
  634. err := t.iterWithStop(t.root, 0, func(currLvl int, k, v []byte) bool {
  635. if currLvl == l && !bytes.Equal(k, t.emptyHash) {
  636. keys = append(keys, k)
  637. }
  638. if currLvl >= l {
  639. return true // to stop the iter from going down
  640. }
  641. return false
  642. })
  643. return keys, err
  644. }
  645. // flp2 computes the floor power of 2, the highest power of 2 under the given
  646. // value.
  647. func flp2(n int) int {
  648. res := 0
  649. for i := n; i >= 1; i-- {
  650. if (i & (i - 1)) == 0 {
  651. res = i
  652. break
  653. }
  654. }
  655. return res
  656. }
  657. // combineInKVSet combines two kv array in one single array without repeated
  658. // keys.
  659. func combineInKVSet(base, toAdd []kv) ([]kv, []int) {
  660. // TODO this is a naive version, this will be implemented in a more
  661. // efficient way or through maps, or through sorted binary search
  662. r := base
  663. var invalids []int
  664. for i := 0; i < len(toAdd); i++ {
  665. e := false
  666. // check if toAdd[i] exists in the base set
  667. for j := 0; j < len(base); j++ {
  668. if bytes.Equal(toAdd[i].k, base[j].k) {
  669. e = true
  670. }
  671. }
  672. if !e {
  673. r = append(r, toAdd[i])
  674. } else {
  675. invalids = append(invalids, toAdd[i].pos)
  676. }
  677. }
  678. return r, invalids
  679. }
  680. // loadVT loads a new virtual tree (vt) from the current Tree, which contains
  681. // the same leafs.
  682. func (t *Tree) loadVT() (vt, error) {
  683. vt := newVT(t.maxLevels, t.hashFunction)
  684. vt.params.dbg = t.dbg
  685. err := t.Iterate(func(k, v []byte) {
  686. switch v[0] {
  687. case PrefixValueEmpty:
  688. case PrefixValueLeaf:
  689. leafK, leafV := ReadLeafValue(v)
  690. if err := vt.add(0, leafK, leafV); err != nil {
  691. panic(err)
  692. }
  693. case PrefixValueIntermediate:
  694. default:
  695. }
  696. })
  697. return vt, err
  698. }
  699. // func computeSimpleAddCost(nLeafs int) int {
  700. // // nLvls 2^nLvls
  701. // nLvls := int(math.Log2(float64(nLeafs)))
  702. // return nLvls * int(math.Pow(2, float64(nLvls)))
  703. // }
  704. //
  705. // func computeFromLeafsAddCost(nLeafs int) int {
  706. // // 2^nLvls * 2 - 1
  707. // nLvls := int(math.Log2(float64(nLeafs)))
  708. // return (int(math.Pow(2, float64(nLvls))) * 2) - 1
  709. // }