Skip to content

Commit 76d0f05

Browse files
authored
Add key-value store (#567)
1 parent 3678c3b commit 76d0f05

File tree

2 files changed

+120
-1
lines changed

2 files changed

+120
-1
lines changed

lib/kvstore.sh

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
kv_create() {
3+
local f=$1
4+
mkdir -p $(dirname $f)
5+
touch $f
6+
}
7+
8+
kv_clear() {
9+
local f=$1
10+
echo "" > $f
11+
}
12+
13+
kv_set() {
14+
if [[ $# -eq 3 ]]; then
15+
local f=$1
16+
if [[ -f $f ]]; then
17+
echo "$2=$3" >> $f
18+
fi
19+
fi
20+
}
21+
22+
kv_get() {
23+
if [[ $# -eq 2 ]]; then
24+
local f=$1
25+
if [[ -f $f ]]; then
26+
grep "^$2=" $f | sed -e "s/^$2=//" | tail -n 1
27+
fi
28+
fi
29+
}
30+
31+
kv_keys() {
32+
local f=$1
33+
local keys=()
34+
35+
if [[ -f $f ]]; then
36+
# get list of keys
37+
while IFS="=" read -r key value; do
38+
keys+=("$key")
39+
done < $f
40+
41+
echo "${keys[@]}" | tr ' ' '\n' | sort -u
42+
fi
43+
}
44+
45+
kv_list() {
46+
local f=$1
47+
48+
kv_keys $f | tr ' ' '\n' | while read -r key; do
49+
if [[ -n $key ]]; then
50+
echo "$key=$(kv_get $f $key)"
51+
fi
52+
done
53+
}

test/unit

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,75 @@ testOutput() {
4646
assertEquals 'should preserve unescaped backslashes' ' Foo \ bar' "${stdout}"
4747
}
4848

49+
testKeyValue() {
50+
local store=$(mktemp)
51+
52+
kv_create $store
53+
54+
kv_set $store key value
55+
kv_set $store foo bar
56+
kv_set $store key other_value
57+
kv_set $store bar baz
58+
59+
assertEquals "other_value" "$(kv_get $store key)"
60+
assertEquals "bar" "$(kv_get $store foo)"
61+
assertEquals "baz" "$(kv_get $store bar)"
62+
63+
# if the key isn't there it should return an empty string
64+
assertEquals "" "$(kv_get $store not_there)"
65+
66+
# kv_keys returns each key on a new line
67+
assertEquals "$(printf "%s\n" bar foo key)" "$(kv_keys $store)"
68+
69+
# kv_list returns key=value on individual lines
70+
assertEquals "$(printf "%s\n" bar=baz foo=bar key=other_value)" "$(kv_list $store)"
71+
72+
# calling create on an existing store doesn't erase it
73+
kv_create $store
74+
assertEquals "$(printf "%s\n" bar=baz foo=bar key=other_value)" "$(kv_list $store)"
75+
76+
# now clear the store
77+
kv_clear $store
78+
79+
assertEquals "" "$(kv_get $store key)"
80+
assertEquals "" "$(kv_keys $store)"
81+
assertEquals "" "$(kv_list $store)"
82+
}
83+
84+
# if the file doesn't exist, everything should be a no-op
85+
testKeyValueNoFile() {
86+
# empty file argument
87+
local empty=""
88+
89+
kv_set $empty key value
90+
91+
assertEquals "$(kv_get $empty key)" ""
92+
assertEquals "$(kv_keys $empty)" ""
93+
assertEquals "$(kv_list $empty)" ""
94+
95+
local store="/tmp/does-not-exist"
96+
97+
kv_set $store key value
98+
99+
assertEquals "" "$(kv_get $store key)"
100+
assertEquals "" "$(kv_keys $store)"
101+
assertEquals "" "$(kv_list $store)"
102+
103+
# running these commands has not created this file
104+
assertTrue "[[ ! -e $store ]]"
105+
106+
local space=" "
107+
kv_set $space key value
108+
109+
assertEquals "$(kv_get $space key)" ""
110+
assertEquals "$(kv_keys $space)" ""
111+
assertEquals "$(kv_list $space)" ""
112+
}
113+
49114
# the modules to be tested
50115
source "$(pwd)"/lib/monitor.sh
51116
source "$(pwd)"/lib/output.sh
117+
source "$(pwd)"/lib/kvstore.sh
52118

53119
# import the testing framework
54-
source "$(pwd)"/test/shunit2
120+
source "$(pwd)"/test/shunit2

0 commit comments

Comments
 (0)