Add support for the DEL command

This commit is contained in:
R. Tyler Croy 2019-01-06 15:53:05 -08:00
parent 033557f713
commit fe40a7d638
No known key found for this signature in database
GPG Key ID: E5C92681BEF6CEA2
6 changed files with 75 additions and 0 deletions

29
acceptance/test_del.py Normal file
View File

@ -0,0 +1,29 @@
#!/usr/bin/env python
import helpers
import random
import sys
import unittest
class DelTest(helpers.MozzoniTest):
def setUp(self):
# Generate a random key for each test case to avoid potential
# collisions
self.key = random.randint(1, sys.maxint)
def test_del(self):
# No keys to remove, but should not error
self.assertEqual(0,
self.r.delete(self.key))
# With a set keys the return should be the number of keys deleted
self.assertTrue(
self.r.set(self.key, 'yey'))
self.assertEqual(1,
self.r.delete(self.key))
if __name__ == '__main__':
random.seed()
unittest.main()

View File

@ -13,6 +13,7 @@ package body Mozzoni.Command_Loader is
Register_Command ("SET", Mozzoni.Commands.Keys.Handle_Set'Access);
Register_Command ("GET", Mozzoni.Commands.Keys.Handle_Get'Access);
Register_Command ("EXISTS", Mozzoni.Commands.Keys.Handle_Exists'Access);
Register_Command ("DEL", Mozzoni.Commands.Keys.Handle_Del'Access);
end Load;
end Mozzoni.Command_Loader;

View File

@ -138,4 +138,21 @@ package body Mozzoni.Commands.Keys is
end Handle_Exists;
procedure Handle_Del (Client : in out Client_Type;
Command : Command_Array_Access) is
Deleted : Natural := 0;
begin
for Key_Item of Command (2 .. Command'Length) loop
if KeyValue.Remove (Key_Item.Value) then
Deleted := Deleted + 1;
end if;
end loop;
Client.Write (':');
Client.Write (Deleted);
Client.Write_Line_Ending;
end Handle_Del;
end Mozzoni.Commands.Keys;

View File

@ -17,4 +17,8 @@ package Mozzoni.Commands.Keys is
Command : Command_Array_Access)
with Pre => Client.Is_Valid and Command /= null;
procedure Handle_Del (Client : in out Client_Type;
Command : Command_Array_Access)
with Pre => Client.Is_Valid and Command /= null;
end Mozzoni.Commands.Keys;

View File

@ -21,6 +21,19 @@ package body Mozzoni.Store is
end Exists;
function Remove (Key : in Key_Type) return Boolean is
-- Need a variable since Delete is an `in out` operation
KV : Hashed_Maps.Map := Store;
begin
if not Hashed_Maps.Contains (KV, Key) then
return False;
end if;
Hashed_Maps.Delete (KV, Key);
return True;
end Remove;
function Is_Expired (Key : in Key_Type) return Boolean is
Value : constant Value_Type := Hashed_Maps.Element (Store, Key);
Now : constant Ada.Calendar.Time := Ada.Calendar.Clock;

View File

@ -56,6 +56,17 @@ package Mozzoni.Store is
function Exists (Key : in Key_Type) return Boolean
with Pre => Mozzoni.Store.Is_Valid_Key (Key);
-- Remove a Key from the store
--
-- To avoid too many traversals of the protected object boundary, this
-- function will return a Boolean to indicate to the caller that the key
-- existed before it was purged.
--
-- @param Key a valid key
-- @return Boolean True if the key existed, otherwise false.
function Remove (Key : in Key_Type) return Boolean
with Pre => Mozzoni.Store.Is_Valid_Key (Key);
---
-- Is_Expired determines whether the given key has expired from the store
--