Skip to content

Commit

Permalink
#807 - Collection of statistics on large queries causes large memory …
Browse files Browse the repository at this point in the history
…issues
  • Loading branch information
ipolevoy committed Nov 5, 2018
1 parent ea27f93 commit b3a19a3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
Expand Up @@ -26,6 +26,9 @@ public class QueryExecutionEvent {

private static final Pattern IN_PATTERN = Pattern.compile("(IN|in)\\s*\\(.*\\)", Pattern.CASE_INSENSITIVE);
private static final Pattern OFFSET_PATTERN = Pattern.compile("(offset|OFFSET|Offset)\\s*\\d*", Pattern.CASE_INSENSITIVE);
private static final Pattern INSERT_INTO_PATTERN = Pattern.compile("INSERT(.|\\n)*INTO(.|\\n)*VALUES(.|\\n)*", Pattern.CASE_INSENSITIVE);



private String query;
private final long time;
Expand All @@ -38,6 +41,11 @@ public QueryExecutionEvent(String query, long time) {
this.query = query.substring(0, query.indexOf("TabSeparated")) + "...";
}

if(INSERT_INTO_PATTERN.matcher(query).matches()){
String lowerCase = query.toLowerCase();
this.query = query.substring(0, lowerCase.indexOf("values")) + " VALUES (...)";
}

this.time = time;
}

Expand Down
Expand Up @@ -125,4 +125,15 @@ public void shouldTruncateAtTabSeparated(){
QueryExecutionEvent event = new QueryExecutionEvent(sql, 1);
the(event.getQuery()).shouldBeEqual("INSERT INTO xxx_events (xxx_id, yyy_id, type, email, user_id, latitude, longitude, ip, user_agent, url, merged_url, created_at, event_date) format ...");
}



@Test // related: https://github.com/javalite/activejdbc/issues/807
public void shouldTruncateInsertInto(){
String sql = Util.readResource("/insert_into.sql");

QueryExecutionEvent event = new QueryExecutionEvent(sql, 1);

the(event.getQuery()).shouldBeEqual("INSERT INTO analytics_events (person_id,other_person_id,location_id,location_name,date,action,type,info,created_at,updated_at) VALUES (...)");
}
}
4 changes: 4 additions & 0 deletions activejdbc/src/test/resources/insert_into.sql
@@ -0,0 +1,4 @@
INSERT INTO analytics_events (person_id,other_person_id,location_id,location_name,date,action,type,info,created_at,updated_at) VALUES
(123, 456, 19, 'Sarasota', '2018-10-31 09:08:17.982', 'send', 'email', 'Subject: Check out this cat video', '2018-10-31 09:09:17.982', '2018-10-31 09:09:17.982'),
(321, 654, 89, 'Indianapolis', '2018-10-31 09:08:18.982', 'read', 'page', 'Title: You\'ll Never Believe What they said', '2018-10-31 09:09:19.982', '2018-10-31 09:09:19.982'),
... '9998 more rows');

0 comments on commit b3a19a3

Please sign in to comment.