Skip to content

Instantly share code, notes, and snippets.

@phiresky
Last active June 6, 2019 08:54
Show Gist options
  • Save phiresky/f9de114c7eda7cf80409a1fe304787ca to your computer and use it in GitHub Desktop.
Save phiresky/f9de114c7eda7cf80409a1fe304787ca to your computer and use it in GitHub Desktop.
zsh histdb example data filler

run in a somewhat interesting dir (e.g. checkout of a linux kernel) with <100k files (otherwise too slow).

mv -i ~/.histdb/zsh-history.db ~/.histdb/realhistory

./makedb.sh | sqlite3 ~/.histdb/zsh-history.db
#!/bin/bash
set -eu
cmds=("ffprobe -v error -i " "cat" "vim" "ls -alh" "wc -l" "yay -S" "cargo build" "rms -rf" "yarn run ts-node")
sql_escape () {
sed -e "s/'/''/g" <<< "$@" | tr -d '\000'
}
started=1400000000
HISTDB_HOST="'$(sql_escape myhost)'"
for i in {1..100}; do
echo "ITER $i" >&2
echo "BEGIN;"
find * -type d | while read dir; do
if (( RANDOM%10 == 0 )); then
( # subshell
cd $dir
find -type f -printf '%P\n' | while read fname; do
if (( RANDOM%5 == 0 )); then
cmd="'$(sql_escape "${cmds[RANDOM%${#cmds[@]}]} $fname")'"
pwd="'$(sql_escape "/tmp/linux/${dir}")'"
started=$((started + (RANDOM%5000) ))
HISTDB_SESSION=1
echo \
"insert into commands (argv) values (${cmd});
insert into places (host, dir) values (${HISTDB_HOST}, ${pwd});
insert into history
(session, command_id, place_id, start_time)
select
${HISTDB_SESSION},
commands.id,
places.id,
${started}
from
commands, places
where
commands.argv = ${cmd} and
places.host = ${HISTDB_HOST} and
places.dir = ${pwd}
;"
fi
done
)
fi
done
echo "COMMIT;"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment