]> CyberLeo.Net >> Repos - CDN/shlib.git/blob - lib/sh/kvs.sh
sh/stopwatch: Add 'time' verb to stopwatch, to retrieve just the times
[CDN/shlib.git] / lib / sh / kvs.sh
1 if [ -z "${__kvs_sh_loaded}" ]
2 then
3   __kvs_sh_loaded=yes
4
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"
8
9 Copyright (c) 2000-2012, CyberLeo
10 All rights reserved.
11
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14
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.
23
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.
34
35 END_OF_COPYRIGHT
36 )
37
38   t="$(printf '\t')"
39
40   [ "${kvs}" ] || {
41     cat <<EOF
42 ${__kvs_sh_VERSION}
43 ${__kvs_sh_DESCRIPTION}
44
45 ${__kvs_sh_COPYRIGHT}
46
47 Set the following variables before sourcing this library:
48
49  kvs  - Path and filename of the kvdb database file
50 EOF
51     kill -ABRT $$
52     exit 1
53   }
54
55   [ -s "${kvs}" ] || echo "${t}${t}# KVSv0 id-key-value database" > "${kvs}"
56
57   # Add or replace a value stored for a given key and id in the kvs
58   kvs_set() {
59     [ "${1}" -a "${2}" ] || return 255
60     local id="${1}"
61     local var="${2}"
62     local val="${3}"
63     printf "%s\t%s\t%s\n" "${id}" "${var}" "${val}" >> "${kvs}"
64   }
65
66   # Fetch the most recent value stored for a given key and id from the kvs
67   kvs_get() {
68     [ "${1}" -a "${2}" ] || return 255
69     local id="${1}"
70     local var="${2}"
71     grep "^${id}${t}${var}${t}" "${kvs}" | tail -n 1 | cut -d"${t}" -f3-
72   }
73
74   # Does the kvs have any entries for a given ID?
75   kvs_has_id() {
76     [ "${1}" ] || return 255
77     local id="${1}"
78     grep -q "^${id}${t}" "${kvs}"
79   }
80
81   # Does the kvs have a given key for a given ID?
82   kvs_has_key() {
83     [ "${1}" -a "${2}" ] || return 255
84     local id="${1}"
85     local var="${2}"
86     grep -q "^${id}${t}${var}${t}" "${kvs}"
87   }
88
89   # Remove a given key with a given ID from the kvs
90   kvs_unset() {
91     [ "${1}" -a "${2}" ] || return 255
92     local id="${1}"
93     local var="${2}"
94     grep -v "^${id}${t}${var}${t}" "${kvs}" > "${kvs}.tmp" && mv -f "${kvs}.tmp" "${kvs}" || rm -f "${kvs}.tmp"
95   }
96
97   # Remove all keys with a given ID from the kvs
98   kvs_unset_all() {
99     [ "${1}" ] || return 255
100     local id="${1}"
101     grep -v "^${id}${t}" "${kvs}" > "${kvs}.tmp" && mv -f "${kvs}.tmp" "${kvs}" || rm -f "${kvs}.tmp"
102   }
103 fi