Skip to content

Commit

Permalink
Support Agensgraph connection
Browse files Browse the repository at this point in the history
Signed-off-by: Prahalad Deshpande <[email protected]>
  • Loading branch information
infominer75 committed Jan 23, 2023
1 parent a53b237 commit f521e55
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 9 deletions.
37 changes: 33 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,23 @@ Advanced functionality APIs can be built upon the core API layer. The small API
* Docker runtime to execute integration tests
* Connectivity to an instance of a graph database for integration tests.

## Supported Graph databases
| Database Name | gograph version |
|---|---|
| [Neo4J](https://neo4j.com/) | v0.1.0 |
| [Memgraph](https://memgraph.com/) | v0.1.0 |
| [Agensgraph](https://github.com/bitnine-oss/agensgraph) | v0.2.0 |

## Source code layout
| Package Name | Description |
|---|---|
| core | Core `struct` and type definitions. Provides the `Connection` interface to be implemented for providing core graph operations related to query execution|
| core | Core `struct` and type definitions. Provides the `Connection` interface to be implemented for providing core graph operations related to query execution |
| query/cypher | Utility `struct`s for building cypher queries
| omg | Object Mapped Graph layer that facilitates storage and retrieval of user defined structs as vertices and edges within the graph database
| omg | Object Mapped Graph layer that facilitates storage and retrieval of user defined structs as vertices and edges within the graph database |
| neo | [Neo4J](https://neo4j.com/) specific implementation of the `Connection` interface
| integrationtests | Integration tests to validate core operations on target graph database instances
| memgraph | [Memgraph](https://memgraph.com/) specific implementation of the `Connection` interface |
| agensgraph | [Agensgraph](https://github.com/bitnine-oss/agensgraph) specific implementation of the `Connection` interface |
| integrationtests | Integration tests to validate core operations on target graph database instances |

## Usage

Expand Down Expand Up @@ -57,6 +65,27 @@ connection, err := memGraphConnectionFactory(protocol, host, realm, port, map[st
It is evident now that the the only place where any database specific information is required is for establishing the initial connection.
Once a `connection` instance to the target database has been obtained, the rest of the interactions with the graph database would occur through the methods of the `Connection` API and do not require any database specific knowledge.

The below code snippet from integration tests shows how to obtain an Agensgraph database connection

```go
host := integrationtests.GetFromEnvWithDefault("AGENS_HOST", "localhost")
portFromEnv := integrationtests.GetFromEnvWithDefault("AGENS_PORT", "5432")
portParsed, err := strconv.ParseInt(portFromEnv, 10, 32)
suite.NoError(err)
port := new(int32)
*port = int32(portParsed)
db := integrationtests.GetFromEnvWithDefault("AGENS_DB", "")
suite.NotEqual("", db)
protocol := integrationtests.GetFromEnvWithDefault("AGENS_PROTOCOL", "")
userName := integrationtests.GetFromEnvWithDefault("AGENS_USER", "")
suite.NotEqual("", userName)
pwd := integrationtests.GetFromEnvWithDefault("AGENS_PWD", "")
suite.NotEqual("", pwd)
agensConnectionFactory := core.GetConnectorFactory("agens")
conn, err := agensConnectionFactory(protocol, host, "", port, core.KVMap{agensgraph.AGENS_PASSWD_KEY: pwd, agensgraph.AGENS_USER_KEY: userName}, core.KVMap{agensgraph.AGENS_DBNAME_KEY: db})
suite.NoError(err)
```

#### Querying and persisting

The below snippet shows the persistence of a vertex and querying it back to the underlying database
Expand Down Expand Up @@ -184,7 +213,7 @@ chmod +x run_all_tests.sh
```

## Functionality Coverage
The `gograph` module has been developed and tested on the community editions of the graph databases (currently Neo4J and Memgraph). There are no APIs around additional functionality that is exposed by enterprise editions of these databases.
The `gograph` module has been developed and tested on the community editions of the graph databases mentioned in supported databases section. There are no APIs around additional functionality that is exposed by enterprise editions of these databases.

However for Neo4J, the ability to specify the target database is supported within the Neo4J connector shipped with this module.

Expand Down
37 changes: 32 additions & 5 deletions agensgraph/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ type agensContextKey string

// query context constants
const (
// ContextKeyQueryTimeoutSeconds can be used in context to specify the query timeout in milli-seconds
// ContextKeyQueryTimeoutSeconds is used in context to specify the query timeout in milli-seconds
// The value of this context parameter is currently ignored
ContextKeyQueryTimeoutMillis = agensContextKey("QueryTimeout")
// ContextKeyIsolationLevel can be used in context to specify the isolation level for query execution
ContextKeyIsolationLevel = agensContextKey("IsolationLevel")
ContextKeyReadOnly = agensContextKey("ReadOnly")
ContextKeyGraphName = agensContextKey("graphName")
// ContextKeyIsolationLevel is used in context to specify the isolation level for query execution
ContextKeyIsolationLevel = agensContextKey("IsolationLevel")
// ContextKeyReadonly is used to specify if the transaction is read-only
ContextKeyReadOnly = agensContextKey("ReadOnly")
// ContextKeyGraph name is used to specify the graph against which the operations should be executed
ContextKeyGraphName = agensContextKey("graphName")
// ContextKeyWriteModeCreate is used to specify if an vertex or edge creation should be done via CREATE instead of a MERGE
ContextKeyWriteModeCreate = agensContextKey("writeModeCreate")
)
const (
Expand All @@ -43,6 +47,29 @@ type queryOptions struct {
writeModeCreate bool
}

// AgensGraphConnection implements a connection to an [Agensgraph] database.
// Agensgraph is backed by PostgreSQL and hence the mechanism of obtaining the initial
// connection is equivalent to connecting to a PostgreSQL instance.
// Agensgraph stores multiple graphs with a schema created for each defined graph.
// Hence the graph name must be specified when executing an operation within the context
// as a string value using the ContextKeyGraphName key.
//
// Compared to other graph databases such as Neo4J, Agensgraph does not allow the MERGE
// operation to create new node or vertex labels. Only CREATE operations can create non-existent
// labels and corresponding vertex or edge.
// Hence applications must provide the correct semantics during a write operation on whether
// the generated cypher query must use CREATE or MERGE clause. By default the write operation
// would be done using a MERGE. This behavior can be overrriden by specifying a boolean
// value of true aganst the context key ContextKeyWriteModeCreate
//
// Isolation levels can be specified by specifying the correct
// sql.IsolationLevel valyes against the context key ContextKeyIsolationLevel key. Defaults
// to the default isolation provided by the database if not specified
//
// Read only transactions can be created by specifying a boolean value of true against
// the context key ContextKeyReadOnly. Defaults to false.
//
// [Agensgraph]: https://github.com/bitnine-oss/agensgraph
type AgensGraphConnection struct {
db *sql.DB
}
Expand Down

0 comments on commit f521e55

Please sign in to comment.