diff options
author | Katze Miau <miaukatzemiau@priveasy.de> | 2011-11-30 10:41:51 (GMT) |
---|---|---|
committer | Katze Miau <miaukatzemiau@priveasy.de> | 2011-11-30 10:41:51 (GMT) |
commit | 4e18152b4d7a17e85389fdbb609afa98617d1673 (patch) | |
tree | 2df3d70703bcea8734069ed21ab4bdba4df6435a /files/common | |
parent | e0a22e2ed3fbd278abc50a1d93856b7b8ac7c4b1 (diff) |
add p2ptbl
P2P tables allow to maintain distributed state using a MVCC key-value
store. This patch adds the executable for manipulating a table but not
the gossip protocol to synchronize it.
Diffstat (limited to 'files/common')
-rwxr-xr-x | files/common/sbin/p2ptbl | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/files/common/sbin/p2ptbl b/files/common/sbin/p2ptbl new file mode 100755 index 0000000..8365915 --- /dev/null +++ b/files/common/sbin/p2ptbl @@ -0,0 +1,84 @@ +#!/bin/sh + +function init { + [ -n "$1" ] || printArgs + [ -f "$1" ] || cat >$1 </dev/null +} + +function get { + [ -n "$2" ] || printArgs + grep -P "^$2\t" "$1" | cut -f3- +} + +function update { + [ -n "$2" -a -n "$3" ] || printArgs + oldversion=$(grep -P "^$2\t" "$1" | cut -f2) + tmpfile="$1.update" + echo -e "$2\t$(($oldversion + 1 + $RANDOM))\t$3" > "$tmpfile" + merge "$1" "$tmpfile" + rm "$tmpfile" +} + +function merge { + sort "$1" "$2" \ + | { + oldkey="" + oldversion=0 + oldval="" + while read key version val; do + if [ "$key" == "$oldkey" ]; then + if [ "$version" -gt "$oldversion" ]; then + oldversion="$version" + oldval="$val" + fi + else + [ -n "$oldkey" ] && echo -e "$oldkey\t$oldversion\t$oldval" + oldkey="$key" + oldversion="$version" + oldval="$val" + fi + done + [ -n "$oldkey" ] && echo -e "$oldkey\t$oldversion\t$oldval" + } > "$1~" + mv "$1~" "$1" +} + +function printArgs { + echo -e "Usage: +$0 init table +$0 get table key +$0 update table key value +$0 merge table foreign-table" + exit -1 +} + +function checkTable { + [ -n "$1" ] || printArgs; + [ -f "$1" ] || { + echo "$1 is no P2P table"; + printArgs; + } +} + +case "$1" in + init) + checkTable "$2" + init "$2" + ;; + get) + checkTable "$2" + get "$2" "$3" + ;; + update) + checkTable "$2" + update "$2" "$3" "$4" + ;; + merge) + checkTable "$2" + checkTable "$3" + merge "$2" "$3" + ;; + *) + printArgs + ;; +esac
\ No newline at end of file |