Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use int to record string length instead of map #2044

Merged
merged 4 commits into from
Jul 1, 2022
Merged
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class InsertPreparedStatementImpl implements PreparedStatement {

private boolean closed = false;
private boolean closeOnComplete = false;
private final Map<Integer, Integer> stringsLen = new HashMap<>();
private Integer stringsLen = 0;

public InsertPreparedStatementImpl(String db, String sql, SQLRouter router) throws SQLException {
this.db = db;
Expand Down Expand Up @@ -219,7 +219,11 @@ public void setString(int i, String s) throws SQLException {
return;
}
byte[] bytes = s.getBytes(CHARSET);
stringsLen.put(i, bytes.length);
// if this index already set, should first reduce length of bytes last time
if (hasSet.size() >= (i - 1) && hasSet.get(i - 1)) {
stringsLen -= ((byte[]) currentDatas.get(i - 1)).length;
}
stringsLen += bytes.length;
hasSet.set(i - 1, true);
currentDatas.set(i - 1, bytes);
}
Expand Down Expand Up @@ -285,7 +289,7 @@ public void clearParameters() throws SQLException {
hasSet.set(i, false);
currentDatas.set(i, null);
}
stringsLen.clear();
stringsLen = 0;
}

@Override
Expand All @@ -296,12 +300,8 @@ public void setObject(int i, Object o, int i1) throws SQLException {

private void buildRow() throws SQLException {
SQLInsertRow currentRow = getSQLInsertRow();
int strLen = 0;
for (Map.Entry<Integer, Integer> entry : stringsLen.entrySet()) {
strLen += entry.getValue();
}

boolean ok = currentRow.Init(strLen);
boolean ok = currentRow.Init(stringsLen);
if (!ok) {
throw new SQLException("init row failed");
}
Expand Down