attached leaderboard persistence

This commit is contained in:
Axel Uhl committed 2011-08-31 18:18:12 +02:00
1 parent faa9122626
commit 28f1b2feea
8 files changed
+93 -10

No files matched your search

@@ -87,12 +87,15 @@ public class SailingServiceImpl extends RemoteServiceServlet implements SailingS
private static final long serialVersionUID = 9031688830194537489L;
private final ServiceTracker<RacingEventService, RacingEventService> racingEventServiceTracker;
private final MongoObjectFactory mongoObjectFactory;
public SailingServiceImpl() {
BundleContext context = Activator.getDefault();
racingEventServiceTracker = new ServiceTracker<RacingEventService, RacingEventService>(
context, RacingEventService.class.getName(), null);
racingEventServiceTracker.open();
mongoObjectFactory = MongoObjectFactory.INSTANCE;
}
public LeaderboardDAO getLeaderboardByName(String leaderboardName, Date date) throws Exception {
@@ -240,7 +243,7 @@ public class SailingServiceImpl extends RemoteServiceServlet implements SailingS
@Override
public void track(RaceRecordDAO rr, String liveURI, String storedURI, boolean trackWind, boolean correctWindByDeclination) throws Exception {
RaceHandle raceHandle = getService().addRace(new URL(rr.paramURL), new URI(liveURI), new URI(storedURI),
MongoWindStoreFactory.INSTANCE.getMongoWindStore(MongoObjectFactory.INSTANCE));
MongoWindStoreFactory.INSTANCE.getMongoWindStore(mongoObjectFactory));
if (trackWind) {
startTrackingWind(raceHandle.getEvent().getName(), raceHandle.getRace().getName(), correctWindByDeclination);
}
@@ -261,7 +264,6 @@ public class SailingServiceImpl extends RemoteServiceServlet implements SailingS
@Override
public void storeTracTracConfiguration(String name, String jsonURL, String liveDataURI, String storedDataURI) throws Exception {
DomainFactory domainFactory = DomainFactory.INSTANCE;
MongoObjectFactory mongoObjectFactory = MongoObjectFactory.INSTANCE;
mongoObjectFactory.storeTracTracConfiguration(domainFactory.createTracTracConfiguration(name, jsonURL, liveDataURI, storedDataURI));
}
@@ -617,6 +619,7 @@ public class SailingServiceImpl extends RemoteServiceServlet implements SailingS
Leaderboard leaderboard = getService().getLeaderboardByName(leaderboardName);
if (leaderboard != null) {
leaderboard.addRaceColumn(columnName, medalRace);
getService().updateStoredLeaderboard(leaderboard);
} else {
throw new IllegalArgumentException("Leaderboard named "+leaderboardName+" not found");
}
@@ -627,6 +630,7 @@ public class SailingServiceImpl extends RemoteServiceServlet implements SailingS
Leaderboard leaderboard = getService().getLeaderboardByName(leaderboardName);
if (leaderboard != null) {
leaderboard.removeRaceColumn(columnName);
getService().updateStoredLeaderboard(leaderboard);
} else {
throw new IllegalArgumentException("Leaderboard named "+leaderboardName+" not found");
}
@@ -637,6 +641,7 @@ public class SailingServiceImpl extends RemoteServiceServlet implements SailingS
Leaderboard leaderboard = getService().getLeaderboardByName(leaderboardName);
if (leaderboard != null) {
leaderboard.getRaceColumnByName(oldColumnName).setName(newColumnName);
getService().updateStoredLeaderboard(leaderboard);
} else {
throw new IllegalArgumentException("Leaderboard named "+leaderboardName+" not found");
}
@@ -652,6 +657,7 @@ public class SailingServiceImpl extends RemoteServiceServlet implements SailingS
RaceInLeaderboard raceColumn = leaderboard.getRaceColumnByName(raceColumnName);
if (raceColumn != null) {
raceColumn.setTrackedRace(trackedRace);
getService().updateStoredLeaderboard(leaderboard);
}
}
}
@@ -682,6 +688,7 @@ public class SailingServiceImpl extends RemoteServiceServlet implements SailingS
RaceInLeaderboard raceColumn = leaderboard.getRaceColumnByName(raceColumnName);
if (raceColumn != null) {
raceColumn.setTrackedRace(null);
getService().updateStoredLeaderboard(leaderboard);
} else {
throw new IllegalArgumentException("Didn't find race "+raceColumnName+" in leaderboard "+leaderboardName);
}
@@ -701,6 +708,7 @@ public class SailingServiceImpl extends RemoteServiceServlet implements SailingS
} else {
leaderboard.setCarriedPoints(competitor, carriedPoints);
}
getService().updateStoredLeaderboard(leaderboard);
} else {
throw new IllegalArgumentException("Didn't find competitor "+competitorName+" in leaderboard "+leaderboardName);
}
@@ -725,6 +733,7 @@ public class SailingServiceImpl extends RemoteServiceServlet implements SailingS
} else {
leaderboard.getScoreCorrection().setMaxPointsReason(competitor, raceColumn, MaxPointsReason.valueOf(maxPointsReasonAsString));
}
getService().updateStoredLeaderboard(leaderboard);
} else {
throw new IllegalArgumentException("Didn't find competitor "+competitorName+" in leaderboard "+leaderboardName);
}
@@ -737,6 +746,7 @@ public class SailingServiceImpl extends RemoteServiceServlet implements SailingS
public int updateLeaderboardScoreCorrection(String leaderboardName, String competitorName, String raceName,
Integer correctedScore, Date date) throws NoWindException {
Leaderboard leaderboard = getService().getLeaderboardByName(leaderboardName);
int result;
if (leaderboard != null) {
Competitor competitor = leaderboard.getCompetitorByName(competitorName);
if (competitor != null) {
@@ -746,10 +756,10 @@ public class SailingServiceImpl extends RemoteServiceServlet implements SailingS
}
if (correctedScore == null) {
leaderboard.getScoreCorrection().uncorrectScore(competitor, raceColumn);
return leaderboard.getNetPoints(competitor, raceColumn, new MillisecondsTimePoint(date));
result = leaderboard.getNetPoints(competitor, raceColumn, new MillisecondsTimePoint(date));
} else {
leaderboard.getScoreCorrection().correctScore(competitor, raceColumn, correctedScore);
return correctedScore;
result =correctedScore;
}
} else {
throw new IllegalArgumentException("Didn't find competitor "+competitorName+" in leaderboard "+leaderboardName);
@@ -757,5 +767,7 @@ public class SailingServiceImpl extends RemoteServiceServlet implements SailingS
} else {
throw new IllegalArgumentException("Didn't find leaderboard "+leaderboardName);
}
getService().updateStoredLeaderboard(leaderboard);
return result;
}
}
@@ -23,4 +23,6 @@ public interface DomainObjectFactory {
Iterable<TracTracConfiguration> getTracTracConfigurations();
Leaderboard loadLeaderboard(String name);
Iterable<Leaderboard> getAllLeaderboards();
}
@@ -40,4 +40,11 @@ public interface MongoObjectFactory {
* factory.
*/
void storeLeaderboard(Leaderboard leaderboard);
/**
* Removes the leaderboard named <code>name</code> from the database.
*/
void removeLeaderboard(String leaderboardName);
void renameLeaderboard(String oldName, String newName);
}
@@ -1,7 +1,9 @@
package com.sap.sailing.mongodb.impl;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -121,7 +123,7 @@ public class DomainObjectFactoryImpl implements DomainObjectFactory {
BasicDBObject query = new BasicDBObject();
query.put(FieldNames.LEADERBOARD_NAME.name(), name);
for (DBObject o : leaderboardCollection.find(query)) {
result = loadLeaderboard(name, o);
result = loadLeaderboard(o);
}
} catch (Throwable t) {
// something went wrong during DB access; report, then use empty new wind track
@@ -129,10 +131,25 @@ public class DomainObjectFactoryImpl implements DomainObjectFactory {
logger.throwing(DomainObjectFactoryImpl.class.getName(), "loadLeaderboard", t);
}
return result;
}
@Override
public Iterable<Leaderboard> getAllLeaderboards() {
DBCollection leaderboardCollection = database.getCollection(CollectionNames.LEADERBOARDS.name());
Set<Leaderboard> result = new HashSet<Leaderboard>();
try {
for (DBObject o : leaderboardCollection.find()) {
result.add(loadLeaderboard(o));
}
} catch (Throwable t) {
// something went wrong during DB access; report, then use empty new wind track
logger.log(Level.SEVERE, "Error connecting to MongoDB, unable to load leaderboards.");
logger.throwing(DomainObjectFactoryImpl.class.getName(), "getAllLeaderboards", t);
}
return result;
}
private Leaderboard loadLeaderboard(String name, DBObject o) {
private Leaderboard loadLeaderboard(DBObject o) {
SettableScoreCorrection scoreCorrection = new ScoreCorrectionImpl();
BasicDBList dbDiscardIndexResultsStartingWithHowManyRaces = (BasicDBList) o.get(FieldNames.LEADERBOARD_DISCARDING_THRESHOLDS.name());
int[] discardIndexResultsStartingWithHowManyRaces = new int[dbDiscardIndexResultsStartingWithHowManyRaces.size()];
@@ -141,7 +158,8 @@ public class DomainObjectFactoryImpl implements DomainObjectFactory {
discardIndexResultsStartingWithHowManyRaces[i++] = (Integer) discardingThresholdAsObject;
}
ThresholdBasedResultDiscardingRule resultDiscardingRule = new ResultDiscardingRuleImpl(discardIndexResultsStartingWithHowManyRaces);
LeaderboardImplWithDelayedCarriedPoints result = new LeaderboardImplWithDelayedCarriedPoints(name, scoreCorrection, resultDiscardingRule);
LeaderboardImplWithDelayedCarriedPoints result = new LeaderboardImplWithDelayedCarriedPoints(
(String) o.get(FieldNames.LEADERBOARD_NAME.name()), scoreCorrection, resultDiscardingRule);
BasicDBList dbRaceColumns = (BasicDBList) o.get(FieldNames.LEADERBOARD_COLUMNS.name());
for (Object dbRaceColumnAsObject : dbRaceColumns) {
BasicDBObject dbRaceColumn = (BasicDBObject) dbRaceColumnAsObject;
@@ -104,6 +104,7 @@ public class MongoObjectFactoryImpl implements MongoObjectFactory {
public void storeLeaderboard(Leaderboard leaderboard) {
DBCollection leaderboardCollection = database.getCollection(CollectionNames.LEADERBOARDS.name());
leaderboardCollection.ensureIndex(FieldNames.LEADERBOARD_NAME.name());
BasicDBObject query = new BasicDBObject(FieldNames.LEADERBOARD_NAME.name(), leaderboard.getName());
BasicDBObject result = new BasicDBObject();
result.put(FieldNames.LEADERBOARD_NAME.name(), leaderboard.getName());
BasicDBList dbRaceColumns = new BasicDBList();
@@ -135,7 +136,7 @@ public class MongoObjectFactoryImpl implements MongoObjectFactory {
dbResultDiscardingThresholds.add(threshold);
}
result.put(FieldNames.LEADERBOARD_DISCARDING_THRESHOLDS.name(), dbResultDiscardingThresholds);
leaderboardCollection.insert(result);
leaderboardCollection.update(query, result, /* upsrt */ true, /* multi */ false);
}
private void storeScoreCorrections(Leaderboard leaderboard, BasicDBObject dbScoreCorrections) {
@@ -165,4 +166,19 @@ public class MongoObjectFactoryImpl implements MongoObjectFactory {
}
}
@Override
public void removeLeaderboard(String leaderboardName) {
DBCollection leaderboardCollection = database.getCollection(CollectionNames.LEADERBOARDS.name());
BasicDBObject query = new BasicDBObject(FieldNames.LEADERBOARD_NAME.name(), leaderboardName);
leaderboardCollection.remove(query);
}
@Override
public void renameLeaderboard(String oldName, String newName) {
DBCollection leaderboardCollection = database.getCollection(CollectionNames.LEADERBOARDS.name());
BasicDBObject query = new BasicDBObject(FieldNames.LEADERBOARD_NAME.name(), oldName);
BasicDBObject renameUpdate = new BasicDBObject("$set", new BasicDBObject(FieldNames.LEADERBOARD_NAME.name(), newName));
leaderboardCollection.update(query, renameUpdate);
}
}
@@ -160,4 +160,6 @@ public interface RacingEventService {
*/
void renameLeaderboard(String oldName, String newName);
void updateStoredLeaderboard(Leaderboard leaderboard);
}
@@ -36,6 +36,8 @@ import com.sap.sailing.domain.tractracadapter.RaceHandle;
import com.sap.sailing.domain.tractracadapter.RaceRecord;
import com.sap.sailing.domain.tractracadapter.RaceTracker;
import com.sap.sailing.expeditionconnector.ExpeditionWindTrackerFactory;
import com.sap.sailing.mongodb.DomainObjectFactory;
import com.sap.sailing.mongodb.MongoObjectFactory;
import com.sap.sailing.util.Util.Triple;
public class RacingEventServiceImpl implements RacingEventService {
@@ -68,9 +70,15 @@ public class RacingEventServiceImpl implements RacingEventService {
private static final String DEFAULT_LEADERBOARD_NAME = "Default Leaderboard";
private Set<DynamicTrackedEvent> eventsObservedForDefaultLeaderboard = new HashSet<DynamicTrackedEvent>();
private final MongoObjectFactory mongoObjectFactory;
private final DomainObjectFactory domainObjectFactory;
public RacingEventServiceImpl() {
domainFactory = DomainFactory.INSTANCE;
domainObjectFactory = DomainObjectFactory.INSTANCE;
mongoObjectFactory = MongoObjectFactory.INSTANCE;
windTrackerFactory = ExpeditionWindTrackerFactory.getInstance();
eventsByName = new HashMap<String, Event>();
raceTrackersByEvent = new HashMap<Event, Set<RaceTracker>>();
@@ -80,6 +88,13 @@ public class RacingEventServiceImpl implements RacingEventService {
// Add one default leaderboard that aggregates all races currently tracked by this service.
// This is more for debugging purposes than for anything else.
addLeaderboard(DEFAULT_LEADERBOARD_NAME, new int[] { 5, 8 });
loadStoredLeaderboards();
}
private void loadStoredLeaderboards() {
for (Leaderboard leaderboard : domainObjectFactory.getAllLeaderboards()) {
leaderboardsByName.put(leaderboard.getName(), leaderboard);
}
}
@Override
@@ -89,6 +104,7 @@ public class RacingEventServiceImpl implements RacingEventService {
synchronized (leaderboardsByName) {
leaderboardsByName.put(name, result);
}
mongoObjectFactory.storeLeaderboard(result);
return result;
}
@@ -104,14 +120,22 @@ public class RacingEventServiceImpl implements RacingEventService {
Leaderboard toRename = leaderboardsByName.remove(oldName);
toRename.setName(newName);
leaderboardsByName.put(newName, toRename);
mongoObjectFactory.renameLeaderboard(oldName, newName);
}
}
@Override
public void updateStoredLeaderboard(Leaderboard leaderboard) {
// TODO this is very brute force; consider updating more fine-grained
mongoObjectFactory.storeLeaderboard(leaderboard);
}
@Override
public void removeLeaderboard(String leaderboardName) {
synchronized (leaderboardsByName) {
leaderboardsByName.remove(leaderboardName);
}
mongoObjectFactory.removeLeaderboard(leaderboardName);
}
@Override