Shubhendu Tripathi has uploaded a new change for review.

Change subject: gluster: BLL queries for volume snapshots list and count
......................................................................

gluster: BLL queries for volume snapshots list and count

Introduced BLL queries for volume snapshots list and its total count for
a given volume.

Change-Id: I00cd2a52c9bd10946b8702d833d6f8f0ebb3f848
Signed-off-by: Shubhendu Tripathi <[email protected]>
---
A 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/gluster/GetGlusterVolumeSnapshotsByVolumeIdQuery.java
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/gluster/GlusterQueriesCommandBase.java
M 
backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/gluster/GlusterVolumeEntity.java
M 
backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/constants/gluster/GlusterConstants.java
M 
backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/queries/VdcQueryType.java
M 
backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/gluster/GlusterVolumeDaoDbFacadeImpl.java
M 
backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/gluster/GlusterVolumeDaoTest.java
M 
backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/gluster/GlusterVolumeSnapshotDaoTest.java
M backend/manager/modules/dal/src/test/resources/fixtures.xml
M 
frontend/webadmin/modules/gwt-common/src/main/resources/org/ovirt/engine/core/Common.gwt.xml
M packaging/dbscripts/gluster_volume_snapshot_sp.sql
A 
packaging/dbscripts/upgrade/03_06_0820_add_snapshot_count_to_gluster_volume.sql
12 files changed, 161 insertions(+), 8 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/75/39275/1

diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/gluster/GetGlusterVolumeSnapshotsByVolumeIdQuery.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/gluster/GetGlusterVolumeSnapshotsByVolumeIdQuery.java
new file mode 100644
index 0000000..c9ed48c
--- /dev/null
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/gluster/GetGlusterVolumeSnapshotsByVolumeIdQuery.java
@@ -0,0 +1,14 @@
+package org.ovirt.engine.core.bll.gluster;
+
+import org.ovirt.engine.core.common.queries.IdQueryParameters;
+
+public class GetGlusterVolumeSnapshotsByVolumeIdQuery<P extends 
IdQueryParameters> extends GlusterQueriesCommandBase<P> {
+    public GetGlusterVolumeSnapshotsByVolumeIdQuery(P parameters) {
+        super(parameters);
+    }
+
+    @Override
+    public void executeQueryCommand() {
+        
getQueryReturnValue().setReturnValue(getGlusterVolumeSnapshotDao().getAllByVolumeId(getParameters().getId()));
+    }
+}
diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/gluster/GlusterQueriesCommandBase.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/gluster/GlusterQueriesCommandBase.java
index 94af3e8..ab30f28 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/gluster/GlusterQueriesCommandBase.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/gluster/GlusterQueriesCommandBase.java
@@ -17,6 +17,7 @@
 import org.ovirt.engine.core.dao.gluster.GlusterHooksDao;
 import org.ovirt.engine.core.dao.gluster.GlusterServerServiceDao;
 import org.ovirt.engine.core.dao.gluster.GlusterVolumeDao;
+import org.ovirt.engine.core.dao.gluster.GlusterVolumeSnapshotDao;
 
 public abstract class GlusterQueriesCommandBase<P extends 
VdcQueryParametersBase> extends QueriesCommandBase<P> {
     protected GlusterQueriesCommandBase(P parameters) {
@@ -56,6 +57,10 @@
         return ClusterUtils.getInstance();
     }
 
+    protected GlusterVolumeSnapshotDao getGlusterVolumeSnapshotDao() {
+        return DbFacade.getInstance().getGlusterVolumeSnapshotDao();
+    }
+
     protected Guid getUpServerId(Guid clusterId) {
         VDS vds = getClusterUtils().getUpServer(clusterId);
         if (vds == null) {
diff --git 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/gluster/GlusterVolumeEntity.java
 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/gluster/GlusterVolumeEntity.java
index 5a0f113..dfe911b 100644
--- 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/gluster/GlusterVolumeEntity.java
+++ 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/gluster/GlusterVolumeEntity.java
@@ -68,6 +68,10 @@
     @Valid
     private List<GlusterBrickEntity> bricks;
 
+    private Integer snapshotsCount;
+
+    private Integer snapMaxLimit;
+
     private GlusterStatus status;
 
     // Gluster and NFS are enabled by default
@@ -299,6 +303,22 @@
         this.bricks = bricks;
     }
 
+    public Integer getSnapshotsCount() {
+        return this.snapshotsCount;
+    }
+
+    public void setSnapshotsCount(Integer value) {
+        this.snapshotsCount = value;
+    }
+
+    public Integer getSnapMaxLimit() {
+        return this.snapMaxLimit;
+    }
+
+    public void setSnapMaxLimit(Integer limit) {
+        this.snapMaxLimit = limit;
+    }
+
     public void removeBrick(GlusterBrickEntity GlusterBrick) {
         bricks.remove(GlusterBrick);
     }
@@ -378,6 +398,8 @@
         result = prime * result + ((bricks == null) ? 0 : bricks.hashCode());
         result = prime * result + ((asyncTask == null) ? 0 : 
asyncTask.hashCode());
         result = prime * result + ((advancedDetails == null) ? 0 : 
advancedDetails.hashCode());
+        result = prime * result + ((snapshotsCount == null) ? 0 : 
snapshotsCount.hashCode());
+        result = prime * result + ((snapMaxLimit == null) ? 0 : 
snapMaxLimit.hashCode());
         return result;
     }
 
@@ -425,6 +447,14 @@
             return false;
         }
 
+        if (!ObjectUtils.objectsEqual(snapshotsCount, 
volume.getSnapshotsCount())) {
+            return false;
+        }
+
+        if (!ObjectUtils.objectsEqual(snapMaxLimit, volume.getSnapMaxLimit())) 
{
+            return false;
+        }
+
         return true;
     }
 
diff --git 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/constants/gluster/GlusterConstants.java
 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/constants/gluster/GlusterConstants.java
index f85b860..e509356 100644
--- 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/constants/gluster/GlusterConstants.java
+++ 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/constants/gluster/GlusterConstants.java
@@ -40,4 +40,5 @@
     public static final String FAILURE_MESSAGE = "failuremessage";
     public static final String JOB_STATUS = "status";
     public static final String JOB_INFO = "info";
+    public static final String VOLUME_SNAPSHOT_MAX_HARD_LIMIT = 
"snap-max-hard-limit";
 }
diff --git 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/queries/VdcQueryType.java
 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/queries/VdcQueryType.java
index 259b876..9ab850a 100644
--- 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/queries/VdcQueryType.java
+++ 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/queries/VdcQueryType.java
@@ -335,6 +335,7 @@
     GetGlusterVolumeRebalanceStatus,
     GetGlusterVolumeRemoveBricksStatus,
     GetGlusterVolumeByTaskId,
+    GetGlusterVolumeSnapshotsByVolumeId,
 
     GetDefaultConfigurationVersion(VdcQueryAuthType.User),
     OsRepository(VdcQueryAuthType.User),
diff --git 
a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/gluster/GlusterVolumeDaoDbFacadeImpl.java
 
b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/gluster/GlusterVolumeDaoDbFacadeImpl.java
index b49f342..7d70724 100644
--- 
a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/gluster/GlusterVolumeDaoDbFacadeImpl.java
+++ 
b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/gluster/GlusterVolumeDaoDbFacadeImpl.java
@@ -16,8 +16,10 @@
 import 
org.ovirt.engine.core.common.businessentities.gluster.GlusterVolumeEntity;
 import 
org.ovirt.engine.core.common.businessentities.gluster.GlusterVolumeOptionEntity;
 import 
org.ovirt.engine.core.common.businessentities.gluster.GlusterVolumeSizeInfo;
+import 
org.ovirt.engine.core.common.businessentities.gluster.GlusterVolumeSnapshotConfig;
 import org.ovirt.engine.core.common.businessentities.gluster.GlusterVolumeType;
 import org.ovirt.engine.core.common.businessentities.gluster.TransportType;
+import org.ovirt.engine.core.common.constants.gluster.GlusterConstants;
 import org.ovirt.engine.core.common.job.JobExecutionStatus;
 import org.ovirt.engine.core.common.job.StepEnum;
 import org.ovirt.engine.core.common.utils.EnumUtils;
@@ -330,7 +332,7 @@
             }
             List<GlusterBrickEntity> bricks = 
dbFacade.getGlusterBrickDao().getBricksOfVolume(volume.getId());
             if (volume.getAsyncTask() != null && 
volume.getAsyncTask().getTaskId() != null) {
-                for (GlusterBrickEntity brick: bricks) {
+                for (GlusterBrickEntity brick : bricks) {
                     if (brick.getAsyncTask() != null && 
brick.getAsyncTask().getTaskId() != null &&
                             
brick.getAsyncTask().getTaskId().equals(volume.getAsyncTask().getTaskId())) {
                         brick.setAsyncTask(volume.getAsyncTask());
@@ -338,6 +340,18 @@
                 }
             }
             volume.setBricks(bricks);
+            GlusterVolumeSnapshotConfig config =
+                    dbFacade.getGlusterVolumeSnapshotConfigDao()
+                            .getConfigByVolumeIdAndName(volume.getClusterId(),
+                                    volume.getId(),
+                                    
GlusterConstants.VOLUME_SNAPSHOT_MAX_HARD_LIMIT);
+            if (config == null || StringUtils.isEmpty(config.getParamValue())) 
{
+                config =
+                        dbFacade.getGlusterVolumeSnapshotConfigDao()
+                                
.getConfigByClusterIdAndName(volume.getClusterId(),
+                                        
GlusterConstants.VOLUME_SNAPSHOT_MAX_HARD_LIMIT);
+            }
+            volume.setSnapMaxLimit(config != null ? 
Integer.valueOf(config.getParamValue()) : 0);
         }
     }
 
@@ -348,6 +362,7 @@
                 createVolumeIdParams(volumeId));
         return glusterVolumeAdvancedDetails;
     }
+
     private static final class GlusterVolumeRowMapper implements 
RowMapper<GlusterVolumeEntity> {
         @Override
         public GlusterVolumeEntity mapRow(ResultSet rs, int rowNum)
@@ -361,6 +376,7 @@
             entity.setStatus(GlusterStatus.valueOf(rs.getString("status")));
             entity.setReplicaCount(rs.getInt("replica_count"));
             entity.setStripeCount(rs.getInt("stripe_count"));
+            entity.setSnapshotsCount(rs.getInt("snapshot_count"));
             return entity;
         }
     }
@@ -380,23 +396,24 @@
             return TransportType.valueOf(rs.getString("transport_type"));
         }
     }
+
     private static final class GlusterAsyncTaskRowMapper implements 
RowMapper<GlusterAsyncTask> {
         @Override
         public GlusterAsyncTask mapRow(ResultSet rs, int rowNum)
                 throws SQLException {
             GlusterAsyncTask asyncTask = new GlusterAsyncTask();
             asyncTask.setTaskId(getGuid(rs, "external_id"));
-            String jobStatus =rs.getString("job_status");
+            String jobStatus = rs.getString("job_status");
             if (asyncTask.getTaskId() != null || 
JobExecutionStatus.STARTED.name().equalsIgnoreCase(jobStatus)) {
                 asyncTask.setJobId(getGuid(rs, "job_job_id"));
                 asyncTask.setJobStatus(JobExecutionStatus.valueOf(jobStatus));
             }
-            String stepStatus =rs.getString("status");
+            String stepStatus = rs.getString("status");
             String stepType = rs.getString("step_type");
-            if(stepType != null && !stepType.isEmpty()){
+            if (stepType != null && !stepType.isEmpty()) {
                 
asyncTask.setType(GlusterTaskType.forValue(StepEnum.valueOf(stepType)));
             }
-            if(stepStatus != null && !stepStatus.isEmpty()){
+            if (stepStatus != null && !stepStatus.isEmpty()) {
                 asyncTask.setStatus(JobExecutionStatus.valueOf(stepStatus));
             }
             return asyncTask;
diff --git 
a/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/gluster/GlusterVolumeDaoTest.java
 
b/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/gluster/GlusterVolumeDaoTest.java
index aa8791e..1e158cd 100644
--- 
a/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/gluster/GlusterVolumeDaoTest.java
+++ 
b/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/gluster/GlusterVolumeDaoTest.java
@@ -471,6 +471,8 @@
         brick.setStatus(GlusterStatus.UP);
         brick.setBrickOrder(0);
         volume.addBrick(brick);
+        volume.setSnapshotsCount(0);
+        volume.setSnapMaxLimit(0);
 
         dao.save(volume);
         return volume;
diff --git 
a/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/gluster/GlusterVolumeSnapshotDaoTest.java
 
b/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/gluster/GlusterVolumeSnapshotDaoTest.java
index 420ac7b..4dbfedd 100644
--- 
a/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/gluster/GlusterVolumeSnapshotDaoTest.java
+++ 
b/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/gluster/GlusterVolumeSnapshotDaoTest.java
@@ -7,10 +7,12 @@
 import static org.junit.Assert.assertTrue;
 
 import java.util.ArrayList;
+import java.util.Date;
 import java.util.List;
 
 import org.junit.Test;
 import 
org.ovirt.engine.core.common.businessentities.gluster.GlusterSnapshotStatus;
+import 
org.ovirt.engine.core.common.businessentities.gluster.GlusterVolumeEntity;
 import 
org.ovirt.engine.core.common.businessentities.gluster.GlusterVolumeSnapshotEntity;
 import org.ovirt.engine.core.compat.Guid;
 import org.ovirt.engine.core.dao.BaseDAOTestCase;
@@ -23,6 +25,7 @@
     private static final String EXISTING_SNAPSHOT_NAME_1 = 
"test-vol-distribute-1-snap2";
     private static final String NEW_SNAPSHOT_NAME = 
"test-vol-distribute-1-snap3";
     private GlusterVolumeSnapshotDao dao;
+    private GlusterVolumeDao volumeDao;
     private GlusterVolumeSnapshotEntity existingSnapshot;
     private GlusterVolumeSnapshotEntity existingSnapshot1;
     private GlusterVolumeSnapshotEntity newSnapshot;
@@ -31,6 +34,7 @@
     public void setUp() throws Exception {
         super.setUp();
         dao = dbFacade.getGlusterVolumeSnapshotDao();
+        volumeDao = dbFacade.getGlusterVolumeDao();
         existingSnapshot = dao.getById(EXISTING_SNAPSHOT_ID);
         existingSnapshot1 = dao.getById(EXISTING_SNAPSHOT_ID_1);
     }
@@ -85,11 +89,17 @@
 
     @Test
     public void testRemove() {
+        GlusterVolumeEntity volume = volumeDao.getById(VOLUME_ID);
+        assertEquals(volume.getSnapshotsCount().intValue(), 2);
+
         dao.remove(EXISTING_SNAPSHOT_ID);
         List<GlusterVolumeSnapshotEntity> snapshots = 
dao.getAllByVolumeId(VOLUME_ID);
 
         assertTrue(snapshots.size() == 1);
         assertFalse(snapshots.contains(existingSnapshot));
+
+        GlusterVolumeEntity volume1 = volumeDao.getById(VOLUME_ID);
+        assertEquals(volume1.getSnapshotsCount().intValue(), 1);
     }
 
     @Test
@@ -98,27 +108,44 @@
         idsToRemove.add(EXISTING_SNAPSHOT_ID);
         idsToRemove.add(EXISTING_SNAPSHOT_ID_1);
 
+        GlusterVolumeEntity volume = volumeDao.getById(VOLUME_ID);
+        assertEquals(volume.getSnapshotsCount().intValue(), 2);
+
         dao.removeAll(idsToRemove);
         List<GlusterVolumeSnapshotEntity> snapshots = 
dao.getAllByVolumeId(VOLUME_ID);
-
         assertTrue(snapshots.isEmpty());
+
+        GlusterVolumeEntity volume1 = volumeDao.getById(VOLUME_ID);
+        assertEquals(volume1.getSnapshotsCount().intValue(), 0);
     }
 
     @Test
     public void testRemoveByName() {
+        GlusterVolumeEntity volume = volumeDao.getById(VOLUME_ID);
+        assertEquals(volume.getSnapshotsCount().intValue(), 2);
+
         dao.removeByName(VOLUME_ID, EXISTING_SNAPSHOT_NAME_1);
         List<GlusterVolumeSnapshotEntity> snapshots = 
dao.getAllByVolumeId(VOLUME_ID);
 
         assertTrue(snapshots.size() == 1);
         assertTrue(snapshots.contains(existingSnapshot));
         assertFalse(snapshots.contains(existingSnapshot1));
+
+        GlusterVolumeEntity volume1 = volumeDao.getById(VOLUME_ID);
+        assertEquals(volume1.getSnapshotsCount().intValue(), 1);
     }
 
     @Test
     public void testRemoveAllByVolumeId() {
+        GlusterVolumeEntity volume = volumeDao.getById(VOLUME_ID);
+        assertEquals(volume.getSnapshotsCount().intValue(), 2);
+
         dao.removeAllByVolumeId(VOLUME_ID);
         List<GlusterVolumeSnapshotEntity> snapshots = 
dao.getAllByVolumeId(VOLUME_ID);
         assertTrue(snapshots.isEmpty());
+
+        GlusterVolumeEntity volume1 = volumeDao.getById(VOLUME_ID);
+        assertEquals(volume1.getSnapshotsCount().intValue(), 0);
     }
 
     @Test
@@ -178,6 +205,7 @@
         snapshot.setVolumeId(VOLUME_ID);
         snapshot.setDescription("test-description");
         snapshot.setStatus(GlusterSnapshotStatus.STARTED);
+        snapshot.setCreatedAt(new Date());
 
         dao.save(snapshot);
         return snapshot;
diff --git a/backend/manager/modules/dal/src/test/resources/fixtures.xml 
b/backend/manager/modules/dal/src/test/resources/fixtures.xml
index 275b836..67d88d9 100644
--- a/backend/manager/modules/dal/src/test/resources/fixtures.xml
+++ b/backend/manager/modules/dal/src/test/resources/fixtures.xml
@@ -6496,6 +6496,7 @@
         <column>status</column>
         <column>replica_count</column>
         <column>stripe_count</column>
+        <column>snapshot_count</column>
         <column>_create_date</column>
         <column>_update_date</column>
         <row>
@@ -6506,6 +6507,7 @@
             <value>UP</value>
             <value>0</value>
             <value>0</value>
+            <value>2</value>
             <value>2010-10-21 03:38:22</value>
             <value>2010-11-29 15:57:10</value>
         </row>
@@ -6517,6 +6519,7 @@
             <value>UP</value>
             <value>2</value>
             <value>0</value>
+            <value>0</value>
             <value>2010-10-21 03:38:22</value>
             <value>2010-11-29 15:57:10</value>
         </row>
diff --git 
a/frontend/webadmin/modules/gwt-common/src/main/resources/org/ovirt/engine/core/Common.gwt.xml
 
b/frontend/webadmin/modules/gwt-common/src/main/resources/org/ovirt/engine/core/Common.gwt.xml
index 0b79965..94ec7c1 100644
--- 
a/frontend/webadmin/modules/gwt-common/src/main/resources/org/ovirt/engine/core/Common.gwt.xml
+++ 
b/frontend/webadmin/modules/gwt-common/src/main/resources/org/ovirt/engine/core/Common.gwt.xml
@@ -341,6 +341,8 @@
         <include 
name="common/businessentities/gluster/GlusterVolumeProfileStats.java"/>
         <include 
name="common/businessentities/gluster/NfsProfileDetails.java"/>
         <include 
name="common/businessentities/gluster/BrickProfileDetails.java"/>
+        <include 
name="common/businessentities/gluster/GlusterVolumeSnapshotEntity.java"/>
+        <include 
name="common/businessentities/gluster/GlusterSnapshotStatus.java"/>
                <!-- Scheduling -->
                <include name="common/scheduling/*.java"/>
                <include name="common/scheduling/parameters/*.java"/>
diff --git a/packaging/dbscripts/gluster_volume_snapshot_sp.sql 
b/packaging/dbscripts/gluster_volume_snapshot_sp.sql
index 37b3884..38ba5a2 100644
--- a/packaging/dbscripts/gluster_volume_snapshot_sp.sql
+++ b/packaging/dbscripts/gluster_volume_snapshot_sp.sql
@@ -17,6 +17,8 @@
         description, status)
     VALUES (v_snapshot_id,  v_snapshot_name, v_volume_id,
         v_description, v_status);
+
+    PERFORM UpdateSnapshotCountInc(v_volume_id, 1);
 END; $procedure$
 LANGUAGE plpgsql;
 
@@ -69,9 +71,15 @@
 Create or replace FUNCTION DeleteGlusterVolumeSnapshotByGuid(v_snapshot_id 
UUID)
     RETURNS VOID
     AS $procedure$
+DECLARE
+ref_volume_id UUID;
 BEGIN
+    SELECT volume_id INTO ref_volume_id FROM gluster_volume_snapshots WHERE 
snapshot_id = v_snapshot_id;
+
     DELETE FROM gluster_volume_snapshots
     WHERE snapshot_id = v_snapshot_id;
+
+    PERFORM UpdateSnapshotCountDec(ref_volume_id, 1);
 END; $procedure$
 LANGUAGE plpgsql;
 
@@ -82,6 +90,10 @@
 BEGIN
     DELETE FROM gluster_volume_snapshots
     WHERE volume_id = v_volume_id;
+
+    UPDATE gluster_volumes
+    SET snapshot_count = 0
+    WHERE id = v_volume_id;
 END; $procedure$
 LANGUAGE plpgsql;
 
@@ -94,6 +106,8 @@
     DELETE FROM gluster_volume_snapshots
     WHERE volume_id = v_volume_id
     AND   snapshot_name = v_snapshot_name;
+
+    PERFORM UpdateSnapshotCountDec(v_volume_id, 1);
 END; $procedure$
 LANGUAGE plpgsql;
 
@@ -101,9 +115,22 @@
 Create or replace FUNCTION DeleteGlusterVolumesSnapshotByIds(v_snapshot_ids 
VARCHAR(5000))
     RETURNS VOID
     AS $procedure$
+DECLARE
+v_volume_id UUID;
+v_snapshot_count integer;
+v_cur CURSOR FOR SELECT volume_id, count(volume_id) FROM 
gluster_volume_snapshots
+WHERE snapshot_id IN (SELECT * FROM fnSplitterUuid(v_snapshot_ids)) GROUP BY 
volume_id;
 BEGIN
-DELETE FROM gluster_volume_snapshots
-WHERE snapshot_id in (select * from fnSplitterUuid(v_snapshot_ids));
+    OPEN v_cur;
+    LOOP
+        FETCH v_cur INTO v_volume_id, v_snapshot_count;
+        EXIT WHEN NOT FOUND;
+        PERFORM UpdateSnapshotCountDec(v_volume_id, v_snapshot_count);
+    END LOOP;
+    CLOSE v_cur;
+
+    DELETE FROM gluster_volume_snapshots
+    WHERE snapshot_id in (select * from fnSplitterUuid(v_snapshot_ids));
 END; $procedure$
 LANGUAGE plpgsql;
 
@@ -223,3 +250,25 @@
     AND     param_name = v_param_name;
 END; $procedure$
 LANGUAGE plpgsql;
+
+
+Create or replace FUNCTION UpdateSnapshotCountInc(v_volume_id UUID, v_num int)
+    RETURNS VOID
+    AS $procedure$
+BEGIN
+    UPDATE gluster_volumes
+    SET snapshot_count = snapshot_count + v_num
+    WHERE id = v_volume_id;
+END; $procedure$
+LANGUAGE plpgsql;
+
+
+Create or replace FUNCTION UpdateSnapshotCountDec(v_volume_id UUID, v_num int)
+    RETURNS VOID
+    AS $procedure$
+BEGIN
+    UPDATE gluster_volumes
+    SET snapshot_count = snapshot_count - v_num
+    WHERE id = v_volume_id;
+END; $procedure$
+LANGUAGE plpgsql;
diff --git 
a/packaging/dbscripts/upgrade/03_06_0820_add_snapshot_count_to_gluster_volume.sql
 
b/packaging/dbscripts/upgrade/03_06_0820_add_snapshot_count_to_gluster_volume.sql
new file mode 100644
index 0000000..ea80a3e
--- /dev/null
+++ 
b/packaging/dbscripts/upgrade/03_06_0820_add_snapshot_count_to_gluster_volume.sql
@@ -0,0 +1 @@
+SELECT fn_db_add_column('gluster_volumes', 'snapshot_count', 'INTEGER NOT NULL 
DEFAULT 0');


-- 
To view, visit https://gerrit.ovirt.org/39275
To unsubscribe, visit https://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I00cd2a52c9bd10946b8702d833d6f8f0ebb3f848
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: ovirt-engine-3.5-gluster
Gerrit-Owner: Shubhendu Tripathi <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to