Skip to content

Commit

Permalink
Add support for proto versioning
Browse files Browse the repository at this point in the history
  • Loading branch information
Ostrzyciel committed Oct 10, 2023
1 parent a75c88d commit 60a1c98
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 13 deletions.
2 changes: 1 addition & 1 deletion core/src/main/protobuf_shared
Submodule protobuf_shared updated 4 files
+1 −0 .gitignore
+11 −1 README.md
+7 −2 grpc.proto
+71 −11 rdf.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,12 @@ trait ProtoDecoder[+TOut]:
def getStreamOpt: Option[RdfStreamOptions]

def ingestRow(row: RdfStreamRow): Option[TOut]

/**
* Checks if the version of the stream is supported.
* Throws an exception if not.
* @param options Options of the stream.
*/
protected final def checkVersion(options: RdfStreamOptions): Unit =
if options.version > Constants.protoVersion then
throw new RdfProtoDeserializationError(s"Unsupported proto version: ${options.version}")
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ sealed abstract class ProtoDecoderImpl[TNode, TDatatype : ClassTag, +TTriple, +T
throw new RdfProtoDeserializationError("Row kind is not set.")

protected def handleOptions(opts: RdfStreamOptions): Unit =
checkVersion(opts)
setStreamOpt(opts)

protected def handleTriple(triple: RdfTriple): Option[TOut] =
Expand Down Expand Up @@ -281,6 +282,7 @@ object ProtoDecoderImpl:
inner.get.ingestRow(row)

private def handleOptions(opts: RdfStreamOptions): Unit =
checkVersion(opts)
if inner.isDefined then
throw new RdfProtoDeserializationError("Stream options are already set." +
"The type of the stream cannot be inferred.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,10 @@ abstract class ProtoEncoder[TNode, -TTriple, -TQuad, -TQuoted](val options: RdfS
private def emitOptions(): Unit =
emittedOptions = true
extraRowsBuffer.append(
RdfStreamRow(RdfStreamRow.Row.Options(options))
RdfStreamRow(RdfStreamRow.Row.Options(
// Override whatever the user set in the options.
options.withVersion(Constants.protoVersion)
))
)


2 changes: 2 additions & 0 deletions core/src/main/scala/eu/ostrzyciel/jelly/core/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ package object core:
val jellyName = "Jelly"
val jellyFileExtension = "jelly"
val jellyContentType = "application/x-jelly-rdf"
val protoVersion = 1
val protoSemanticVersion = "1.0.0"
34 changes: 24 additions & 10 deletions core/src/test/scala/eu/ostrzyciel/jelly/core/ProtoDecoderSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,9 @@ class ProtoDecoderSpec extends AnyWordSpec, Matchers:

for ((testCase, streamType, streamName, expected) <- cases) do
s"decode $streamName" in {
val opts = JellyOptions.smallGeneralized.withStreamType(streamType)
val opts = JellyOptions.smallGeneralized
.withStreamType(streamType)
.withVersion(Constants.protoVersion)
val decoder = MockConverterFactory.anyStatementDecoder
val decoded = testCase
.encoded(opts)
Expand Down Expand Up @@ -388,30 +390,42 @@ class ProtoDecoderSpec extends AnyWordSpec, Matchers:
}

val streamTypeCases = Seq(
(MockConverterFactory.triplesDecoder, "Triples", RdfStreamType.QUADS),
(MockConverterFactory.quadsDecoder, "Quads", RdfStreamType.TRIPLES),
(MockConverterFactory.graphsDecoder, "Graphs", RdfStreamType.QUADS),
(MockConverterFactory.graphsAsQuadsDecoder, "GraphsAsQuads", RdfStreamType.TRIPLES),
(MockConverterFactory.anyStatementDecoder, "AnyStatement", RdfStreamType.UNSPECIFIED),
(() => MockConverterFactory.triplesDecoder, "Triples", RdfStreamType.TRIPLES, RdfStreamType.QUADS),
(() => MockConverterFactory.quadsDecoder, "Quads", RdfStreamType.QUADS, RdfStreamType.GRAPHS),
(() => MockConverterFactory.graphsDecoder, "Graphs", RdfStreamType.GRAPHS, RdfStreamType.QUADS),
(() => MockConverterFactory.graphsAsQuadsDecoder, "GraphsAsQuads", RdfStreamType.GRAPHS, RdfStreamType.TRIPLES),
(() => MockConverterFactory.anyStatementDecoder, "AnyStatement", RdfStreamType.TRIPLES, RdfStreamType.UNSPECIFIED),
)

for (decoder, decName, streamType) <- streamTypeCases do
for (decoderFactory, decName, streamType, invalidStreamType) <- streamTypeCases do
s"a ${decName}Decoder" should {
"throw exception on an empty stream type" in {
val data = wrapEncodedFull(Seq(JellyOptions.smallGeneralized))
val error = intercept[RdfProtoDeserializationError] {
decoder.ingestRow(data.head)
decoderFactory().ingestRow(data.head)
}
error.getMessage should include ("stream type is not")
}

"throw exception on an invalid stream type" in {
val data = wrapEncodedFull(Seq(
JellyOptions.smallGeneralized.withStreamType(streamType),
JellyOptions.smallGeneralized.withStreamType(invalidStreamType),
))
val error = intercept[RdfProtoDeserializationError] {
decoder.ingestRow(data.head)
decoderFactory().ingestRow(data.head)
}
error.getMessage should include ("stream type is not")
}

"throw exception on an unsupported proto version" in {
val data = wrapEncodedFull(Seq(
JellyOptions.smallGeneralized
.withStreamType(streamType)
.withVersion(Constants.protoVersion + 1)
))
val error = intercept[RdfProtoDeserializationError] {
decoderFactory().ingestRow(data.head)
}
error.getMessage should include("Unsupported proto version")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ object ProtoTestCases:
val GRAPH_REPEAT: RdfGraph = RdfGraph(RdfGraph.Graph.Repeat(RdfRepeat()))

def wrapEncoded(rows: Seq[RowValue]): Seq[RdfStreamRow.Row] = rows map {
case v: RdfStreamOptions => RdfStreamRow.Row.Options(v)
case v: RdfStreamOptions => v.version match
// If the version is not set, set it to the current version
case 0 => RdfStreamRow.Row.Options(v.withVersion(Constants.protoVersion))
// Otherwise assume we are checking version compatibility
case _ => RdfStreamRow.Row.Options(v)
case v: RdfDatatypeEntry => RdfStreamRow.Row.Datatype(v)
case v: RdfPrefixEntry => RdfStreamRow.Row.Prefix(v)
case v: RdfNameEntry => RdfStreamRow.Row.Name(v)
Expand Down

0 comments on commit 60a1c98

Please sign in to comment.