Yair Zaslavsky has uploaded a new change for review. Change subject: engine: Introduction of command entity dao ......................................................................
engine: Introduction of command entity dao Introduction of command entity database support (upgrade script, stored procedures, DAO, DAO tests) Change-Id: I0aa9e4b2bb7e51c25419e70a119734abac72dae0 Signed-off-by: Yair Zaslavsky <[email protected]> --- A backend/manager/dbscripts/command_entities_sp.sql A backend/manager/dbscripts/upgrade/03_03_0040_create_command_entity_table.sql M backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/DbFacade.java A backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/CommandEntityDao.java A backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/CommandEntityDaoDbFacadeImpl.java M backend/manager/modules/dal/src/main/jdbc-resources/engine-daos.properties A backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/CommandEntityDaoTest.java M backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/FixturesTool.java M backend/manager/modules/dal/src/test/resources/fixtures.xml 9 files changed, 317 insertions(+), 0 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/04/14104/1 diff --git a/backend/manager/dbscripts/command_entities_sp.sql b/backend/manager/dbscripts/command_entities_sp.sql new file mode 100644 index 0000000..9b105de --- /dev/null +++ b/backend/manager/dbscripts/command_entities_sp.sql @@ -0,0 +1,76 @@ +CREATE OR REPLACE FUNCTION InsertCommandEntity (v_id uuid, v_command_type int, v_parent_command_id uuid, v_data text, v_created_at timestamp) + RETURNS void AS +$procedure$ +BEGIN + BEGIN + INSERT INTO command_entities(command_id, command_type, parent_command_id, data, created_at) + VALUES(v_id, v_command_type, v_parent_command_id, v_data, v_created_at); + END; + + RETURN; +END; $procedure$ + LANGUAGE plpgsql; + + +CREATE OR REPLACE FUNCTION UpdateCommandEntity (v_id uuid, v_command_type int, v_parent_command_id uuid, v_data text, v_created_at timestamp) + RETURNS void AS +$procedure$ +BEGIN + BEGIN + UPDATE command_entities set command_type = v_command_type , parent_command_id = v_parent_command_id, + data = v_data, created_at = v_created_at where command_id = v_id; + END; + + RETURN; +END; $procedure$ + LANGUAGE plpgsql; + +CREATE OR REPLACE FUNCTION GetCommandEntityByCommandEntityId (v_command_id uuid) + RETURNS SETOF command_entities AS +$procedure$ +BEGIN + RETURN QUERY SELECT command_entities.* + FROM command_entities + WHERE command_id = v_command_id; +END; $procedure$ + LANGUAGE plpgsql; + +CREATE OR REPLACE FUNCTION GetCommandEntitiesByParentId (v_command_id uuid) + RETURNS SETOF command_entities AS +$procedure$ +BEGIN + RETURN QUERY SELECT command_entities.* + FROM command_entities + WHERE parent_command_id = v_command_id; +END; $procedure$ + LANGUAGE plpgsql; + +CREATE OR REPLACE FUNCTION GetAllFromCommandEntities () + RETURNS SETOF command_entities AS +$procedure$ +BEGIN + RETURN QUERY SELECT * from command_entities; +END; $procedure$ + LANGUAGE plpgsql; + +CREATE OR REPLACE FUNCTION DeleteCommandEntity(v_command_id uuid) + RETURNS void AS +$procedure$ +BEGIN + BEGIN + delete from command_entities where command_id = v_command_id; + END; + RETURN; +END; $procedure$ + LANGUAGE plpgsql; + +CREATE OR REPLACE FUNCTION DeleteCommandEntitiesByParentId(v_command_id uuid) + RETURNS void AS +$procedure$ +BEGIN + BEGIN + delete from command_entities where parent_command_id = v_command_id; + END; + RETURN; +END; $procedure$ + LANGUAGE plpgsql; diff --git a/backend/manager/dbscripts/upgrade/03_03_0040_create_command_entity_table.sql b/backend/manager/dbscripts/upgrade/03_03_0040_create_command_entity_table.sql new file mode 100644 index 0000000..62216ee --- /dev/null +++ b/backend/manager/dbscripts/upgrade/03_03_0040_create_command_entity_table.sql @@ -0,0 +1,21 @@ +Create or replace FUNCTION __temp_fn_db_add_command_entities_tables() +RETURNS void +AS $function$ +BEGIN + -- Add command entities table + CREATE TABLE command_entities + ( + command_id UUID NOT NULL, + command_type int, + parent_command_id UUID REFERENCES command_entities(command_id) ON DELETE CASCADE, + data text, + created_at timestamp, + CONSTRAINT pk_command_entities PRIMARY KEY(command_id) + ) WITH OIDS; + CREATE INDEX IDX_command_entities_parent_id ON command_entities(parent_command_id); + +END; $function$ +LANGUAGE plpgsql; + +select __temp_fn_db_add_command_entities_tables(); +drop function __temp_fn_db_add_command_entities_tables(); diff --git a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/DbFacade.java b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/DbFacade.java index 505aee1..66b481e 100644 --- a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/DbFacade.java +++ b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/DbFacade.java @@ -13,6 +13,7 @@ import org.ovirt.engine.core.common.businessentities.ActionGroup; import org.ovirt.engine.core.common.businessentities.BaseDisk; import org.ovirt.engine.core.common.businessentities.BusinessEntity; +import org.ovirt.engine.core.common.businessentities.CommandEntity; import org.ovirt.engine.core.common.businessentities.DiskImage; import org.ovirt.engine.core.common.businessentities.DiskImageDynamic; import org.ovirt.engine.core.common.businessentities.Image; @@ -49,6 +50,7 @@ import org.ovirt.engine.core.dao.BaseDiskDao; import org.ovirt.engine.core.dao.BookmarkDAO; import org.ovirt.engine.core.dao.BusinessEntitySnapshotDAO; +import org.ovirt.engine.core.dao.CommandEntityDao; import org.ovirt.engine.core.dao.DAO; import org.ovirt.engine.core.dao.DaoFactory; import org.ovirt.engine.core.dao.DbUserDAO; @@ -147,6 +149,7 @@ put(image_storage_domain_map.class, ImageStorageDomainMapDao.class); put(permissions.class, PermissionDAO.class); put(Image.class, ImageDao.class); + put(CommandEntity.class, CommandEntityDao.class); } }; @@ -461,6 +464,15 @@ } /** + * Retrieves the singleton instance of {@link CommandEntityDao}. + * + * @return the dao + */ + public CommandEntityDao getCommandEntityDao() { + return getDao(CommandEntityDao.class); + } + + /** * Returns the singleton instance of {@link InterfaceDao}. * * @return the dao diff --git a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/CommandEntityDao.java b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/CommandEntityDao.java new file mode 100644 index 0000000..f292ecd --- /dev/null +++ b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/CommandEntityDao.java @@ -0,0 +1,14 @@ +package org.ovirt.engine.core.dao; + +import java.util.List; + +import org.ovirt.engine.core.common.businessentities.CommandEntity; +import org.ovirt.engine.core.compat.Guid; + +public interface CommandEntityDao extends GenericDao<CommandEntity, Guid> { + + List<CommandEntity> getByParentId(Guid parentId); + + void RemoveCommandByParentId(Guid parentId); + +} diff --git a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/CommandEntityDaoDbFacadeImpl.java b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/CommandEntityDaoDbFacadeImpl.java new file mode 100644 index 0000000..1e4a2a7 --- /dev/null +++ b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/CommandEntityDaoDbFacadeImpl.java @@ -0,0 +1,82 @@ +package org.ovirt.engine.core.dao; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.ovirt.engine.core.common.action.VdcActionType; +import org.ovirt.engine.core.common.businessentities.CommandEntity; +import org.ovirt.engine.core.compat.Guid; +import org.ovirt.engine.core.compat.NGuid; +import org.ovirt.engine.core.dal.dbbroker.DbFacadeUtils; +import org.ovirt.engine.core.utils.SerializationFactory; +import org.springframework.jdbc.core.RowMapper; +import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; + +public class CommandEntityDaoDbFacadeImpl extends DefaultGenericDaoDbFacade<CommandEntity, Guid> implements CommandEntityDao { + + private static RowMapper<CommandEntity> mapper = new RowMapper<CommandEntity>() { + + @Override + public CommandEntity mapRow(ResultSet resultSet, int rowNum) throws SQLException { + CommandEntity result = new CommandEntity(); + result.setId(Guid.createGuidFromString(resultSet.getString("command_id"))); + result.setCreatedAt(DbFacadeUtils.fromDate(resultSet.getTimestamp("created_at"))); + result.setCommandType(VdcActionType.forValue(resultSet.getInt("command_type"))); + result.setParentCommandId(NGuid.createGuidFromString(resultSet.getString("parent_command_id"))); + result.setData(deserializeData(resultSet.getString("data"))); + return result; + } + }; + + public CommandEntityDaoDbFacadeImpl() { + super("CommandEntity"); + setProcedureNameForGetAll("GetAllFromCommandEntities"); + } + + @Override + protected MapSqlParameterSource createFullParametersMapper(CommandEntity entity) { + return getCustomMapSqlParameterSource().addValue("id", entity.getId()) + .addValue("command_type", entity.getCommandType()) + .addValue("parent_command_id", entity.getParentCommandId()) + .addValue("data", serializeData(entity.getData())) + .addValue("created_at", entity.getCreatedAt()); + } + + @Override + protected MapSqlParameterSource createIdParameterMapper(Guid id) { + return getCustomMapSqlParameterSource().addValue("command_id", id); + } + + @Override + protected RowMapper<CommandEntity> createEntityRowMapper() { + return mapper; + } + + private static String serializeData(Map<String, Object> data) { + return SerializationFactory.getSerializer().serialize(data); + } + + @SuppressWarnings("unchecked") + private static Map<String, Object> deserializeData(String payload) { + return SerializationFactory.getDeserializer().deserialize(payload, + HashMap.class); + } + + @Override + public List<CommandEntity> getByParentId(Guid parentId) { + MapSqlParameterSource parameterSource = getCustomMapSqlParameterSource().addValue("command_id", parentId); + return getCallsHandler().executeReadList("GetCommandEntitiesByParentId", + mapper, + parameterSource); + } + + @Override + public void RemoveCommandByParentId(Guid parentId) { + MapSqlParameterSource parameterSource = getCustomMapSqlParameterSource().addValue("command_id", parentId); + getCallsHandler().executeModification("DeleteCommandEntitiesByParentId", parameterSource); + } + +} diff --git a/backend/manager/modules/dal/src/main/jdbc-resources/engine-daos.properties b/backend/manager/modules/dal/src/main/jdbc-resources/engine-daos.properties index 19de137..0084801 100644 --- a/backend/manager/modules/dal/src/main/jdbc-resources/engine-daos.properties +++ b/backend/manager/modules/dal/src/main/jdbc-resources/engine-daos.properties @@ -58,3 +58,4 @@ NetworkViewDao=org.ovirt.engine.core.dao.network.NetworkViewDaoDbFacadeImpl VmGuestAgentInterfaceDao=org.ovirt.engine.core.dao.VmGuestAgentInterfaceDaoDbFacadeImpl GlusterHooksDao=org.ovirt.engine.core.dao.gluster.GlusterHooksDaoDbFacadeImpl +CommandEntityDao=org.ovirt.engine.core.dao.CommandEntityDaoDbFacadeImpl diff --git a/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/CommandEntityDaoTest.java b/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/CommandEntityDaoTest.java new file mode 100644 index 0000000..b5832f2 --- /dev/null +++ b/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/CommandEntityDaoTest.java @@ -0,0 +1,81 @@ +package org.ovirt.engine.core.dao; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.junit.Test; +import org.ovirt.engine.core.common.action.VdcActionType; +import org.ovirt.engine.core.common.businessentities.CommandEntity; +import org.ovirt.engine.core.compat.Guid; + +public class CommandEntityDaoTest extends BaseGenericDaoTestCase<Guid, CommandEntity, CommandEntityDao> { + + @Override + protected CommandEntity generateNewEntity() { + + CommandEntity commandEntity = new CommandEntity(); + commandEntity.setCommandType(VdcActionType.AddVm); + commandEntity.setCreatedAt(new Date(System.currentTimeMillis())); + commandEntity.setId(Guid.NewGuid()); + commandEntity.setParentCommandId(null); + Map<String, Object> data = new HashMap<String, Object>(); + data.put("name", "oVirt"); + commandEntity.setData(data); + return commandEntity; + } + + @Override + protected void updateExistingEntity() { + // TODO Auto-generated method stub + + } + + @Override + protected Guid getExistingEntityId() { + return FixturesTool.EXISTING_COMMAND_ENTITY_ID; + } + + @Override + protected CommandEntityDao prepareDao() { + return dbFacade.getCommandEntityDao(); + } + + @Override + protected Guid generateNonExistingId() { + return Guid.NewGuid(); + } + + @Override + protected int getEneitiesTotalCount() { + return 3; + } + + @Test + public void testRemoveByParent() { + List<CommandEntity> childCommands = dbFacade.getCommandEntityDao().getByParentId(getExistingEntityId()); + assertNotNull(childCommands); + assertTrue(childCommands.size() > 0); + dbFacade.getCommandEntityDao().RemoveCommandByParentId(getExistingEntityId()); + List<CommandEntity> childCommandsAfterRemoval = + dbFacade.getCommandEntityDao().getByParentId(getExistingEntityId()); + assertNotNull(childCommandsAfterRemoval); + assertEquals(childCommandsAfterRemoval.size(), 0); + } + + @Test + public void testGetAllByParent() { + List<CommandEntity> childCommands = dbFacade.getCommandEntityDao().getByParentId(getExistingEntityId()); + assertNotNull(childCommands); + assertTrue(childCommands.size() > 0); + for (CommandEntity command : childCommands) { + assertEquals(command.getParentCommandId(), getExistingEntityId()); + } + } + +} diff --git a/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/FixturesTool.java b/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/FixturesTool.java index 59a2c25..8f99c40 100644 --- a/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/FixturesTool.java +++ b/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/FixturesTool.java @@ -356,4 +356,6 @@ public static final Guid NEW_HOOK_ID = new Guid("d2cb2f73-fab3-4a42-93f0-d5e4c069a43f"); public static final Guid CLUSTER_ID = new Guid("ae956031-6be2-43d6-bb8f-5191c9253314"); + + public static final Guid EXISTING_COMMAND_ENTITY_ID = new Guid("340fd52b-3400-4cdd-8d3f-c9d03704b0a1"); } diff --git a/backend/manager/modules/dal/src/test/resources/fixtures.xml b/backend/manager/modules/dal/src/test/resources/fixtures.xml index 6ade1ef..b2018d9 100644 --- a/backend/manager/modules/dal/src/test/resources/fixtures.xml +++ b/backend/manager/modules/dal/src/test/resources/fixtures.xml @@ -40,6 +40,34 @@ </row> </table> + <table name="command_entities"> + <column>command_id</column> + <column>command_type</column> + <column>parent_command_id</column> + <column>created_at</column> + <column>data</column> + <row> + <value>340fd52b-3400-4cdd-8d3f-c9d03704b0a1</value> + <value>230</value> <!-- Remove Disk --> + <null/> + <value>2010-11-29 15:57:10</value> + <null/> + </row> + <row> + <value>340fd52b-3400-4cdd-8d3f-c9d03704b0a2</value> + <value>211</value> <!-- Remove Image --> + <value>340fd52b-3400-4cdd-8d3f-c9d03704b0a1</value> + <value>2010-11-29 15:57:10</value> + <null/> + </row> + <row> + <value>340fd52b-3400-4cdd-8d3f-c9d03704b0a3</value> + <value>211</value> <!-- Remove Image --> + <value>340fd52b-3400-4cdd-8d3f-c9d03704b0a1</value> + <value>2010-11-29 15:57:10</value> + <null/> + </row> + </table> <table name="async_tasks"> <column>task_id</column> -- To view, visit http://gerrit.ovirt.org/14104 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I0aa9e4b2bb7e51c25419e70a119734abac72dae0 Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: Yair Zaslavsky <[email protected]> _______________________________________________ Engine-patches mailing list [email protected] http://lists.ovirt.org/mailman/listinfo/engine-patches
