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 2 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
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 = 1000;

public MavenResolverIO(DSLContext dbContext, File baseDir) {
this(dbContext, baseDir, new ObjectMapperBuilder().build());
Expand Down Expand Up @@ -109,24 +110,29 @@ private File dbFile() {
public Set<Pom> readFromDB() {
LOG.info("Collecting poms from DB ...");

var poms = new HashSet<Pom>();

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);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}).collect(Collectors.toSet());
}
}
}

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

Expand Down