From 44f6b40e79ba45c62d8bb61e30bb389510f767ed Mon Sep 17 00:00:00 2001 From: Jacob Schmidt Date: Sun, 13 Apr 2025 16:49:05 -0500 Subject: [PATCH] feat: Implement LLEN command to get list length This commit introduces the LLEN command, which allows users to retrieve the length of a list stored in the database. - Added `HandleLLengthCommand` function to `ListOperations.cs` to handle the LLEN command logic. This function retrieves the list associated with the given key and returns its length. If the key does not exist, it returns 0. - Updated `Firefly.cs` to include the LLEN command in the command processing logic, mapping the "LLEN" command to the `HandleLLengthCommand` function. --- src/Firefly.cs | 5 ++++- src/ListOperations.cs | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/Firefly.cs b/src/Firefly.cs index a1d2280..be3bbba 100644 --- a/src/Firefly.cs +++ b/src/Firefly.cs @@ -188,7 +188,10 @@ namespace Firefly case "LINDEX": return HandleLIndexCommand(parts.Length > 1 ? string.Join(" ", parts.Skip(1)) : ""); - + + case "LLEN": + return HandleLLengthCommand(parts.Length > 1 ? parts[1] : ""); + case "LRANGE": return HandleLRangeCommand(parts.Length > 1 ? string.Join(" ", parts.Skip(1)) : ""); diff --git a/src/ListOperations.cs b/src/ListOperations.cs index 716af73..58fb450 100644 --- a/src/ListOperations.cs +++ b/src/ListOperations.cs @@ -152,6 +152,30 @@ namespace Firefly : Encoding.UTF8.GetBytes("$-1\r\n"); } + /// + /// Handles the LLEN command which returns the length of a list. + /// + /// Command arguments containing the key + /// The length of the list or 0 if the key does not exist + static byte[] HandleLLengthCommand(string args) + { + if (string.IsNullOrWhiteSpace(args)) + { + return Encoding.UTF8.GetBytes("-ERR wrong number of arguments for 'llen' command\r\n"); + } + + string key = args.Trim(); + int length = 0; + + // Use helper function to safely read the list with read lock + ListStoreWithReadLock(key, list => + { + length = list.Count; + }); + + return Encoding.UTF8.GetBytes($":{length}\r\n"); + } + /// /// Handles the LRANGE command which returns a range of elements from a list. ///