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: add data client support for list api calls #64

Merged
merged 19 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
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.
211 changes: 209 additions & 2 deletions lib/src/cache_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,50 @@ import 'package:momento/src/internal/utils/validators.dart';

import 'config/logger.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 @@ -120,6 +148,185 @@ class CacheClient implements ICacheClient {
return _dataClient.delete(cacheName, key);
}

@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
Loading