Allon Mureinik has uploaded a new change for review. Change subject: core: Exaple: Use UUID for handling database Guids ......................................................................
core: Exaple: Use UUID for handling database Guids The Postgres JDBC Driver natively supports the java.lang.UUID type as a representation of a database uuid, in both directions - 1. ResultSet#getOtbject(int) and ResultSet#getObject(String) return a UUID instance when applied to a uuid column. 2. PreparedStatement#setObject(int, Object) can receive a UUID and apply it to a uuid column. Using the proper type has several benefits: 1. Save the CPU operations on UUID.getString() and Guid(String) when using ResultSet.getObject(int) or ResultSet.getObject(String). 2. Save the CPU operations on Guid.toString() and having the DATABASE convert a String passed to a PreparedStatement to a uuid. 3. Bind Guids as native uuids instead of varchars, making sure that indexes will be used in the query. This patch is an example how this should be done, presented on StoragePoolIsoMapDAODbFacadeImpl. This DAO was chosen since it's one of the simplest DAOs and requires the least amount of code changes. If this patch will be accepted, subsequent patches will apply the same changes to the other DAOs. Change-Id: I4d30e1ab7303ecad68dbc9005ac5c6ef733f1707 Signed-off-by: Allon Mureinik <[email protected]> --- M backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/CustomMapSqlParameterSource.java M backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/BaseDAODbFacade.java M backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/StoragePoolIsoMapDAODbFacadeImpl.java M backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dal/dbbroker/CustomMapSqlParameterSourceTest.java 4 files changed, 61 insertions(+), 4 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/81/16281/1 diff --git a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/CustomMapSqlParameterSource.java b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/CustomMapSqlParameterSource.java index c836996..5118c8b 100644 --- a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/CustomMapSqlParameterSource.java +++ b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/CustomMapSqlParameterSource.java @@ -26,7 +26,9 @@ // lets check if we need to translate value if (tmpValue.getClass().isEnum()) { tmpValue = extractEnumValue(tmpValue); - } else if (tmpValue instanceof Guid || tmpValue instanceof Version) { + } else if (tmpValue instanceof Guid) { + tmpValue = ((Guid) tmpValue).getUuid(); + } else if (tmpValue instanceof Version) { tmpValue = value.toString(); } } diff --git a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/BaseDAODbFacade.java b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/BaseDAODbFacade.java index ab7aab3..833a83d 100644 --- a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/BaseDAODbFacade.java +++ b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/BaseDAODbFacade.java @@ -2,6 +2,7 @@ import java.sql.ResultSet; import java.sql.SQLException; +import java.util.UUID; import org.ovirt.engine.core.compat.Guid; import org.ovirt.engine.core.dal.dbbroker.CustomMapSqlParameterSource; @@ -107,4 +108,58 @@ } } + /** + * Returns a {@link Guid} representing the column's value or a default value if the column was <code>null</code>. + * + * <b>Note:</b> Postgres' driver returns a {@link UUID} when {@link ResultSet#getObject(String)} is called on a + * uuid column. This behavior is trusted to work with Postgres 8.x and 9.x and is used to achieve the best + * performance. If it is ever broken on Postgres' side, this method should be rewritten. + * + * @param resultSet the ResultSet to extract the result from. + * @param columnName the name of the column. + * @param defaultValue The value to return if the column is <code>null</code>. + * @return a {@link Guid} representing the UUID in the column, or the default value if it was <code>null</code>. + * @throws SQLException If resultSet does not contain columnName or its value cannot be cast to {@link UUID}. + */ + private static Guid getGuid(ResultSet resultSet, String columnName, Guid defaultValue) throws SQLException { + UUID uuid = (UUID) resultSet.getObject(columnName); + if (uuid == null || resultSet.wasNull()) { + return defaultValue; + } + return new Guid(uuid); + } + + /** + * Returns a {@link Guid} representing the column's value or a <code>null</code> + * if the column was <code>null</code>. + * + * <b>Note:</b> Postgres' driver returns a {@link UUID} when {@link ResultSet#getObject(String)} is called on a + * uuid column. This behavior is trusted to work with Postgres 8.x and 9.x and is used to achieve the best + * performance. If it is ever broken on Postgres' side, this method should be rewritten. + * + * @param resultSet the ResultSet to extract the result from. + * @param columnName the name of the column. + * @return a {@link Guid} representing the UUID in the column, or the default value if it was <code>null</code>. + * @throws SQLException If resultSet does not contain columnName or its value cannot be cast to {@link UUID}. + */ + protected static Guid getGuidDefaultNull(ResultSet resultSet, String columnName) throws SQLException { + return getGuid(resultSet, columnName, null); + } + + /** + * Returns a {@link Guid} representing the column's value or a {@link Guid#Empty} + * if the column was <code>null</code>. + * + * <b>Note:</b> Postgres' driver returns a {@link UUID} when {@link ResultSet#getObject(String)} is called on a + * uuid column. This behavior is trusted to work with Postgres 8.x and 9.x and is used to achieve the best + * performance. If it is ever broken on Postgres' side, this method should be rewritten. + * + * @param resultSet the ResultSet to extract the result from. + * @param columnName the name of the column. + * @return a {@link Guid} representing the UUID in the column, or the default value if it was <code>null</code>. + * @throws SQLException If resultSet does not contain columnName or its value cannot be cast to {@link UUID}. + */ + protected static Guid getGuidDefaultEmpty(ResultSet resultSet, String columnName) throws SQLException { + return getGuid(resultSet, columnName, Guid.Empty); + } } diff --git a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/StoragePoolIsoMapDAODbFacadeImpl.java b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/StoragePoolIsoMapDAODbFacadeImpl.java index 1ed4f12..e3be8f4 100644 --- a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/StoragePoolIsoMapDAODbFacadeImpl.java +++ b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/StoragePoolIsoMapDAODbFacadeImpl.java @@ -19,8 +19,8 @@ @Override public StoragePoolIsoMap mapRow(ResultSet rs, int rowNum) throws SQLException { StoragePoolIsoMap entity = new StoragePoolIsoMap(); - entity.setstorage_id(Guid.createGuidFromStringDefaultEmpty(rs.getString("storage_id"))); - entity.setstorage_pool_id(Guid.createGuidFromString(rs.getString("storage_pool_id"))); + entity.setstorage_id(getGuidDefaultEmpty(rs, "storage_id")); + entity.setstorage_pool_id(getGuidDefaultNull(rs, "storage_pool_id")); entity.setstatus(StorageDomainStatus.forValue(rs.getInt("status"))); return entity; } diff --git a/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dal/dbbroker/CustomMapSqlParameterSourceTest.java b/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dal/dbbroker/CustomMapSqlParameterSourceTest.java index 8c2e2b1..54743e8 100644 --- a/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dal/dbbroker/CustomMapSqlParameterSourceTest.java +++ b/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dal/dbbroker/CustomMapSqlParameterSourceTest.java @@ -84,7 +84,7 @@ paramSource.addValue(paramName, guid); assertEquals("wrong value returned from parameter source", - guid.toString(), + guid.getUuid(), paramSource.getValue(paramName)); } -- To view, visit http://gerrit.ovirt.org/16281 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I4d30e1ab7303ecad68dbc9005ac5c6ef733f1707 Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: Allon Mureinik <[email protected]> _______________________________________________ Engine-patches mailing list [email protected] http://lists.ovirt.org/mailman/listinfo/engine-patches
