maintenance: Add --purge-package to delete rows/files by package name.

Used by the performance tests (AppLaunch.apk) to purge files for
iorap-trial runs.

Test: iorap.cmd.maintenance --package-name com.whatever.name /data/misc/iorapd/sqlite.db
Bug: 150880186
Change-Id: Ifdb0db2b0fcd66eba9e65e94b856929a882e8a75
diff --git a/src/db/clean_up.cc b/src/db/clean_up.cc
index b3eba46..49f1657 100644
--- a/src/db/clean_up.cc
+++ b/src/db/clean_up.cc
@@ -40,6 +40,7 @@
     std::string file_path = raw_trace.file_path;
     LOG(DEBUG) << "Remove file: " << file_path;
     std::filesystem::remove(file_path.c_str());
+    raw_trace.Delete();
   }
 
   // Remove compiled traces.
@@ -50,6 +51,7 @@
     std::string file_path = prefetch_file->file_path;
     LOG(DEBUG) << "Remove file: " << file_path;
     std::filesystem::remove(file_path.c_str());
+    prefetch_file->Delete();
   }
 }
 
@@ -75,4 +77,25 @@
   }
 }
 
+void CleanUpFilesForPackage(const std::string& db_path,
+                            const std::string& package_name) {
+  iorap::db::SchemaModel db_schema = db::SchemaModel::GetOrCreate(db_path);
+  db::DbHandle db{db_schema.db()};
+  CleanUpFilesForPackage(db, package_name);
+}
+
+
+void CleanUpFilesForPackage(const db::DbHandle& db,
+                            const std::string& package_name) {
+  std::vector<PackageModel> packages = PackageModel::SelectByName(db, package_name.c_str());;
+
+  for (PackageModel& package : packages) {
+    CleanUpFilesForPackage(db, package.id, package.name, package.version);
+  }
+
+  if (packages.empty()) {
+    LOG(DEBUG) << "No package rows to clean up " << package_name;
+  }
+}
+
 }  // namespace iorap::db
diff --git a/src/db/clean_up.h b/src/db/clean_up.h
index 82a26fd..93ef9c4 100644
--- a/src/db/clean_up.h
+++ b/src/db/clean_up.h
@@ -32,6 +32,14 @@
                             int package_id,
                             const std::string& package_name,
                             int64_t version);
+
+// Clean up all package rows (and files) associated with a package by name.
+void CleanUpFilesForPackage(const std::string& db_path,
+                            const std::string& package_name);
+
+// Clean up all package rows (and files) associated with a package by name.
+void CleanUpFilesForPackage(const db::DbHandle& db,
+                            const std::string& package_name);
 }  // namespace iorap::db
 
 #endif  // IORAP_SRC_DB_CLEANER_H_
diff --git a/src/db/models.h b/src/db/models.h
index 1feac47..df6beb6 100644
--- a/src/db/models.h
+++ b/src/db/models.h
@@ -585,18 +585,20 @@
     return p;
   }
 
-  static std::optional<PackageModel> SelectByName(DbHandle db, const char* name) {
+  static std::vector<PackageModel> SelectByName(DbHandle db, const char* name) {
     ScopedLockDb lock{db};
 
-    std::string query = "SELECT * FROM packages WHERE name = ?1 LIMIT 1;";
+    std::string query = "SELECT * FROM packages WHERE name = ?1;";
     DbStatement stmt = DbStatement::Prepare(db, query, name);
 
+    std::vector<PackageModel> packages;
+
     PackageModel p{db};
-    if (!DbQueryBuilder::SelectOnce(stmt, p.id, p.name, p.version)) {
-      return std::nullopt;
+    while (DbQueryBuilder::SelectOnce(stmt, p.id, p.name, p.version)) {
+      packages.push_back(p);
     }
 
-    return p;
+    return packages;
   }
 
   static std::optional<PackageModel> SelectByNameAndVersion(DbHandle db,