Skip to content

Commit 066654d

Browse files
authored
storage: create parent directories if they don't exist (#281)
prior to this change, the test would fail with: ``` java.io.FileNotFoundException: /tmp/foo/bar (No such file or directory) at java.base/java.io.RandomAccessFile.open0(Native Method) at java.base/java.io.RandomAccessFile.open(RandomAccessFile.java:345) at java.base/java.io.RandomAccessFile.<init>(RandomAccessFile.java:259) at java.base/java.io.RandomAccessFile.<init>(RandomAccessFile.java:214) at flatgraph.storage.Serialization$.writeGraph(Serialization.scala:23) ```
1 parent da76b98 commit 066654d

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

core/src/main/scala/flatgraph/storage/Serialization.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import flatgraph.storage.Manifest.*
88
import java.io.ByteArrayOutputStream
99
import java.nio.channels.FileChannel
1010
import java.nio.charset.StandardCharsets
11-
import java.nio.file.Path
11+
import java.nio.file.{Files, Path}
1212
import java.nio.{ByteBuffer, ByteOrder}
1313
import java.util.concurrent.atomic.AtomicLong
1414
import scala.collection.mutable
@@ -19,6 +19,10 @@ object Serialization {
1919
val fileOffset = new AtomicLong(16)
2020
val stringPool = mutable.LinkedHashMap[String, Int]()
2121

22+
// ensure parent directory exists
23+
val parentDir = storagePath.getParent
24+
if (Files.notExists(parentDir)) Files.createDirectories(parentDir)
25+
2226
val fileChannel =
2327
new java.io.RandomAccessFile(storagePath.toAbsolutePath.toFile, "rw").getChannel
2428

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package flatgraph
2+
3+
import flatgraph.TestSchema.testSerialization
4+
import flatgraph.misc.DebugDump.debugDump
5+
import flatgraph.storage.{Deserialization, Serialization}
6+
import org.scalatest.matchers.should.Matchers
7+
import org.scalatest.wordspec.AnyWordSpec
8+
9+
import java.nio.file.Files
10+
11+
class SerializationTests extends AnyWordSpec with Matchers {
12+
13+
"creates parent directories" in {
14+
val testRoot = Files.createTempDirectory(getClass.getSimpleName)
15+
val storagePath = testRoot.resolve("non-existing-parent/graph.fg")
16+
17+
val schema = TestSchema.make(1, 1)
18+
val graph = new Graph(schema)
19+
20+
val diff0 = new DiffGraphBuilder(schema)
21+
val V0_0 = new GenericDNode(0)
22+
val V0_1 = new GenericDNode(0)
23+
diff0
24+
._addEdge(V0_0, V0_1, 0)
25+
._addEdge(V0_1, V0_0, 0)
26+
DiffGraphApplier.applyDiff(graph, diff0)
27+
val originalDump = debugDump(graph)
28+
originalDump shouldBe
29+
"""#Node numbers (kindId, nnodes) (0: 2), total 2
30+
|Node kind 0. (eid, nEdgesOut, nEdgesIn): (0, 2 [dense], 2 [dense]),
31+
| V0_0 [0] -> V0_1
32+
| V0_0 [0] <- V0_1
33+
| V0_1 [0] -> V0_0
34+
| V0_1 [0] <- V0_0
35+
|""".stripMargin
36+
37+
Serialization.writeGraph(graph, storagePath)
38+
39+
val deserialized = Deserialization.readGraph(storagePath, Option(graph.schema))
40+
val newDump = debugDump(deserialized)
41+
originalDump shouldBe newDump
42+
}
43+
44+
}

0 commit comments

Comments
 (0)