diff options
Diffstat (limited to 'src/wasp-db.el')
| -rw-r--r-- | src/wasp-db.el | 48 |
1 files changed, 37 insertions, 11 deletions
diff --git a/src/wasp-db.el b/src/wasp-db.el index 46999b5e..516fcfde 100644 --- a/src/wasp-db.el +++ b/src/wasp-db.el @@ -39,17 +39,20 @@ (defun w/db-parse-value () "Parse a single RESP value from the current buffer." + (w/write-log (format "parsing: %S" (buffer-string))) (when-let ((c (char-after))) (delete-char 1) (cl-case c (?+ (w/db-parse-rest)) (?: (string-to-number (w/db-parse-rest))) (?$ - (let* ((len (string-to-number (w/db-parse-rest))) - (ret (w/devour (point) (+ (point) len)))) - (w/munch ?\r) - (w/munch ?\n) - ret)) + (let ((len (string-to-number (w/db-parse-rest)))) + (if (= len -1) + "" + (let ((ret (w/devour (point) (+ (point) len)))) + (w/munch ?\r) + (w/munch ?\n) + ret)))) (?* (let ((len (string-to-number (w/db-parse-rest)))) (--map (w/db-parse-value) (-iota len)))) @@ -73,17 +76,23 @@ If not, return nil." (insert data) (set-marker (process-mark proc) (point)) (goto-char (point-min)) - (while (w/db-parse-response)))) + (condition-case err + (while (w/db-parse-response)) + (error + (w/write-chat-event (format "Database crashed, error: %s" err)) + (w/db-disconnect))))) (defun w/db-encode (x) "Encode X for Redis." (cond - ((listp x) (format "*%d\r\n%s\r\n" (length x) (apply #'s-concat (-map #'w/db-encode x)))) - ((stringp x) (format "$%d\r\n%s\r\n" (string-bytes x) x)))) - + ((listp x) ;; encode lists as arrays + (format "*%d\r\n%s\r\n" (length x) (apply #'s-concat (-map #'w/db-encode x)))) + ((stringp x) ;; encode strings as bulk strings + (format "$%d\r\n%s\r\n" (string-bytes x) x)))) (defun w/db-send-raw (msg) "Send MSG to Redis." + (w/write-log (format "sending to redis: %s" msg)) (process-send-string w/db-process msg)) (defun w/db-cmd (cmd k) @@ -100,7 +109,12 @@ If not, return nil." (defun w/db-connect () "Connect to Redis." (w/db-disconnect) + (queue-clear w/db-callback-queue) + (with-current-buffer (get-buffer-create w/db-buffer) + (set-buffer-multibyte nil) + (erase-buffer)) (make-network-process + :coding 'no-conversion :name w/db-process :buffer nil :host w/db-host @@ -109,11 +123,23 @@ If not, return nil." (defun w/db-set (key val) "Set KEY to VAL in Redis." - (w/db-cmd `("SET" ,key ,val) (lambda (_) (message "ok")))) + (if (and (stringp key) (stringp val)) + (w/db-cmd `("SET" ,key ,val) (lambda (_) nil)) + (error "Redis key and value must be strings"))) (defun w/db-get (key k) "Get KEY from Redis and pass the corresponding value to K." - (w/db-cmd `("GET" ,key) k)) + (if (stringp key) + (w/db-cmd `("GET" ,key) k) + (error "Redis key must be string"))) + +(defun w/db-hset (key hkey val &rest vals) + "Set HKEY in hash KEY to VAL in Redis." + (w/db-cmd `("HSET" ,key ,hkey ,val ,@vals) (lambda (_) nil))) + +(defun w/db-hget (key hkey k) + "Get HKEY in hash KEY from Redis and pass the corresponding value to K." + (w/db-cmd `("HGET" ,key ,hkey) k)) (provide 'wasp-db) ;;; wasp-db.el ends here |
