From 6265c6f01ae59bc583631ea9897b60c98aaff698 Mon Sep 17 00:00:00 2001 From: "marcins78@gmail.com" Date: Wed, 2 May 2012 12:14:21 +0000 Subject: [PATCH] Moving league match cache to separate class. --- .../lotro/db/CachedLeagueMatchDAO.java | 115 +++++++++++++++++ .../gempukku/lotro/db/DbLeagueMatchDAO.java | 109 ++++++++++++++++ .../com/gempukku/lotro/db/LeagueMatchDAO.java | 121 +++--------------- .../gempukku/lotro/league/LeagueService.java | 49 ++----- .../lotro/server/provider/DaoProvider.java | 2 +- 5 files changed, 248 insertions(+), 148 deletions(-) create mode 100644 gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/CachedLeagueMatchDAO.java create mode 100644 gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/DbLeagueMatchDAO.java diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/CachedLeagueMatchDAO.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/CachedLeagueMatchDAO.java new file mode 100644 index 000000000..10520473c --- /dev/null +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/CachedLeagueMatchDAO.java @@ -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> _cachedMatches = new ConcurrentHashMap>(); + + 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 getLeagueMatches(League league) { + _readWriteLock.readLock().lock(); + try { + Collection 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 getLeagueSerieMatches(League league, LeagueSerieData leagueSerie) { + _readWriteLock.readLock().lock(); + try { + Collection 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 getLeagueMatchesInWriteLock(League league) { + Collection leagueMatches; + leagueMatches = _cachedMatches.get(getLeagueCacheKey(league)); + if (leagueMatches == null) { + leagueMatches = _leagueMatchDAO.getLeagueMatches(league); + _cachedMatches.put(getLeagueCacheKey(league), leagueMatches); + } + return leagueMatches; + } + + private Collection getLeagueSerieMatchesInWriteLock(League league, LeagueSerieData leagueSerie) { + Collection 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(); + } + } +} diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/DbLeagueMatchDAO.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/DbLeagueMatchDAO.java new file mode 100644 index 000000000..a05762105 --- /dev/null +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/DbLeagueMatchDAO.java @@ -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 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 result = new HashSet(); + 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 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 result = new HashSet(); + 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); + } + } +} diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/LeagueMatchDAO.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/LeagueMatchDAO.java index 7faecd639..daf96f222 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/LeagueMatchDAO.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/LeagueMatchDAO.java @@ -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 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 result = new HashSet(); - 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 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 result = new HashSet(); - 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 getLeagueMatches(League league); + + public Collection getLeagueSerieMatches(League league, LeagueSerieData leagueSerie); + + public void addPlayedMatch(League league, LeagueSerieData leagueSeason, LeagueMatch match); +} diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/league/LeagueService.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/league/LeagueService.java index faa42c8aa..35cf06713 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/league/LeagueService.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/league/LeagueService.java @@ -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 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> _playersParticipating = new ConcurrentHashMap>(); private Map> _playersNotParticipating = new ConcurrentHashMap>(); - private ExpireObjectCache> _cachedLeagueMatches = new ExpireObjectCache>(); - private ExpireObjectCache> _cachedSerieMatches = new ExpireObjectCache>(); - private ExpireObjectCache> _cachedLeaguePoints = new ExpireObjectCache>(); private ExpireObjectCache> _cachedSeriePoints = new ExpireObjectCache>(); @@ -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 getLeagueMatches(final League league) { - return _cachedLeagueMatches.getCachedObject(league, - new Producable>() { - @Override - public Collection produce(League key) { - return new CopyOnWriteArraySet(_leagueMatchDao.getLeagueMatches(league)); - } - }); - } - - private Collection getLeagueSerieMatches(final League league, final LeagueSerieData serie) { - return _cachedSerieMatches.getCachedObject(serie, - new Producable>() { - @Override - public Collection produce(LeagueSerieData key) { - return new CopyOnWriteArraySet(_leagueMatchDao.getLeagueSerieMatches(league, serie)); - } - }); - } - private Map getLeaguePoints(final League league) { return _cachedLeaguePoints.getCachedObject(league, new Producable>() { @@ -276,12 +247,8 @@ public class LeagueService { } private void addMatch(League league, LeagueSerieData serie, String winner, String loser) { - Collection leagueMatches = getLeagueMatches(league); - Collection 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 getPlayerMatchesInSerie(League league, LeagueSerieData serie, String player) { - final Collection allMatches = getLeagueSerieMatches(league, serie); + final Collection allMatches = _leagueMatchDao.getLeagueSerieMatches(league, serie); Set result = new HashSet(); for (LeagueMatch match : allMatches) { if (match.getWinner().equals(player) || match.getLoser().equals(player)) @@ -335,14 +302,14 @@ public class LeagueService { private List createLeagueSerieStandings(League league, LeagueSerieData leagueSerie) { final Map points = getLeagueSeriePoints(league, leagueSerie); - final Collection matches = getLeagueSerieMatches(league, leagueSerie); + final Collection matches = _leagueMatchDao.getLeagueSerieMatches(league, leagueSerie); return createStandingsForMatchesAndPoints(points, matches); } private List createLeagueStandings(League league) { final Map points = getLeaguePoints(league); - final Collection matches = getLeagueMatches(league); + final Collection matches = _leagueMatchDao.getLeagueMatches(league); return createStandingsForMatchesAndPoints(points, matches); } diff --git a/gemp-lotr/gemp-lotr-web-server/src/main/java/com/gempukku/lotro/server/provider/DaoProvider.java b/gemp-lotr/gemp-lotr-web-server/src/main/java/com/gempukku/lotro/server/provider/DaoProvider.java index 15ffefe11..e38c35cbf 100644 --- a/gemp-lotr/gemp-lotr-web-server/src/main/java/com/gempukku/lotro/server/provider/DaoProvider.java +++ b/gemp-lotr/gemp-lotr-web-server/src/main/java/com/gempukku/lotro/server/provider/DaoProvider.java @@ -88,7 +88,7 @@ public class DaoProvider implements InjectableProvider { private synchronized Injectable getLeagueMatchDaoSafely() { if (_leagueMatchDAOInjectable == null) { - final LeagueMatchDAO leagueMatchDao = new LeagueMatchDAO(_dbAccess); + final LeagueMatchDAO leagueMatchDao = new DbLeagueMatchDAO(_dbAccess); _leagueMatchDAOInjectable = new Injectable() { @Override public LeagueMatchDAO getValue() {