Skip to content

Commit f8e5bd1

Browse files
committed
change to tasks and base deletion on file index instead of name.
1 parent 4489050 commit f8e5bd1

File tree

3 files changed

+36
-16
lines changed

3 files changed

+36
-16
lines changed

firebase-ml-modeldownloader/src/main/java/com/google/firebase/ml/modeldownloader/internal/ModelFileDownloadService.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -409,11 +409,7 @@ public File loadNewlyDownloadedModelFile(CustomModel model) {
409409
new CustomModel(
410410
model.getName(), model.getModelHash(), model.getSize(), 0, newModelFile.getPath()));
411411

412-
try {
413-
maybeCleanUpOldModels();
414-
} catch (FirebaseMlException fex) {
415-
Log.d(TAG, "Failed to clean up old models.");
416-
}
412+
maybeCleanUpOldModels();
417413

418414
return newModelFile;
419415
} else if (statusCode == DownloadManager.STATUS_FAILED) {
@@ -428,17 +424,22 @@ public File loadNewlyDownloadedModelFile(CustomModel model) {
428424
return null;
429425
}
430426

431-
private void maybeCleanUpOldModels() throws FirebaseMlException {
427+
private Task<Void> maybeCleanUpOldModels() {
432428
if (!isInitialLoad) {
433-
return;
429+
return Tasks.forResult(null);
434430
}
435431

436-
// only do once per initialization
432+
// only do once per initialization.
437433
isInitialLoad = false;
438434

439435
// for each custom model directory, find out the latest model and delete the other files.
440-
// If no corresponding model, clean up the full direcorty.
441-
fileManager.deleteNonLatestCustomModels();
436+
// If no corresponding model, clean up the full directory.
437+
try {
438+
fileManager.deleteNonLatestCustomModels();
439+
} catch (FirebaseMlException fex) {
440+
Log.d(TAG, "Failed to clean up old models.");
441+
}
442+
return Tasks.forResult(null);
442443
}
443444

444445
private FirebaseMlException getExceptionAccordingToDownloadManager(Long downloadId) {

firebase-ml-modeldownloader/src/main/java/com/google/firebase/ml/modeldownloader/internal/ModelFileManager.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,23 +204,30 @@ public synchronized File moveModelToDestinationFolder(
204204
* @param latestModelFilePath The file path to the latest custom model.
205205
*/
206206
@WorkerThread
207-
public synchronized boolean deleteOldModels(
207+
public synchronized void deleteOldModels(
208208
@NonNull String modelName, @NonNull String latestModelFilePath) {
209209
File modelFolder = getModelDirUnsafe(modelName);
210210
if (!modelFolder.exists()) {
211-
return false;
211+
return;
212212
}
213213

214+
File latestFile = new File(latestModelFilePath);
215+
int latestIndex = Integer.parseInt(latestFile.getName());
214216
File[] modelFiles = modelFolder.listFiles();
215217

216218
boolean isAllDeleted = true;
219+
int fileInt;
217220
for (File modelFile : modelFiles) {
218-
if (!modelFile.getPath().equals(latestModelFilePath) && !deleteRecursively(modelFile)) {
219-
isAllDeleted = false;
221+
try {
222+
fileInt = Integer.parseInt(modelFile.getName());
223+
} catch (NumberFormatException ex) {
224+
// unexpected file - ignore
225+
fileInt = Integer.MAX_VALUE;
226+
}
227+
if (fileInt < latestIndex) {
228+
isAllDeleted = isAllDeleted && modelFile.delete();
220229
}
221230
}
222-
// Only return true if all old models are deleted.
223-
return isAllDeleted;
224231
}
225232

226233
/**

firebase-ml-modeldownloader/src/test/java/com/google/firebase/ml/modeldownloader/internal/ModelFileManagerTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,18 @@ public void deleteOldModels_deleteModel() throws FirebaseMlException, FileNotFou
238238
assertTrue(new File(modelDestinationFolder + "/1").exists());
239239
}
240240

241+
@Test
242+
public void deleteOldModels_deleteModel_keepNewer()
243+
throws FirebaseMlException, FileNotFoundException {
244+
MoveFileToDestination(modelDestinationFolder, testModelFile, CUSTOM_MODEL_NO_FILE, 0);
245+
MoveFileToDestination(modelDestinationFolder, testModelFile2, CUSTOM_MODEL_NO_FILE, 1);
246+
247+
fileManager.deleteOldModels(MODEL_NAME, modelDestinationFolder + "/0");
248+
249+
assertTrue(new File(modelDestinationFolder + "/0").exists());
250+
assertTrue(new File(modelDestinationFolder + "/1").exists());
251+
}
252+
241253
@Test
242254
public void deleteOldModels_noModelsToDelete() throws FirebaseMlException, FileNotFoundException {
243255
MoveFileToDestination(modelDestinationFolder, testModelFile, CUSTOM_MODEL_NO_FILE, 0);

0 commit comments

Comments
 (0)