Skip to content

Commit

Permalink
Protect the 'keys' key and relax 'EXISTS' command
Browse files Browse the repository at this point in the history
  • Loading branch information
aw committed Jul 31, 2020
1 parent 6e293f1 commit 19fb09b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 5 additions & 4 deletions libkv.l
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) )
Expand Down Expand Up @@ -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 ()
Expand Down
2 changes: 1 addition & 1 deletion module.l
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
6 changes: 3 additions & 3 deletions test/test_kv.l
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@
(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")
(assert-equal "task5" (kv-cmd-lindex (kv-name "tasks") 4) "[LINDEX] Should return the key at index 5")
(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")
]
Expand Down

0 comments on commit 19fb09b

Please sign in to comment.