Moving league match cache to separate class.
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
package com.gempukku.lotro.db;
|
||||
|
||||
import com.gempukku.lotro.db.vo.League;
|
||||
import com.gempukku.lotro.db.vo.LeagueMatch;
|
||||
import com.gempukku.lotro.league.LeagueSerieData;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.locks.ReadWriteLock;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
|
||||
public class CachedLeagueMatchDAO implements LeagueMatchDAO {
|
||||
private LeagueMatchDAO _leagueMatchDAO;
|
||||
private ReadWriteLock _readWriteLock = new ReentrantReadWriteLock();
|
||||
|
||||
private Map<String, Collection<LeagueMatch>> _cachedMatches = new ConcurrentHashMap<String, Collection<LeagueMatch>>();
|
||||
|
||||
public CachedLeagueMatchDAO(LeagueMatchDAO leagueMatchDAO) {
|
||||
_leagueMatchDAO = leagueMatchDAO;
|
||||
}
|
||||
|
||||
private String getLeagueCacheKey(League league) {
|
||||
return league.getType();
|
||||
}
|
||||
|
||||
private String getLeagueSerieCacheKey(League league, LeagueSerieData leagueSerie) {
|
||||
int serieIndex = league.getLeagueData().getSeries().indexOf(leagueSerie);
|
||||
return league.getType() + "-serieIndex:" + serieIndex;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<LeagueMatch> getLeagueMatches(League league) {
|
||||
_readWriteLock.readLock().lock();
|
||||
try {
|
||||
Collection<LeagueMatch> leagueMatches = _cachedMatches.get(getLeagueCacheKey(league));
|
||||
if (leagueMatches == null) {
|
||||
_readWriteLock.readLock().unlock();
|
||||
_readWriteLock.writeLock().lock();
|
||||
try {
|
||||
leagueMatches = getLeagueMatchesInWriteLock(league);
|
||||
} finally {
|
||||
_readWriteLock.readLock().lock();
|
||||
_readWriteLock.writeLock().unlock();
|
||||
}
|
||||
}
|
||||
return Collections.unmodifiableCollection(leagueMatches);
|
||||
} finally {
|
||||
_readWriteLock.readLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<LeagueMatch> getLeagueSerieMatches(League league, LeagueSerieData leagueSerie) {
|
||||
_readWriteLock.readLock().lock();
|
||||
try {
|
||||
Collection<LeagueMatch> leagueSerieMatches = _cachedMatches.get(getLeagueSerieCacheKey(league, leagueSerie));
|
||||
if (leagueSerieMatches == null) {
|
||||
_readWriteLock.readLock().unlock();
|
||||
_readWriteLock.writeLock().lock();
|
||||
try {
|
||||
leagueSerieMatches = getLeagueSerieMatchesInWriteLock(league, leagueSerie);
|
||||
} finally {
|
||||
_readWriteLock.readLock().lock();
|
||||
_readWriteLock.writeLock().unlock();
|
||||
}
|
||||
}
|
||||
return Collections.unmodifiableCollection(leagueSerieMatches);
|
||||
} finally {
|
||||
_readWriteLock.readLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
private Collection<LeagueMatch> getLeagueMatchesInWriteLock(League league) {
|
||||
Collection<LeagueMatch> leagueMatches;
|
||||
leagueMatches = _cachedMatches.get(getLeagueCacheKey(league));
|
||||
if (leagueMatches == null) {
|
||||
leagueMatches = _leagueMatchDAO.getLeagueMatches(league);
|
||||
_cachedMatches.put(getLeagueCacheKey(league), leagueMatches);
|
||||
}
|
||||
return leagueMatches;
|
||||
}
|
||||
|
||||
private Collection<LeagueMatch> getLeagueSerieMatchesInWriteLock(League league, LeagueSerieData leagueSerie) {
|
||||
Collection<LeagueMatch> leagueSerieMatches;
|
||||
leagueSerieMatches = _cachedMatches.get(getLeagueSerieCacheKey(league, leagueSerie));
|
||||
if (leagueSerieMatches == null) {
|
||||
leagueSerieMatches = _leagueMatchDAO.getLeagueSerieMatches(league, leagueSerie);
|
||||
_cachedMatches.put(getLeagueSerieCacheKey(league, leagueSerie), leagueSerieMatches);
|
||||
}
|
||||
return leagueSerieMatches;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addPlayedMatch(League league, LeagueSerieData leagueSeason, LeagueMatch match) {
|
||||
_readWriteLock.writeLock().lock();
|
||||
try {
|
||||
getLeagueMatchesInWriteLock(league).add(match);
|
||||
getLeagueSerieMatchesInWriteLock(league, leagueSeason).add(match);
|
||||
_leagueMatchDAO.addPlayedMatch(league, leagueSeason, match);
|
||||
} finally {
|
||||
_readWriteLock.writeLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public void clearCache() {
|
||||
_readWriteLock.writeLock().lock();
|
||||
try {
|
||||
_cachedMatches.clear();
|
||||
} finally {
|
||||
_readWriteLock.writeLock().unlock();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package com.gempukku.lotro.db;
|
||||
|
||||
import com.gempukku.lotro.db.vo.League;
|
||||
import com.gempukku.lotro.db.vo.LeagueMatch;
|
||||
import com.gempukku.lotro.league.LeagueSerieData;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class DbLeagueMatchDAO implements LeagueMatchDAO {
|
||||
private DbAccess _dbAccess;
|
||||
|
||||
public DbLeagueMatchDAO(DbAccess dbAccess) {
|
||||
_dbAccess = dbAccess;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<LeagueMatch> getLeagueMatches(League league) {
|
||||
try {
|
||||
Connection conn = _dbAccess.getDataSource().getConnection();
|
||||
try {
|
||||
PreparedStatement statement = conn.prepareStatement("select winner, loser from league_match where league_type=?");
|
||||
try {
|
||||
statement.setString(1, league.getType());
|
||||
ResultSet rs = statement.executeQuery();
|
||||
try {
|
||||
Set<LeagueMatch> result = new HashSet<LeagueMatch>();
|
||||
while (rs.next()) {
|
||||
String winner = rs.getString(1);
|
||||
String loser = rs.getString(2);
|
||||
|
||||
result.add(new LeagueMatch(winner, loser));
|
||||
}
|
||||
return result;
|
||||
} finally {
|
||||
rs.close();
|
||||
}
|
||||
} finally {
|
||||
statement.close();
|
||||
}
|
||||
} finally {
|
||||
conn.close();
|
||||
}
|
||||
} catch (SQLException exp) {
|
||||
throw new RuntimeException(exp);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<LeagueMatch> getLeagueSerieMatches(League league, LeagueSerieData leagueSerie) {
|
||||
try {
|
||||
Connection conn = _dbAccess.getDataSource().getConnection();
|
||||
try {
|
||||
PreparedStatement statement = conn.prepareStatement("select winner, loser from league_match where league_type=? and season_type=?");
|
||||
try {
|
||||
statement.setString(1, league.getType());
|
||||
statement.setString(2, leagueSerie.getName());
|
||||
ResultSet rs = statement.executeQuery();
|
||||
try {
|
||||
Set<LeagueMatch> result = new HashSet<LeagueMatch>();
|
||||
while (rs.next()) {
|
||||
String winner = rs.getString(1);
|
||||
String loser = rs.getString(2);
|
||||
|
||||
result.add(new LeagueMatch(winner, loser));
|
||||
}
|
||||
return result;
|
||||
} finally {
|
||||
rs.close();
|
||||
}
|
||||
} finally {
|
||||
statement.close();
|
||||
}
|
||||
} finally {
|
||||
conn.close();
|
||||
}
|
||||
} catch (SQLException exp) {
|
||||
throw new RuntimeException(exp);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addPlayedMatch(League league, LeagueSerieData leagueSeason, LeagueMatch match) {
|
||||
try {
|
||||
Connection conn = _dbAccess.getDataSource().getConnection();
|
||||
try {
|
||||
PreparedStatement statement = conn.prepareStatement("insert into league_match (league_type, season_type, winner, loser) values (?, ?, ?, ?)");
|
||||
try {
|
||||
statement.setString(1, league.getType());
|
||||
statement.setString(2, leagueSeason.getName());
|
||||
statement.setString(3, match.getWinner());
|
||||
statement.setString(4, match.getLoser());
|
||||
statement.execute();
|
||||
} finally {
|
||||
statement.close();
|
||||
}
|
||||
} finally {
|
||||
conn.close();
|
||||
}
|
||||
} catch (SQLException exp) {
|
||||
throw new RuntimeException(exp);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,106 +1,15 @@
|
||||
package com.gempukku.lotro.db;
|
||||
|
||||
import com.gempukku.lotro.db.vo.League;
|
||||
import com.gempukku.lotro.db.vo.LeagueMatch;
|
||||
import com.gempukku.lotro.league.LeagueSerieData;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class LeagueMatchDAO {
|
||||
private DbAccess _dbAccess;
|
||||
|
||||
public LeagueMatchDAO(DbAccess dbAccess) {
|
||||
_dbAccess = dbAccess;
|
||||
}
|
||||
|
||||
public Collection<LeagueMatch> getLeagueMatches(League league) {
|
||||
try {
|
||||
Connection conn = _dbAccess.getDataSource().getConnection();
|
||||
try {
|
||||
PreparedStatement statement = conn.prepareStatement("select winner, loser from league_match where league_type=?");
|
||||
try {
|
||||
statement.setString(1, league.getType());
|
||||
ResultSet rs = statement.executeQuery();
|
||||
try {
|
||||
Set<LeagueMatch> result = new HashSet<LeagueMatch>();
|
||||
while (rs.next()) {
|
||||
String winner = rs.getString(1);
|
||||
String loser = rs.getString(2);
|
||||
|
||||
result.add(new LeagueMatch(winner, loser));
|
||||
}
|
||||
return result;
|
||||
} finally {
|
||||
rs.close();
|
||||
}
|
||||
} finally {
|
||||
statement.close();
|
||||
}
|
||||
} finally {
|
||||
conn.close();
|
||||
}
|
||||
} catch (SQLException exp) {
|
||||
throw new RuntimeException(exp);
|
||||
}
|
||||
}
|
||||
|
||||
public Collection<LeagueMatch> getLeagueSerieMatches(League league, LeagueSerieData leagueSerie) {
|
||||
try {
|
||||
Connection conn = _dbAccess.getDataSource().getConnection();
|
||||
try {
|
||||
PreparedStatement statement = conn.prepareStatement("select winner, loser from league_match where league_type=? and season_type=?");
|
||||
try {
|
||||
statement.setString(1, league.getType());
|
||||
statement.setString(2, leagueSerie.getName());
|
||||
ResultSet rs = statement.executeQuery();
|
||||
try {
|
||||
Set<LeagueMatch> result = new HashSet<LeagueMatch>();
|
||||
while (rs.next()) {
|
||||
String winner = rs.getString(1);
|
||||
String loser = rs.getString(2);
|
||||
|
||||
result.add(new LeagueMatch(winner, loser));
|
||||
}
|
||||
return result;
|
||||
} finally {
|
||||
rs.close();
|
||||
}
|
||||
} finally {
|
||||
statement.close();
|
||||
}
|
||||
} finally {
|
||||
conn.close();
|
||||
}
|
||||
} catch (SQLException exp) {
|
||||
throw new RuntimeException(exp);
|
||||
}
|
||||
}
|
||||
|
||||
public void addPlayedMatch(League league, LeagueSerieData leagueSeason, LeagueMatch match) {
|
||||
try {
|
||||
Connection conn = _dbAccess.getDataSource().getConnection();
|
||||
try {
|
||||
PreparedStatement statement = conn.prepareStatement("insert into league_match (league_type, season_type, winner, loser) values (?, ?, ?, ?)");
|
||||
try {
|
||||
statement.setString(1, league.getType());
|
||||
statement.setString(2, leagueSeason.getName());
|
||||
statement.setString(3, match.getWinner());
|
||||
statement.setString(4, match.getLoser());
|
||||
statement.execute();
|
||||
} finally {
|
||||
statement.close();
|
||||
}
|
||||
} finally {
|
||||
conn.close();
|
||||
}
|
||||
} catch (SQLException exp) {
|
||||
throw new RuntimeException(exp);
|
||||
}
|
||||
}
|
||||
}
|
||||
package com.gempukku.lotro.db;
|
||||
|
||||
import com.gempukku.lotro.db.vo.League;
|
||||
import com.gempukku.lotro.db.vo.LeagueMatch;
|
||||
import com.gempukku.lotro.league.LeagueSerieData;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
public interface LeagueMatchDAO {
|
||||
public Collection<LeagueMatch> getLeagueMatches(League league);
|
||||
|
||||
public Collection<LeagueMatch> getLeagueSerieMatches(League league, LeagueSerieData leagueSerie);
|
||||
|
||||
public void addPlayedMatch(League league, LeagueSerieData leagueSeason, LeagueMatch match);
|
||||
}
|
||||
|
||||
@@ -5,10 +5,7 @@ import com.gempukku.lotro.PlayerStanding;
|
||||
import com.gempukku.lotro.cache.ExpireObjectCache;
|
||||
import com.gempukku.lotro.cache.Producable;
|
||||
import com.gempukku.lotro.collection.CollectionsManager;
|
||||
import com.gempukku.lotro.db.LeagueDAO;
|
||||
import com.gempukku.lotro.db.LeagueMatchDAO;
|
||||
import com.gempukku.lotro.db.LeagueParticipationDAO;
|
||||
import com.gempukku.lotro.db.LeaguePointsDAO;
|
||||
import com.gempukku.lotro.db.*;
|
||||
import com.gempukku.lotro.db.vo.CollectionType;
|
||||
import com.gempukku.lotro.db.vo.League;
|
||||
import com.gempukku.lotro.db.vo.LeagueMatch;
|
||||
@@ -21,7 +18,6 @@ import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
|
||||
public class LeagueService {
|
||||
private Comparator<PlayerStanding> LEAGUE_STANDING_COMPARATOR =
|
||||
@@ -33,7 +29,7 @@ public class LeagueService {
|
||||
private LeagueDAO _leagueDao;
|
||||
private LeaguePointsDAO _leaguePointsDao;
|
||||
// Cached on this layer
|
||||
private LeagueMatchDAO _leagueMatchDao;
|
||||
private CachedLeagueMatchDAO _leagueMatchDao;
|
||||
private LeagueParticipationDAO _leagueParticipationDAO;
|
||||
private CollectionsManager _collectionsManager;
|
||||
|
||||
@@ -43,9 +39,6 @@ public class LeagueService {
|
||||
private Map<League, Set<String>> _playersParticipating = new ConcurrentHashMap<League, Set<String>>();
|
||||
private Map<League, Set<String>> _playersNotParticipating = new ConcurrentHashMap<League, Set<String>>();
|
||||
|
||||
private ExpireObjectCache<League, Collection<LeagueMatch>> _cachedLeagueMatches = new ExpireObjectCache<League, Collection<LeagueMatch>>();
|
||||
private ExpireObjectCache<LeagueSerieData, Collection<LeagueMatch>> _cachedSerieMatches = new ExpireObjectCache<LeagueSerieData, Collection<LeagueMatch>>();
|
||||
|
||||
private ExpireObjectCache<League, Map<String, LeaguePointsDAO.Points>> _cachedLeaguePoints = new ExpireObjectCache<League, Map<String, LeaguePointsDAO.Points>>();
|
||||
private ExpireObjectCache<LeagueSerieData, Map<String, LeaguePointsDAO.Points>> _cachedSeriePoints = new ExpireObjectCache<LeagueSerieData, Map<String, LeaguePointsDAO.Points>>();
|
||||
|
||||
@@ -56,7 +49,7 @@ public class LeagueService {
|
||||
LeagueParticipationDAO leagueParticipationDAO, CollectionsManager collectionsManager) {
|
||||
_leagueDao = leagueDao;
|
||||
_leaguePointsDao = leaguePointsDao;
|
||||
_leagueMatchDao = leagueMatchDao;
|
||||
_leagueMatchDao = new CachedLeagueMatchDAO(leagueMatchDao);
|
||||
_leagueParticipationDAO = leagueParticipationDAO;
|
||||
_collectionsManager = collectionsManager;
|
||||
}
|
||||
@@ -66,18 +59,16 @@ public class LeagueService {
|
||||
_leagueStandings.clear();
|
||||
_activeLeaguesLoadedDate = 0;
|
||||
|
||||
_cachedLeagueMatches.clearCache();
|
||||
_leagueMatchDao.clearCache();
|
||||
_cachedLeaguePoints.clearCache();
|
||||
_cachedSerieMatches.clearCache();
|
||||
_cachedSeriePoints.clearCache();
|
||||
}
|
||||
|
||||
private synchronized void ensureLoadedCurrentLeagues() {
|
||||
int currentDate = DateUtils.getCurrentDate();
|
||||
if (currentDate != _activeLeaguesLoadedDate) {
|
||||
_cachedLeagueMatches.clearCache();
|
||||
_leagueMatchDao.clearCache();
|
||||
_cachedLeaguePoints.clearCache();
|
||||
_cachedSerieMatches.clearCache();
|
||||
_cachedSeriePoints.clearCache();
|
||||
|
||||
try {
|
||||
@@ -92,26 +83,6 @@ public class LeagueService {
|
||||
}
|
||||
}
|
||||
|
||||
private Collection<LeagueMatch> getLeagueMatches(final League league) {
|
||||
return _cachedLeagueMatches.getCachedObject(league,
|
||||
new Producable<League, Collection<LeagueMatch>>() {
|
||||
@Override
|
||||
public Collection<LeagueMatch> produce(League key) {
|
||||
return new CopyOnWriteArraySet<LeagueMatch>(_leagueMatchDao.getLeagueMatches(league));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private Collection<LeagueMatch> getLeagueSerieMatches(final League league, final LeagueSerieData serie) {
|
||||
return _cachedSerieMatches.getCachedObject(serie,
|
||||
new Producable<LeagueSerieData, Collection<LeagueMatch>>() {
|
||||
@Override
|
||||
public Collection<LeagueMatch> produce(LeagueSerieData key) {
|
||||
return new CopyOnWriteArraySet<LeagueMatch>(_leagueMatchDao.getLeagueSerieMatches(league, serie));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private Map<String, LeaguePointsDAO.Points> getLeaguePoints(final League league) {
|
||||
return _cachedLeaguePoints.getCachedObject(league,
|
||||
new Producable<League, Map<String, LeaguePointsDAO.Points>>() {
|
||||
@@ -276,12 +247,8 @@ public class LeagueService {
|
||||
}
|
||||
|
||||
private void addMatch(League league, LeagueSerieData serie, String winner, String loser) {
|
||||
Collection<LeagueMatch> leagueMatches = getLeagueMatches(league);
|
||||
Collection<LeagueMatch> leagueSerieMatches = getLeagueSerieMatches(league, serie);
|
||||
LeagueMatch match = new LeagueMatch(winner, loser);
|
||||
_leagueMatchDao.addPlayedMatch(league, serie, match);
|
||||
leagueMatches.add(match);
|
||||
leagueSerieMatches.add(match);
|
||||
}
|
||||
|
||||
private void awardPrizesToPlayer(League league, LeagueSerieData serie, String player, boolean winner) {
|
||||
@@ -302,7 +269,7 @@ public class LeagueService {
|
||||
}
|
||||
|
||||
private Collection<LeagueMatch> getPlayerMatchesInSerie(League league, LeagueSerieData serie, String player) {
|
||||
final Collection<LeagueMatch> allMatches = getLeagueSerieMatches(league, serie);
|
||||
final Collection<LeagueMatch> allMatches = _leagueMatchDao.getLeagueSerieMatches(league, serie);
|
||||
Set<LeagueMatch> result = new HashSet<LeagueMatch>();
|
||||
for (LeagueMatch match : allMatches) {
|
||||
if (match.getWinner().equals(player) || match.getLoser().equals(player))
|
||||
@@ -335,14 +302,14 @@ public class LeagueService {
|
||||
|
||||
private List<PlayerStanding> createLeagueSerieStandings(League league, LeagueSerieData leagueSerie) {
|
||||
final Map<String, LeaguePointsDAO.Points> points = getLeagueSeriePoints(league, leagueSerie);
|
||||
final Collection<LeagueMatch> matches = getLeagueSerieMatches(league, leagueSerie);
|
||||
final Collection<LeagueMatch> matches = _leagueMatchDao.getLeagueSerieMatches(league, leagueSerie);
|
||||
|
||||
return createStandingsForMatchesAndPoints(points, matches);
|
||||
}
|
||||
|
||||
private List<PlayerStanding> createLeagueStandings(League league) {
|
||||
final Map<String, LeaguePointsDAO.Points> points = getLeaguePoints(league);
|
||||
final Collection<LeagueMatch> matches = getLeagueMatches(league);
|
||||
final Collection<LeagueMatch> matches = _leagueMatchDao.getLeagueMatches(league);
|
||||
|
||||
return createStandingsForMatchesAndPoints(points, matches);
|
||||
}
|
||||
|
||||
@@ -88,7 +88,7 @@ public class DaoProvider implements InjectableProvider<Context, Type> {
|
||||
|
||||
private synchronized Injectable<LeagueMatchDAO> getLeagueMatchDaoSafely() {
|
||||
if (_leagueMatchDAOInjectable == null) {
|
||||
final LeagueMatchDAO leagueMatchDao = new LeagueMatchDAO(_dbAccess);
|
||||
final LeagueMatchDAO leagueMatchDao = new DbLeagueMatchDAO(_dbAccess);
|
||||
_leagueMatchDAOInjectable = new Injectable<LeagueMatchDAO>() {
|
||||
@Override
|
||||
public LeagueMatchDAO getValue() {
|
||||
|
||||
Reference in New Issue
Block a user