Skip to content

Commit

Permalink
feat: BRouter Segments are now updated daily, avoid a download if all…
Browse files Browse the repository at this point in the history
… segments exist locally and are from the same day

https://forum.routeconverter.com/thread-3858.html
  • Loading branch information
cpesch committed Jan 23, 2024
1 parent e1f0c15 commit 04900cc
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 38 deletions.
80 changes: 44 additions & 36 deletions brouter/src/main/java/slash/navigation/brouter/BRouter.java
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,46 @@ private void downloadAndWait(Collection<Downloadable> segments) {
downloadManager.waitForCompletion(downloads);
}

private Collection<Downloadable> collectDownloadables(Collection<String> uris) {
Collection<Downloadable> result = new HashSet<>();

for (String key : uris) {
Downloadable downloadable = getSegments().getDownloadable(key);
if (downloadable == null) {
log.warning(format("Cannot find downloadable for segment %s", key));
continue;
}
result.add(downloadable);
}
return result;
}

private boolean existAllSegmentsFromSameDay(Collection<Downloadable> segments) {
Checksum latestChecksum = null;

for (Downloadable downloadable : segments) {
File file = createSegmentFile(downloadable.getUri());
Checksum fileChecksum = null;
try {
fileChecksum = createChecksum(file, false);
} catch (IOException e) {
log.warning(format("Cannot calculate checksum for %s: %s", file, e.getLocalizedMessage()));
}

// file does not exist or failed to calculate checksum
if (fileChecksum == null)
return false;

if (latestChecksum == null)
latestChecksum = fileChecksum;

// file is from a different day than existing file
else if (!fileChecksum.sameDay(latestChecksum))
return false;
}
return true;
}

public DownloadFuture downloadRoutingDataFor(String mapIdentifier, List<LongitudeAndLatitude> longitudeAndLatitudes) {
if (!isInitialized()) {
return new DownloadFutureImpl(Collections.emptySet());
Expand All @@ -389,42 +429,10 @@ public DownloadFuture downloadRoutingDataFor(String mapIdentifier, List<Longitud
uris.addAll(createFileKeys(longitudeAndLatitude.longitude, longitudeAndLatitude.latitude));
}

Collection<Downloadable> segments = new HashSet<>();
Checksum latestChecksum = null;

for (String key : uris) {
Downloadable downloadable = getSegments().getDownloadable(key);
if (downloadable != null) {
File file = createSegmentFile(downloadable.getUri());
if (!file.exists())
segments.add(downloadable);

if (latestChecksum == null || downloadable.getLatestChecksum() != null &&
downloadable.getLatestChecksum().laterThan(latestChecksum))
latestChecksum = downloadable.getLatestChecksum();
}
}

// all segments have to be from the same (latest) date
if (latestChecksum != null) {
for (String key : uris) {
Downloadable downloadable = getSegments().getDownloadable(key);
if (downloadable != null) {
File file = createSegmentFile(downloadable.getUri());
if (file.exists()) {
Checksum fileChecksum = null;
try {
fileChecksum = createChecksum(file, false);
} catch (IOException e) {
log.warning(format("Cannot calculate checksum for %s: %s", file, e.getLocalizedMessage()));
}

if (fileChecksum != null && latestChecksum.laterThan(fileChecksum))
segments.add(downloadable);
}
}
}
}
Collection<Downloadable> segments = collectDownloadables(uris);
// if all segments exist locally and are from the same day, we don't need to download them
if(existAllSegmentsFromSameDay(segments))
segments = new HashSet<>();
return new DownloadFutureImpl(segments);
}

Expand Down
5 changes: 5 additions & 0 deletions common/src/main/java/slash/common/type/CompactCalendar.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
public class CompactCalendar {
private static final Logger log = Logger.getLogger(CompactCalendar.class.getName());
public static final TimeZone UTC = TimeZone.getTimeZone("UTC");
private static final long MILLI_SECONDS_PER_DAY = 24 * 60 * 60 * 1000;

private final long timeInMillis;
private final String timeZoneId;
Expand Down Expand Up @@ -158,6 +159,10 @@ public boolean before(CompactCalendar other) {
return getCalendar().before(other.getCalendar());
}

public boolean sameDay(CompactCalendar other) {
return getTimeInMillis() / MILLI_SECONDS_PER_DAY == other.getTimeInMillis()/ MILLI_SECONDS_PER_DAY;
}

public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Expand Down
15 changes: 15 additions & 0 deletions common/src/test/java/slash/common/type/CompactCalendarTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,19 @@ public void testAsUTCTimeInTimeZone() {
assertEquals("UTC", inTimeZone.getTimeZoneId());
}

@Test
public void testSameDay() {
CompactCalendar today = CompactCalendar.now();
CompactCalendar todayMinusAMilli = fromMillis(today.getTimeInMillis() - 1);
CompactCalendar yesterday = fromMillis(today.getTimeInMillis() - 24 * 60 * 60 * 1000);
CompactCalendar tomorrow = fromMillis(today.getTimeInMillis() + 24 * 60 * 60 * 1000);

assertTrue(today.sameDay(today));
assertTrue(today.sameDay(todayMinusAMilli));
assertTrue(yesterday.sameDay(yesterday));
assertTrue(tomorrow.sameDay(tomorrow));

assertFalse(today.sameDay(yesterday));
assertFalse(today.sameDay(tomorrow));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ public String getSHA1() {
return sha1;
}

public boolean sameDay(Checksum other) {
return other == null || getLastModified() != null && other.getLastModified() != null &&
getLastModified().sameDay(other.getLastModified());
}

public boolean laterThan(Checksum other) {
return other == null || getLastModified() != null && other.getLastModified() != null &&
getLastModified().after(other.getLastModified());
Expand Down
4 changes: 2 additions & 2 deletions route-converter-build/buildNumber.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#maven.buildNumber.plugin properties file
#Sat Dec 23 15:21:41 CET 2023
buildNumber0=379
#Tue Jan 23 18:07:06 CET 2024
buildNumber0=380

0 comments on commit 04900cc

Please sign in to comment.