Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

storage: create parent directories if they don't exist #281

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion core/src/main/scala/flatgraph/storage/Serialization.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import flatgraph.storage.Manifest.*
import java.io.ByteArrayOutputStream
import java.nio.channels.FileChannel
import java.nio.charset.StandardCharsets
import java.nio.file.Path
import java.nio.file.{Files, Path}
import java.nio.{ByteBuffer, ByteOrder}
import java.util.concurrent.atomic.AtomicLong
import scala.collection.mutable
Expand All @@ -19,6 +19,10 @@ object Serialization {
val fileOffset = new AtomicLong(16)
val stringPool = mutable.LinkedHashMap[String, Int]()

// ensure parent directory exists
val parentDir = storagePath.getParent
if (Files.notExists(parentDir)) Files.createDirectories(parentDir)

val fileChannel =
new java.io.RandomAccessFile(storagePath.toAbsolutePath.toFile, "rw").getChannel

Expand Down
44 changes: 44 additions & 0 deletions core/src/test/scala/flatgraph/SerializationTests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package flatgraph

import flatgraph.TestSchema.testSerialization
import flatgraph.misc.DebugDump.debugDump
import flatgraph.storage.{Deserialization, Serialization}
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec

import java.nio.file.Files

class SerializationTests extends AnyWordSpec with Matchers {

"creates parent directories" in {
val testRoot = Files.createTempDirectory(getClass.getSimpleName)
val storagePath = testRoot.resolve("non-existing-parent/graph.fg")

val schema = TestSchema.make(1, 1)
val graph = new Graph(schema)

val diff0 = new DiffGraphBuilder(schema)
val V0_0 = new GenericDNode(0)
val V0_1 = new GenericDNode(0)
diff0
._addEdge(V0_0, V0_1, 0)
._addEdge(V0_1, V0_0, 0)
DiffGraphApplier.applyDiff(graph, diff0)
val originalDump = debugDump(graph)
originalDump shouldBe
"""#Node numbers (kindId, nnodes) (0: 2), total 2
|Node kind 0. (eid, nEdgesOut, nEdgesIn): (0, 2 [dense], 2 [dense]),
| V0_0 [0] -> V0_1
| V0_0 [0] <- V0_1
| V0_1 [0] -> V0_0
| V0_1 [0] <- V0_0
|""".stripMargin

Serialization.writeGraph(graph, storagePath)

val deserialized = Deserialization.readGraph(storagePath, Option(graph.schema))
val newDump = debugDump(deserialized)
originalDump shouldBe newDump
}

}
Loading