From 4e18152b4d7a17e85389fdbb609afa98617d1673 Mon Sep 17 00:00:00 2001 From: Katze Miau Date: Wed, 30 Nov 2011 10:41:51 +0000 Subject: 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. 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 "$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 -- cgit v0.10.1