feat: Implement LLEN command to get list length
Some checks failed
Build / build (push) Failing after 1m1s

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.
This commit is contained in:
Jacob Schmidt 2025-04-13 16:49:05 -05:00
parent 9503e7c42d
commit 44f6b40e79
2 changed files with 28 additions and 1 deletions

View File

@ -189,6 +189,9 @@ 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)) : "");

View File

@ -152,6 +152,30 @@ namespace Firefly
: Encoding.UTF8.GetBytes("$-1\r\n");
}
/// <summary>
/// Handles the LLEN command which returns the length of a list.
/// </summary>
/// <param name="args">Command arguments containing the key</param>
/// <returns>The length of the list or 0 if the key does not exist</returns>
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");
}
/// <summary>
/// Handles the LRANGE command which returns a range of elements from a list.
/// </summary>