Skip to main content

A lightweight in-memory graph database

Project description

ParvusDB a simple, in-memory graph database

Requirements

  • python-igraph
  • hy
  • python 3.5

Installation

pip3 install parvusdb

What is ParvusDB

ParvusDB is a small python3 library for handling graph operations. It acts as an in-memory graph database. It is meant to be used for small graphs (a few hundred nodes)

Graph format

Graphs are written as a collection of edges and vertices. A vertex is written as

{}(a)

where a is the name of the vertex. This name is used for the operations on the vertex iself. A vertex can have properties written inside the brackets

{'tag': 'PERSON', 'text': 'john'}(a)

The text inside the brackets is in the JSON format. Each of these properties is associated to the node and stored inside the graph

The edges are written as

{}(a,b)

where a is the source node name and b is the target node name. As for the vertex, properties can be added inside the brackets

{'relation': 'LIVES_AT'}(a,b)

A vertex can have a name too, though it must be given as a property

{'relation': 'LIVES_AT', 'name': 'r1'}(a,b)

A triplet can therefore be written in the form.

{'tag': 'PERSON', 'text': 'john'}(a), {'relation': 'LIVES_AT'}(a,b), {'tag': 'PLACE', 'text': 'London'}(b)

Keywords of the graph database

There are 6 commands (they must be typed in upper case)

  • CREATE
  • DELETE
  • MATCH
  • RETURN
  • SET
  • WHERE

The keyword CREATE

This command creates the graph on the right hand side

CREATE {'tag': 'PERSON', 'text': 'john'}(a), {'relation': 'LIVES_AT'}(a,b), {'tag': 'PLACE', 'text': 'London'}(b);

The keyword MATCH

This command matches a graph with the topology and properties specified in the right hand side

MATCH {'tag': 'PERSON'}(a), {'relation': 'LIVES_AT', 'name': 'r1'}(a,b), {'tag': 'PLACE'}(b);

The keyword DELETE

This keyword deletes the vertex or edge with the names on the right hand side

DELETE a, b, r1

The keyword WHERE

This keyword let the user specify a LISP code as a condition for the match. For example, if we want the "text" parameter of the node a to be in a list of names

MATCH {'tag': 'PERSON'}(a), {'relation': 'LIVES_AT', 'name': 'r1'}(a,b), {'tag': 'PLACE'}(b)
  WHERE (in (get a "text") ["john" "joseph" "joachim"]);

The keyword SET

This command let us modify the content of a graph. For example, if we want to change the text of the node a

MATCH {'tag': 'PERSON'}(a), {'relation': 'LIVES_AT', 'name': 'r1'}(a,b), {'tag': 'PLACE'}(b)
SET (assoc a "text" "not john anymore");

The keyword RETURN

This command let returns the properties of a specific node or edge

MATCH {'tag': 'PERSON'}(a), {'relation': 'LIVES_AT', 'name': 'r1'}(a,b), {'tag': 'PLACE'}(b)
RETURN a, r1;

The return value would be a list of the form

[{'a': {'tag': 'PERSON', 'text': 'john'}, 'r1': {'relation': 'LIVES_AT'}}]

If no vertex or edge name is specified, the system return the whole graph (in the 'parvusdb format' ).

Python3 code examples

Let's add a triplet to a graph

from igraph import Graph
from parvusdb import GraphDatabase


if __name__ == '__main__':
    g = Graph(directed=True)
    db = GraphDatabase(g)

    creation_string = """
    CREATE {'tag': 'PERSON', 'text': 'john'}(a), {'relation': 'LIVES_AT'}(a,b), 
           {'tag': 'PLACE', 'text': 'London'}(b) 
    RETURN;
    """
    lst = db.query(creation_string)
    print(lst)

which brings the output

[{'GRAPH': "{'tag': 'PERSON', 'text': 'john'}(a), {'tag': 'PLACE', 'text': 'London'}(b), {'relation': 'LIVES_AT'}(a,b)"}]

Then we can try to match elements of the triplet

from igraph import Graph, plot
from parvusdb import GraphDatabase


if __name__ == '__main__':
    g = Graph(directed=True)
    db = GraphDatabase(g)

    creation_string = """
    CREATE {'tag': 'PERSON', 'text': 'john'}(a), {'relation': 'LIVES_AT'}(a,b),
           {'tag': 'PLACE', 'text': 'London'}(b);
    """

    match_string = """
    MATCH {}(a), {'relation': 'LIVES_AT'}(a,b), {}(b) 
    RETURN a,b;
    """

    lst = db.query(creation_string)
    lst = db.query(match_string)
    print(lst)

with output

[{'a': {'name': 'a', 'tag': 'PERSON', 'text': 'john'}, 'b': {'name': 'b', 'tag': 'PLACE', 'text': 'London'}}]

We can limit the matching process by using WHERE

from igraph import Graph, plot
from parvusdb import GraphDatabase


if __name__ == '__main__':
    g = Graph(directed=True)
    db = GraphDatabase(g)


    creation_string = """
    CREATE {'tag': 'PERSON', 'text': 'john'}(a), {'relation': 'LIVES_AT'}(a,b),
           {'tag': 'PLACE', 'text': 'London'}(b)
    CREATE {'tag': 'PERSON', 'text': 'joseph'}(v1), {'relation': 'LIVES_AT'}(v1,v2),
           {'tag': 'PLACE', 'text': 'London'}(v2)
    """

    match_string = """
    MATCH {}(_a), {'relation': 'LIVES_AT'}(_a,_b), {}(_b)
      WHERE (= (get _a "text") "joseph")
    RETURN _a,_b;
    """

    lst = db.query(creation_string)
    lst = db.query(match_string)
    print(lst)

with output

[{'_b': {'text': 'London', 'tag': 'PLACE', 'name': 'v2'}, '_a': {'text': 'joseph', 'tag': 'PERSON', 'name': 'v1'}}]

TODO

  • Handling of errors in GraphDatabase.query_lines()
  • Ability to add LISP code outside WHERE and SET statements
  • Create a stand-alone command line for the database
  • Ability to match multiple items

Known issues

  • The igraph library does not like multiple edges on the same nodes, therefore the MATCH function would not work correctly in those cases

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

parvusdb-0.0.27.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

parvusdb-0.0.27-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file parvusdb-0.0.27.tar.gz.

File metadata

  • Download URL: parvusdb-0.0.27.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.8

File hashes

Hashes for parvusdb-0.0.27.tar.gz
Algorithm Hash digest
SHA256 6038c3274f5d079cdf90f7f39d74a94a3921be431f1b2331ae0e71e0980afa90
MD5 c99d17a798c80527bba0b408f1ba7364
BLAKE2b-256 8336a3a2e5501c65dae1de339a692c6f41699af68c95ab32a11bc72c069039e6

See more details on using hashes here.

File details

Details for the file parvusdb-0.0.27-py3-none-any.whl.

File metadata

  • Download URL: parvusdb-0.0.27-py3-none-any.whl
  • Upload date:
  • Size: 11.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.8

File hashes

Hashes for parvusdb-0.0.27-py3-none-any.whl
Algorithm Hash digest
SHA256 92ffe3d7ba5648461c33f71837909e9063991137f6aede559fa841ce49074689
MD5 b48cd518949e17c5ff2ce5412fc6ce7b
BLAKE2b-256 8a73ea82267bbe294ce65ce38f960a711d17d0ec3dc2f382fd119508ffe49a2b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page