Skip to content

Commit

Permalink
optimize: optimize the speed of buildLockKey (#3700)
Browse files Browse the repository at this point in the history
  • Loading branch information
caohdgege authored May 3, 2021
1 parent c3a441c commit 0e98040
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 16 deletions.
1 change: 1 addition & 0 deletions changes/1.5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Seata 是一款开源的分布式事务解决方案,提供高性能和简单
- [[#3654](https://github.com/seata/seata/pull/3654)] 修正拼写,applicationContex -> applicationContext
- [[#3687](https://github.com/seata/seata/pull/3687)] 修复某些场景下无法重试全局锁的问题
- [[#3689](https://github.com/seata/seata/pull/3689)] 修正script/server/config/file.properties中属性编写错误
- [[#3700](https://github.com/seata/seata/pull/3700)] 优化buildLockKey方法的效率

### test:

Expand Down
1 change: 1 addition & 0 deletions changes/en-us/1.5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
- [[#3654](https://github.com/seata/seata/pull/3654)] fix typo,applicationContex -> applicationContext
- [[#3687](https://github.com/seata/seata/pull/3687)] fix the case that could not retry acquire global lock
- [[#3689](https://github.com/seata/seata/pull/3689)] modify the attribute prefix in the file file.properties
- [[#3700](https://github.com/seata/seata/pull/3700)] optimize the speed of buildLockKey


### test:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,10 @@ protected String buildLockKey(TableRecords rowsIncludingPK) {
sb.append(":");
int filedSequence = 0;
List<Map<String, Field>> pksRows = rowsIncludingPK.pkRows();
List<String> primaryKeysOnlyName = getTableMeta().getPrimaryKeyOnlyName();
for (Map<String, Field> rowMap : pksRows) {
int pkSplitIndex = 0;
for (String pkName : getTableMeta().getPrimaryKeyOnlyName()) {
for (String pkName : primaryKeysOnlyName) {
if (pkSplitIndex > 0) {
sb.append("_");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public Map<String,List<Object>> getPkValues() throws SQLException {
List<String> pkColumnNameList = getTableMeta().getPrimaryKeyOnlyName();
Boolean isContainsPk = containsPK();
//when there is only one pk in the table
if (getTableMeta().getPrimaryKeyOnlyName().size() == 1) {
if (pkColumnNameList.size() == 1) {
if (isContainsPk) {
pkValuesMap = getPkValuesByColumn();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,10 @@ public Map<String,List<Object>> getPkValues() throws SQLException {
//when there is only one pk in the table
if (isContainsPk) {
pkValuesMap = getPkValuesByColumn();
}
else if (containsColumns()) {
} else if (containsColumns()) {
String columnName = getTableMeta().getPrimaryKeyOnlyName().get(0);
pkValuesMap = Collections.singletonMap(columnName, getGeneratedKeys());
}
else {
} else {
pkValuesMap = getPkValuesByColumn();
}
return pkValuesMap;
Expand All @@ -82,9 +80,9 @@ public Map<String,List<Object>> getPkValuesByColumn() throws SQLException {
List<Object> pkValues = pkValuesMap.get(pkKey);

if (!pkValues.isEmpty() && pkValues.get(0) instanceof SqlSequenceExpr) {
pkValuesMap.put(pkKey,getPkValuesBySequence((SqlSequenceExpr) pkValues.get(0)));
pkValuesMap.put(pkKey, getPkValuesBySequence((SqlSequenceExpr) pkValues.get(0)));
} else if (pkValues.size() == 1 && pkValues.get(0) instanceof SqlMethodExpr) {
pkValuesMap.put(pkKey,getGeneratedKeys());
pkValuesMap.put(pkKey, getGeneratedKeys());
} else if (pkValues.size() == 1 && pkValues.get(0) instanceof Null) {
throw new NotSupportYetException("oracle not support null");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,10 @@ public Map<String,List<Object>> getPkValues() throws SQLException {
//when there is only one pk in the table
if (isContainsPk) {
pkValuesMap = getPkValuesByColumn();
}
else if (containsColumns()) {
} else if (containsColumns()) {
String columnName = getTableMeta().getPrimaryKeyOnlyName().get(0);
pkValuesMap = Collections.singletonMap(columnName, getGeneratedKeys());
}
else {
} else {
pkValuesMap = getPkValuesByColumn();
}
return pkValuesMap;
Expand All @@ -84,11 +82,11 @@ public Map<String,List<Object>> getPkValuesByColumn() throws SQLException {
String pkKey = pkValuesMap.keySet().iterator().next();
List<Object> pkValues = pkValuesMap.get(pkKey);
if (!pkValues.isEmpty() && pkValues.get(0) instanceof SqlSequenceExpr) {
pkValuesMap.put(pkKey,getPkValuesBySequence((SqlSequenceExpr) pkValues.get(0)));
pkValuesMap.put(pkKey, getPkValuesBySequence((SqlSequenceExpr) pkValues.get(0)));
} else if (!pkValues.isEmpty() && pkValues.get(0) instanceof SqlMethodExpr) {
pkValuesMap.put(pkKey,getGeneratedKeys());
pkValuesMap.put(pkKey, getGeneratedKeys());
} else if (!pkValues.isEmpty() && pkValues.get(0) instanceof SqlDefaultExpr) {
pkValuesMap.put(pkKey,getPkValuesByDefault());
pkValuesMap.put(pkKey, getPkValuesByDefault());
}

return pkValuesMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ public static TableRecords empty(TableMeta tableMeta) {
public static TableRecords buildRecords(TableMeta tmeta, ResultSet resultSet) throws SQLException {
TableRecords records = new TableRecords(tmeta);
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
Map<String, ColumnMeta> primaryKeyMap = tmeta.getPrimaryKeyMap();
int columnCount = resultSetMetaData.getColumnCount();

while (resultSet.next()) {
Expand All @@ -195,7 +196,7 @@ public static TableRecords buildRecords(TableMeta tmeta, ResultSet resultSet) th
int dataType = col.getDataType();
Field field = new Field();
field.setName(col.getColumnName());
if (tmeta.getPrimaryKeyMap().containsKey(colName)) {
if (primaryKeyMap.containsKey(colName)) {
field.setKeyType(KeyType.PRIMARY_KEY);
}
field.setType(dataType);
Expand Down

0 comments on commit 0e98040

Please sign in to comment.