Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use lazy fetch in MavenResolverIO when reading POMs from the DB #472

Merged
merged 4 commits into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion analyzer/restapi-plugin/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ FROM openjdk:11-jre-slim
WORKDIR /app
COPY ./target/*.jar ./rest-api.jar

ENTRYPOINT java $JAVA_OPTS -jar /app/rest-api.jar $0 $@
ENTRYPOINT java $JAVA_OPTS -XX:+UseG1GC -XX:+UseStringDeduplication -XX:-CompactStrings -jar /app/rest-api.jar $0 $@

EXPOSE 8080
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,10 @@ public class KnowledgeBaseConnector {
@PostConstruct
public void connectToKnowledgeBase() {
try {
logger.info("Establishing connection to the " + kbForge + " KnowledgeBase at " + kbUrl + ", user " + kbUser +"...");
dbContext = PostgresConnector.getDSLContext(kbUrl, kbUser, true);
logger.info("Establishing connection to the " + kbForge + " KnowledgeBase at " + kbUrl + ", user " + kbUser + "...");
// JDBC auto-commit should be false: (1) Postgres doesn't like it when using non-zero fetch size.
// (2) It is not usually a good practice.
dbContext = PostgresConnector.getDSLContext(kbUrl, kbUser, false);
kbDao = new MetadataDao(dbContext);
} catch (SQLException e) {
logger.error("Couldn't connect to the KnowledgeBase", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class MavenResolverIO {
private DSLContext dbContext;
private File baseDir;
private ObjectMapper om;
private final int PG_FETCH_SIZE = 10000;

public MavenResolverIO(DSLContext dbContext, File baseDir) {
this(dbContext, baseDir, new ObjectMapperBuilder().build());
Expand Down Expand Up @@ -91,6 +92,7 @@ private void saveToDisk(Set<Pom> poms) {
LOG.info("Saving poms to {} ...", to);
try {
var tmp = tmpFile();
createFileIfDoNotExist(tmp);
om.writeValue(tmp, poms);
FileUtils.moveFile(tmp, to);
} catch (IOException e) {
Expand All @@ -109,24 +111,34 @@ private File dbFile() {
public Set<Pom> readFromDB() {
LOG.info("Collecting poms from DB ...");

var poms = new HashSet<Pom>();
var numberOfFetchedPoms = 0;

var dbRes = dbContext.select( //
PACKAGE_VERSIONS.METADATA, //
PACKAGE_VERSIONS.ID) //
.from(PACKAGE_VERSIONS) //
.where(PACKAGE_VERSIONS.METADATA.isNotNull()) //
.fetch();
.where(PACKAGE_VERSIONS.METADATA.isNotNull()).fetchSize(this.PG_FETCH_SIZE); //

var poms = dbRes.stream() //
.map(x -> {
try (var cursor = dbRes.fetchLazy()) {
while (cursor.hasNext()) {
var record = cursor.fetchNext();
if (record != null) {
try {
var json = x.component1().data();
var json = record.component1().data();
var pom = simplify(om.readValue(json, Pom.class));
pom.id = x.component2();
return pom;
pom.id = record.component2();
poms.add(pom);
numberOfFetchedPoms++;
if (numberOfFetchedPoms % PG_FETCH_SIZE == 0) {
LOG.info("Fetched {} POMs", numberOfFetchedPoms);
}
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}).collect(Collectors.toSet());
}
}
}

LOG.info("Found {} poms in DB", poms.size());

Expand Down Expand Up @@ -162,4 +174,17 @@ private static IMavenResolver init(Set<Pom> poms) {
LOG.info("Initialization done");
return new MavenResolver(dpdRes, depRes);
}

private void createFileIfDoNotExist(File file) {
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
LOG.error("Could not create file {}", file.toPath());
}
} else {
LOG.info("File {} exists!", file.toPath());
}
}
}