Skip to content

Commit

Permalink
move the info file in the tar exports to the start of the payload to …
Browse files Browse the repository at this point in the history
…improve client performance
  • Loading branch information
andponlin committed Nov 8, 2023
1 parent 512db53 commit a865526
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2022, Andrew Lindesay
* Copyright 2019-2023, Andrew Lindesay
* Distributed under the terms of the MIT License.
*/

Expand All @@ -9,11 +9,11 @@
import com.google.common.base.Strings;
import org.apache.cayenne.ObjectContext;
import org.apache.cayenne.query.ObjectSelect;
import org.haiku.haikudepotserver.support.exception.ObjectNotFoundException;
import org.haiku.haikudepotserver.dataobjects.auto._PkgScreenshot;
import org.haiku.haikudepotserver.dataobjects.auto._PkgSupplement;
import org.haiku.haikudepotserver.dataobjects.support.MutableCreateAndModifyTimestamped;
import org.haiku.haikudepotserver.support.SingleCollector;
import org.haiku.haikudepotserver.support.exception.ObjectNotFoundException;

import java.time.Clock;
import java.util.*;
Expand All @@ -35,6 +35,25 @@ public static Optional<PkgSupplement> tryGetByBasePkgName(
return Optional.ofNullable(ObjectSelect.query(PkgSupplement.class).where(BASE_PKG_NAME.eq(basePkgName)).selectOne(context));
}

public static Optional<Date> getLatestIconModifyTimestamp(ObjectContext context) {
Preconditions.checkArgument(null != context, "the context must be supplied");
return Optional.ofNullable(
ObjectSelect.columnQuery(PkgSupplement.class, PkgSupplement.ICON_MODIFY_TIMESTAMP)
.where(PkgSupplement.ICON_MODIFY_TIMESTAMP.isNotNull())
.orderBy(PkgSupplement.ICON_MODIFY_TIMESTAMP.desc())
.limit(1)
.selectOne(context));
}

public static Optional<Date> tryGetLatestModifyTimestamp(ObjectContext context) {
Preconditions.checkArgument(null != context, "the context must be supplied");
return Optional.ofNullable(
ObjectSelect.columnQuery(PkgSupplement.class, PkgSupplement.MODIFY_TIMESTAMP)
.orderBy(PkgSupplement.ICON_MODIFY_TIMESTAMP.desc())
.limit(1)
.selectOne(context));
}

/**
* <p>If there is a change-log then this will return it, otherwise it will return an empty optional.</p>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ public void run(
query.setFetchLimit(getBatchSize());
int countLastQuery;

// this coming early means that if a client is trying to find this data
// then they will find it more quickly saving time for the client.

appendArchiveInfo(state, getLatestModifiedTimestamp(specification));

do {
query.setFetchOffset(offset);
List<DataRow> queryResults = context.performQuery(query);
Expand All @@ -91,8 +96,6 @@ public void run(
}

} while(countLastQuery > 0);

appendArchiveInfo(state);
}

LOGGER.info("did produce report for {} entries in {}ms", offset, stopwatch.elapsed(TimeUnit.MILLISECONDS));
Expand All @@ -105,6 +108,8 @@ public void run(

abstract String getPathComponentTop();

abstract Date getLatestModifiedTimestamp(T specification);

private void appendFromRawRows(State state, List<DataRow> rows)
throws IOException {
for(DataRow row : rows) {
Expand All @@ -118,15 +123,17 @@ private void appendFromRawRows(State state, List<DataRow> rows)
* <p>Adds a little informational file into the tar-ball.</p>
*/

private void appendArchiveInfo(State state) throws IOException {
private void appendArchiveInfo(
State state,
Date latestModifiedTimestamp) throws IOException {
ArchiveInfo archiveInfo = new ArchiveInfo(
DateTimeHelper.secondAccuracyDatePlusOneSecond(state.latestModifiedTimestamp),
DateTimeHelper.secondAccuracyDatePlusOneSecond(latestModifiedTimestamp),
runtimeInformationService.getProjectVersion());

byte[] payload = objectMapper.writeValueAsBytes(archiveInfo);
TarArchiveEntry tarEntry = new TarArchiveEntry(getPathComponentTop() + "/info.json");
tarEntry.setSize(payload.length);
tarEntry.setModTime(roundTimeToSecondPlusOne(state.latestModifiedTimestamp));
tarEntry.setModTime(roundTimeToSecondPlusOne(latestModifiedTimestamp));
state.tarArchiveOutputStream.putArchiveEntry(tarEntry);
state.tarArchiveOutputStream.write(payload);
state.tarArchiveOutputStream.closeArchiveEntry();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.lang3.StringUtils;
import org.haiku.haikudepotserver.dataobjects.PkgIcon;
import org.haiku.haikudepotserver.dataobjects.PkgSupplement;
import org.haiku.haikudepotserver.pkg.model.PkgIconExportArchiveJobSpecification;
import org.haiku.haikudepotserver.support.RuntimeInformationService;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -103,4 +104,10 @@ SQLTemplate createQuery(PkgIconExportArchiveJobSpecification specification) {
.getQueryDescriptor("AllPkgIcons").buildQuery();
}

@Override
Date getLatestModifiedTimestamp(PkgIconExportArchiveJobSpecification specification) {
return PkgSupplement.getLatestIconModifyTimestamp(serverRuntime.newContext())
.orElse(new Date(0L));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import org.apache.cayenne.query.SQLTemplate;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.lang3.StringUtils;
import org.haiku.haikudepotserver.dataobjects.Pkg;
import org.haiku.haikudepotserver.dataobjects.PkgSupplement;
import org.haiku.haikudepotserver.pkg.model.PkgScreenshotExportArchiveJobSpecification;
import org.haiku.haikudepotserver.support.RuntimeInformationService;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -102,4 +104,15 @@ SQLTemplate createQuery(PkgScreenshotExportArchiveJobSpecification specification
return query;
}

Date getLatestModifiedTimestamp(PkgScreenshotExportArchiveJobSpecification specification) {
if (Strings.isNullOrEmpty(specification.getPkgName())) {
return PkgSupplement.tryGetLatestModifyTimestamp(serverRuntime.newContext())
.orElse(new Date(0L));
}

return Pkg.getByName(serverRuntime.newContext(), specification.getPkgName())
.getPkgSupplement()
.getLatestPkgModifyTimestampSecondAccuracy();
}

}

0 comments on commit a865526

Please sign in to comment.