Rearrange "insert" argument order in REPL

This commit is contained in:
Ehmry - 2019-02-16 23:03:27 +01:00
parent 45792ba620
commit d0d264c42e
2 changed files with 31 additions and 11 deletions

View File

@ -1,7 +1,7 @@
when not isMainModule: when not isMainModule:
{.error: "this module is not a library, import blobsets instead".} {.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 cbor
import ./blobsets, ./blobsets/filestores, import ./blobsets, ./blobsets/filestores,
./blobsets/httpservers, ./blobsets/httpstores ./blobsets/httpservers, ./blobsets/httpstores
@ -502,10 +502,12 @@ proc ingestFunc(env: Env; args: NodeObj): NodeRef =
proc insertFunc(env: Env; args: NodeObj): NodeRef = proc insertFunc(env: Env; args: NodeObj): NodeRef =
assertArgCount(args, 3) assertArgCount(args, 3)
let let
trie = args.atom trie = args.atom.bs
name = args.next.atom blob = args.next.atom
blob = args.next.next.atom name = args.next.next.atom.str
newNode(newAtom(env.store.insert(trie.bs, name.str, blob.blob, blob.size))) 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 = proc blobFunc(env: Env; args: NodeObj): NodeRef =
assertArgCount(args, 1) assertArgCount(args, 1)
@ -549,6 +551,15 @@ proc mergeFunc(env: Env; args: NodeObj): NodeRef =
proc pathFunc(env: Env; arg: NodeObj): NodeRef = proc pathFunc(env: Env; arg: NodeObj): NodeRef =
result = arg.atom.str.newAtomPath.newNode 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 = proc removeFunc(env: Env; args: NodeObj): NodeRef =
assertArgCount(args, 2) assertArgCount(args, 2)
let let
@ -609,6 +620,7 @@ proc newEnv(store: BlobStore): Env =
result.bindEnv "map", mapFunc result.bindEnv "map", mapFunc
#result.bindEnv "merge", mergeFunc #result.bindEnv "merge", mergeFunc
result.bindEnv "path", pathFunc result.bindEnv "path", pathFunc
result.bindEnv "random", randomFunc
result.bindEnv "remove", removeFunc result.bindEnv "remove", removeFunc
result.bindEnv "search", searchFunc result.bindEnv "search", searchFunc
result.bindEnv "union", unionFunc result.bindEnv "union", unionFunc
@ -685,7 +697,14 @@ else:
quit("failed to connect to store at $1 ($2)" % [host, getCurrentExceptionMsg()]) 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() = proc replMain() =
randomize()
var scripted: bool var scripted: bool
for kind, key, value in getopt(): for kind, key, value in getopt():
if kind == cmdShortOption and key == "s": if kind == cmdShortOption and key == "s":
@ -799,6 +818,7 @@ proc main() =
break break
case normalize(cmd) case normalize(cmd)
of "": quit("no subcommand specified") of "": quit("no subcommand specified")
of "empty": emptyMain()
of "repl": replMain() of "repl": replMain()
of "dump": dumpMain() of "dump": dumpMain()
of "ingest": ingestMain() of "ingest": ingestMain()

View File

@ -298,7 +298,7 @@ func contains*(bs: BlobSet; name: string): bool =
found = true found = true
result = found 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. ## This procedure is recursive to a depth of keyBits/keyChunkBits.
# TODO: not functional? # TODO: not functional?
doAssert(depth < (keyBits div keyChunkBits), "key space exhausted during insert") 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: if result.table[i].key == l.key:
raise newException(KeyError, "key collision in blob set") raise newException(KeyError, "key collision in blob set")
var subtrie = newBlobSet() var subtrie = newBlobSet()
subtrie = subtrie.insert(result.table[i], depth, "<unknown>") subtrie = subtrie.insert(result.table[i], depth)
subtrie = subtrie.insert(l, depth, name) subtrie = subtrie.insert(l, depth)
result.table[i] = subtrie result.table[i] = subtrie
assert(result.table[i].kind == hotNode) assert(result.table[i].kind == hotNode)
else: else:
result.bitmap = result.bitmap or (1'u64 shl key.sparseIndex) result.bitmap = result.bitmap or (1'u64 shl key.sparseIndex)
result.table.insert(l, result.compactIndex(key)) 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`. ## Insert set node `node` into `trie`.
func insert*(t: BlobSet; name: string; blob: BlobId; size: BiggestInt): BlobSet = func insert*(t: BlobSet; name: string; blob: BlobId; size: BiggestInt): BlobSet =
## Insert a blob hash into a trie. ## Insert a blob hash into a trie.
# TODO: this is not functional! # TODO: this is not functional!
let leaf = BlobSet(kind: leafNode, key: name.toKey, blob: blob, size: size) 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 = func remove(trie: BlobSet; key: Key; depth: int): BlobSet =
result = trie result = trie
@ -655,7 +655,7 @@ proc randomApply*(store: BlobStore; trie: BlobSet; seed: int64;
rng = initRand(seed) rng = initRand(seed)
retry = 0 retry = 0
trie = trie trie = trie
i = rng.rand(countSetBits(trie.bitmap)-1) i = rng.rand(max(1, countSetBits(trie.bitmap))-1)
while trie.bitmap != 0: while trie.bitmap != 0:
let next = trie.table[i] let next = trie.table[i]
case next.kind case next.kind