Skip to content

Commit 684ca0a

Browse files
authored
Merge pull request #18 from nubank/implementing-cycling-of-events
Instead of discar new events, remove the older ones
2 parents 778aa4c + 6fb3c34 commit 684ca0a

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

lib/src/event_buffer.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ class EventBuffer {
3434
/// Adds a raw event hash to the buffer
3535
Future<void> add(Event event) async {
3636
if (length >= config.maxStoredEvents) {
37-
print('Max stored events reached. Discarding event.');
38-
return;
37+
print('Max stored events reached. Drop first event');
38+
await store.drop(1);
3939
}
4040

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

44-
if (length >= config.bufferSize && numEvents == null) {
44+
if (length >= config.bufferSize) {
4545
await flush();
4646
}
4747
}
@@ -92,6 +92,8 @@ class EventBuffer {
9292

9393
Future<void> _deleteEvents(List<int> eventIds) async {
9494
await store.delete(eventIds);
95-
numEvents = null;
95+
if (numEvents >= length) {
96+
numEvents = null;
97+
}
9698
}
9799
}

lib/src/store.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ class Store {
5050
return _count(db);
5151
}
5252

53+
Future<void> drop(int count) async {
54+
final db = await _getDb();
55+
if (db == null) {
56+
return;
57+
}
58+
final resultCount = await db.rawDelete(
59+
'DELETE FROM $EVENTS_TABLE WHERE $COL_ID IN (SELECT T2.$COL_ID FROM $EVENTS_TABLE T2 ORDER BY T2.$COL_ID LIMIT $count)');
60+
length -= resultCount;
61+
}
62+
5363
Future<void> delete(List<int> eventIds) async {
5464
final db = await _getDb();
5565
if (db == null) {

0 commit comments

Comments
 (0)