@@ -54,13 +54,18 @@ public class SnapshotDataStoreDaoImpl extends GenericDaoBase<SnapshotDataStoreVO
5454 private SearchBuilder <SnapshotDataStoreVO > snapshotSearch ;
5555 private SearchBuilder <SnapshotDataStoreVO > storeSnapshotSearch ;
5656 private SearchBuilder <SnapshotDataStoreVO > snapshotIdSearch ;
57+ private SearchBuilder <SnapshotDataStoreVO > volumeIdSearch ;
5758
5859 private final String parentSearch = "select store_id, store_role, snapshot_id from cloud.snapshot_store_ref where store_id = ? "
5960 + " and store_role = ? and volume_id = ? and state = 'Ready'" + " order by created DESC " + " limit 1" ;
6061 private final String findLatestSnapshot = "select store_id, store_role, snapshot_id from cloud.snapshot_store_ref where " +
6162 " store_role = ? and volume_id = ? and state = 'Ready'" +
6263 " order by created DESC " +
6364 " limit 1" ;
65+ private final String findOldestSnapshot = "select store_id, store_role, snapshot_id from cloud.snapshot_store_ref where " +
66+ " store_role = ? and volume_id = ? and state = 'Ready'" +
67+ " order by created ASC " +
68+ " limit 1" ;
6469
6570 @ Override
6671 public boolean configure (String name , Map <String , Object > params ) throws ConfigurationException {
@@ -110,6 +115,10 @@ public boolean configure(String name, Map<String, Object> params) throws Configu
110115 snapshotIdSearch .and ("snapshot_id" , snapshotIdSearch .entity ().getSnapshotId (), SearchCriteria .Op .EQ );
111116 snapshotIdSearch .done ();
112117
118+ volumeIdSearch = createSearchBuilder ();
119+ volumeIdSearch .and ("volume_id" , volumeIdSearch .entity ().getVolumeId (), SearchCriteria .Op .EQ );
120+ volumeIdSearch .done ();
121+
113122 return true ;
114123 }
115124
@@ -225,7 +234,34 @@ public SnapshotDataStoreVO findLatestSnapshotForVolume(Long volumeId, DataStoreR
225234 return findByStoreSnapshot (role , sid , snid );
226235 }
227236 } catch (SQLException e ) {
228- s_logger .debug ("Failed to find parent snapshot: " + e .toString ());
237+ s_logger .debug ("Failed to find latest snapshot for volume: " + volumeId + " due to: " + e .toString ());
238+ } finally {
239+ try {
240+ if (pstmt != null )
241+ pstmt .close ();
242+ } catch (SQLException e ) {
243+ }
244+ }
245+ return null ;
246+ }
247+
248+ @ Override
249+ public SnapshotDataStoreVO findOldestSnapshotForVolume (Long volumeId , DataStoreRole role ) {
250+ TransactionLegacy txn = TransactionLegacy .currentTxn ();
251+ PreparedStatement pstmt = null ;
252+ ResultSet rs = null ;
253+ try {
254+ pstmt = txn .prepareStatement (findOldestSnapshot );
255+ pstmt .setString (1 , role .toString ());
256+ pstmt .setLong (2 , volumeId );
257+ rs = pstmt .executeQuery ();
258+ while (rs .next ()) {
259+ long sid = rs .getLong (1 );
260+ long snid = rs .getLong (3 );
261+ return findByStoreSnapshot (role , sid , snid );
262+ }
263+ } catch (SQLException e ) {
264+ s_logger .debug ("Failed to find oldest snapshot for volume: " + volumeId + " due to: " + e .toString ());
229265 } finally {
230266 try {
231267 if (pstmt != null )
@@ -367,4 +403,13 @@ public void updateStoreRoleToCache(long storeId) {
367403 }
368404 }
369405
406+ @ Override
407+ public void updateVolumeIds (long oldVolId , long newVolId ) {
408+ SearchCriteria <SnapshotDataStoreVO > sc = volumeIdSearch .create ();
409+ sc .setParameters ("volume_id" , oldVolId );
410+ SnapshotDataStoreVO snapshot = createForUpdate ();
411+ snapshot .setVolumeId (newVolId );
412+ UpdateBuilder ub = getUpdateBuilder (snapshot );
413+ update (ub , sc , null );
414+ }
370415}
0 commit comments