Skip to content

Commit

Permalink
change file header to less common and easier detectable magicbytes (#225
Browse files Browse the repository at this point in the history
)

* unix `file` util reports `data` as type
* `cat` and friends print `FLT GRPH...`
  • Loading branch information
mpollmeier authored Jul 12, 2024
1 parent 852eeb9 commit 647fc02
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
5 changes: 4 additions & 1 deletion core/src/main/scala/flatgraph/storage/Deserialization.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import java.nio.channels.FileChannel
import java.nio.charset.StandardCharsets
import java.nio.file.Path
import java.nio.{ByteBuffer, ByteOrder}
import java.util.Arrays
import scala.collection.mutable

object Deserialization {
Expand Down Expand Up @@ -151,7 +152,9 @@ object Deserialization {
}
header.flip()

if (header.getLong() != Keys.Header)
val headerBytes = new Array[Byte](Keys.Header.length)
header.get(headerBytes)
if (!Arrays.equals(headerBytes, Keys.Header))
throw new DeserializationException(s"expected header (`${Keys.Header}`), but found ${header.getLong}")

val manifestOffset = header.getLong()
Expand Down
11 changes: 3 additions & 8 deletions core/src/main/scala/flatgraph/storage/Serialization.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,11 @@ object Serialization {

def writeGraph(g: Graph, storagePath: Path): Unit = {
val fileOffset = new AtomicLong(16)

// else null
val stringPool = mutable.LinkedHashMap[String, Int]()

val fileChannel =
new java.io.RandomAccessFile(
storagePath.toAbsolutePath.toFile,
"rw"
).getChannel // if (conf.filename != null) { new java.io.RandomAccessFile("/tmp/foo.fg", "w").getChannel }}
new java.io.RandomAccessFile(storagePath.toAbsolutePath.toFile, "rw").getChannel

try {
innerWriteGraph(g, stringPool, fileOffset, fileChannel)
} finally {
Expand All @@ -50,7 +46,6 @@ object Serialization {
.collect {
case deleted: GNode if AccessHelpers.isDeleted(deleted) => deleted.seq()
}
.toArray
val size = g.nodeCountByKind(nodeKind)
nodes.addOne(new Manifest.NodeItem(nodeLabel, size, deletions))
}
Expand Down Expand Up @@ -102,7 +97,7 @@ object Serialization {
var pos = filePtr.get()
val header = new Array[Byte](16)
val headerBuf = ByteBuffer.wrap(header)
headerBuf.order(ByteOrder.LITTLE_ENDIAN).asLongBuffer().put(Keys.Header).put(pos)
headerBuf.order(ByteOrder.LITTLE_ENDIAN).put(Keys.Header).asLongBuffer().put(pos)
headerBuf.position(0)
var headPos = 0L
while (headerBuf.hasRemaining()) {
Expand Down
10 changes: 8 additions & 2 deletions core/src/main/scala/flatgraph/storage/package.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package flatgraph

import java.nio.charset.StandardCharsets

package object storage {

/** file header in order to be able to detect the file type */
val HeaderSize = 16 // size for than enough space for the MagicBytes
val MagicBytesString = "FLT GRPH"
val MagicBytes = MagicBytesString.getBytes(StandardCharsets.UTF_8)

object StorageType {
val Bool = "bool"
val Byte = "byte"
Expand Down Expand Up @@ -34,8 +41,7 @@ package object storage {
val Properties = "properties"
val StringPoolLength = "stringPoolLength"
val StringPoolBytes = "stringPoolBytes"
val Header = 0xdeadbeefdeadbeefL
val Header = MagicBytes
}

val HeaderSize = 16
}
2 changes: 1 addition & 1 deletion odb-convert/src/main/scala/flatgraph/convert/Convert.scala
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ object Convert {
var pos = filePtr.get()
val header = new Array[Byte](16)
val headerBuf = ByteBuffer.wrap(header)
headerBuf.order(ByteOrder.LITTLE_ENDIAN).asLongBuffer().put(Keys.Header).put(pos)
headerBuf.order(ByteOrder.LITTLE_ENDIAN).put(Keys.Header).asLongBuffer().put(pos)
headerBuf.position(0)
var headPos = 0L
while (headerBuf.hasRemaining()) {
Expand Down

0 comments on commit 647fc02

Please sign in to comment.