files deduped in only 2 sql queries - no roundtrips

business logic encapsulated outside of swingworker
This commit is contained in:
Brian Sweeney 2018-03-19 14:00:09 -06:00
parent babf376d8b
commit cc355d99b6
2 changed files with 63 additions and 40 deletions

View File

@ -19,6 +19,7 @@
*/ */
package org.sleuthkit.autopsy.commonfilesearch; package org.sleuthkit.autopsy.commonfilesearch;
import java.sql.ResultSet;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
@ -27,8 +28,10 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.logging.Level; import java.util.logging.Level;
import org.sleuthkit.autopsy.casemodule.Case;
import org.sleuthkit.autopsy.coreutils.Logger; import org.sleuthkit.autopsy.coreutils.Logger;
import org.sleuthkit.datamodel.AbstractFile; import org.sleuthkit.datamodel.AbstractFile;
import org.sleuthkit.datamodel.SleuthkitCase;
import org.sleuthkit.datamodel.TskCoreException; import org.sleuthkit.datamodel.TskCoreException;
/** /**
@ -40,13 +43,41 @@ public class CommonFilesMetaData {
private final List<AbstractFile> dedupedFiles; private final List<AbstractFile> dedupedFiles;
private final Map<String, Integer> instanceCountMap; private final Map<String, Integer> instanceCountMap;
private final Map<Long, String> dataSourceMap; private final Map<Long, String> dataSourceIdToNameMap;
private final Map<String, String> dataSourcesMap; private final Map<String, String> md5ToDataSourcesStringMap;
private SleuthkitCase sleuthkitCase;
CommonFilesMetaData() {
dedupedFiles = new ArrayList<>();
instanceCountMap = new HashMap<>();
md5ToDataSourcesStringMap = new HashMap<>();
dataSourceIdToNameMap = new HashMap<>();
this.sleuthkitCase = Case.getOpenCase().getSleuthkitCase();
this.loadDataSourcesMap();
this.collateFiles();
}
private void loadDataSourcesMap(){
SleuthkitCase.CaseDbQuery query = this.sleuthkitCase.executeQuery("select obj_id, name from tsk_files where obj_id in (SELECT obj_id FROM tsk_objects WHERE obj_id in (select obj_id from data_source_info))");
ResultSet resultSet = query.getResultSet();
while(resultSet.next()){
Long objectId = resultSet.getLong(1);
String dataSourceName = resultSet.getString(2);
this.dataSourceIdToNameMap.put(objectId, dataSourceName);
}
}
CommonFilesMetaData(List<AbstractFile> theDedupedFiles, Map<String, String> theDataSourceMap, Map<String, Integer> theInstanceCountMap) { CommonFilesMetaData(List<AbstractFile> theDedupedFiles, Map<String, String> theDataSourceMap, Map<String, Integer> theInstanceCountMap) {
dedupedFiles = theDedupedFiles; dedupedFiles = theDedupedFiles;
instanceCountMap = theInstanceCountMap; instanceCountMap = theInstanceCountMap;
dataSourcesMap = theDataSourceMap; md5ToDataSourcesStringMap = theDataSourceMap;
dataSourceIdToNameMap = new HashMap<>();
} }
public List<AbstractFile> getFilesList() { public List<AbstractFile> getFilesList() {
@ -58,7 +89,7 @@ public class CommonFilesMetaData {
} }
public Map<String, String> getDataSourceMap() { public Map<String, String> getDataSourceMap() {
return Collections.unmodifiableMap(dataSourcesMap); return Collections.unmodifiableMap(md5ToDataSourcesStringMap);
} }
/** /**
@ -72,10 +103,9 @@ public class CommonFilesMetaData {
* @return object with deduped file list and maps of files to data sources * @return object with deduped file list and maps of files to data sources
* and number instances * and number instances
*/ */
static CommonFilesMetaData CollateFiles(List<AbstractFile> files) { private void collateFiles() {
List<AbstractFile> deDupedFiles = new ArrayList<>();
java.util.Map<String, String> dataSourceMap = new HashMap<>(); List<AbstractFile> files = this.sleuthkitCase.findAllFilesWhere("md5 in (select md5 from tsk_files where (known != 1 OR known IS NULL) GROUP BY md5 HAVING COUNT(*) > 1) order by md5");
java.util.Map<String, Integer> instanceCountMap = new HashMap<>();
AbstractFile previousFile = null; AbstractFile previousFile = null;
String previousMd5 = ""; String previousMd5 = "";
@ -91,9 +121,9 @@ public class CommonFilesMetaData {
addDataSource(dataSources, file); addDataSource(dataSources, file);
} else { } else {
if (previousFile != null) { if (previousFile != null) {
deDupedFiles.add(previousFile); this.dedupedFiles.add(previousFile);
instanceCountMap.put(previousMd5, instanceCount); this.instanceCountMap.put(previousMd5, instanceCount);
dataSourceMap.put(previousMd5, String.join(", ", dataSources)); this.md5ToDataSourcesStringMap.put(previousMd5, String.join(", ", dataSources));
} }
previousFile = file; previousFile = file;
previousMd5 = currentMd5; previousMd5 = currentMd5;
@ -102,16 +132,11 @@ public class CommonFilesMetaData {
addDataSource(dataSources, file); addDataSource(dataSources, file);
} }
} }
CommonFilesMetaData data = new CommonFilesMetaData(deDupedFiles, dataSourceMap, instanceCountMap);
return data;
} }
private static void addDataSource(Set<String> dataSources, AbstractFile file) { private void addDataSource(Set<String> dataSources, AbstractFile file) {
try { long datasourceId = file.getDataSourceObjectId();
dataSources.add(file.getDataSource().getName()); String dataSourceName = this.dataSourceIdToNameMap.get(datasourceId);
} catch (TskCoreException ex) { dataSources.add(dataSourceName);
LOGGER.log(Level.WARNING, String.format("Unable to determine data source for file with ID: {0}.", file.getId()), ex);
dataSources.add("Unknown Source");
}
} }
} }

View File

@ -75,11 +75,11 @@ public final class CommonFilesPanel extends javax.swing.JPanel {
String title = Bundle.CommonFilesPanel_search_results_title(); String title = Bundle.CommonFilesPanel_search_results_title();
String pathText = Bundle.CommonFilesPanel_search_results_pathText(); String pathText = Bundle.CommonFilesPanel_search_results_pathText();
new SwingWorker<List<AbstractFile>, Void>() { new SwingWorker<CommonFilesMetaData, Void>() {
@Override @Override
@SuppressWarnings("FinallyDiscardsException") @SuppressWarnings("FinallyDiscardsException")
protected List<AbstractFile> doInBackground() throws TskCoreException, NoCurrentCaseException, SQLException { //return type should be CommonFilesMetaData - done will be adjusted accordingly protected CommonFilesMetaData doInBackground() throws TskCoreException, NoCurrentCaseException, SQLException { //return type should be CommonFilesMetaData - done will be adjusted accordingly
//contents of this whole function should be wrapped in a business logic class for the sake of testing //contents of this whole function should be wrapped in a business logic class for the sake of testing
@ -93,22 +93,22 @@ public final class CommonFilesPanel extends javax.swing.JPanel {
Use file.getDataSourceID() to get datasourceid and map it to the appropriate row from above Use file.getDataSourceID() to get datasourceid and map it to the appropriate row from above
*/ */
Case currentCase = Case.getOpenCase(); // Case currentCase = Case.getOpenCase();
SleuthkitCase tskDb = currentCase.getSleuthkitCase(); // SleuthkitCase tskDb = currentCase.getSleuthkitCase();
//
// CaseDbQuery query = tskDb.executeQuery("select obj_id, name from tsk_files where obj_id in (SELECT obj_id FROM tsk_objects WHERE obj_id in (select obj_id from data_source_info))");
//
// ResultSet resultSet = query.getResultSet();
//
// Map<Long, String> dataSourceMap = new HashMap<>();
//
// while(resultSet.next()){
// Long objectId = resultSet.getLong(1);
// String dataSourceName = resultSet.getString(2);
// dataSourceMap.put(objectId, dataSourceName);
// }
CaseDbQuery query = tskDb.executeQuery("select obj_id, name from tsk_files where obj_id in (SELECT obj_id FROM tsk_objects WHERE obj_id in (select obj_id from data_source_info))"); return new CommonFilesMetaData();
ResultSet resultSet = query.getResultSet();
Map<Long, String> dataSourceMap = new HashMap<>();
while(resultSet.next()){
Long objectId = resultSet.getLong(0);
String dataSourceName = resultSet.getString(1);
dataSourceMap.put(objectId, dataSourceName);
}
return tskDb.findAllFilesWhere("md5 in (select md5 from tsk_files where (known != 1 OR known IS NULL) GROUP BY md5 HAVING COUNT(*) > 1) order by md5");
} }
@Override @Override
@ -116,9 +116,7 @@ public final class CommonFilesPanel extends javax.swing.JPanel {
try { try {
super.done(); super.done();
List<AbstractFile> contentList = get(); // CommonFilesMetaData metadata = get();
//// To background thread (doInBackground)
CommonFilesMetaData metadata = CommonFilesMetaData.CollateFiles(contentList); //
CommonFilesSearchNode contentFilesNode = new CommonFilesSearchNode(metadata); CommonFilesSearchNode contentFilesNode = new CommonFilesSearchNode(metadata);