-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path.zflai_store
86 lines (72 loc) · 3.21 KB
/
.zflai_store
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Copyright (c) 2018 Sebastian Gniazdowski
#
# .zflai_store
#
# Performs a store operation, memory-to-disk basically, or in other words
# makes data in RAM persistent. The data has multiple traits already resolved:
# - target database
# - target table
# - type (log or record)
#
# It still needs resolution of the special keywords: %ID, %CN, %ESECS.
local entry tpe sel_db sel_table sel_table_resolved timestamp input priv_arr_name db_string key cause="$1"
local -A priv_arr_map count_hash
integer priv_arr_index=0 arr_len entry_count=0
for entry in "${DATA[@]}"; do
if [[ "$entry" = (#b)[\ $'\t']#([^:]##)::([^:]##)::([^:]##)::([0-9]##)[\ $'\t']##\|[\ $'\t']##(*) ]]; then
tpe="${match[1]}" sel_db="${match[2]}" sel_table="${match[3]}" timestamp="${match[4]}" input="${match[5]}"
if [[ -n "${priv_arr_map[${sel_db}__%__${sel_table}]}" ]]; then
priv_arr_name="${priv_arr_map[${sel_db}__%__${sel_table}]}"
else
(( ++ priv_arr_index ))
priv_arr_name="SUB_DATA_${priv_arr_index}"
local -a "$priv_arr_name"
priv_arr_map[${sel_db}__%__${sel_table}]="$priv_arr_name"
fi
arr_len="${(P)#priv_arr_name}"
# Append is fast on recent Zshells (uses realloc)
priv_arr_name="${priv_arr_name}[$(( arr_len + 1 ))]"
: "${(P)priv_arr_name::=$entry}"
(( ++ entry_count ))
count_hash[${DB_DEFS[${sel_db}_<access>_engine]}]=$(( ${count_hash[${DB_DEFS[${sel_db}_<access>_engine]}]:-0} + 1 ))
fi
done
# Clear, as data has been redistributed to SUB_DATA_* arrays
DATA=()
local header=""
builtin strftime -s header '%Y%m%d-%H:%M:%S' "$EPOCHSECONDS"
header="------ ↓ [$header] MEMORY TO DISK #$entry_count (agent: ${sysparams[pid]}, save cause: $cause) ↓ ------"
.zflai_run_log "$header"
header="Routing ";
for key in "${(k)count_hash[@]}"; do
header+="${count_hash[$key]} log(s) to $key, "
done
[[ "$header" != "Routing " ]] && .zflai_run_log "${header%, }"
local -A _xchg_map_
_xchg_map_=(
"%TABLE%" "null"
"%ID%" "$ZUID_ID"
"%CN%" "$ZUID_CODENAME"
)
local _xchg_pat_="(%TABLE%|%ID%|%CN%)"
integer idx
for (( idx=1; idx <= priv_arr_index; ++ idx )); do
priv_arr_name="SUB_DATA_${idx}"
db_string="${(k)priv_arr_map[(r)$priv_arr_name]}"
sel_db="${db_string%%__%__*}"
sel_table="${db_string##*__%__}"
.zflai_resolve "$sel_table" sel_table_resolved
_xchg_map_[%TABLE%]="$sel_table_resolved"
[[ "$db_string" = [[:space:]]# ]] && { print "ERROR: didn't find db_string for $priv_arr_name"; continue; }
if [[ "${DB_DEFS[${sel_db}_<access>_engine]}" = "sqlite3" ]]; then
.zflai_sqlite_store "$sel_db" "$sel_table" "$priv_arr_name" "$sel_table_resolved"
elif [[ "${DB_DEFS[${sel_db}_<access>_engine]}" = "file" ]]; then
.zflai_file_store "$sel_db" "$sel_table" "$priv_arr_name" "$sel_table_resolved"
elif [[ "${DB_DEFS[${sel_db}_<access>_engine]}" = "elastic-search" ]]; then
.zflai_elasticsearch_store "$sel_db" "$sel_table" "$priv_arr_name"
elif [[ "${DB_DEFS[${sel_db}_<access>_engine]}" = "mysql" ]]; then
.zflai_mysql_store "$sel_db" "$sel_table" "$priv_arr_name" "$sel_table_resolved"
fi
unset "$priv_arr_name"
done
# vim:ft=zsh:et