Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to generate gremlin queries from bytecode in python

Is it possible to generate gremlin script from the bytecode?

I am working on a POC in which I need to query graph Azure CosmosDB database via Gremlin API.

Currently, Azure CosmosDB does not support bytecode. Azure development team has started working on this but no release timeline has been published so far.

I would like to prepare working code which would require minimum refactoring in future when bytecode support will be generally available.

Based on the Apache TinkerPop docs, there are two ways of submitting Gremlin queries: bytecode and script

# script
client = Client('ws://localhost:8182/gremlin', 'g')
list = client.submit("g.V().has('person','name',name).out('knows')",{'name': 'marko'}).all()

# bytecode
g = traversal().withRemote(DriverRemoteConnection('ws://localhost:8182/gremlin','g'))
list = g.V().has("person","name","marko").out("knows").toList()

The "bytecode way" seems to me much more efficient (syntax checking, IDE intellisens, etc.) moreover I am interested in creating the DSL (Domain Specific Language).

Would it be possible to use the fluent api and serialize it to string, in a way similar to this:

client = Client('ws://localhost:8182/gremlin', 'g')
g = traversal()
q = g.V().has("person","name","marko").out("knows").toString()
list = client.submit(q).all()

I am using python 3.5 and gremlinpython 3.4.0

like image 980
Sebastian Widz Avatar asked Mar 20 '19 20:03

Sebastian Widz


1 Answers

It's definitely possible to generate a String representation of a traversal from bytecode. TinkerPop already does it for Groovy and Python scripts (for various reasons, primarily for testing but it has it's other uses like supporting lambdas in bytecode and for other utilitarian purposes). We accomplish this through ScriptTranslator implementations and there is one for Groovy and two for Python (where one is actually for Jython). The problem of course is that all of these ScriptTranslator instances are for technically for the JVM and it sounds like you need something for native Python.

Perhaps you could examine the PythonTranslator code and implement that in native Python? It's basically just a bunch of if-then and string concatenation.

like image 64
stephen mallette Avatar answered Nov 18 '22 11:11

stephen mallette