randomApply

This commit is contained in:
Ehmry - 2019-02-16 12:48:08 +01:00
parent a0aeaaef09
commit 45792ba620
2 changed files with 26 additions and 1 deletions

View File

@ -645,3 +645,25 @@ proc union*(store: BlobStore; sets: varargs[BlobSet]): BlobSet =
assert(not bs.isnil)
bs.apply(freshInsert)
result = fresh
import random
proc randomApply*(store: BlobStore; trie: BlobSet; seed: int64;
f: proc(id: BlobId; size: BiggestInt)) =
## Apply to random leaf if the set is not empty.
var
rng = initRand(seed)
retry = 0
trie = trie
i = rng.rand(countSetBits(trie.bitmap)-1)
while trie.bitmap != 0:
let next = trie.table[i]
case next.kind
of leafNode:
f(next.blob, next.size)
break
of coldNode:
trie.table[i] = store.load(next.setId)
of hotNode:
trie = next
i = rng.rand(countSetBits(trie.bitmap)-1)

View File

@ -41,7 +41,7 @@ suite "store":
let
name = $i
blob = blobHash name
bs = insert(bs, name, blob, 0)
bs = insert(bs, name, blob, i)
setId = commit(client, bs).setId
test "load":
@ -60,3 +60,6 @@ suite "store":
echo "inserted ", name, " - ", name.toKey
echo "applied ", name, " - ", ($(i xor 0x8000)).toKey
raiseAssert("apply succedded for a key not inserted")
for i in 1..count:
store.randomApply(bs, i) do (id: BlobId; size: BiggestInt):
echo "randomApply: ", id, " ", size