commit: 1271f372c1b9485b899a364e11e41514d15c9302 Author: Anna (cybertailor) Vyalkova <cyber+gentoo <AT> sysrq <DOT> in> AuthorDate: Sun Jan 21 20:27:36 2024 +0000 Commit: Anna Vyalkova <cyber+gentoo <AT> sysrq <DOT> in> CommitDate: Mon Jan 22 17:33:29 2024 +0000 URL: https://gitweb.gentoo.org/repo/proj/guru.git/commit/?id=1271f372
databases.eclass: replace "--get-depend" with variables It slows down metadata regeneration, so associative arrays are used instead. Suggested-by: sam_ Signed-off-by: Anna (cybertailor) Vyalkova <cyber+gentoo <AT> sysrq.in> eclass/databases.eclass | 111 +++++++++++++++++++++++++++--------------------- 1 file changed, 63 insertions(+), 48 deletions(-) diff --git a/eclass/databases.eclass b/eclass/databases.eclass index d906f2a547..9c2457e93e 100644 --- a/eclass/databases.eclass +++ b/eclass/databases.eclass @@ -1,4 +1,4 @@ -# Copyright 2022 Gentoo Authors +# Copyright 2022-2024 Gentoo Authors # Distributed under the terms of the GNU General Public License v2 # @ECLASS: databases.eclass @@ -37,10 +37,6 @@ # # Returns the directory where the server stores database files. # -# --get-depend [use1,use2,...] -# -# Returns a dependency string (to be included in BDEPEND). -# # --get-logfile # # Returns the path to the server's log file. @@ -76,18 +72,21 @@ # # ... # +# DATABASES_REQ_USE=( +# [postgres]="xml" +# ) # inherit databases distutils-r1 # # ... # -# BDEPEND="$(eredis --get-depend)" +# BDEPEND="test? ( ${DATABASES_DEPEND[postgres]} )" # # distutils_enable_tests pytest # # src_test() { -# eredis --start 16739 +# epostgres --start 65432 # distutils-r1_src_test -# eredis --stop +# epostgres --stop # } # @CODE @@ -99,47 +98,66 @@ esac if [[ ! ${_DATABASES_ECLASS} ]]; then _DATABASES_ECLASS=1 -# ============================================================================== -# GENERIC FUNCTIONS -# ============================================================================== +# @ECLASS_VARIABLE: DATABASES_REQ_USE +# @PRE_INHERIT +# @DEFAULT_UNSET +# @DESCRIPTION: +# Bash associative array of USE flags required to be enabled on database +# servers, formed as a USE-dependency string. +# +# Keys are helper function names without the "e" prefix. -# @FUNCTION: _databases_gen_depend -# @USAGE: <funcname> <required use> -# @INTERNAL +# @ECLASS_VARIABLE: DATABASES_DEPEND +# @OUTPUT_VARIABLE # @DESCRIPTION: -# Get a dependency string for the given helper function. -_databases_gen_depend() { - local srvname=${1:1} - local req_use=${2} - - local pkg_dep - case ${srvname} in - memcached) - pkg_dep="net-misc/memcached" - ;; - mongod) - pkg_dep="dev-db/mongodb" - ;; - mysql) - pkg_dep="virtual/mysql" - req_use="server,${req_use}" - ;; - postgres) - pkg_dep="dev-db/postgresql" - req_use="server,${req_use}" - ;; - redis) - pkg_dep="dev-db/redis" - ;; - *) - die "${ECLASS}: unknown database: ${srvname}" - esac +# This is an eclass-generated bash associative array of dependency strings for +# database servers. +# +# Keys are helper function names without the "e" prefix. +declare -Ag DATABASES_DEPEND=() - req_use=${req_use%,} # strip trailing comma - printf "%s" "${pkg_dep}" - [[ ${req_use} ]] && \ - printf "[%s]" "${req_use}" +# @FUNCTION: _databases_set_globals +# @INTERNAL +_databases_set_globals() { + local -A db_pkgs=( + [memcached]="net-misc/memcached" + [mongod]="dev-db/mongodb" + [mysql]="virtual/mysql" + [postgres]="dev-db/postgresql" + [redis]="dev-db/redis" + ) + + local -A db_useflags=( + [mysql]="server" + [postgres]="server" + ) + + if declare -p DATABASES_REQ_USE &>/dev/null; then + [[ $(declare -p DATABASES_REQ_USE) == "declare -A"* ]] || \ + die "DATABASES_REQ_USE must be declared as an associative array" + fi + + local name dep usestr + for name in "${!db_pkgs[@]}"; do + dep=${db_pkgs[${name}]?} + usestr=${db_useflags[${name}]} + usestr+=",${DATABASES_REQ_USE[${name}]}" + # strip leading/trailing commas + usestr=${usestr#,} + usestr=${usestr%,} + + [[ ${usestr} ]] && usestr="[${usestr}]" + DATABASES_DEPEND[${name?}]="${dep?}${usestr}" + done + + readonly DATABASES_DEPEND } +_databases_set_globals +unset -f _databases_set_globals + +# ============================================================================== +# GENERIC FUNCTIONS +# ============================================================================== # @FUNCTION: _databases_die # @USAGE: <funcname> [msg] @@ -198,9 +216,6 @@ _databases_dispatch() { --die) _databases_die ${funcname} "${@}" ;; - --get-depend) - _databases_gen_depend ${funcname} "${@}" - ;; --get-dbpath) echo "${T}"/${funcname}/db/ ;;