-
Notifications
You must be signed in to change notification settings - Fork 845
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: avoid string allocation for oid/rows parsing in command tag (#1232
- Loading branch information
Showing
6 changed files
with
278 additions
and
28 deletions.
There are no files selected for viewing
108 changes: 108 additions & 0 deletions
108
pgjdbc/src/main/java/org/postgresql/core/CommandCompleteParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Copyright (c) 2018, PostgreSQL Global Development Group | ||
* See the LICENSE file in the project root for more information. | ||
*/ | ||
|
||
package org.postgresql.core; | ||
|
||
import org.postgresql.util.GT; | ||
import org.postgresql.util.PSQLException; | ||
import org.postgresql.util.PSQLState; | ||
|
||
/** | ||
* Parses {@code oid} and {@code rows} from a {@code CommandComplete (B)} message (end of Execute). | ||
*/ | ||
public final class CommandCompleteParser { | ||
private long oid; | ||
private long rows; | ||
|
||
public CommandCompleteParser() { | ||
} | ||
|
||
public long getOid() { | ||
return oid; | ||
} | ||
|
||
public long getRows() { | ||
return rows; | ||
} | ||
|
||
void set(long oid, long rows) { | ||
this.oid = oid; | ||
this.rows = rows; | ||
} | ||
|
||
/** | ||
* Parses {@code CommandComplete (B)} message. | ||
* Status is in the format of "COMMAND OID ROWS" where both 'OID' and 'ROWS' are optional | ||
* and COMMAND can have spaces within it, like CREATE TABLE. | ||
* | ||
* @param status COMMAND OID ROWS message | ||
* @throws PSQLException in case the status cannot be parsed | ||
*/ | ||
public void parse(String status) throws PSQLException { | ||
// Assumption: command neither starts nor ends with a digit | ||
if (!Parser.isDigitAt(status, status.length() - 1)) { | ||
set(0, 0); | ||
return; | ||
} | ||
|
||
// Scan backwards, while searching for a maximum of two number groups | ||
// COMMAND OID ROWS | ||
// COMMAND ROWS | ||
long oid = 0; | ||
long rows = 0; | ||
try { | ||
int lastSpace = status.lastIndexOf(' '); | ||
// Status ends with a digit => it is ROWS | ||
if (Parser.isDigitAt(status, lastSpace + 1)) { | ||
rows = Parser.parseLong(status, lastSpace + 1, status.length()); | ||
|
||
if (Parser.isDigitAt(status, lastSpace - 1)) { | ||
int penultimateSpace = status.lastIndexOf(' ', lastSpace - 1); | ||
if (Parser.isDigitAt(status, penultimateSpace + 1)) { | ||
oid = Parser.parseLong(status, penultimateSpace + 1, lastSpace); | ||
} | ||
} | ||
} | ||
} catch (NumberFormatException e) { | ||
// This should only occur if the oid or rows are out of 0..Long.MAX_VALUE range | ||
throw new PSQLException( | ||
GT.tr("Unable to parse the count in command completion tag: {0}.", status), | ||
PSQLState.CONNECTION_FAILURE, e); | ||
} | ||
set(oid, rows); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "CommandStatus{" | ||
+ "oid=" + oid | ||
+ ", rows=" + rows | ||
+ '}'; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
|
||
CommandCompleteParser that = (CommandCompleteParser) o; | ||
|
||
if (oid != that.oid) { | ||
return false; | ||
} | ||
return rows == that.rows; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int result = (int) (oid ^ (oid >>> 32)); | ||
result = 31 * result + (int) (rows ^ (rows >>> 32)); | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
pgjdbc/src/test/java/org/postgresql/core/CommandCompleteParserNegativeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright (c) 2018, PostgreSQL Global Development Group | ||
* See the LICENSE file in the project root for more information. | ||
*/ | ||
|
||
package org.postgresql.core; | ||
|
||
import org.postgresql.util.PSQLException; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
|
||
import java.util.Arrays; | ||
|
||
@RunWith(Parameterized.class) | ||
public class CommandCompleteParserNegativeTest { | ||
|
||
@Parameterized.Parameter(0) | ||
public String input; | ||
|
||
@Parameterized.Parameters(name = "input={0}") | ||
public static Iterable<Object[]> data() { | ||
return Arrays.asList(new Object[][]{ | ||
{"SELECT 0_0 42"}, | ||
{"SELECT 42 0_0"}, | ||
{"SELECT 0_0 0_0"}, | ||
}); | ||
} | ||
|
||
@Test | ||
public void run() throws PSQLException { | ||
CommandCompleteParser parser = new CommandCompleteParser(); | ||
try { | ||
parser.parse(input); | ||
Assert.fail("CommandCompleteParser should throw NumberFormatException for " + input); | ||
} catch (PSQLException e) { | ||
Throwable cause = e.getCause(); | ||
if (cause == null) { | ||
throw e; | ||
} | ||
if (!(cause instanceof NumberFormatException)) { | ||
throw e; | ||
} | ||
// NumerFormatException is expected | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
pgjdbc/src/test/java/org/postgresql/core/CommandCompleteParserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright (c) 2018, PostgreSQL Global Development Group | ||
* See the LICENSE file in the project root for more information. | ||
*/ | ||
|
||
package org.postgresql.core; | ||
|
||
import org.postgresql.util.PSQLException; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
|
||
import java.util.Arrays; | ||
|
||
@RunWith(Parameterized.class) | ||
public class CommandCompleteParserTest { | ||
|
||
@Parameterized.Parameter(0) | ||
public String input; | ||
@Parameterized.Parameter(1) | ||
public long oid; | ||
@Parameterized.Parameter(2) | ||
public long rows; | ||
|
||
@Parameterized.Parameters(name = "input={0}, oid={1}, rows={2}") | ||
public static Iterable<Object[]> data() { | ||
return Arrays.asList(new Object[][]{ | ||
{"SELECT 0", 0, 0}, | ||
{"SELECT -42", 0, 0}, | ||
{"SELECT", 0, 0}, | ||
{"", 0, 0}, | ||
{"A", 0, 0}, | ||
{"SELECT 42", 0, 42}, | ||
{"UPDATE 43 42", 43, 42}, | ||
{"UPDATE 43 " + Long.MAX_VALUE, 43, Long.MAX_VALUE}, | ||
{"UPDATE " + Long.MAX_VALUE + " " + Long.MAX_VALUE, Long.MAX_VALUE, Long.MAX_VALUE}, | ||
{"UPDATE " + (Long.MAX_VALUE / 10) + " " + (Long.MAX_VALUE / 10), (Long.MAX_VALUE / 10), | ||
(Long.MAX_VALUE / 10)}, | ||
{"UPDATE " + (Long.MAX_VALUE / 100) + " " + (Long.MAX_VALUE / 100), (Long.MAX_VALUE / 100), | ||
(Long.MAX_VALUE / 100)}, | ||
{"CREATE TABLE " + (Long.MAX_VALUE / 100) + " " + (Long.MAX_VALUE / 100), | ||
(Long.MAX_VALUE / 100), (Long.MAX_VALUE / 100)}, | ||
{"CREATE TABLE", 0, 0}, | ||
{"CREATE OR DROP OR DELETE TABLE 42", 0, 42}, | ||
}); | ||
} | ||
|
||
@Test | ||
public void run() throws PSQLException { | ||
CommandCompleteParser expected = new CommandCompleteParser(); | ||
CommandCompleteParser actual = new CommandCompleteParser(); | ||
expected.set(oid, rows); | ||
actual.parse(input); | ||
Assert.assertEquals(input, expected, actual); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters