Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@
.project
.settings/
target
.idea
*.iml
3 changes: 2 additions & 1 deletion src/main/java/com/redhat/victims/database/VictimsSQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,8 @@ protected static class Query {
+ "WHERE filehash IN (?) " + "GROUP BY record";
protected final static String FILEHASH_COUNT_PER_RECORD = "SELECT record, count(*) FROM filehashes GROUP BY record";
protected final static String FILEHASHES = "SELECT record, filehash FROM filehashes";
protected final static String PROPERTY_MATCH = "SELECT record FROM meta WHERE prop = ? AND value = ?";
protected final static String PROPERTY_MATCH = "SELECT record, COUNT(record) as count FROM TABLE (p varchar = ?, "
+ "v varchar = ? ) t INNER JOIN meta ON t.p = meta.prop AND t.v = meta.value GROUP BY record";
protected final static String RECORD_COUNT = "SELECT COUNT(*) from records";
}
}
55 changes: 14 additions & 41 deletions src/main/java/com/redhat/victims/database/VictimsSqlDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -282,32 +282,24 @@ public HashSet<String> getVulnerabilities(HashMap<String, String> props)
HashSet<String> cves = new HashSet<String>();

int requiredMinCount = props.size();
HashMap<Integer, MutableInteger> matchedPropCount = new HashMap<Integer, MutableInteger>();

ResultSet rs;
PreparedStatement ps;
Connection connection = getConnection();
try {
for (String key : props.keySet()) {
String value = props.get(key);
ps = setObjects(connection, Query.PROPERTY_MATCH, key,
value);
rs = ps.executeQuery();
while (rs.next()) {
Integer id = rs.getInt("record");
if (!matchedPropCount.containsKey(id)) {
matchedPropCount.put(id, new MutableInteger());
} else {
MutableInteger count = matchedPropCount.get(id);
count.increment();
if (count.get() == requiredMinCount) {
cves.addAll(getVulnerabilities(id));
}
}
}
rs.close();
ps.close();
}
ps = setObjects(connection, Query.PROPERTY_MATCH,
props.keySet().toArray(),
props.values().toArray());
rs = ps.executeQuery();
while (rs.next()) {
Integer id = rs.getInt("record");
Integer count = rs.getInt("count");
if (count == requiredMinCount) {
cves.addAll(getVulnerabilities(id));
}
}
rs.close();
ps.close();
} finally {
connection.close();
}
Expand Down Expand Up @@ -427,24 +419,5 @@ public int getRecordCount() throws VictimsException {
return count;
}

/**
* This class is used internally to store counts.
*
* @author abn
*
*/
protected static class MutableInteger {
/*
* http://stackoverflow.com/questions/81346
*/
int value = 1; // note that we start at 1 since we're counting

public void increment() {
++value;
}

public int get() {
return value;
}
}

}