Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement ListLength #134

Merged
merged 1 commit into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/Momento.Sdk/Incubating/Internal/ScsDataClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -473,4 +473,23 @@ public async Task<CacheListRemoveAllResponse> ListRemoveAllAsync(string cacheNam
}
return new CacheListRemoveAllResponse();
}

public async Task<CacheListLengthResponse> ListLengthAsync(string cacheName, string listName)
{
_ListLengthRequest request = new()
{
ListName = listName.ToByteString(),
};
_ListLengthResponse response;

try
{
response = await this.grpcManager.Client.ListLengthAsync(request, MetadataWithCache(cacheName), deadline: CalculateDeadline());
}
catch (Exception e)
{
throw CacheExceptionMapper.Convert(e);
}
return new CacheListLengthResponse(response);
}
}
16 changes: 16 additions & 0 deletions src/Momento.Sdk/Incubating/Responses/CacheListLengthResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Momento.Protos.CacheClient;

namespace Momento.Sdk.Incubating.Responses;

public class CacheListLengthResponse
{
public int Length { get; private set; } = 0;

public CacheListLengthResponse(_ListLengthResponse response)
{
if (response.ListCase == _ListLengthResponse.ListOneofCase.Found)
{
Length = checked((int)response.Found.Length);
}
}
}
17 changes: 17 additions & 0 deletions src/Momento.Sdk/Incubating/SimpleCacheClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,23 @@ public async Task<CacheListRemoveAllResponse> ListRemoveAllAsync(string cacheNam
return await this.dataClient.ListRemoveAllAsync(cacheName, listName, value);
}

/// <summary>
/// Calculate the length of a list in the cache.
///
/// A list that does not exist is interpreted to have length 0.
malandis marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
/// <param name="cacheName">Name of the cache to perform the lookup in.</param>
/// <param name="listName">The list to calculate length.</param>
/// <returns>Task representing the length of the list.</returns>
/// <exception cref="ArgumentNullException">Any of <paramref name="cacheName"/> or <paramref name="listName"/> is <see langword="null"/>.</exception>
public async Task<CacheListLengthResponse> ListLengthAsync(string cacheName, string listName)
{
Utils.ArgumentNotNull(cacheName, nameof(cacheName));
Utils.ArgumentNotNull(listName, nameof(listName));

return await this.dataClient.ListLengthAsync(cacheName, listName);
}

/// <inheritdoc />
public void Dispose()
{
Expand Down
28 changes: 28 additions & 0 deletions tests/Integration/Momento.Sdk.Incubating.Tests/ListTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -562,4 +562,32 @@ public async Task ListRemoveAllAsync_ValueIsString_ListNotThereNoop()
await client.ListRemoveAllAsync(cacheName, listName, Utils.NewGuidString());
Assert.Equal(CacheGetStatus.MISS, (await client.ListFetchAsync(cacheName, listName)).Status);
}

[Theory]
[InlineData(null, "my-list")]
[InlineData("cache", null)]
public async Task ListLengthAsync_NullChecks_ThrowsException(string cacheName, string listName)
{
await Assert.ThrowsAsync<ArgumentNullException>(async () => await client.ListLengthAsync(cacheName, listName));
}

[Fact]
public async Task ListLengthAsync_ListIsMissing_HappyPath()
{
var lengthResponse = await client.ListLengthAsync(cacheName, Utils.NewGuidString());
Assert.Equal(0, lengthResponse.Length);
}

[Fact]
public async Task ListLengthAsync_ListIsFound_HappyPath()
{
var listName = Utils.NewGuidString();
foreach (var i in Enumerable.Range(0, 10))
{
await client.ListPushBackAsync(cacheName, listName, Utils.NewGuidByteArray(), false);
}

var lengthResponse = await client.ListLengthAsync(cacheName, listName);
Assert.Equal(10, lengthResponse.Length);
}
}