From d0d264c42e0ea9d6a2dce8973fb92700c322aedb Mon Sep 17 00:00:00 2001 From: Emery Hemingway Date: Sat, 16 Feb 2019 23:03:27 +0100 Subject: [PATCH] Rearrange "insert" argument order in REPL --- src/blobset.nim | 30 +++++++++++++++++++++++++----- src/blobsets.nim | 12 ++++++------ 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/src/blobset.nim b/src/blobset.nim index 4d4e4f7..2624793 100644 --- a/src/blobset.nim +++ b/src/blobset.nim @@ -1,7 +1,7 @@ when not isMainModule: {.error: "this module is not a library, import blobsets instead".} -import std/asyncdispatch, std/nre, std/os, std/strutils, std/tables, std/parseopt, std/streams, std/rdstdin +import std/asyncdispatch, std/nre, std/os, std/strutils, std/tables, std/parseopt, std/streams, std/rdstdin, std/random import cbor import ./blobsets, ./blobsets/filestores, ./blobsets/httpservers, ./blobsets/httpstores @@ -502,10 +502,12 @@ proc ingestFunc(env: Env; args: NodeObj): NodeRef = proc insertFunc(env: Env; args: NodeObj): NodeRef = assertArgCount(args, 3) let - trie = args.atom - name = args.next.atom - blob = args.next.next.atom - newNode(newAtom(env.store.insert(trie.bs, name.str, blob.blob, blob.size))) + trie = args.atom.bs + blob = args.next.atom + name = args.next.next.atom.str + let newBs = env.store.insert(trie, name, blob.blob, blob.size) + doAssert(not newBs.isNil) + newNode(newAtom(newBs)) proc blobFunc(env: Env; args: NodeObj): NodeRef = assertArgCount(args, 1) @@ -549,6 +551,15 @@ proc mergeFunc(env: Env; args: NodeObj): NodeRef = proc pathFunc(env: Env; arg: NodeObj): NodeRef = result = arg.atom.str.newAtomPath.newNode +proc randomFunc(env: Env; arg: NodeObj): NodeRef = + assertArgCount(arg, 1) + let bs = arg.atom.bs + var random: NodeRef + env.store.randomApply(bs, rand(int.high)) do (id: BlobId; size: BiggestInt): + random = newNode(newAtom(id, size)) + if random.isNil: newNodeList() + else: random + proc removeFunc(env: Env; args: NodeObj): NodeRef = assertArgCount(args, 2) let @@ -609,6 +620,7 @@ proc newEnv(store: BlobStore): Env = result.bindEnv "map", mapFunc #result.bindEnv "merge", mergeFunc result.bindEnv "path", pathFunc + result.bindEnv "random", randomFunc result.bindEnv "remove", removeFunc result.bindEnv "search", searchFunc result.bindEnv "union", unionFunc @@ -685,7 +697,14 @@ else: quit("failed to connect to store at $1 ($2)" % [host, getCurrentExceptionMsg()]) ]# +proc emptyMain() = + let + store = openStore() + bs = store.commit(newBlobSet()) + echo bs.setId.toHex + proc replMain() = + randomize() var scripted: bool for kind, key, value in getopt(): if kind == cmdShortOption and key == "s": @@ -799,6 +818,7 @@ proc main() = break case normalize(cmd) of "": quit("no subcommand specified") + of "empty": emptyMain() of "repl": replMain() of "dump": dumpMain() of "ingest": ingestMain() diff --git a/src/blobsets.nim b/src/blobsets.nim index ca2a3c0..077d2d2 100644 --- a/src/blobsets.nim +++ b/src/blobsets.nim @@ -298,7 +298,7 @@ func contains*(bs: BlobSet; name: string): bool = found = true result = found -func insert(trie, l: BlobSet; depth: int; name = ""): BlobSet = +func insert(trie, l: BlobSet; depth: int): BlobSet = ## This procedure is recursive to a depth of keyBits/keyChunkBits. # TODO: not functional? doAssert(depth < (keyBits div keyChunkBits), "key space exhausted during insert") @@ -317,22 +317,22 @@ func insert(trie, l: BlobSet; depth: int; name = ""): BlobSet = if result.table[i].key == l.key: raise newException(KeyError, "key collision in blob set") var subtrie = newBlobSet() - subtrie = subtrie.insert(result.table[i], depth, "") - subtrie = subtrie.insert(l, depth, name) + subtrie = subtrie.insert(result.table[i], depth) + subtrie = subtrie.insert(l, depth) result.table[i] = subtrie assert(result.table[i].kind == hotNode) else: result.bitmap = result.bitmap or (1'u64 shl key.sparseIndex) result.table.insert(l, result.compactIndex(key)) -func insert*(trie, node: BlobSet; name = ""): BlobSet = insert(trie, node, 0, name) +func insert*(trie, node: BlobSet): BlobSet = insert(trie, node, 0) ## Insert set node `node` into `trie`. func insert*(t: BlobSet; name: string; blob: BlobId; size: BiggestInt): BlobSet = ## Insert a blob hash into a trie. # TODO: this is not functional! let leaf = BlobSet(kind: leafNode, key: name.toKey, blob: blob, size: size) - insert(t, leaf, name) + insert(t, leaf) func remove(trie: BlobSet; key: Key; depth: int): BlobSet = result = trie @@ -655,7 +655,7 @@ proc randomApply*(store: BlobStore; trie: BlobSet; seed: int64; rng = initRand(seed) retry = 0 trie = trie - i = rng.rand(countSetBits(trie.bitmap)-1) + i = rng.rand(max(1, countSetBits(trie.bitmap))-1) while trie.bitmap != 0: let next = trie.table[i] case next.kind