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(logging): enable queue rotation #3656

Merged
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
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