Skip to content

Commit

Permalink
Merge pull request #18 from nubank/implementing-cycling-of-events
Browse files Browse the repository at this point in the history
Instead of discar new events, remove the older ones
  • Loading branch information
luisrjaeger authored Jul 1, 2024
2 parents 778aa4c + 6fb3c34 commit 684ca0a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/src/event_buffer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ class EventBuffer {
/// Adds a raw event hash to the buffer
Future<void> add(Event event) async {
if (length >= config.maxStoredEvents) {
print('Max stored events reached. Discarding event.');
return;
print('Max stored events reached. Drop first event');
await store.drop(1);
}

event.timestamp = TimeUtils().currentTime();
await store.add(event);

if (length >= config.bufferSize && numEvents == null) {
if (length >= config.bufferSize) {
await flush();
}
}
Expand Down Expand Up @@ -92,6 +92,8 @@ class EventBuffer {

Future<void> _deleteEvents(List<int> eventIds) async {
await store.delete(eventIds);
numEvents = null;
if (numEvents >= length) {
numEvents = null;
}
}
}
10 changes: 10 additions & 0 deletions lib/src/store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ class Store {
return _count(db);
}

Future<void> drop(int count) async {
final db = await _getDb();
if (db == null) {
return;
}
final resultCount = await db.rawDelete(
'DELETE FROM $EVENTS_TABLE WHERE $COL_ID IN (SELECT T2.$COL_ID FROM $EVENTS_TABLE T2 ORDER BY T2.$COL_ID LIMIT $count)');
length -= resultCount;
}

Future<void> delete(List<int> eventIds) async {
final db = await _getDb();
if (db == null) {
Expand Down

0 comments on commit 684ca0a

Please sign in to comment.