Skip to content

Commit 6d1c036

Browse files
committed
Optimize bulk imports a bit more.
1 parent 9155353 commit 6d1c036

File tree

3 files changed

+55
-9
lines changed

3 files changed

+55
-9
lines changed

codepulse/src/main/scala/com/secdec/codepulse/data/trace/slick/EncountersDao.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import com.secdec.codepulse.data.trace._
2727
*
2828
* @author robertf
2929
*/
30-
private[slick] class EncountersDao(val driver: JdbcProfile, val recordingMetadata: RecordingMetadataDao, val treeNodeData: TreeNodeDataDao) {
30+
private[slick] class EncountersDao(val driver: JdbcProfile, val recordingMetadata: RecordingMetadataDao, val treeNodeData: TreeNodeDataDao) extends SlickHelpers {
3131
import driver.simple._
3232

3333
class Encounters(tag: Tag) extends Table[(Option[Int], Int)](tag, "node_encounters") {
@@ -50,6 +50,6 @@ private[slick] class EncountersDao(val driver: JdbcProfile, val recordingMetadat
5050
}
5151

5252
def store(entries: Iterable[(Option[Int], Int)])(implicit session: Session) {
53-
encounters ++= entries
53+
fastImport { encounters ++= entries }
5454
}
5555
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Code Pulse: A real-time code coverage testing tool. For more information
3+
* see http://code-pulse.com
4+
*
5+
* Copyright (C) 2014 Applied Visions - http://securedecisions.avi.com
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
package com.secdec.codepulse.data.trace.slick
21+
22+
import scala.slick.driver.JdbcProfile
23+
import scala.slick.jdbc.{ StaticQuery => Q }
24+
25+
/** Some helpers for the Slick DAOs.
26+
*
27+
* @author robertf
28+
*/
29+
private[slick] trait SlickHelpers {
30+
val driver: JdbcProfile
31+
import driver.simple._
32+
33+
/** Wrap thunk in DB calls to speed up large imports.
34+
* Ideas here are from http://www.h2database.com/html/performance.html#fast_import
35+
*/
36+
def fastImport[T](thunk: => T)(implicit session: Session): T = {
37+
try {
38+
(Q updateNA "SET LOG 0; SET UNDO_LOG 0;").execute
39+
thunk
40+
} finally {
41+
(Q updateNA "SET LOG 2; SET UNDO_LOG 1;").execute
42+
}
43+
}
44+
}

codepulse/src/main/scala/com/secdec/codepulse/data/trace/slick/TreeNodeDataDao.scala

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import com.secdec.codepulse.data.trace.{ TreeNodeData => TreeNode, _ }
2828
*
2929
* @author robertf
3030
*/
31-
private[slick] class TreeNodeDataDao(val driver: JdbcProfile) {
31+
private[slick] class TreeNodeDataDao(val driver: JdbcProfile) extends SlickHelpers {
3232
import driver.simple._
3333

3434
class TreeNodeData(tag: Tag) extends Table[TreeNode](tag, "tree_node_data") {
@@ -123,31 +123,33 @@ private[slick] class TreeNodeDataDao(val driver: JdbcProfile) {
123123
}
124124

125125
def storeMethodSignatures(signatures: Iterable[(String, Int)])(implicit session: Session) {
126-
methodSignatureNodeMap ++= signatures
126+
fastImport { methodSignatureNodeMap ++= signatures }
127127
}
128128

129129
def storeJsp(jspPath: String, nodeId: Int)(implicit session: Session) {
130130
jspNodeMap += jspPath -> nodeId
131131
}
132132

133133
def storeJsps(jsps: Iterable[(String, Int)])(implicit session: Session) {
134-
jspNodeMap ++= jsps
134+
fastImport { jspNodeMap ++= jsps }
135135
}
136136

137137
def storeNode(node: TreeNode)(implicit session: Session) {
138138
treeNodeData += node
139139
}
140140

141141
def storeNodes(nodes: Iterable[TreeNode])(implicit session: Session) {
142-
treeNodeData ++= nodes
142+
fastImport { treeNodeData ++= nodes }
143143
}
144144

145145
def getTraced()(implicit session: Session) = tracedNodes.list
146146

147147
def storeTracedValues(values: Iterable[(Int, Option[Boolean])])(implicit session: Session) {
148-
tracedNodes ++= values flatMap {
149-
case (id, Some(traced)) => Some(id -> traced)
150-
case _ => None
148+
fastImport {
149+
tracedNodes ++= values flatMap {
150+
case (id, Some(traced)) => Some(id -> traced)
151+
case _ => None
152+
}
151153
}
152154
}
153155

0 commit comments

Comments
 (0)