Skip to content

Instantly share code, notes, and snippets.

@thiagoh
Last active June 10, 2018 03:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thiagoh/ab66e8495bb634fa726f28b7b258760b to your computer and use it in GitHub Desktop.
Save thiagoh/ab66e8495bb634fa726f28b7b258760b to your computer and use it in GitHub Desktop.
RiverDB the simplest and smallest db of all
#!/bin/bash
cmd=$1
key=$2
value=$3
file=db.data
if [[ "$cmd" = "" ]]; then
echo "Invalid command";
exit 1;
fi
if [[ "$key" = "" ]] || [[ "$key" = *":"* ]] ; then
echo "Invalid key. Key cannot be empty or have such characters: ':'";
exit 1;
fi
if [[ "$cmd" = "get" ]]; then
# Using tac and sed GNU Commands in OSX
gtac $file | gsed -r -n 0,/^$key:\(.+\)/s/^$key:\(.+\)/\\1/p
exit 0;
fi
if [[ "$cmd" = "set" ]]; then
echo "$key:$value" >> $file
exit 0;
fi
## Used for testing purposes
if [[ "$cmd" = "fill" ]]; then
n=0
while [[ $n -lt $2 ]]; do
key=`gdate +%s%N | md5`
./river set ${key:0:8} pre_value_`gdate +%s%N | md5`_pos_value
n=$(($n+1))
done
fi
$ riverdb set foo bar
$ riverdb get foo
bar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment