Skip to content

Commit 3d41e66

Browse files
author
Steve Salas
committed
Changed database-exists test
Supports PageStore (.h2.db) and MVStore (.mv.db) for master and project databases.
1 parent 5975eec commit 3d41e66

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

codepulse/src/main/scala/com/secdec/codepulse/data/model/slick/SlickH2ProjectDataProvider.scala

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,25 @@ class SlickH2ProjectDataProvider(folder: File, actorSystem: ActorSystem) extends
4141

4242
private val cache = collection.mutable.Map.empty[ProjectId, SlickProjectData]
4343

44+
val MasterDbName = "master"
45+
val PageStoreFileSuffix = ".h2.db"
46+
val MultiVersionStoreFileSuffix = ".mv.db"
47+
4448
private object ProjectFilename {
45-
def apply(projectId: ProjectId) = s"${getDbName(projectId)}.mv.db"
49+
def apply(folder: File, projectId: ProjectId) = {
50+
val dbName = getDbName(projectId)
51+
52+
// The current H2 database driver reads existing PageStore db files
53+
// but will create new db files using MVStore.
54+
val dbPageStoreFilename = s"$dbName$PageStoreFileSuffix"
55+
if ((folder / dbPageStoreFilename).exists)
56+
dbPageStoreFilename
57+
else
58+
s"$dbName$MultiVersionStoreFileSuffix"
59+
}
4660
def getDbName(projectId: ProjectId) = s"project-${projectId.num}"
4761

48-
val NameRegex = raw"project-(\d+)\.mv\.db".r
62+
val NameRegex = raw"project-(\d+)\.(?:h2|mv)\.db".r
4963

5064
def unapply(filename: String): Option[ProjectId] = filename match {
5165
case NameRegex(ProjectId(id)) => Some(id)
@@ -54,9 +68,9 @@ class SlickH2ProjectDataProvider(folder: File, actorSystem: ActorSystem) extends
5468
}
5569

5670
private val masterData = {
57-
val needsInit = !(folder / "master.mv.db").exists
71+
val needsInit = !((folder / s"$MasterDbName$MultiVersionStoreFileSuffix").exists || (folder / s"$MasterDbName$PageStoreFileSuffix").exists)
5872

59-
val db = Database.forURL(s"jdbc:h2:file:${(folder / "master").getCanonicalPath};DB_CLOSE_DELAY=10", driver = "org.h2.Driver")
73+
val db = Database.forURL(s"jdbc:h2:file:${(folder / MasterDbName).getCanonicalPath};DB_CLOSE_DELAY=10", driver = "org.h2.Driver")
6074
val data = new SlickMasterData(db, H2Driver)
6175

6276
if (needsInit) data.init
@@ -67,7 +81,7 @@ class SlickH2ProjectDataProvider(folder: File, actorSystem: ActorSystem) extends
6781
def getProject(id: ProjectId): ProjectData = getProjectInternal(id)
6882

6983
private def getProjectInternal(id: ProjectId, suppressInit: Boolean = false) = cache.getOrElseUpdate(id, {
70-
val needsInit = !(folder / ProjectFilename(id)).exists
84+
val needsInit = !(folder / ProjectFilename(folder, id)).exists
7185

7286
val db = Database.forURL(s"jdbc:h2:file:${(folder / ProjectFilename.getDbName(id)).getCanonicalPath};DB_CLOSE_DELAY=10", driver = "org.h2.Driver")
7387
val data = new SlickProjectData(id, db, H2Driver, masterData.metadataMaster get id.num, EncountersBufferSize, EncountersFlushInterval, actorSystem)

0 commit comments

Comments
 (0)