Skip to content

Commit

Permalink
feat: add data client support for list api calls (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
bruuuuuuuce committed Jan 9, 2024
1 parent 1ad44d5 commit 566b3fe
Show file tree
Hide file tree
Showing 17 changed files with 1,582 additions and 1 deletion.
11 changes: 11 additions & 0 deletions lib/momento.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ library momento;

export 'src/topic_client.dart';
export 'src/cache_client.dart';
export 'src/utils/collection_ttl.dart';
export 'src/auth/credential_provider.dart';
export 'src/config/topic_configurations.dart';
export 'src/config/cache_configurations.dart';
Expand All @@ -19,5 +20,15 @@ export 'src/messages/responses/cache/control/list_caches_response.dart';
export 'src/messages/responses/cache/data/scalar/delete_response.dart';
export 'src/messages/responses/cache/data/scalar/get_response.dart';
export 'src/messages/responses/cache/data/scalar/set_response.dart';
export 'src/messages/responses/cache/data/list/list_fetch.dart';
export 'src/messages/responses/cache/data/list/list_concatenate_back.dart';
export 'src/messages/responses/cache/data/list/list_concatenate_front.dart';
export 'src/messages/responses/cache/data/list/list_length.dart';
export 'src/messages/responses/cache/data/list/list_pop_back.dart';
export 'src/messages/responses/cache/data/list/list_pop_front.dart';
export 'src/messages/responses/cache/data/list/list_push_back.dart';
export 'src/messages/responses/cache/data/list/list_push_front.dart';
export 'src/messages/responses/cache/data/list/list_remove_value.dart';
export 'src/messages/responses/cache/data/list/list_retain.dart';

// TODO: Export any libraries intended for clients of this package.
209 changes: 209 additions & 0 deletions lib/src/cache_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,49 @@ import 'package:momento/src/internal/utils/validators.dart';
import 'config/logger.dart';

abstract class ICacheClient {
// Control plane RPCs
Future<CreateCacheResponse> createCache(String cacheName);

Future<DeleteCacheResponse> deleteCache(String cacheName);

Future<ListCachesResponse> listCaches();

// Unary RPCs
Future<GetResponse> get(String cacheName, Value key);

Future<SetResponse> set(String cacheName, Value key, Value value,
{Duration? ttl});

Future<DeleteResponse> delete(String cacheName, Value key);

// List Collection RPCs
Future<ListConcatenateBackResponse> listConcatenateBack(
String cacheName, String listName, List<Value> values,
{CollectionTtl? ttl, int? truncateFrontToSize});

Future<ListConcatenateFrontResponse> listConcatenateFront(
String cacheName, String listName, List<Value> values,
{CollectionTtl? ttl, int? truncateBackToSize});

Future<ListFetchResponse> listFetch(String cacheName, String listName,
{int? startIndex, int? endIndex});
Future<ListLengthResponse> listLength(String cacheName, String listName);
Future<ListPopBackResponse> listPopBack(String cacheName, String listName);
Future<ListPopFrontResponse> listPopFront(String cacheName, String listName);

Future<ListPushBackResponse> listPushBack(
String cacheName, String listName, Value value,
{CollectionTtl? ttl, int? truncateFrontToSize});

Future<ListPushFrontResponse> listPushFront(
String cacheName, String listName, Value value,
{CollectionTtl? ttl, int? truncateBackToSize});

Future<ListRemoveValueResponse> listRemoveValue(
String cacheName, String listName, Value value);

Future<ListRetainResponse> listRetain(String cacheName, String listName,
{int? startIndex, int? endIndex, CollectionTtl? ttl});
Future<void> close();
}

Expand Down Expand Up @@ -201,6 +231,185 @@ class CacheClient implements ICacheClient {
/// Close the client and free up all associated resources.
///
/// NOTE: the client object will not be usable after calling this method.
@override
Future<ListConcatenateBackResponse> listConcatenateBack(
String cacheName, String listName, List<Value> values,
{CollectionTtl? ttl, int? truncateFrontToSize}) {
try {
validateCacheName(cacheName);
validateListName(listName);
validateList(values);
return _dataClient.listConcatenateBack(cacheName, listName, values,
ttl: ttl, truncateFrontToSize: truncateFrontToSize);
} catch (e) {
if (e is SdkException) {
return Future.value(ListConcatenateBackError(e));
} else {
return Future.value(ListConcatenateBackError(
UnknownException("Unexpected error: $e", null, null)));
}
}
}

@override
Future<ListConcatenateFrontResponse> listConcatenateFront(
String cacheName, String listName, List<Value> values,
{CollectionTtl? ttl, int? truncateBackToSize}) {
try {
validateCacheName(cacheName);
validateListName(listName);
validateList(values);
return _dataClient.listConcatenateFront(cacheName, listName, values,
ttl: ttl, truncateBackToSize: truncateBackToSize);
} catch (e) {
if (e is SdkException) {
return Future.value(ListConcatenateFrontError(e));
} else {
return Future.value(ListConcatenateFrontError(
UnknownException("Unexpected error: $e", null, null)));
}
}
}

@override
Future<ListFetchResponse> listFetch(String cacheName, String listName,
{int? startIndex, int? endIndex}) {
try {
validateCacheName(cacheName);
validateListName(listName);
return _dataClient.listFetch(cacheName, listName,
startIndex: startIndex, endIndex: endIndex);
} catch (e) {
if (e is SdkException) {
return Future.value(ListFetchError(e));
} else {
return Future.value(ListFetchError(
UnknownException("Unexpected error: $e", null, null)));
}
}
}

@override
Future<ListLengthResponse> listLength(String cacheName, String listName) {
try {
validateCacheName(cacheName);
validateListName(listName);
return _dataClient.listLength(cacheName, listName);
} catch (e) {
if (e is SdkException) {
return Future.value(ListLengthError(e));
} else {
return Future.value(ListLengthError(
UnknownException("Unexpected error: $e", null, null)));
}
}
}

@override
Future<ListPopBackResponse> listPopBack(String cacheName, String listName) {
try {
validateCacheName(cacheName);
validateListName(listName);
return _dataClient.listPopBack(cacheName, listName);
} catch (e) {
if (e is SdkException) {
return Future.value(ListPopBackError(e));
} else {
return Future.value(ListPopBackError(
UnknownException("Unexpected error: $e", null, null)));
}
}
}

@override
Future<ListPopFrontResponse> listPopFront(String cacheName, String listName) {
try {
validateCacheName(cacheName);
validateListName(listName);
return _dataClient.listPopFront(cacheName, listName);
} catch (e) {
if (e is SdkException) {
return Future.value(ListPopFrontError(e));
} else {
return Future.value(ListPopFrontError(
UnknownException("Unexpected error: $e", null, null)));
}
}
}

@override
Future<ListPushBackResponse> listPushBack(
String cacheName, String listName, Value value,
{CollectionTtl? ttl, int? truncateFrontToSize}) {
try {
validateCacheName(cacheName);
validateListName(listName);
return _dataClient.listPushBack(cacheName, listName, value,
ttl: ttl, truncateFrontToSize: truncateFrontToSize);
} catch (e) {
if (e is SdkException) {
return Future.value(ListPushBackError(e));
} else {
return Future.value(ListPushBackError(
UnknownException("Unexpected error: $e", null, null)));
}
}
}

@override
Future<ListPushFrontResponse> listPushFront(
String cacheName, String listName, Value value,
{CollectionTtl? ttl, int? truncateBackToSize}) {
try {
validateCacheName(cacheName);
validateListName(listName);
return _dataClient.listPushFront(cacheName, listName, value,
ttl: ttl, truncateBackToSize: truncateBackToSize);
} catch (e) {
if (e is SdkException) {
return Future.value(ListPushFrontError(e));
} else {
return Future.value(ListPushFrontError(
UnknownException("Unexpected error: $e", null, null)));
}
}
}

@override
Future<ListRemoveValueResponse> listRemoveValue(
String cacheName, String listName, Value value) {
try {
validateCacheName(cacheName);
validateListName(listName);
return _dataClient.listRemoveValue(cacheName, listName, value);
} catch (e) {
if (e is SdkException) {
return Future.value(ListRemoveValueError(e));
} else {
return Future.value(ListRemoveValueError(
UnknownException("Unexpected error: $e", null, null)));
}
}
}

@override
Future<ListRetainResponse> listRetain(String cacheName, String listName,
{int? startIndex, int? endIndex, CollectionTtl? ttl}) {
try {
validateCacheName(cacheName);
validateListName(listName);
return _dataClient.listRetain(cacheName, listName,
startIndex: startIndex, endIndex: endIndex, ttl: ttl);
} catch (e) {
if (e is SdkException) {
return Future.value(ListRetainError(e));
} else {
return Future.value(ListRetainError(
UnknownException("Unexpected error: $e", null, null)));
}
}
}

@override
Future<void> close() async {
await _dataClient.close();
Expand Down

0 comments on commit 566b3fe

Please sign in to comment.