Skip to content

Commit

Permalink
feat(logging): enable queue rotation (#3656)
Browse files Browse the repository at this point in the history
  • Loading branch information
NikaHsn authored and khatruong2009 committed Nov 27, 2023
1 parent e5ab84d commit a4c05e5
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ class DartQueuedItemStore implements QueuedItemStore, Closeable {
}

@override
FutureOr<void> addItem(String string, String timestamp) {
FutureOr<void> addItem(
String string,
String timestamp, {
bool enableQueueRotation = false,
}) {
throw UnimplementedError('addItem() has not been implemented.');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,16 @@ class DartQueuedItemStore implements QueuedItemStore, Closeable {
final DriftQueuedItemStore _database;

@override
Future<void> addItem(String string, String timestamp) {
return _database.addItem(string, timestamp);
Future<void> addItem(
String string,
String timestamp, {
bool enableQueueRotation = false,
}) async {
return _database.addItem(
string,
timestamp,
enableQueueRotation: enableQueueRotation,
);
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,17 @@ class DartQueuedItemStore
String get runtimeTypeName => 'DartQueuedItemStore';

@override
Future<void> addItem(String string, String timestamp) async {
Future<void> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,15 @@ class DriftQueuedItemStore extends _$DriftQueuedItemStore
}

@override
Future<void> addItem(String value, String timestamp) async {
Future<void> 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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,15 @@ class IndexedDbAdapter implements QueuedItemStore {
}

@override
Future<void> addItem(String string, String timestamp) async {
Future<void> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,16 @@ class InMemoryQueuedItemStore implements QueuedItemStore {
LinkedHashMap<int, QueuedItem>();

@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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> 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<void> addItem(
String string,
String timestamp, {
bool enableQueueRotation = false,
});

/// Get the first [count] items from the queue.
FutureOr<Iterable<QueuedItem>> getCount(int count);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit a4c05e5

Please sign in to comment.