diff --git a/packages/logging_cloudwatch/amplify_logging_cloudwatch/lib/src/queued_item_store/dart_queued_item_store.stub.dart b/packages/logging_cloudwatch/amplify_logging_cloudwatch/lib/src/queued_item_store/dart_queued_item_store.stub.dart index 659be2c439..74f5ac28b9 100644 --- a/packages/logging_cloudwatch/amplify_logging_cloudwatch/lib/src/queued_item_store/dart_queued_item_store.stub.dart +++ b/packages/logging_cloudwatch/amplify_logging_cloudwatch/lib/src/queued_item_store/dart_queued_item_store.stub.dart @@ -23,7 +23,11 @@ class DartQueuedItemStore implements QueuedItemStore, Closeable { } @override - FutureOr addItem(String string, String timestamp) { + FutureOr addItem( + String string, + String timestamp, { + bool enableQueueRotation = false, + }) { throw UnimplementedError('addItem() has not been implemented.'); } diff --git a/packages/logging_cloudwatch/amplify_logging_cloudwatch/lib/src/queued_item_store/dart_queued_item_store.vm.dart b/packages/logging_cloudwatch/amplify_logging_cloudwatch/lib/src/queued_item_store/dart_queued_item_store.vm.dart index 17de9554c1..1cd2e348e4 100644 --- a/packages/logging_cloudwatch/amplify_logging_cloudwatch/lib/src/queued_item_store/dart_queued_item_store.vm.dart +++ b/packages/logging_cloudwatch/amplify_logging_cloudwatch/lib/src/queued_item_store/dart_queued_item_store.vm.dart @@ -25,8 +25,16 @@ class DartQueuedItemStore implements QueuedItemStore, Closeable { final DriftQueuedItemStore _database; @override - Future addItem(String string, String timestamp) { - return _database.addItem(string, timestamp); + Future addItem( + String string, + String timestamp, { + bool enableQueueRotation = false, + }) async { + return _database.addItem( + string, + timestamp, + enableQueueRotation: enableQueueRotation, + ); } @override diff --git a/packages/logging_cloudwatch/amplify_logging_cloudwatch/lib/src/queued_item_store/dart_queued_item_store.web.dart b/packages/logging_cloudwatch/amplify_logging_cloudwatch/lib/src/queued_item_store/dart_queued_item_store.web.dart index 7f8b79f397..0dc36a615c 100644 --- a/packages/logging_cloudwatch/amplify_logging_cloudwatch/lib/src/queued_item_store/dart_queued_item_store.web.dart +++ b/packages/logging_cloudwatch/amplify_logging_cloudwatch/lib/src/queued_item_store/dart_queued_item_store.web.dart @@ -29,9 +29,17 @@ class DartQueuedItemStore String get runtimeTypeName => 'DartQueuedItemStore'; @override - Future addItem(String string, String timestamp) async { + Future addItem( + String string, + String timestamp, { + bool enableQueueRotation = false, + }) async { final db = await _database; - await db.addItem(string, timestamp); + await db.addItem( + string, + timestamp, + enableQueueRotation: enableQueueRotation, + ); } @override diff --git a/packages/logging_cloudwatch/amplify_logging_cloudwatch/lib/src/queued_item_store/drift/drift_queued_item_store.dart b/packages/logging_cloudwatch/amplify_logging_cloudwatch/lib/src/queued_item_store/drift/drift_queued_item_store.dart index c4e35c683a..b2f7406a0f 100644 --- a/packages/logging_cloudwatch/amplify_logging_cloudwatch/lib/src/queued_item_store/drift/drift_queued_item_store.dart +++ b/packages/logging_cloudwatch/amplify_logging_cloudwatch/lib/src/queued_item_store/drift/drift_queued_item_store.dart @@ -57,7 +57,15 @@ class DriftQueuedItemStore extends _$DriftQueuedItemStore } @override - Future addItem(String value, String timestamp) async { + Future addItem( + String value, + String timestamp, { + bool enableQueueRotation = false, + }) async { + if (enableQueueRotation) { + final toDelete = await getCount(1); + await deleteItems(toDelete); + } await into(driftQueuedItems).insert( DriftQueuedItemsCompanion( value: Value(value), diff --git a/packages/logging_cloudwatch/amplify_logging_cloudwatch/lib/src/queued_item_store/index_db/indexed_db_adapter.dart b/packages/logging_cloudwatch/amplify_logging_cloudwatch/lib/src/queued_item_store/index_db/indexed_db_adapter.dart index 19da0f00a1..37c1812e78 100644 --- a/packages/logging_cloudwatch/amplify_logging_cloudwatch/lib/src/queued_item_store/index_db/indexed_db_adapter.dart +++ b/packages/logging_cloudwatch/amplify_logging_cloudwatch/lib/src/queued_item_store/index_db/indexed_db_adapter.dart @@ -67,7 +67,15 @@ class IndexedDbAdapter implements QueuedItemStore { } @override - Future addItem(String string, String timestamp) async { + Future addItem( + String string, + String timestamp, { + bool enableQueueRotation = false, + }) async { + if (enableQueueRotation) { + final toDelete = await getCount(1); + await deleteItems(toDelete); + } await _databaseOpenEvent; await _getObjectStore() .push({'value': string, 'timestamp': timestamp}).future; diff --git a/packages/logging_cloudwatch/amplify_logging_cloudwatch/test/queued_item_store_test.dart b/packages/logging_cloudwatch/amplify_logging_cloudwatch/test/queued_item_store_test.dart index 8b80fc23ae..4c18fc12d8 100644 --- a/packages/logging_cloudwatch/amplify_logging_cloudwatch/test/queued_item_store_test.dart +++ b/packages/logging_cloudwatch/amplify_logging_cloudwatch/test/queued_item_store_test.dart @@ -36,6 +36,21 @@ void main() { expect(readValues, equals(values)); }); + test('writes values to storage with enable queue rotation', () async { + const values = ['0', '1', '2', '3', '4', '5']; + for (final value in values) { + await db.addItem( + value, + DateTime.now().toIso8601String(), + enableQueueRotation: true, + ); + } + + final readItems = await getAll(); + expect(readItems.length, 1); + expect(readItems.first.value, values.last); + }); + test('returns first n items in storage', () async { const values = ['0', '1', '2', '3', '4', '5']; for (final value in values) { diff --git a/packages/logging_cloudwatch/aws_logging_cloudwatch/lib/src/queued_item_store/in_memory_queued_item_store.dart b/packages/logging_cloudwatch/aws_logging_cloudwatch/lib/src/queued_item_store/in_memory_queued_item_store.dart index e07cc6f617..252956a969 100644 --- a/packages/logging_cloudwatch/aws_logging_cloudwatch/lib/src/queued_item_store/in_memory_queued_item_store.dart +++ b/packages/logging_cloudwatch/aws_logging_cloudwatch/lib/src/queued_item_store/in_memory_queued_item_store.dart @@ -19,7 +19,16 @@ class InMemoryQueuedItemStore implements QueuedItemStore { LinkedHashMap(); @override - void addItem(String string, String timestamp) { + void addItem( + String string, + String timestamp, { + bool enableQueueRotation = false, + }) { + if (enableQueueRotation) { + final toDelete = _database.values.take(1); + deleteItems(toDelete); + } + final queuedItem = QueuedItem( id: _nextId, value: string, diff --git a/packages/logging_cloudwatch/aws_logging_cloudwatch/lib/src/queued_item_store/queued_item_store.dart b/packages/logging_cloudwatch/aws_logging_cloudwatch/lib/src/queued_item_store/queued_item_store.dart index 128790c886..24e197d449 100644 --- a/packages/logging_cloudwatch/aws_logging_cloudwatch/lib/src/queued_item_store/queued_item_store.dart +++ b/packages/logging_cloudwatch/aws_logging_cloudwatch/lib/src/queued_item_store/queued_item_store.dart @@ -6,7 +6,13 @@ import 'dart:async'; /// Database for storing strings. abstract interface class QueuedItemStore { /// Insert an item to the end of the queue. - FutureOr addItem(String string, String timestamp); + /// If [enableQueueRotation] is `true` it removes the first item from the + /// queue and adds the new item to the end of the queue. + FutureOr addItem( + String string, + String timestamp, { + bool enableQueueRotation = false, + }); /// Get the first [count] items from the queue. FutureOr> getCount(int count); diff --git a/packages/logging_cloudwatch/aws_logging_cloudwatch/test/queued_item_store/queued_item_store_test.dart b/packages/logging_cloudwatch/aws_logging_cloudwatch/test/queued_item_store/queued_item_store_test.dart index 48e73ca64e..ec2bca8874 100644 --- a/packages/logging_cloudwatch/aws_logging_cloudwatch/test/queued_item_store/queued_item_store_test.dart +++ b/packages/logging_cloudwatch/aws_logging_cloudwatch/test/queued_item_store/queued_item_store_test.dart @@ -36,6 +36,21 @@ void main() { expect(readValues, equals(values)); }); + test('writes values to storage with queue rotation enabled', () async { + const values = ['0', '1', '2', '3', '4', '5']; + for (final value in values) { + await db.addItem( + value, + DateTime.now().toIso8601String(), + enableQueueRotation: true, + ); + } + + final readItems = await db.getAll(); + expect(readItems.length, 1); + expect(readItems.first.value, values.last); + }); + test('returns first n items in storage', () async { const values = ['0', '1', '2', '3', '4', '5']; for (final value in values) {