Menu

Torque - Cog's graph query language.

Torque is a query language inspired by Gizmo

Cog stores graph as triples:

vertex <predicate> vertex

Sample data

alice follows bob
bob follows fred
bob status cool_person
charlie follows bob
charlie follows dani
dani follows bob
dani follows greg
dani status cool_person
emily follows fred
fred follows greg
greg status cool_person

Loading triples:

from cog.torque import Loader
from cog.torque import Graph

loader = Loader('path/to/dbdir')
loader.load_triples('path/to/triple_file', "graph_name")

Loading edge list:

from cog.torque import Loader
from cog.torque import Graph
loader = Loader('path/to/dbdir')
loader.load_edgelist('path/to/edgelist', "graph_name")

Torque query examples

starting from a vertex, follow all outgoing edges and list all vertices

from cog.torque import Graph
g = Graph(graph_name="people", cog_dir='path/to/dbdir')
g.v("bob").out().all()

{"result": [{"id": "greg", "id": "alice"}]}

starting from a vertex, follow all outgoing edges and count vertices

from cog.torque import Graph
g = Graph(graph_name="people", cog_dir='path/to/dbdir')
g.v("bob").out().count()

'2'

starting from a vertex, follow all out going edges and tag them

from cog.torque import Graph
g = Graph(graph_name="people", cog_dir='path/to/dbdir')
g.v("bob").out().tag("source").out().tag("target").all()

{"result": [{"source": "<fred>", "id": "<greg>", "target": "<greg>"}]</greg></greg></fred>}

By tagging the vertices 'source' and 'target', the resulting graph can be visualized using Sigma JS

starting from a vertex, follow all incoming edges and list all vertices

from cog.torque import Graph
g = Graph(graph_name="people", cog_dir='path/to/dbdir')
g.v("bob").inc().all()

{"result": [{"id": "alice", "id": "dani"}]}

Adding vertices

from cog.torque import Graph
g = Graph(graph_name="people", cog_dir='path/to/dbdir')
g.put("A","letters","B").put("B","letters","C").put("C","letters","D")
g.put("Z","letters","D")
g.v("A").out(["letters"]).out().out().inc().all()

Query makes multiple hops on outgoing edges.

{"result": [{"id": "C"}, {"id": "Z"}]}'

Low level key-value store API:

Every record inserted into Cog's key-value store is directly persisted on to disk. It stores and retrieves data based
on hash values of the keys, it can perform fast look ups (O(1) avg) and fast (O(1) avg) inserts.

from cog.database import Cog

cogdb = Cog('path/to/dbdir')

# create a namespace
cogdb.create_namespace("my_namespace")

# create new table
cogdb.create_table("new_db", "my_namespace")

# put some data
cogdb.put(('key','val'))

# retrieve data 
cogdb.get('key')

# put some more data
cogdb.put(('key2','val2'))

# scan
scanner = cogdb.scanner()
for r in scanner:
    print r

# delete data
cogdb.delete('key1')
Posted by Arun Mahendra 2018-07-27

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.