Using Rexster Server-Side Scripts with Bulbs

194 views
Skip to first unread message

James Thornton

unread,
Nov 11, 2013, 10:59:26 PM11/11/13
to gremli...@googlegroups.com
Hi Everyone -

I reworked Bulbs' Gremlin scripts module to make it easier to work with server-side scripts, and I laid the groundwork for autoloading Model-specific namespaces.

With server-scripts, you don't have to send the method body on each request so this allows for better code organization and reuse.

Most of the changes are in bulbs/groovy.py:
https://github.com/espeed/bulbs/blob/master/bulbs/groovy.py

See the Oct 26+ commits: https://github.com/espeed/bulbs/commits/master

Here's an overview of what to do...

To enable server side scripts, configure Rexster as per its docs:

...and in Bulbs Config, set server_scripts = True
https://github.com/espeed/bulbs/blob/master/bulbs/config.py

This will configure Bulbs' scripts.get("some_method_name") to return the Gremlin script's method signature instead of the method body and thus Bulbs will use the method signature as the value for "script" in the Rexster request.

For example, Bulbs/Rexster's outV() method is executed via a Gremlin script:
https://github.com/espeed/bulbs/blob/master/bulbs/rexster/client.py#L561

If you set the Python log level to DEBUG, it will display the request values sent to Rexster -- notice the "script" value in the POST body for james.outV("knows"):

>>> from bulbs.rexster import Graph, DEBUG
>>> g = Graph()
>>> g.config.server_scripts = True
>>> g.config.set_logger(DEBUG)

>>> james = g.vertices.create(name="James")
POST url: http://localhost:8182/graphs/emptygraph/vertices
POST body: {"name":"James"}

>>> julie = g.vertices.create(name="Julie")
POST url: http://localhost:8182/graphs/emptygraph/vertices
POST body: {"name":"Julie"}

>>> g.edges.create(james, "knows", julie)
POST url: http://localhost:8182/graphs/emptygraph/edges
POST body: {"_label":"knows","_outV":471,"_inV":472}
<Edge: http://localhost:8182/graphs/emptygraph/edges/473>

>>> james.outV("knows")
POST url: http://localhost:8182/graphs/emptygraph/tp/gremlin
POST body: {"load":["gremlin"],"params":{"start":null,"_id":471,"limit":null,"label":"knows"},
"script":"outV(_id, label, start, limit)"}
<generator object <genexpr> at 0x1de4140>

The "script" value is set to outV's Gremlin method signature defined here:
https://github.com/espeed/bulbs/blob/master/bulbs/gremlin.groovy#L72

The "params" value corresponds to the method signature, but it doesn't differ when config.server_scripts is set to True.

The "load" value contains a list of the server-side groovy file names required by the method, sans the .groovy extensions:
https://github.com/espeed/bulbs/blob/master/bulbs/rexster/client.py#L355

Like Rexster, Bulbs uses the first part of the Groovy filename as a namespace. 

For example, methods defined in gremlin.groovy files are added to the Bulbs "gremlin" namespace. 

All of Bulbs' pre-defined Gremlin scripts are defined in "gremlin.groovy" files and thus "gremlin" is the default namespace:
You can have multiple/additional "gremlin.groovy" files in your app. Do this if you want to keep everything under the same namespace or if you want to override a pre-defined method:

>>> g.scripts.update("/path/to/gremlin.groovy")


You can create app-specific and model-specific namespaces by defining your Gremlin methods in files with names like myapp.groovy or somemodel.groovy:

>>> g.scripts.update("/path/to/myapp.groovy")
>>> g.scripts.update("/path/to/somemodel.groovy")

To generate concatenated server-side script files for each namespace, use the g.make_script_files() method:

>>> g.make_script_files() # write files to the current dir, or...
>>> g.make_script_files("/path/to/scripts/dir") # write files to the specified dir

The make_scripte_files() method will create a separate .groovy file for each namespace. If you override a method in a namespace, only the latest will be included in the generated file. 


Let me know if you have any questions.

- James

Stephen Mallette

unread,
Nov 12, 2013, 7:51:49 AM11/12/13
to gremli...@googlegroups.com
Hi James....this looks like a nice addition to Bulbs.  I think it's the first Rexster Client to take advantage of that Rexster feature.  Thanks!

Stephen


--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

project2501

unread,
Nov 12, 2013, 12:29:09 PM11/12/13
to gremli...@googlegroups.com
That's great.

I guess I will also ask (slightly off-topic) if Bulbs and this feature support Titan 0.3.2 directly?

Thanks for the great work James!

James Thornton

unread,
Nov 12, 2013, 12:53:59 PM11/12/13
to gremli...@googlegroups.com
Yes, Titan Server supports server-side scripts, and Bulbs 0.3.24 supports Titan 0.4.

- James 


Reply all
Reply to author
Forward
0 new messages