Skip to content

Commit 06f617c

Browse files
author
Michael Cuthbert
authored
Merge pull request #83 from osmlab/ljdelight/batchHttpFix
Avoid unnecessary http requests due to map getOrDefault
2 parents 9edc554 + 56317b0 commit 06f617c

File tree

4 files changed

+28
-6
lines changed

4 files changed

+28
-6
lines changed

src/main/java/org/maproulette/client/batch/BatchUploader.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,11 @@ public Tuple<Long, Long> addTask(final Challenge challenge, final Task task)
8888
{
8989
identifier = this.getDefaultProjectIdentifier();
9090
}
91-
final var projectBatch = this.projectBatchMap.getOrDefault(identifier,
92-
new ProjectBatch(identifier, this.configuration));
91+
92+
final long finalIdentifier = identifier;
93+
final var projectBatch = this.projectBatchMap.computeIfAbsent(identifier,
94+
k -> new ProjectBatch(finalIdentifier, this.configuration));
95+
9396
final var challengeId = projectBatch.addTask(challenge, task);
9497
this.projectBatchMap.put(identifier, projectBatch);
9598
return new Tuple<>(identifier, challengeId);
@@ -131,6 +134,16 @@ public void flushAll()
131134
});
132135
}
133136

137+
/**
138+
* Get the project id for the configuration's default project name. If the project id is not yet
139+
* known, HTTP requests will be make to (1) get the default project and (2) if the default
140+
* project doesn't exist create it. <br>
141+
* THIS METHOD MAY MAKE EXTERNAL HTTP REQUESTS. <br>
142+
*
143+
* @return project id
144+
* @throws MapRouletteException
145+
* when the requests fail
146+
*/
134147
private long getDefaultProjectIdentifier() throws MapRouletteException
135148
{
136149
if (this.defaultProjectIdentifier == -1)

src/main/java/org/maproulette/client/batch/ChallengeBatch.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.maproulette.client.exception.MapRouletteRuntimeException;
1616
import org.maproulette.client.model.Challenge;
1717
import org.maproulette.client.model.Task;
18+
import org.maproulette.client.utilities.ObjectMapperSingleton;
1819
import org.slf4j.Logger;
1920
import org.slf4j.LoggerFactory;
2021

@@ -36,7 +37,7 @@ public class ChallengeBatch
3637
{
3738
private static final int MAXIMUM_BATCH_SIZE = 5000;
3839
private final Logger logger = LoggerFactory.getLogger(ChallengeBatch.class);
39-
private final ObjectMapper mapper = new ObjectMapper();
40+
private final ObjectMapper mapper = ObjectMapperSingleton.getMapper();
4041
private final IMapRouletteConnection connection;
4142
private final long challengeId;
4243
private final int maxBatchSize;
@@ -116,6 +117,8 @@ public synchronized void addTask(final Task task) throws MapRouletteException
116117
this.batch.add(task);
117118
if (this.batch.size() >= this.maxBatchSize)
118119
{
120+
this.logger.debug("FLUSHING queued tasks as batch size {} meets max {}",
121+
this.batch.size(), this.maxBatchSize);
119122
this.flush();
120123
}
121124
}

src/main/java/org/maproulette/client/batch/ProjectBatch.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,10 @@ public synchronized long addTask(final Challenge challenge, final Task task)
8383
{
8484
challengeId = challenge.getId();
8585
}
86-
final var challengeBatch = this.batch.getOrDefault(challengeId,
87-
new ChallengeBatch(this.configuration, challenge));
86+
87+
final var challengeBatch = this.batch.computeIfAbsent(challengeId,
88+
k -> new ChallengeBatch(this.configuration, challenge));
89+
8890
task.setParent(challengeId);
8991
challengeBatch.addTask(task);
9092
this.batch.put(challengeId, challengeBatch);

src/main/java/org/maproulette/client/connection/MapRouletteConnection.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ public MapRouletteConnection(final MapRouletteConfiguration configuration)
5757
@Override
5858
public Optional<String> execute(final Query query) throws MapRouletteException
5959
{
60-
log.trace("Request: {} {} data={}", query.getMethodName(), query.getUri(), query.getData());
60+
log.debug("Request: {} {}", query.getMethodName(), query.getUri());
61+
if (log.isTraceEnabled())
62+
{
63+
log.trace("data={}", query.getData());
64+
}
6165

6266
// add authentication to the query
6367
query.addHeader(KEY_API_KEY, this.configuration.getApiKey());

0 commit comments

Comments
 (0)