001package co.codewizards.cloudstore.local.persistence; 002 003import static java.util.Objects.*; 004 005import java.util.Collection; 006 007import javax.jdo.PersistenceManager; 008import javax.jdo.Query; 009 010import org.slf4j.Logger; 011import org.slf4j.LoggerFactory; 012 013public class ModificationDao extends Dao<Modification, ModificationDao> { 014 private static final Logger logger = LoggerFactory.getLogger(ModificationDao.class); 015 016 /** 017 * Gets those {@link Modification}s being assigned to the given {@link Modification#getRemoteRepository() remoteRepository} 018 * whose {@link Modification#getLocalRevision() localRevision} is greater than the given {@code localRevision}. 019 * @param remoteRepository the {@link Modification#getRemoteRepository() remoteRepository} the queried modifications are assigned to. 020 * @param localRevision the {@link Modification#getLocalRevision() localRevision}, after which the modifications 021 * to be queried where created. 022 * @return those {@link Modification}s matching the given criteria. Never <code>null</code>, but maybe empty. 023 */ 024 public Collection<Modification> getModificationsAfter(final RemoteRepository remoteRepository, final long localRevision) { 025 requireNonNull(remoteRepository, "remoteRepository"); 026 final PersistenceManager pm = pm(); 027 final FetchPlanBackup fetchPlanBackup = FetchPlanBackup.createFrom(pm); 028 final Query query = pm.newNamedQuery(getEntityClass(), "getModificationsAfter_remoteRepository_localRevision"); 029 try { 030 clearFetchGroups(); 031 long startTimestamp = System.currentTimeMillis(); 032 @SuppressWarnings("unchecked") 033 Collection<Modification> modifications = (Collection<Modification>) query.execute(remoteRepository, localRevision); 034 logger.debug("getModificationsAfter: query.execute(...) took {} ms.", System.currentTimeMillis() - startTimestamp); 035 036 fetchPlanBackup.restore(pm); 037 startTimestamp = System.currentTimeMillis(); 038 modifications = load(modifications); 039 logger.debug("getModificationsAfter: Loading result-set with {} elements took {} ms.", modifications.size(), System.currentTimeMillis() - startTimestamp); 040 041 return modifications; 042 } finally { 043 query.closeAll(); 044 fetchPlanBackup.restore(pm); 045 } 046 } 047 048 public Collection<Modification> getModificationsBeforeOrEqual(final RemoteRepository remoteRepository, final long localRevision) { 049 requireNonNull(remoteRepository, "remoteRepository"); 050 final PersistenceManager pm = pm(); 051 final FetchPlanBackup fetchPlanBackup = FetchPlanBackup.createFrom(pm); 052 final Query query = pm.newNamedQuery(getEntityClass(), "getModificationsBeforeOrEqual_remoteRepository_localRevision"); 053 try { 054 clearFetchGroups(); 055 long startTimestamp = System.currentTimeMillis(); 056 @SuppressWarnings("unchecked") 057 Collection<Modification> modifications = (Collection<Modification>) query.execute(remoteRepository, localRevision); 058 logger.debug("getModificationsBeforeOrEqual: query.execute(...) took {} ms.", System.currentTimeMillis() - startTimestamp); 059 060 fetchPlanBackup.restore(pm); 061 startTimestamp = System.currentTimeMillis(); 062 modifications = load(modifications); 063 logger.debug("getModificationsBeforeOrEqual: Loading result-set with {} elements took {} ms.", modifications.size(), System.currentTimeMillis() - startTimestamp); 064 065 return modifications; 066 } finally { 067 query.closeAll(); 068 fetchPlanBackup.restore(pm); 069 } 070 } 071 072 /** 073 * Gets all {@link Modification}s being assigned to the given {@link Modification#getRemoteRepository() remoteRepository}. 074 * @param remoteRepository the {@link Modification#getRemoteRepository() remoteRepository} the queried modifications are assigned to. 075 * @return those {@link Modification}s matching the given criteria. Never <code>null</code>, but maybe empty. 076 */ 077 public Collection<Modification> getModifications(final RemoteRepository remoteRepository) { 078 return getModificationsAfter(remoteRepository, -1); 079 } 080}