From 19fb09b43c79f45a9c41eedca2a1a8a435eb2974 Mon Sep 17 00:00:00 2001 From: Alex Williams Date: Fri, 31 Jul 2020 13:38:37 +0000 Subject: [PATCH] Protect the 'keys' key and relax 'EXISTS' command --- CHANGELOG.md | 5 +++++ libkv.l | 9 +++++---- module.l | 2 +- test/test_kv.l | 6 +++--- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 205ebe5..16d9e51 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 0.15.1 (2020-07-31) + + * Move the global list of all keys to '%stats%/keys' so it can't be deleted or modified + * Allow 'EXISTS' command on all keys including keys prefixed with '%stats%/' + ## 0.15.0 (2020-07-31) * Consolidate all server library code into one file: libkv.l diff --git a/libkv.l b/libkv.l index 7549d8c..2a75d59 100644 --- a/libkv.l +++ b/libkv.l @@ -102,8 +102,8 @@ 0 ] # return 0 if no key is specified [de kv-cmd-exists (Key Elements) - (if (and Key (not (pre? "%stats%/" Key))) - (cnt '((N) (unless (pre? "%stats%/" N) (kv-value N))) (conc (list Key) Elements)) + (if Key + (cnt '((N) (kv-value N)) (conc (list Key) Elements)) 0 ] # return 0 if no key is specified [de kv-cmd-ident (Child Elements) @@ -150,7 +150,7 @@ [de kv-cmd-set (Key Value) (when (and Key Value (not (pre? "%stats%/" Key)) (set (kv-name Key) Value)) - (push1 (kv-name "keys") Key) # keep a list of all the keys + (push1 (kv-name "%stats%/keys") Key) # keep a list of all the keys "OK" ] [de kv-cmd-get (Key) @@ -196,6 +196,7 @@ [de kv-info-memory () (make (link + (cons "total_keys" (length *KV/%stats%/keys)) (cons "used_memory" (* (heap) 1024 1024)) (cons "used_memory_human" (pack (heap) "M")) (cons "used_memory_startup" *KV_startup_memory) ) @@ -285,7 +286,7 @@ # Write all the known keys to a temporary DB file [de kv-save-db-keys () (out *KV_db_tmp - (mapcar kv-save-data (kv-cmd-get "keys") ] + (mapcar kv-save-data (kv-cmd-get "%stats%/keys") ] # Perform some maintenance tasks when save ends [de kv-save-cleanup () diff --git a/module.l b/module.l index 8d26e45..7bbf789 100644 --- a/module.l +++ b/module.l @@ -1,6 +1,6 @@ [de APP_INFO ("name" "picolisp-kv") - ("version" "0.15.0") + ("version" "0.15.1") ("summary" "Redis-inspired in-memory key/value store written in PicoLisp") ("source" "https://github.com/aw/picolisp-kv") ("author" "Alexander Williams") diff --git a/test/test_kv.l b/test/test_kv.l index 730cbdf..236c327 100644 --- a/test/test_kv.l +++ b/test/test_kv.l @@ -16,7 +16,7 @@ (assert-nil (kv-cmd-rpush "tasks") "[RPUSH] Should return NIL if only 1 key is provided") (assert-nil (kv-cmd-rpush "tasks" (kv-name "tasks")) "[RPUSH] Should return NIL if no elements are provided") (assert-nil (kv-cmd-rpush "tasks" (kv-name "tasks") "element 1") "[RPUSH] Should return NIL if the elements aren't a list") - (kv-cmd-del "keys") + (kv-cmd-del "%stats%/keys") (assert-equal 5 (kv-cmd-rpush "tasks" (kv-name "tasks") '("task1" "task2" "task3" "task4" "task5")) "[RPUSH] Should return the length of the new list") (assert-equal 7 (kv-cmd-rpush "tasks" (kv-name "tasks") '("task6" "task7")) "[RPUSH] Should return the extended length of the list") (assert-equal "task1" (kv-cmd-lindex (kv-name "tasks") 0) "[LINDEX] Should return the key at index 0") @@ -24,8 +24,8 @@ (assert-equal "task7" (kv-cmd-lindex (kv-name "tasks") -1) "[LINDEX] Should return the last key") (assert-equal "task6" (kv-cmd-lindex (kv-name "tasks") -2) "[LINDEX] Should return the penultimate key") (assert-nil (kv-cmd-lindex (kv-name "tasks") 10) "[LINDEX] Should return NIL if no value exists at the index") - (assert-equal 1 (kv-cmd-llen (kv-name "keys")) "[LLEN] Should return the number of keys in the keys list") - (assert-equal "tasks" (kv-cmd-lindex (kv-name "keys") 0) "[LINDEX] Should return the name of the key in the keys list") + (assert-equal 1 (kv-cmd-llen (kv-name "%stats%/keys")) "[LLEN] Should return the number of keys in the keys list") + (assert-equal "tasks" (kv-cmd-lindex (kv-name "%stats%/keys") 0) "[LINDEX] Should return the name of the key in the keys list") (assert-equal "task1" (kv-cmd-lpop (kv-name "tasks")) "[LPOP] Should return the first value added to the tasks list") (assert-equal 6 (kv-cmd-llen (kv-name "tasks")) "[LLEN] Should return the number of keys remaining in the tasks list") ]