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:
{.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()

View File

@ -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, "<unknown>")
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