Skip to content

Commit 4484245

Browse files
bsikandernoorul
authored andcommitted
fix(jobserver): Simplify and optimize query (spark-jobserver#977)
- If the binary is already available in the binaries_contents then the select query will load all the binary content because we have a select * statement. A map(_.binId) is added to do specific select - Since output of first insert statement is not used anywhere, there is no need to get the binId back. This avoids the following queries SELECT SCOPE_IDENTITY() WHERE SCOPE_IDENTITY() IS NOT NULL; - Remove extra map(bc => bc.*) to simplify the code Change-Id: I7f8c1372bf8db22dac999a641e9e965f3f0adea0
1 parent 04bdbcf commit 4484245

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

job-server/src/main/scala/spark/jobserver/io/JobSqlDAO.scala

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -196,16 +196,13 @@ class JobSqlDAO(config: Config) extends JobDAO with FileCacher {
196196
// Insert JarInfo and its jar into db and return the primary key associated with that row
197197
private def insertBinaryInfo(binInfo: BinaryInfo, binBytes: Array[Byte]): Future[Int] = {
198198
val hash = calculateBinaryHash(binBytes);
199-
val dbAction = (for {
200-
binId <- binaries.returning(binaries.map(_.binId)) +=
201-
(-1, binInfo.appName, binInfo.binaryType.name, convertDateJodaToSql(binInfo.uploadTime), hash)
202-
_ <- binariesContents.filter(_.binHash === hash).result.headOption.flatMap{
203-
case None =>
204-
binariesContents.map(bc => bc.*) += (hash, new SerialBlob(binBytes))
205-
case Some(bc) =>
206-
DBIO.successful(None) // no-op
207-
}
208-
} yield binId).transactionally
199+
val dbAction = (binaries +=
200+
(-1, binInfo.appName, binInfo.binaryType.name, convertDateJodaToSql(binInfo.uploadTime), hash))
201+
.andThen(binariesContents.filter(_.binHash === hash).map(_.binHash)
202+
.result.headOption.flatMap {
203+
case Some(bc) => DBIO.successful(1)
204+
case None => binariesContents += (hash, new SerialBlob(binBytes))
205+
}).transactionally
209206
db.run(dbAction)
210207
}
211208

0 commit comments

Comments
 (0)