Skip to content

Commit bff1e92

Browse files
committed
add tests for loading rocks config file
1 parent df26f37 commit bff1e92

File tree

4 files changed

+103
-13
lines changed

4 files changed

+103
-13
lines changed

src/main/scala/com/scalableminds/fossildb/db/RocksDBStore.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,20 @@ class RocksDBManager(dataDir: Path, columnFamilies: List[String], optionsFilePat
3030
}
3131
val columnFamilyHandles = new util.ArrayList[ColumnFamilyHandle]
3232
var options = new DBOptions()
33-
.setCreateIfMissing(true)
34-
.setCreateMissingColumnFamilies(true)
3533
var cfListRef: mutable.Buffer[ColumnFamilyDescriptor] = mutable.Buffer()
3634
optionsFilePathOpt.map { optionsFilePath =>
3735
try {
3836
org.rocksdb.OptionsUtil.loadOptionsFromFile(optionsFilePath, Env.getDefault, options, cfListRef.asJava)
3937
logger.info("successfully loaded rocksdb options from " + optionsFilePath)
4038
} catch {
4139
case e: Exception => {
42-
logger.error("Failed to load rocksdb options from file " + optionsFilePath + ": " + e.getMessage)
43-
System.exit(1)
40+
throw new Exception("Failed to load rocksdb options from file " + optionsFilePath, e)
4441
}
4542
}
4643
}
44+
options = options
45+
.setCreateIfMissing(true)
46+
.setCreateMissingColumnFamilies(true)
4747
logger.info("Opening RocksDB at " + dataDir.toAbsolutePath)
4848
val db = RocksDB.open(
4949
options,

src/test/scala/com/scalableminds/fossildb/FossilDBSuite.scala

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import org.scalatest.{BeforeAndAfterEach, FlatSpec}
1414

1515
import scala.concurrent.ExecutionContext
1616

17-
class FossilDBSuite extends FlatSpec with BeforeAndAfterEach {
18-
val testTempDir = "testData"
17+
class FossilDBSuite extends FlatSpec with BeforeAndAfterEach with TestHelpers {
18+
val testTempDir = "testData1"
1919
val dataDir = Paths.get(testTempDir, "data")
2020
val backupDir = Paths.get(testTempDir, "backup")
2121

@@ -35,13 +35,6 @@ class FossilDBSuite extends FlatSpec with BeforeAndAfterEach {
3535
val anotherKey = "anotherKey"
3636
val aThirdKey = "aThirdKey"
3737

38-
private def deleteRecursively(file: File): Unit = {
39-
if (file.isDirectory)
40-
file.listFiles.foreach(deleteRecursively)
41-
if (file.exists && !file.delete)
42-
throw new Exception(s"Unable to delete ${file.getAbsolutePath}")
43-
}
44-
4538
override def beforeEach = {
4639
deleteRecursively(new File(testTempDir))
4740
new File(testTempDir).mkdir()
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright (C) 2011-2018 scalable minds UG (haftungsbeschränkt) & Co. KG. <http://scm.io>
3+
*/
4+
package com.scalableminds.fossildb
5+
6+
import java.io.File
7+
import java.nio.file.Paths
8+
9+
import com.scalableminds.fossildb.db.StoreManager
10+
import org.rocksdb.{ColumnFamilyDescriptor, DBOptions, Env}
11+
import org.scalatest.{BeforeAndAfterEach, FlatSpec}
12+
13+
import scala.collection.mutable
14+
import scala.collection.JavaConverters._
15+
import scala.io.Source
16+
17+
class RocksOptionsSuite extends FlatSpec with BeforeAndAfterEach with TestHelpers {
18+
19+
val testTempDir = "testData2"
20+
val dataDir = Paths.get(testTempDir, "data")
21+
val backupDir = Paths.get(testTempDir, "backup")
22+
23+
val collectionA = "collectionA"
24+
val collectionB = "collectionB"
25+
26+
val columnFamilies = List(collectionA, collectionB)
27+
28+
29+
30+
override def beforeEach = {
31+
deleteRecursively(new File(testTempDir))
32+
new File(testTempDir).mkdir()
33+
}
34+
35+
override def afterEach = {
36+
deleteRecursively(new File(testTempDir))
37+
}
38+
39+
40+
41+
"Initializing the StoreManager" should "load and use a specified config file" in {
42+
val file = new File(testTempDir, "testConfig.ini")
43+
writeToFile(file, "[Version]\n rocksdb_version=5.11.3\n options_file_version=1.1\n\n[DBOptions]\n stats_dump_period_sec=700\n\n[CFOptions \"default\"]\n\n")
44+
45+
val storeManager = new StoreManager(dataDir, backupDir, columnFamilies, Some(file.getPath))
46+
47+
var options = new DBOptions()
48+
.setStatsDumpPeriodSec(100)
49+
var cfListRef: mutable.Buffer[ColumnFamilyDescriptor] = mutable.Buffer()
50+
// if successful, the rocksdb writes the loaded options to a file that can then be retreived with loadLatestOptions
51+
// we test that that one now includes the value 700 from the file above, rather than the 100 specified as a default
52+
org.rocksdb.OptionsUtil.loadLatestOptions(dataDir.toString, Env.getDefault, options, cfListRef.asJava)
53+
assert(options.statsDumpPeriodSec() == 700)
54+
storeManager.close
55+
}
56+
57+
it should "fail if specified config file does not exist" in {
58+
assertThrows[Exception] {
59+
new StoreManager(dataDir, backupDir, columnFamilies, Some("nonExistingPath.ini"))
60+
}
61+
}
62+
63+
it should "fail if specified config file is invalid" in {
64+
val file = new File(testTempDir, "testConfig.ini")
65+
writeToFile(file, "[Version]\n rocksdb_version=5.11.3\n options_file_version=1.1\n\n[DBOptions]\n stats_dump_period_sec=700")
66+
67+
assertThrows[Exception] {
68+
new StoreManager(dataDir, backupDir, columnFamilies, Some(file.getPath))
69+
}
70+
}
71+
72+
73+
74+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (C) 2011-2018 scalable minds UG (haftungsbeschränkt) & Co. KG. <http://scm.io>
3+
*/
4+
package com.scalableminds.fossildb
5+
6+
import java.io.{BufferedWriter, File, FileWriter}
7+
8+
trait TestHelpers {
9+
10+
protected def deleteRecursively(file: File): Unit = {
11+
if (file.isDirectory)
12+
file.listFiles.foreach(deleteRecursively)
13+
if (file.exists && !file.delete)
14+
throw new Exception(s"Unable to delete ${file.getAbsolutePath}")
15+
}
16+
17+
protected def writeToFile(file: File, content: String): Unit = {
18+
val bw = new BufferedWriter(new FileWriter(file))
19+
bw.write(content)
20+
bw.close()
21+
}
22+
23+
}

0 commit comments

Comments
 (0)