Removing one database table from use.

This commit is contained in:
marcins78@gmail.com
2012-05-04 14:59:24 +00:00
parent fa4838a2a2
commit 9a845c8ebb
7 changed files with 41 additions and 329 deletions

View File

@@ -1,104 +0,0 @@
package com.gempukku.lotro.db;
import com.gempukku.lotro.db.vo.League;
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.HashMap;
import java.util.Map;
public class DbLeaguePointsDAO implements LeaguePointsDAO {
private DbAccess _dbAccess;
public DbLeaguePointsDAO(DbAccess dbAccess) {
_dbAccess = dbAccess;
}
@Override
public void addPoints(League league, LeagueSerieData serie, String playerName, int points) {
try {
Connection conn = _dbAccess.getDataSource().getConnection();
try {
PreparedStatement statement = conn.prepareStatement("insert into league_points (league_type, season_type, player_name, points) values (?, ?, ?, ?)");
try {
statement.setString(1, league.getType());
statement.setString(2, serie.getName());
statement.setString(3, playerName);
statement.setInt(4, points);
statement.execute();
} finally {
statement.close();
}
} finally {
conn.close();
}
} catch (SQLException exp) {
throw new RuntimeException(exp);
}
}
@Override
public Map<String, Points> getLeaguePoints(League league) {
try {
Connection conn = _dbAccess.getDataSource().getConnection();
try {
PreparedStatement statement = conn.prepareStatement("select player_name, sum(points), count(*) from league_points where league_type=? group by player_name order by 2 desc, 3 asc");
try {
statement.setString(1, league.getType());
ResultSet rs = statement.executeQuery();
try {
return createPoints(rs);
} finally {
rs.close();
}
} finally {
statement.close();
}
} finally {
conn.close();
}
} catch (SQLException exp) {
throw new RuntimeException(exp);
}
}
private Map<String, Points> createPoints(ResultSet rs) throws SQLException {
Map<String, Points> result = new HashMap<String, Points>();
while (rs.next()) {
String playerName = rs.getString(1);
int sumPoints = rs.getInt(2);
int gamesPlayed = rs.getInt(3);
result.put(playerName, new Points(sumPoints, gamesPlayed));
}
return result;
}
@Override
public Map<String, Points> getLeagueSeriePoints(League league, LeagueSerieData serie) {
try {
Connection conn = _dbAccess.getDataSource().getConnection();
try {
PreparedStatement statement = conn.prepareStatement("select player_name, sum(points), count(*) from league_points where league_type=? and season_type=? group by player_name order by 2 desc, 3 asc");
try {
statement.setString(1, league.getType());
statement.setString(2, serie.getName());
ResultSet rs = statement.executeQuery();
try {
return createPoints(rs);
} finally {
rs.close();
}
} finally {
statement.close();
}
} finally {
conn.close();
}
} catch (SQLException exp) {
throw new RuntimeException(exp);
}
}
}

View File

@@ -1,32 +0,0 @@
package com.gempukku.lotro.db;
import com.gempukku.lotro.db.vo.League;
import com.gempukku.lotro.league.LeagueSerieData;
import java.util.Map;
public interface LeaguePointsDAO {
public void addPoints(League league, LeagueSerieData serie, String playerName, int points);
public Map<String, Points> getLeaguePoints(League league);
public Map<String, Points> getLeagueSeriePoints(League league, LeagueSerieData serie);
public static class Points {
private final int _points;
private final int _gamesPlayed;
public Points(int points, int gamesPlayed) {
_points = points;
_gamesPlayed = gamesPlayed;
}
public int getPoints() {
return _points;
}
public int getGamesPlayed() {
return _gamesPlayed;
}
}
}

View File

@@ -1,116 +0,0 @@
package com.gempukku.lotro.league;
import com.gempukku.lotro.db.LeaguePointsDAO;
import com.gempukku.lotro.db.vo.League;
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 CachedLeaguePointsDAO implements LeaguePointsDAO {
private LeaguePointsDAO _leaguePointsDAO;
private Map<String, Map<String, Points>> _cachedPoints = new ConcurrentHashMap<String, Map<String, Points>>();
private ReadWriteLock _readWriteLock = new ReentrantReadWriteLock();
public CachedLeaguePointsDAO(LeaguePointsDAO leaguePointsDAO) {
_leaguePointsDAO = leaguePointsDAO;
}
@Override
public void addPoints(League league, LeagueSerieData serie, String playerName, int points) {
_readWriteLock.writeLock().lock();
try {
Map<String, Points> leaguePointsInWriteLock = getLeaguePointsInWriteLock(league);
Points previousPointsInLeague = leaguePointsInWriteLock.get(playerName);
Points newLeaguePoints = new Points(
points + ((previousPointsInLeague != null) ? previousPointsInLeague.getPoints() : 0),
1 + ((previousPointsInLeague != null) ? previousPointsInLeague.getGamesPlayed() : 0));
leaguePointsInWriteLock.put(playerName, newLeaguePoints);
Map<String, Points> leagueSeriePointsInWriteLock = getLeagueSeriePointsInWriteLock(league, serie);
Points previousPointsInLeagueSerie = leagueSeriePointsInWriteLock.get(playerName);
Points newSeriePoints = new Points(
points + ((previousPointsInLeagueSerie != null) ? previousPointsInLeagueSerie.getPoints() : 0),
1 + ((previousPointsInLeagueSerie != null) ? previousPointsInLeagueSerie.getGamesPlayed() : 0));
leagueSeriePointsInWriteLock.put(playerName, newSeriePoints);
_leaguePointsDAO.addPoints(league, serie, playerName, points);
} finally {
_readWriteLock.writeLock().unlock();
}
}
@Override
public Map<String, Points> getLeaguePoints(League league) {
_readWriteLock.readLock().lock();
try {
Map<String, Points> leaguePoints = _cachedPoints.get(LeagueMapKeys.getLeagueMapKey(league));
if (leaguePoints == null) {
_readWriteLock.readLock().unlock();
_readWriteLock.writeLock().lock();
try {
leaguePoints = getLeaguePointsInWriteLock(league);
} finally {
_readWriteLock.readLock().lock();
_readWriteLock.writeLock().unlock();
}
}
return Collections.unmodifiableMap(leaguePoints);
} finally {
_readWriteLock.readLock().unlock();
}
}
@Override
public Map<String, Points> getLeagueSeriePoints(League league, LeagueSerieData serie) {
_readWriteLock.readLock().lock();
try {
Map<String, Points> leagueMatches = _cachedPoints.get(LeagueMapKeys.getLeagueMapKey(league));
if (leagueMatches == null) {
_readWriteLock.readLock().unlock();
_readWriteLock.writeLock().lock();
try {
leagueMatches = getLeaguePointsInWriteLock(league);
} finally {
_readWriteLock.readLock().lock();
_readWriteLock.writeLock().unlock();
}
}
return Collections.unmodifiableMap(leagueMatches);
} finally {
_readWriteLock.readLock().unlock();
}
}
private Map<String, Points> getLeaguePointsInWriteLock(League league) {
Map<String, Points> leaguePoints;
leaguePoints = _cachedPoints.get(LeagueMapKeys.getLeagueMapKey(league));
if (leaguePoints == null) {
leaguePoints = new ConcurrentHashMap<String, Points>(_leaguePointsDAO.getLeaguePoints(league));
_cachedPoints.put(LeagueMapKeys.getLeagueMapKey(league), leaguePoints);
}
return leaguePoints;
}
private Map<String, Points> getLeagueSeriePointsInWriteLock(League league, LeagueSerieData leagueSerie) {
Map<String, Points> leagueSeriePoints;
leagueSeriePoints = _cachedPoints.get(LeagueMapKeys.getLeagueSerieMapKey(league, leagueSerie));
if (leagueSeriePoints == null) {
leagueSeriePoints = new ConcurrentHashMap<String, Points>(_leaguePointsDAO.getLeagueSeriePoints(league, leagueSerie));
_cachedPoints.put(LeagueMapKeys.getLeagueSerieMapKey(league, leagueSerie), leagueSeriePoints);
}
return leagueSeriePoints;
}
public void clearCache() {
_readWriteLock.writeLock().lock();
try {
_cachedPoints.clear();
} finally {
_readWriteLock.writeLock().unlock();
}
}
}

View File

@@ -6,7 +6,6 @@ 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.vo.CollectionType;
import com.gempukku.lotro.db.vo.League;
import com.gempukku.lotro.db.vo.LeagueMatch;
@@ -19,6 +18,7 @@ import java.io.IOException;
import java.sql.SQLException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
public class LeagueService {
private Comparator<PlayerStanding> LEAGUE_STANDING_COMPARATOR =
@@ -30,7 +30,6 @@ public class LeagueService {
private LeagueDAO _leagueDao;
// Cached on this layer
private CachedLeaguePointsDAO _leaguePointsDao;
private CachedLeagueMatchDAO _leagueMatchDao;
private CachedLeagueParticipationDAO _leagueParticipationDAO;
@@ -42,10 +41,9 @@ public class LeagueService {
private int _activeLeaguesLoadedDate;
private List<League> _activeLeagues;
public LeagueService(LeagueDAO leagueDao, LeaguePointsDAO leaguePointsDao, LeagueMatchDAO leagueMatchDao,
public LeagueService(LeagueDAO leagueDao, LeagueMatchDAO leagueMatchDao,
LeagueParticipationDAO leagueParticipationDAO, CollectionsManager collectionsManager) {
_leagueDao = leagueDao;
_leaguePointsDao = new CachedLeaguePointsDAO(leaguePointsDao);
_leagueMatchDao = new CachedLeagueMatchDAO(leagueMatchDao);
_leagueParticipationDAO = new CachedLeagueParticipationDAO(leagueParticipationDAO);
_collectionsManager = collectionsManager;
@@ -57,7 +55,6 @@ public class LeagueService {
_activeLeaguesLoadedDate = 0;
_leagueMatchDao.clearCache();
_leaguePointsDao.clearCache();
_leagueParticipationDAO.clearCache();
}
@@ -65,7 +62,6 @@ public class LeagueService {
int currentDate = DateUtils.getCurrentDate();
if (currentDate != _activeLeaguesLoadedDate) {
_leagueMatchDao.clearCache();
_leaguePointsDao.clearCache();
_leagueParticipationDAO.clearCache();
try {
@@ -148,9 +144,6 @@ public class LeagueService {
public void reportLeagueGameResult(League league, LeagueSerieData serie, String winner, String loser) {
_leagueMatchDao.addPlayedMatch(league, serie, winner, loser);
_leaguePointsDao.addPoints(league, serie, winner, 2);
_leaguePointsDao.addPoints(league, serie, loser, 1);
_leagueStandings.remove(LeagueMapKeys.getLeagueMapKey(league));
_leagueSerieStandings.remove(LeagueMapKeys.getLeagueSerieMapKey(league, serie));
@@ -208,7 +201,7 @@ public class LeagueService {
}
private List<PlayerStanding> createLeagueSerieStandings(League league, LeagueSerieData leagueSerie) {
final Map<String, LeaguePointsDAO.Points> points = _leaguePointsDao.getLeagueSeriePoints(league, leagueSerie);
final Collection<String> playersParticipating = _leagueParticipationDAO.getUsersParticipating(league);
final Collection<LeagueMatch> matches = _leagueMatchDao.getLeagueMatches(league);
Set<LeagueMatch> matchesInSerie = new HashSet<LeagueMatch>();
@@ -217,37 +210,51 @@ public class LeagueService {
matchesInSerie.add(match);
}
return createStandingsForMatchesAndPoints(points, matchesInSerie);
return createStandingsForMatchesAndPoints(playersParticipating, matchesInSerie);
}
private List<PlayerStanding> createLeagueStandings(League league) {
final Map<String, LeaguePointsDAO.Points> points = _leaguePointsDao.getLeaguePoints(league);
final Collection<String> playersParticipating = _leagueParticipationDAO.getUsersParticipating(league);
final Collection<LeagueMatch> matches = _leagueMatchDao.getLeagueMatches(league);
return createStandingsForMatchesAndPoints(points, matches);
return createStandingsForMatchesAndPoints(playersParticipating, matches);
}
private List<PlayerStanding> createStandingsForMatchesAndPoints(Map<String, LeaguePointsDAO.Points> points, Collection<LeagueMatch> matches) {
private List<PlayerStanding> createStandingsForMatchesAndPoints(Collection<String> playersParticipating, Collection<LeagueMatch> matches) {
Map<String, List<String>> playerOpponents = new HashMap<String, List<String>>();
Map<String, Integer> playerWins = new HashMap<String, Integer>();
Map<String, Integer> playerLoss = new HashMap<String, Integer>();
Map<String, AtomicInteger> playerWinCounts = new HashMap<String, AtomicInteger>();
Map<String, AtomicInteger> playerLossCounts = new HashMap<String, AtomicInteger>();
// Initialize the list
for (String playerName : playersParticipating) {
playerOpponents.put(playerName, new ArrayList<String>());
playerWinCounts.put(playerName, new AtomicInteger(0));
playerLossCounts.put(playerName, new AtomicInteger(0));
}
for (LeagueMatch leagueMatch : matches) {
appendPlayer(playerOpponents, leagueMatch.getWinner(), leagueMatch.getLoser());
appendPlayer(playerOpponents, leagueMatch.getLoser(), leagueMatch.getWinner());
appendMatch(playerWins, playerLoss, leagueMatch.getWinner(), leagueMatch.getLoser());
playerOpponents.get(leagueMatch.getWinner()).add(leagueMatch.getLoser());
playerOpponents.get(leagueMatch.getLoser()).add(leagueMatch.getWinner());
playerWinCounts.get(leagueMatch.getWinner()).incrementAndGet();
playerLossCounts.get(leagueMatch.getLoser()).incrementAndGet();
}
List<PlayerStanding> leagueStandings = new LinkedList<PlayerStanding>();
for (Map.Entry<String, LeaguePointsDAO.Points> playerPoints : points.entrySet()) {
PlayerStanding standing = new PlayerStanding(playerPoints.getKey(), playerPoints.getValue().getPoints(), playerPoints.getValue().getGamesPlayed());
List<String> opponents = playerOpponents.get(playerPoints.getKey());
for (String playerName : playersParticipating) {
int points = playerWinCounts.get(playerName).intValue() * 2 + playerLossCounts.get(playerName).intValue();
int gamesPlayed = playerWinCounts.get(playerName).intValue() + playerLossCounts.get(playerName).intValue();
PlayerStanding standing = new PlayerStanding(playerName, points, gamesPlayed);
List<String> opponents = playerOpponents.get(playerName);
int opponentWins = 0;
int opponentGames = 0;
for (String opponent : opponents) {
opponentWins += playerWins.get(opponent);
opponentGames += playerWins.get(opponent) + playerLoss.get(opponent);
opponentWins += playerWinCounts.get(opponent).intValue();
opponentGames += playerWinCounts.get(opponent).intValue() + playerLossCounts.get(opponent).intValue();
}
standing.setOpponentWin(opponentWins * 1f / opponentGames);
if (opponentGames != 0)
standing.setOpponentWin(opponentWins * 1f / opponentGames);
else
standing.setOpponentWin(0f);
leagueStandings.add(standing);
}
@@ -266,32 +273,6 @@ public class LeagueService {
return leagueStandings;
}
private void appendMatch(Map<String, Integer> playerWins, Map<String, Integer> playerLoss, String winner, String loser) {
append(playerWins, winner);
append(playerLoss, loser);
if (!playerWins.containsKey(loser))
playerWins.put(loser, 0);
if (!playerLoss.containsKey(winner))
playerLoss.put(winner, 0);
}
private void append(Map<String, Integer> playerCounts, String player) {
Integer count = playerCounts.get(player);
if (count == null)
count = 0;
count++;
playerCounts.put(player, count);
}
private void appendPlayer(Map<String, List<String>> playerOpponents, String player, String opponent) {
List<String> opponents = playerOpponents.get(player);
if (opponents == null) {
opponents = new LinkedList<String>();
playerOpponents.put(player, opponents);
}
opponents.add(opponent);
}
public boolean canPlayRankedGame(League league, LeagueSerieData season, String player) {
int maxMatches = season.getMaxMatches();
Collection<LeagueMatch> playedInSeason = getPlayerMatchesInSerie(league, season, player);

View File

@@ -5,7 +5,6 @@ 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.vo.League;
import com.gempukku.lotro.db.vo.LeagueMatch;
import org.junit.Test;
@@ -37,7 +36,6 @@ public class LeagueServiceTest {
Mockito.when(leagueDao.loadActiveLeagues(Mockito.anyInt())).thenReturn(leagues);
LeaguePointsDAO leaguePointsDAO = Mockito.mock(LeaguePointsDAO.class);
LeagueMatchDAO leagueMatchDAO = Mockito.mock(LeagueMatchDAO.class);
Set<LeagueMatch> matches = new HashSet<LeagueMatch>();
@@ -47,7 +45,7 @@ public class LeagueServiceTest {
LeagueParticipationDAO leagueParticipationDAO = Mockito.mock(LeagueParticipationDAO.class);
CollectionsManager collectionsManager = Mockito.mock(CollectionsManager.class);
LeagueService leagueService = new LeagueService(leagueDao, leaguePointsDAO, leagueMatchDAO, leagueParticipationDAO, collectionsManager);
LeagueService leagueService = new LeagueService(leagueDao, leagueMatchDAO, leagueParticipationDAO, collectionsManager);
assertTrue(leagueService.canPlayRankedGame(league, leagueSerie, "player1"));
assertTrue(leagueService.canPlayRankedGameAgainst(league, leagueSerie, "player1", "player2"));
@@ -91,7 +89,6 @@ public class LeagueServiceTest {
Mockito.when(leagueDao.loadActiveLeagues(Mockito.anyInt())).thenReturn(leagues);
LeaguePointsDAO leaguePointsDAO = Mockito.mock(LeaguePointsDAO.class);
LeagueMatchDAO leagueMatchDAO = Mockito.mock(LeagueMatchDAO.class);
Set<LeagueMatch> matches = new HashSet<LeagueMatch>();
@@ -102,7 +99,7 @@ public class LeagueServiceTest {
LeagueParticipationDAO leagueParticipationDAO = Mockito.mock(LeagueParticipationDAO.class);
CollectionsManager collectionsManager = Mockito.mock(CollectionsManager.class);
LeagueService leagueService = new LeagueService(leagueDao, leaguePointsDAO, leagueMatchDAO, leagueParticipationDAO, collectionsManager);
LeagueService leagueService = new LeagueService(leagueDao, leagueMatchDAO, leagueParticipationDAO, collectionsManager);
assertTrue(leagueService.canPlayRankedGame(league, leagueSerie, "player1"));
assertFalse(leagueService.canPlayRankedGameAgainst(league, leagueSerie, "player1", "player2"));
@@ -138,7 +135,6 @@ public class LeagueServiceTest {
Mockito.when(leagueDao.loadActiveLeagues(Mockito.anyInt())).thenReturn(leagues);
LeaguePointsDAO leaguePointsDAO = Mockito.mock(LeaguePointsDAO.class);
LeagueMatchDAO leagueMatchDAO = Mockito.mock(LeagueMatchDAO.class);
Set<LeagueMatch> matches = new HashSet<LeagueMatch>();
@@ -146,9 +142,14 @@ public class LeagueServiceTest {
Mockito.when(leagueMatchDAO.getLeagueMatches(league)).thenReturn(new HashSet<LeagueMatch>(matches));
LeagueParticipationDAO leagueParticipationDAO = Mockito.mock(LeagueParticipationDAO.class);
Set<String> players = new HashSet<String>();
players.add("player1");
players.add("player2");
players.add("player3");
Mockito.when(leagueParticipationDAO.getUsersParticipating(league)).thenReturn(players);
CollectionsManager collectionsManager = Mockito.mock(CollectionsManager.class);
LeagueService leagueService = new LeagueService(leagueDao, leaguePointsDAO, leagueMatchDAO, leagueParticipationDAO, collectionsManager);
LeagueService leagueService = new LeagueService(leagueDao, leagueMatchDAO, leagueParticipationDAO, collectionsManager);
leagueService.reportLeagueGameResult(league, leagueSerie, "player1", "player2");
leagueService.reportLeagueGameResult(league, leagueSerie, "player1", "player3");

View File

@@ -22,7 +22,6 @@ public class DaoProvider implements InjectableProvider<Context, Type> {
private Injectable<LeagueDAO> _leagueDaoInjectable;
private Injectable<LeagueMatchDAO> _leagueMatchDAOInjectable;
private Injectable<LeaguePointsDAO> _leaguePointsDAOInjectable;
private Injectable<LeagueParticipationDAO> _leagueParticipationDAOInjectable;
private DbAccess _dbAccess;
@@ -51,8 +50,6 @@ public class DaoProvider implements InjectableProvider<Context, Type> {
return getLeagueDaoSafely();
else if (type.equals(LeagueMatchDAO.class))
return getLeagueMatchDaoSafely();
else if (type.equals(LeaguePointsDAO.class))
return getLeaguePointsDaoSafely();
else if (type.equals(LeagueParticipationDAO.class))
return getLeagueParticipationDaoSafely();
else if (type.equals(MerchantDAO.class))
@@ -73,19 +70,6 @@ public class DaoProvider implements InjectableProvider<Context, Type> {
return _leagueParticipationDAOInjectable;
}
private synchronized Injectable<LeaguePointsDAO> getLeaguePointsDaoSafely() {
if (_leaguePointsDAOInjectable == null) {
final LeaguePointsDAO leaguePointsDAO = new DbLeaguePointsDAO(_dbAccess);
_leaguePointsDAOInjectable = new Injectable<LeaguePointsDAO>() {
@Override
public LeaguePointsDAO getValue() {
return leaguePointsDAO;
}
};
}
return _leaguePointsDAOInjectable;
}
private synchronized Injectable<LeagueMatchDAO> getLeagueMatchDaoSafely() {
if (_leagueMatchDAOInjectable == null) {
final LeagueMatchDAO leagueMatchDao = new DbLeagueMatchDAO(_dbAccess);

View File

@@ -46,8 +46,6 @@ public class ServerProvider implements InjectableProvider<Context, Type> {
@Context
private LeagueMatchDAO _leagueMatchDao;
@Context
private LeaguePointsDAO _leaguePointsDao;
@Context
private LeagueParticipationDAO _leagueParticipationDao;
@Context
private GameHistoryDAO _gameHistoryDao;
@@ -100,7 +98,7 @@ public class ServerProvider implements InjectableProvider<Context, Type> {
private synchronized Injectable<LeagueService> getLeagueServiceInjectable() {
if (_leagueServiceInjectable == null) {
final LeagueService leagueService = new LeagueService(_leagueDao, _leaguePointsDao, _leagueMatchDao, _leagueParticipationDao, getCollectionsManagerInjectable().getValue());
final LeagueService leagueService = new LeagueService(_leagueDao, _leagueMatchDao, _leagueParticipationDao, getCollectionsManagerInjectable().getValue());
_leagueServiceInjectable = new Injectable<LeagueService>() {
@Override
public LeagueService getValue() {