use a single query to get all mime type counts.

This commit is contained in:
millmanorama 2017-07-11 22:32:24 +02:00
parent 214fe440c8
commit 2afd45d861
3 changed files with 40 additions and 49 deletions

View File

@ -96,7 +96,7 @@ public final class FileTypes implements AutopsyVisitableItem {
super(new RootContentChildren(Arrays.asList( super(new RootContentChildren(Arrays.asList(
new FileTypesByExtension(FileTypes.this), new FileTypesByExtension(FileTypes.this),
new FileTypesByMimeType(FileTypes.this))), new FileTypesByMimeType(FileTypes.this))),
Lookups.singleton(NAME)); Lookups.singleton(NAME));
setName(NAME); setName(NAME);
setDisplayName(NAME); setDisplayName(NAME);
this.setIconBaseWithExtension("org/sleuthkit/autopsy/images/file_types.png"); this.setIconBaseWithExtension("org/sleuthkit/autopsy/images/file_types.png");
@ -205,7 +205,7 @@ public final class FileTypes implements AutopsyVisitableItem {
abstract String getDisplayNameBase(); abstract String getDisplayNameBase();
abstract String getQuery(); abstract long calculateItems() throws Exception;
/** /**
* Updates the display name of the mediaSubTypeNode to include the count * Updates the display name of the mediaSubTypeNode to include the count
@ -219,7 +219,7 @@ public final class FileTypes implements AutopsyVisitableItem {
new SwingWorker<Long, Void>() { new SwingWorker<Long, Void>() {
@Override @Override
protected Long doInBackground() throws Exception { protected Long doInBackground() throws Exception {
return skCase.countFilesWhere(getQuery()); return calculateItems();
} }
@Override @Override
@ -232,6 +232,7 @@ public final class FileTypes implements AutopsyVisitableItem {
logger.log(Level.WARNING, "Failed to get count of files for " + getDisplayNameBase(), ex); logger.log(Level.WARNING, "Failed to get count of files for " + getDisplayNameBase(), ex);
} }
} }
}.execute(); }.execute();
} else { } else {
setDisplayName(getDisplayNameBase() + ((childCount < 0) ? "" setDisplayName(getDisplayNameBase() + ((childCount < 0) ? ""

View File

@ -325,10 +325,9 @@ public final class FileTypesByExtension implements AutopsyVisitableItem {
} }
@Override @Override
String getQuery() { long calculateItems() throws Exception {
return createQuery(filter); return skCase.countFilesWhere(createQuery(filter));
} }
} }
private static String createQuery(FileTypesByExtension.SearchFilterInterface filter) { private static String createQuery(FileTypesByExtension.SearchFilterInterface filter) {

View File

@ -26,10 +26,10 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Observable; import java.util.Observable;
import java.util.Observer; import java.util.Observer;
import java.util.logging.Level; import java.util.logging.Level;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.openide.nodes.ChildFactory; import org.openide.nodes.ChildFactory;
import org.openide.nodes.Children; import org.openide.nodes.Children;
@ -61,7 +61,7 @@ public final class FileTypesByMimeType extends Observable implements AutopsyVisi
* which exist in the database. This hashmap will store them with the media * which exist in the database. This hashmap will store them with the media
* type as the key and a list of media subtypes as the value. * type as the key and a list of media subtypes as the value.
*/ */
private final HashMap<String, List<String>> existingMimeTypes = new HashMap<>(); private final HashMap<String, Map<String, Long>> existingMimeTypes = new HashMap<>();
private static final Logger LOGGER = Logger.getLogger(FileTypesByMimeType.class.getName()); private static final Logger LOGGER = Logger.getLogger(FileTypesByMimeType.class.getName());
private final FileTypes typesRoot; private final FileTypes typesRoot;
@ -95,35 +95,35 @@ public final class FileTypesByMimeType extends Observable implements AutopsyVisi
* files in it, and populate the hashmap with those results. * files in it, and populate the hashmap with those results.
*/ */
private void populateHashMap() { private void populateHashMap() {
StringBuilder allDistinctMimeTypesQuery = new StringBuilder(); String query = "SELECT mime_type,count(*) as count from tsk_files "
allDistinctMimeTypesQuery.append("SELECT DISTINCT mime_type from tsk_files where mime_type IS NOT null"); //NON-NLS + " where mime_type IS NOT null "
allDistinctMimeTypesQuery.append(" AND dir_type = ").append(TskData.TSK_FS_NAME_TYPE_ENUM.REG.getValue()); //NON-NLS + " AND dir_type = " + TskData.TSK_FS_NAME_TYPE_ENUM.REG.getValue()
allDistinctMimeTypesQuery.append(" AND (type IN (").append(TskData.TSK_DB_FILES_TYPE_ENUM.FS.ordinal()).append(","); //NON-NLS + " AND (type IN ("
allDistinctMimeTypesQuery.append(TskData.TSK_DB_FILES_TYPE_ENUM.CARVED.ordinal()).append(","); + TskData.TSK_DB_FILES_TYPE_ENUM.FS.ordinal() + ","
allDistinctMimeTypesQuery.append(TskData.TSK_DB_FILES_TYPE_ENUM.DERIVED.ordinal()).append(","); + TskData.TSK_DB_FILES_TYPE_ENUM.CARVED.ordinal() + ","
if (!UserPreferences.hideSlackFilesInViewsTree()) { + TskData.TSK_DB_FILES_TYPE_ENUM.DERIVED.ordinal() + ","
allDistinctMimeTypesQuery.append(TskData.TSK_DB_FILES_TYPE_ENUM.SLACK.ordinal()).append(","); + TskData.TSK_DB_FILES_TYPE_ENUM.LOCAL.ordinal()
} + ((UserPreferences.hideSlackFilesInViewsTree()) ? ""
allDistinctMimeTypesQuery.append(TskData.TSK_DB_FILES_TYPE_ENUM.LOCAL.ordinal()).append("))"); : ("," + TskData.TSK_DB_FILES_TYPE_ENUM.SLACK.ordinal()))
+ "))" + " GROUP BY mime_type";
synchronized (existingMimeTypes) { synchronized (existingMimeTypes) {
existingMimeTypes.clear(); existingMimeTypes.clear();
if (skCase == null) { if (skCase == null) {
return; return;
} }
try (SleuthkitCase.CaseDbQuery dbQuery = skCase.executeQuery(allDistinctMimeTypesQuery.toString())) { try (SleuthkitCase.CaseDbQuery dbQuery = skCase.executeQuery(query)) {
ResultSet resultSet = dbQuery.getResultSet(); ResultSet resultSet = dbQuery.getResultSet();
while (resultSet.next()) { while (resultSet.next()) {
final String mime_type = resultSet.getString("mime_type"); //NON-NLS final String mime_type = resultSet.getString("mime_type"); //NON-NLS
if (!mime_type.isEmpty()) { if (!mime_type.isEmpty()) {
String mimeType[] = mime_type.split("/");
//if the mime_type contained multiple slashes then everything after the first slash will become the subtype //if the mime_type contained multiple slashes then everything after the first slash will become the subtype
final String mimeMediaSubType = StringUtils.join(ArrayUtils.subarray(mimeType, 1, mimeType.length), "/"); final String mediaType = StringUtils.substringBefore(mime_type, "/");
if (mimeType.length > 1 && !mimeType[0].isEmpty() && !mimeMediaSubType.isEmpty()) { final String subType = StringUtils.removeStart(mime_type, mediaType + "/");
if (!existingMimeTypes.containsKey(mimeType[0])) { if (!mediaType.isEmpty() && !subType.isEmpty()) {
existingMimeTypes.put(mimeType[0], new ArrayList<>()); final long count = resultSet.getLong("count");
} existingMimeTypes.computeIfAbsent(mediaType, t -> new HashMap<>())
existingMimeTypes.get(mimeType[0]).add(mimeMediaSubType); .put(subType, count);
} }
} }
} }
@ -133,11 +133,10 @@ public final class FileTypesByMimeType extends Observable implements AutopsyVisi
} }
setChanged(); setChanged();
notifyObservers(); notifyObservers();
} }
FileTypesByMimeType( FileTypes typesRoot) { FileTypesByMimeType(FileTypes typesRoot) {
this.skCase = typesRoot.getSleuthkitCase(); this.skCase = typesRoot.getSleuthkitCase();
this.typesRoot = typesRoot; this.typesRoot = typesRoot;
this.pcl = (PropertyChangeEvent evt) -> { this.pcl = (PropertyChangeEvent evt) -> {
@ -228,7 +227,9 @@ public final class FileTypesByMimeType extends Observable implements AutopsyVisi
} }
boolean isEmpty() { boolean isEmpty() {
return existingMimeTypes.isEmpty(); synchronized (existingMimeTypes) {
return existingMimeTypes.isEmpty();
}
} }
} }
@ -310,7 +311,7 @@ public final class FileTypesByMimeType extends Observable implements AutopsyVisi
@Override @Override
protected boolean createKeys(List<String> mediaTypeNodes) { protected boolean createKeys(List<String> mediaTypeNodes) {
mediaTypeNodes.addAll(existingMimeTypes.get(mediaType)); mediaTypeNodes.addAll(existingMimeTypes.get(mediaType).keySet());
return true; return true;
} }
@ -333,7 +334,7 @@ public final class FileTypesByMimeType extends Observable implements AutopsyVisi
private final String subType; private final String subType;
private MediaSubTypeNode(String mimeType) { private MediaSubTypeNode(String mimeType) {
super(typesRoot,Children.create(new MediaSubTypeNodeChildren(mimeType), true)); super(typesRoot, Children.create(new MediaSubTypeNodeChildren(mimeType), true));
this.mimeType = mimeType; this.mimeType = mimeType;
this.subType = StringUtils.substringAfter(mimeType, "/"); this.subType = StringUtils.substringAfter(mimeType, "/");
super.setName(mimeType); super.setName(mimeType);
@ -374,25 +375,15 @@ public final class FileTypesByMimeType extends Observable implements AutopsyVisi
return subType; return subType;
} }
/**
* Get children count without actually loading all nodes
*
* @return count(*) - the number of items that will be shown in this
* items Directory Listing
*/
@Override @Override
String getQuery() { long calculateItems() {
return createQuery(mimeType); return existingMimeTypes.get(StringUtils.substringBefore(mimeType, "/")).get(subType);
}
}
/**
* Get children count without actually loading all nodes
*
* @return count(*) - the number of items that will be shown in this items
* Directory Listing
*/
static private long calculateItems(SleuthkitCase sleuthkitCase, String mime_type) {
try {
return sleuthkitCase.countFilesWhere(createQuery(mime_type));
} catch (TskCoreException ex) {
LOGGER.log(Level.SEVERE, "Error getting file search view count", ex); //NON-NLS
return 0;
} }
} }