Merge "Schedule delete for files as soon as they are written" into rvc-dev

This commit is contained in:
Julia Reynolds
2020-03-03 17:36:21 +00:00
committed by Android (Google) Code Review
2 changed files with 45 additions and 8 deletions

View File

@@ -266,13 +266,17 @@ public class NotificationHistoryDatabase {
mHistoryFiles.removeLast();
} else {
// all remaining files are newer than the cut off; schedule jobs to delete
final long deletionTime = creationTime + (retentionDays * HISTORY_RETENTION_MS);
scheduleDeletion(currentOldestFile.getBaseFile(), deletionTime);
scheduleDeletion(currentOldestFile.getBaseFile(), creationTime, retentionDays);
}
}
}
}
private void scheduleDeletion(File file, long creationTime, int retentionDays) {
final long deletionTime = creationTime + (retentionDays * HISTORY_RETENTION_MS);
scheduleDeletion(file, deletionTime);
}
private void scheduleDeletion(File file, long deletionTime) {
if (DEBUG) {
Slog.d(TAG, "Scheduling deletion for " + file.getName() + " at " + deletionTime);
@@ -330,17 +334,28 @@ public class NotificationHistoryDatabase {
}
};
private final class WriteBufferRunnable implements Runnable {
final class WriteBufferRunnable implements Runnable {
long currentTime = 0;
AtomicFile latestNotificationsFile;
@Override
public void run() {
if (DEBUG) Slog.d(TAG, "WriteBufferRunnable");
synchronized (mLock) {
final AtomicFile latestNotificationsFiles = new AtomicFile(
new File(mHistoryDir, String.valueOf(System.currentTimeMillis())));
if (currentTime == 0) {
currentTime = System.currentTimeMillis();
}
if (latestNotificationsFile == null) {
latestNotificationsFile = new AtomicFile(
new File(mHistoryDir, String.valueOf(currentTime)));
}
try {
writeLocked(latestNotificationsFiles, mBuffer);
mHistoryFiles.addFirst(latestNotificationsFiles);
writeLocked(latestNotificationsFile, mBuffer);
mHistoryFiles.addFirst(latestNotificationsFile);
mBuffer = new NotificationHistory();
scheduleDeletion(latestNotificationsFile.getBaseFile(), currentTime,
HISTORY_RETENTION_DAYS);
} catch (IOException e) {
Slog.e(TAG, "Failed to write buffer to disk. not flushing buffer", e);
}
@@ -440,7 +455,7 @@ public class NotificationHistoryDatabase {
@Override
public void run() {
if (DEBUG) Slog.d(TAG, "RemoveConversationRunnable");
if (DEBUG) Slog.d(TAG, "RemoveConversationRunnable " + mPkg + " " + mConversationId);
synchronized (mLock) {
// Remove from pending history
mBuffer.removeConversationFromWrite(mPkg, mConversationId);

View File

@@ -328,4 +328,26 @@ public class NotificationHistoryDatabaseTest extends UiServiceTestCase {
verify(nh).removeConversationFromWrite("pkg", "convo");
verify(af, never()).startWrite();
}
@Test
public void testWriteBufferRunnable() throws Exception {
NotificationHistory nh = mock(NotificationHistory.class);
when(nh.getPooledStringsToWrite()).thenReturn(new String[]{});
when(nh.getNotificationsToWrite()).thenReturn(new ArrayList<>());
NotificationHistoryDatabase.WriteBufferRunnable wbr =
mDataBase.new WriteBufferRunnable();
mDataBase.mBuffer = nh;
wbr.currentTime = 5;
wbr.latestNotificationsFile = mock(AtomicFile.class);
File file = mock(File.class);
when(file.getName()).thenReturn("5");
when(wbr.latestNotificationsFile.getBaseFile()).thenReturn(file);
wbr.run();
assertThat(mDataBase.mHistoryFiles.size()).isEqualTo(1);
assertThat(mDataBase.mBuffer).isNotEqualTo(nh);
verify(mAlarmManager, times(1)).setExactAndAllowWhileIdle(anyInt(), anyLong(), any());
}
}