From ca0509556acf30cfc7020c9aa9de02fd5d017033 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sat, 13 Jun 2020 18:40:34 +0200 Subject: [PATCH] fix/optimize getKey functions --- bashbot.sh | 4 ++-- modules/jsonDB.sh | 12 ++++-------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 5f09f36..6a91124 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -11,7 +11,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-5-g5baab14 +#### $$VERSION$$ v0.98-dev-6-ge0470aa # # Exit Codes: # - 0 sucess (hopefully) @@ -55,7 +55,7 @@ _round_float() { } getConfigKey() { [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 - sed -n 's/\["'"$1"'"\]\t*"\(.*\)"/\1/p' <"${BOTDATABASE}.jssh" | tail -n 1 + [ -r "${BOTDATABASE}.jssh" ] && sed -n 's/\["'"$1"'"\]\t*"\(.*\)"/\1/p' <"${BOTDATABASE}.jssh" | tail -n 1 } # get location and name of bashbot.sh diff --git a/modules/jsonDB.sh b/modules/jsonDB.sh index e486ebf..15e64af 100644 --- a/modules/jsonDB.sh +++ b/modules/jsonDB.sh @@ -5,7 +5,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.98-dev-5-g5baab14 +#### $$VERSION$$ v0.98-dev-6-ge0470aa # # source from commands.sh to use jsonDB functions # @@ -124,12 +124,10 @@ if _exists flock; then jssh_getKeyDB() { [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 local DB; DB="$(jssh_checkDB "$2")" - declare -A oldARR # start atomic delete here, exclusive max wait 1s { flock -s -w 1 200 - Json2Array "oldARR" <"${DB}" + [ -r "${DB}" ] && sed -n 's/\["'"$1"'"\]\t*"\(.*\)"/\1/p' <"${DB}" | tail -n 1 } 200>"${DB}${BASHBOT_LOCKNAME}" - printf '%s' "${oldARR["$1"]}" } @@ -288,9 +286,7 @@ jssh_deleteKeyDB_async() { jssh_getKeyDB_async() { [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 local DB; DB="$(jssh_checkDB "$2")" - declare -A oldARR - Json2Array "oldARR" <"${DB}" - printf '%s' "${oldARR["$1"]}" + [ -r "${DB}" ] && sed -n 's/\["'"$1"'"\]\t*"\(.*\)"/\1/p' <"${DB}" | tail -n 1 } jssh_countKeyDB_async() { @@ -326,7 +322,7 @@ jssh_clearDB_async() { # read JSON.sh style data and asssign to an ARRAY # $1 ARRAY name, must be declared with "declare -A ARRAY" before calling Json2Array() { - # shellcheck source=./commands.sh + # shellcheck disable=SC1091,SC1090 [ -z "$1" ] || source <( printf "$1"'=( %s )' "$(sed -E -n -e '/\["[-0-9a-zA-Z_,."]+"\]\+*\t/ s/\t/=/gp' -e 's/=(true|false)/="\1"/')" ) } # get Config Key from jssh file without jsshDB