Skip to content

Commit

Permalink
Merge pull request #327 from softwareconstruction240/fix-memory-overf…
Browse files Browse the repository at this point in the history
…low-all-latest-submissions

filter with index before sorting
  • Loading branch information
pawlh authored Apr 24, 2024
2 parents 8bf5edc + 7b3d804 commit 80ff644
Showing 1 changed file with 37 additions and 25 deletions.
62 changes: 37 additions & 25 deletions src/main/java/edu/byu/cs/dataAccess/sql/SubmissionSqlDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@
import edu.byu.cs.model.Phase;
import edu.byu.cs.model.Submission;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.Instant;
import java.util.*;

import static java.sql.Types.NULL;

public class SubmissionSqlDao implements SubmissionDao {
private static final ColumnDefinition[] COLUMN_DEFINITIONS = {
new ColumnDefinition<Submission>("net_id", Submission::netId),
Expand All @@ -32,6 +29,7 @@ public class SubmissionSqlDao implements SubmissionDao {
new ColumnDefinition<Submission>("rubric", s -> new Gson().toJson(s.rubric())),
new ColumnDefinition<Submission>("admin", Submission::admin)
};

private static Submission readSubmission(ResultSet rs) throws SQLException {
String netId = rs.getString("net_id");
String repoUrl = rs.getString("repo_url");
Expand All @@ -55,6 +53,7 @@ private static Submission readSubmission(ResultSet rs) throws SQLException {
public void insertSubmission(Submission submission) throws DataAccessException {
sqlReader.insertItem(submission);
}

@Override
public Collection<Submission> getSubmissionsForPhase(String netId, Phase phase) throws DataAccessException {
return sqlReader.executeQuery(
Expand All @@ -80,30 +79,43 @@ public Collection<Submission> getAllLatestSubmissions() throws DataAccessExcepti

@Override
public Collection<Submission> getAllLatestSubmissions(int batchSize) throws DataAccessException {
return sqlReader.executeQuery(
"""
WHERE timestamp IN (
SELECT MAX(timestamp)
FROM %s
GROUP BY net_id, phase
)
ORDER BY timestamp DESC
""".formatted(sqlReader.getTableName()) +
(batchSize >= 0 ? "LIMIT ?" : ""),
ps -> {
if (batchSize >= 0) {
ps.setInt(1, batchSize);
}
});
try (var connection = SqlDb.getConnection()) {
var statement = connection.prepareStatement(
"""
SELECT s.net_id, s.repo_url, s.timestamp, s.phase, s.passed, s.score, s.num_commits, s.head_hash, s.notes, s.rubric, s.admin
FROM submission s
INNER JOIN (
SELECT net_id, phase, MAX(timestamp) AS max_timestamp
FROM submission
GROUP BY net_id, phase
) s2 ON s.net_id = s2.net_id AND s.phase = s2.phase AND s.timestamp = s2.max_timestamp
ORDER BY s2.max_timestamp DESC
""" +
(batchSize >= 0 ? "LIMIT ?" : "")
);
if (batchSize >= 0) {
statement.setInt(1, batchSize);
}
try (var results = statement.executeQuery()) {
List<Submission> submissions = new ArrayList<>();
while (results.next()) {
Submission submission = readSubmission(results);
submissions.add(submission);
}
return submissions;
}
} catch (SQLException e) {
throw new DataAccessException("Error getting latest submissions", e);
}
}

@Override
public void removeSubmissionsByNetId(String netId) throws DataAccessException {
sqlReader.executeUpdate(
"""
DELETE FROM %s
WHERE net_id = ?
""".formatted(sqlReader.getTableName()),
DELETE FROM %s
WHERE net_id = ?
""".formatted(sqlReader.getTableName()),
ps -> ps.setString(1, netId)
);
}
Expand All @@ -128,10 +140,10 @@ public Submission getFirstPassingSubmission(String netId, Phase phase) throws Da
public float getBestScoreForPhase(String netId, Phase phase) throws DataAccessException {
return sqlReader.executeQuery(
"""
SELECT max(score) as highestScore
FROM %s
WHERE net_id = ? AND phase = ?
""".formatted(sqlReader.getTableName()),
SELECT max(score) as highestScore
FROM %s
WHERE net_id = ? AND phase = ?
""".formatted(sqlReader.getTableName()),
ps -> {
ps.setString(1, netId);
ps.setString(2, phase.toString());
Expand Down

0 comments on commit 80ff644

Please sign in to comment.