Skip to content

Commit

Permalink
Rectify bug in gettree
Browse files Browse the repository at this point in the history
  • Loading branch information
djellemah committed Jun 9, 2024
1 parent 036e6b8 commit 6596a00
Show file tree
Hide file tree
Showing 6 changed files with 360 additions and 80 deletions.
23 changes: 23 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ opt-level = 's'
wit-bindgen = { version = "0.26.0", default-features = false, features = ["realloc"] }
serde = "*"
serde_json = "*"
pretty_assertions = "1.4.0"

[package.metadata.component.target]
path = "wit"

131 changes: 106 additions & 25 deletions cmds.fish
Original file line number Diff line number Diff line change
@@ -1,44 +1,143 @@
alias gli=golem-cli
#
# This contains fish shell commands to:
# - build and deploy the component
# - interact with the component once it's deployed

alias gli golem-cli
alias build="cargo component build"

################################
# building the KV store
function add_component
golem-cli component add --component-name slkvs target/wasm32-wasi/release/slkvs.wasm
end

# first-time only
function deploy --description "for the first time deploy of a component with its worker"
cargo component build --release || return 1
golem-cli worker delete --worker-name fst --component-name slkvs
golem-cli component update --component-name slkvs target/wasm32-wasi/release/slkvs.wasm
golem-cli worker add --worker-name fst --component-name slkvs
end

# for updates use this
function redeploy -a version --description "redeploy to a version"
cargo component build --release || return 1

# Use `golem-cli component update` to figure out which version to use.
# Normal output is something like
# Updated component with ID 3825415a-f2f9-42dc-99d3-715ff89690a0. New version: 1. Component size is 168531 bytes.
set result_msg (golem-cli component update --component-name slkvs target/wasm32-wasi/release/slkvs.wasm)
echo vs: (count $result_msg)

# extract version
set captures (string match --regex -g 'Updated component with ID (.*?) New version: (\d+). Component size is (\d+) bytes.*' $result_msg)
if test $status -ne 0
echo Failed to match output from `golem-cli component update`: "\n$result_msg"
return 1
else
echo -e (string join \\n $result_msg)
end
set target_version $captures[2]

# do the update
echo -n "Updating to component version $target_version... "
golem-cli worker update --worker-name fst --target-version $target_version --mode auto --component-name slkvs
end

function worker_restart
golem-cli worker delete --worker-name fst --component-name slkvs
golem-cli worker add --worker-name fst --component-name slkvs
end

################################
# talking to the KV store
function gli_quote
for v in $argv
echo -n "$v" | jq -Rs
end
end

function gli_parameters
# create an array in fish, since all variables are arrays.
set -l quoted (gli_quote $argv)
set joined (string join , $quoted)
echo "[$joined]"
end

function gli_noquote_parameters
set joined (string join , $argv)
echo "[$joined]"
end

function get
golem-cli worker invoke-and-await --component-name=slkvs \
--worker-name=fst \
--function=golem:component/api/get \
--parameters="[\"$argv[1]\"]"
--parameters=(gli_parameters $argv[1])
end

function hgettree
set component_id (gli_component_id)
set worker_name fst
set function_name golem:component/api/gettree

set params "{\"params\": $(gli_parameters $argv[1])}"
set url "http://localhost:9881/v2/components/$component_id/workers/$worker_name/invoke-and-await?function=$function_name&calling-convention=Component"
echo -e (curl --silent --json $params $url) | jq .result[0] | string unescape | jq .
end

function gettree
golem-cli worker invoke-and-await --component-name=slkvs \
--worker-name=fst \
--function=golem:component/api/gettree \
--parameters="[\"$argv[1]\"]"
--parameters=(gli_parameters $argv[1])
end

function delete
golem-cli worker invoke-and-await --component-name=slkvs \
--worker-name=fst \
--function=golem:component/api/delete \
--parameters="[\"$argv[1]\"]"
--parameters=(gli_parameters $argv[1])
end

function add
golem-cli worker invoke-and-await --component-name=slkvs \
--worker-name=fst \
--function=golem:component/api/add \
--parameters="[\"$argv[1]\", \"$argv[2]\"]"
--parameters=(gli_parameters $argv[1] $argv[2])
end

function listpaths
golem-cli worker invoke-and-await \
--component-name=slkvs \
--worker-name=fst \
--function=golem:component/api/listpaths \
--parameters=$argv
--parameters=(gli_parameters $argv[1])
end

function gli_component_id
set result_msg (gli component get --component-name slkvs)
set captures (string match --regex -g 'Component with ID (.*?). Version: (\d+). Component size is (\d+) bytes.*' $result_msg)
if test $status -ne 0
echo Failed to match output from `golem-cli component update`: "\n$result_msg"
return 1
end

set component_id $captures[1]
echo $component_id
end

function hlistpaths --description "invoke listpaths via http api"
set component_id (gli_component_id)
set worker_name fst
set function_name golem:component/api/listpaths
# curl -d '{"function": "listpaths"}' "http://localhost:9881/v2/components/$component_id/workers/$worker_name/invoke-and-await?function=$function_name&calling-convention=Component"
# Content-Type: multipart/form-data
# curl -F '{"params": null}' "http://localhost:9881/v2/components/$component_id/workers/$worker_name/invoke-and-await?function=$function_name&calling-convention=Component"
# curl -F "function=$function_name" -F "calling-convention=Component" "http://localhost:9881/v2/components/$component_id/workers/$worker_name/invoke-and-await"
set json_rsp (curl --silent --json '{"params": []}' "http://localhost:9881/v2/components/$component_id/workers/$worker_name/invoke-and-await?function=$function_name&calling-convention=Component")
echo $json_rsp | jq .result[0]
end

function addtree
Expand All @@ -58,7 +157,7 @@ function addtree
--component-name=slkvs \
--worker-name=fst \
--function=golem:component/api/addtree \
--parameters="[\"$argv[1]\", $escaped_tree]"
--parameters=(gli_noquote_parameters (gli_quote $argv[1]) $escaped_tree)
end

function drop
Expand All @@ -67,21 +166,3 @@ function drop
--worker-name=fst \
--function=golem:component/api/drop
end

function deploy
cargo component build --release || return 1
gli worker delete --worker-name fst --component-name slkvs
gli component update --component-name slkvs target/wasm32-wasi/release/slkvs.wasm
gli worker add --worker-name fst --component-name slkvs
end

function redeploy -a version --description "redeploy to a version"
cargo component build --release || return 1
gli component update --component-name slkvs target/wasm32-wasi/release/slkvs.wasm
gli worker update --worker-name fst --target-version $argv[1] --mode auto --component-name slkvs
end

function worker_restart
gli worker delete --worker-name fst --component-name slkvs
gli worker add --worker-name fst --component-name slkvs
end
11 changes: 10 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,16 @@ impl Guest for Component {
}

fn gettree(path: String) -> Option<String> {
STATE.with_borrow(|state| state.gettree(path) )
STATE.with_borrow(|state| {
let subtree = state.gettree(path);
// TODO This is a hack and not entirely correct. There could be exactly one
// value at `path`, and it could indeed by null.
if subtree == tree::Collector::Empty {
None
} else {
Some(subtree.to_json().to_string())
}
})
}

fn drop() {
Expand Down
Loading

0 comments on commit 6596a00

Please sign in to comment.