chiark / gitweb /
speed up use of sa-learn
authorColin Watson <cjwatson@chiark.greenend.org.uk>
Sun, 31 May 2009 04:34:13 +0000 (04:34 +0000)
committerColin Watson <cjwatson@chiark.greenend.org.uk>
Sun, 31 May 2009 04:34:13 +0000 (04:34 +0000)
sa-learn-cache [new file with mode: 0755]

diff --git a/sa-learn-cache b/sa-learn-cache
new file mode 100755 (executable)
index 0000000..b51c4e7
--- /dev/null
@@ -0,0 +1,40 @@
+#! /bin/sh
+# Depends: chiark-utils-bin
+set -e
+
+DIR="$HOME/tmp/sa-learn-cache"
+
+LOCKED=false
+if [ "$1" = --locked ]; then
+    LOCKED=:
+    shift
+fi
+
+if [ "$1" = --spam ]; then
+    CACHE="$DIR/spam"
+elif [ "$1" = --ham ]; then
+    CACHE="$DIR/ham"
+else
+    echo "don't know how to handle anything other than --spam or --ham" >&2
+    exit 1
+fi
+LOCK="$CACHE.lock"
+
+mkdir -p "$DIR"
+
+if ! $LOCKED; then
+    exec with-lock-ex -w "$LOCK" "$0" --locked "$@"
+else
+    if [ -e "$CACHE" ]; then
+       SIZE="$(stat -c %s "$CACHE")"
+    else
+       SIZE=0
+    fi
+    cat >> "$CACHE"
+    if [ "$SIZE" -gt 4194304 ]; then
+       sa-learn --mbox --no-sync "$@" < "$CACHE"
+       rm -f "$CACHE"
+    fi
+fi
+
+exit 0