1 if [ -z "${__kvs_sh_loaded}" ]
5 __kvs_sh_VERSION="KVS v0.1"
6 __kvs_sh_DESCRIPTION="Simple bourne shell based key-value store"
7 __kvs_sh_COPYRIGHT=$( cat <<"END_OF_COPYRIGHT"
9 Copyright (c) 2000-2012, CyberLeo
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
15 * Redistributions of the source code must retain the above copyright
16 notice, this list of conditions, and the following disclaimer.
17 * Redistributions in binary form must reproduce the above copyright
18 notice, this list of conditions, and the following disclaimer in the
19 documentation and/or other materials provided with the distribution.
20 * Neither the name of the organization nor the names of its contributors
21 may be used to endorse or promote products derived from this software
22 without specific prior written permission.
24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
28 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
30 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 HOWEVER CAUSED AND ON ANY THEORY OF LIABILTY, WHETHER IN CONTRACT, STRICT
32 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 ${__kvs_sh_DESCRIPTION}
47 Set the following variables before sourcing this library:
49 kvs - Path and filename of the kvdb database file
55 [ -s "${kvs}" ] || echo "${t}${t}# KVSv0 id-key-value database" > "${kvs}"
57 # Add or replace a value stored for a given key and id in the kvs
59 [ "${1}" -a "${2}" ] || return 255
63 printf "%s\t%s\t%s\n" "${id}" "${var}" "${val}" >> "${kvs}"
66 # Fetch the most recent value stored for a given key and id from the kvs
68 [ "${1}" -a "${2}" ] || return 255
71 grep "^${id}${t}${var}${t}" "${kvs}" | tail -n 1 | cut -d"${t}" -f3-
74 # Does the kvs have any entries for a given ID?
76 [ "${1}" ] || return 255
78 grep -q "^${id}${t}" "${kvs}"
81 # Does the kvs have a given key for a given ID?
83 [ "${1}" -a "${2}" ] || return 255
86 grep -q "^${id}${t}${var}${t}" "${kvs}"
89 # Remove a given key with a given ID from the kvs
91 [ "${1}" -a "${2}" ] || return 255
94 grep -v "^${id}${t}${var}${t}" "${kvs}" > "${kvs}.tmp" && mv -f "${kvs}.tmp" "${kvs}" || rm -f "${kvs}.tmp"
97 # Remove all keys with a given ID from the kvs
99 [ "${1}" ] || return 255
101 grep -v "^${id}${t}" "${kvs}" > "${kvs}.tmp" && mv -f "${kvs}.tmp" "${kvs}" || rm -f "${kvs}.tmp"