From a4636668a7d50247b2a3feb93321930c2e5d4f2a Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Tue, 19 May 2020 19:26:10 +0200 Subject: [PATCH] optimized version of countKeyDB --- bashbot.sh | 4 +--- modules/jsonDB.sh | 19 ++++++++++++------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index f7b5d04..51b00f9 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.94-7-g3d92bf3 +#### $$VERSION$$ v0.96-dev-2-gb6f36c6 # # Exit Codes: # - 0 sucess (hopefully) @@ -168,8 +168,6 @@ if [[ ! "${BOTTOKEN}" =~ ^[0-9]{8,10}:[a-zA-Z0-9_-]{35}$ ]]; then echo -e "${ORANGE}Posilbe problem in the charatcers part, len is $(($(wc -c <<<"${BOTTOKEN#*:}")-1))${NC}" fi -exit - ################## # here we start with the real stuff URL="${BASHBOT_URL:-https://api.telegram.org/bot}${BOTTOKEN}" diff --git a/modules/jsonDB.sh b/modules/jsonDB.sh index b29442b..33b72d3 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.96-dev-1-g4b36432 +#### $$VERSION$$ v0.96-dev-2-gb6f36c6 # # source from commands.sh to use jsonDB functions # @@ -123,16 +123,22 @@ if _exists flock; then # $1 key name, can onyl contain -a-zA-Z0-9,._ # $2 filename (must exist!), must be relative to BASHBOT_ETC, and not contain '..' # $3 optional count, value added to count3r, add 1 if empty + # side effect: if $3 is not given, we add to end of file to be as fast as possible jssh_countKeyDB() { [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 - local DB COUNT="1"; DB="$(jssh_checkDB "$2")" + local DB; DB="$(jssh_checkDB "$2")" declare -A oldARR # start atomic delete here, exclusive max wait 10s { flock -e -w 10 200 Json2Array "oldARR" <"${DB}" - (( oldARR["$1"]+=COUNT )); - # it's append, but last one counts, its a simple DB ... - printf '["%s"]\t"%s"\n' "${1//,/\",\"}" "${oldARR["$1"]//\"/\\\"}" >>"${DB}" + if [ "$3" != "" ]; then + (( oldARR["$1"]+="$3" )); + Array2Json "oldARR" >"${DB}" + else + # it's append, but last one counts, its a simple DB ... + (( oldARR["$1"]++ )); + printf '["%s"]\t"%s"\n' "${1//,/\",\"}" "${oldARR["$1"]//\"/\\\"}" >>"${DB}" + fi } 200>"${DB}${BASHBOT_LOCKNAME}" } @@ -269,12 +275,11 @@ jssh_deleteKeyDB_async() { jssh_countKeyDB_async() { [[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3 local DB COUNT="1"; DB="$(jssh_checkDB "$2")" + [ "$3" != "" ] && COUNT="$3" declare -A oldARR # start atomic delete here, exclusive max wait 10s Json2Array "oldARR" <"${DB}" (( oldARR["$1"]+=COUNT )); - # it's append, but last one counts, its a simple DB ... - #printf '["%s"]\t"%s"\n' "${1//,/\",\"}" "${oldARR["$1"]//\"/\\\"}" >>"${DB}" Array2Json "oldARR" >"${DB}" }