-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Description Adds a procedure for decoding a protobuf value from within Cypher, given the byte string, a schema, and a type name. Also fixes a few bugs in the extant Protobuf tests and adds throwable documentation to the protobuf utilities. In order to avoid performing a file read on every invocation of `parseProtobuf` (name chosen to match `parseJson`), the `ProtobufParser` has been wrapped with a cache, owned by the AppState. This cache will retain up to 10 schema-type pairs, cycling through them as necessary. This slightly aids the protobuf construction problem by making it more explicit, putting the blocking work on the blocking dispatcher. ## Type of change Delete options that are not relevant, add if necessary - [x] Bug fix (non-breaking change which fixes an issue) - [x] New feature (non-breaking change which adds functionality) - [x] This change requires a documentation update # How Has This Been Tested? Describe the tests performed, including instructions to reproduce, and relevant details for your test configuration - [x] New unit tests added and pass # Checklist: - [x] I have performed a self-review of my code - [x] I have verified my code doesn't add an implementation for something that already exists - [x] I have commented my code, particularly in hard-to-understand areas - [x] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [x] I have added tests that prove my change is effective and works - [x] New and existing unit tests pass locally with my changes GitOrigin-RevId: 1100243f70feb9dba91ac7bdd50ed0b2a6f6eda5
- Loading branch information
1 parent
a0685d6
commit be1b85b
Showing
15 changed files
with
299 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
quine/src/main/scala/com/thatdot/quine/app/ingest/serialization/CypherParseProtobuf.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.thatdot.quine.app.ingest.serialization | ||
|
||
import java.net.URL | ||
|
||
import org.apache.pekko.stream.scaladsl.Source | ||
import org.apache.pekko.util.Timeout | ||
|
||
import com.typesafe.scalalogging.LazyLogging | ||
|
||
import com.thatdot.quine.graph.cypher | ||
import com.thatdot.quine.graph.cypher.{ | ||
Expr, | ||
Parameters, | ||
ProcedureExecutionLocation, | ||
QueryContext, | ||
Type, | ||
UserDefinedProcedure, | ||
UserDefinedProcedureSignature, | ||
Value | ||
} | ||
import com.thatdot.quine.model.QuineId | ||
import com.thatdot.quine.util.StringInput.filenameOrUrl | ||
|
||
/** Parse a protobuf message into a Cypher map according to a schema provided by a schema cache. | ||
* Because loading the schema is asynchronous, this must be a procedure rather than a function. | ||
*/ | ||
class CypherParseProtobuf(private val cache: ProtobufParser.Cache) extends UserDefinedProcedure with LazyLogging { | ||
def name: String = "parseProtobuf" | ||
|
||
def canContainUpdates: Boolean = false | ||
|
||
def isIdempotent: Boolean = true | ||
|
||
def canContainAllNodeScan: Boolean = false | ||
|
||
def call(context: QueryContext, arguments: Seq[Value], location: ProcedureExecutionLocation)(implicit | ||
parameters: Parameters, | ||
timeout: Timeout | ||
): Source[Vector[Value], _] = { | ||
val (bytes, schemaUrl, typeName): (Array[Byte], URL, String) = arguments match { | ||
case Seq(Expr.Bytes(bytes, bytesRepresentId), Expr.Str(schemaUrl), Expr.Str(typeName)) => | ||
if (bytesRepresentId) | ||
logger.info( | ||
s"""Received an ID (${QuineId(bytes).pretty(location.idProvider)}) as a source of | ||
|bytes to parse a protobuf value of type: $typeName.""".stripMargin.replace('\n', ' ') | ||
) | ||
(bytes, filenameOrUrl(schemaUrl), typeName) | ||
case _ => | ||
throw wrongSignature(arguments) | ||
} | ||
Source.future(cache.getFuture(schemaUrl, typeName)).map { parser => | ||
Vector(parser.parseBytes(bytes)) | ||
} | ||
} | ||
|
||
def signature: UserDefinedProcedureSignature = UserDefinedProcedureSignature( | ||
arguments = Seq("bytes" -> Type.Bytes, "schemaUrl" -> Type.Str, "typeName" -> Type.Str), | ||
outputs = Seq("value" -> cypher.Type.Map), | ||
description = "Parses a protobuf message into a Cypher map value" | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.