Skip to content

Commit 096311f

Browse files
committed
Data storage: On idle, schedule a timer to flush to disk after a few mins.
This is to handle the case when collection stops and wifi not available for uploading.
1 parent 0ffe83e commit 096311f

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

Diff for: src/org/mozilla/mozstumbler/service/datahandling/DataStorageManager.java

+24-1
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
import java.util.HashMap;
2020
import java.util.Map;
2121
import java.util.Properties;
22+
import java.util.Timer;
23+
import java.util.TimerTask;
2224

2325
/* Stores reports in memory (mCurrentReports) until MAX_REPORTS_IN_MEMORY,
2426
* then writes them to disk as a .gz file. The name of the file has
2527
* the time written, the # of reports, and the # of cells and wifis.
2628
*
27-
* Each .gz file is typically 1-5KB.
29+
* Each .gz file is typically 1-5KB. File name example: reports-t1406863343313-r4-w25-c7.gz
2830
*
2931
* The sync stats are written as a key-value pair file (not zipped).
3032
*
@@ -51,6 +53,7 @@ public class DataStorageManager {
5153
private ReportBatchIterator mReportBatchIterator;
5254
private StorageIsEmptyTracker mTracker;
5355
private ReportFileList mFileList;
56+
private Timer mFlushMemoryBuffersToDiskTimer;
5457

5558
final static String SEP_REPORT_COUNT = "-r";
5659
final static String SEP_WIFI_COUNT = "-w";
@@ -417,13 +420,33 @@ public synchronized void saveCurrentReportsToDisk() throws IOException {
417420
public synchronized void insert(String report, int wifiCount, int cellCount) throws IOException {
418421
notifyStorageIsEmpty(false);
419422

423+
if (mFlushMemoryBuffersToDiskTimer != null) {
424+
mFlushMemoryBuffersToDiskTimer.cancel();
425+
mFlushMemoryBuffersToDiskTimer = null;
426+
}
427+
420428
mCurrentReports.reports.add(report);
421429
mCurrentReports.wifiCount = wifiCount;
422430
mCurrentReports.cellCount = cellCount;
423431

424432
if (mCurrentReports.reports.size() >= MAX_REPORTS_IN_MEMORY) {
425433
// save to disk
426434
saveCurrentReportsToDisk();
435+
} else {
436+
// Schedule a timer to flush to disk after a few mins.
437+
// If collection stops and wifi not available for uploading, the memory buffer is flushed to disk.
438+
final int kMillis = 1000 * 60 * 3;
439+
mFlushMemoryBuffersToDiskTimer = new Timer();
440+
mFlushMemoryBuffersToDiskTimer.schedule(new TimerTask() {
441+
@Override
442+
public void run() {
443+
try {
444+
saveCurrentReportsToDisk();
445+
} catch (IOException ex) {
446+
Log.e(LOG_TAG, "mFlushMemoryBuffersToDiskTimer exception", ex);
447+
}
448+
}
449+
}, kMillis);
427450
}
428451
}
429452

0 commit comments

Comments
 (0)