Generic Redis-based graph connection manager
Project description
Redis Graph Connection Manager
Redis Graph Connection Manager (module name: redisgraph) is a Python module designed to efficiently manage and query domain-to-member edge graphs using Redis as the backend.
Online Documentation
https://redisgraph.readthedocs.io/en/latest/
Dependencies
- Python 3.8 or above
- Redis server (for backend storage)
redisPython client library (install viapip install redis)
Features
| Feature | Description |
|---|---|
| Add Edge | Add outgoing and incoming edges between domains and subjects. |
| Add Multiple Edges | Efficiently add multiple outgoing edges from a domain to a list of subjects. |
| Paginated Edge Retrieval | Retrieve active and removed edges with pagination support. |
| All Active Edges | Fetch all active outgoing edges for a domain without version tracking. |
| Intersection Query | Find common members between two domain graphs. |
| Incremental Versioning | Maintain atomic version numbers for domains and subjects for tracking changes. |
| Version Tracking | Track and retrieve the version (score) of each edge. |
| Soft Delete Edges | Remove outgoing edges with soft delete (negative version) for historical tracking. |
| Remove Domain | Remove all outgoing and incoming edges for a domain |
| Redis Backend | All operations are backed by a Redis server for performance and scalability. |
Algorithm Overview
Add Edge
When adding an edge from a domain entity (e.g., user1) to a subject (e.g., subject1):
- add_connection(domain, subject):
1. Add `subject1` to the outgoing set of `user1` with the next atomic domain version number.
2. Add `user1` to the incoming set of `subject1` with the next atomic subject version number.
This ensures that both outgoing and incoming relationships are tracked and versioned for efficient querying and higher-level historical tracking. This also serves the purpose of updating the edge graph in both directions for existing entries.
Remove Edge
When removing an edge from a domain entity (e.g., user1) to a subject (e.g., subject1):
- remove_connection(domain, subject):
1. Fetch the next atomic version number for the domain: `dv`.
2. Add `subject1` to the outgoing set of `user1` with `(-1)*dv` (soft delete).
3. Remove `user1` from the incoming set of `subject1` (hard delete).
This approach allows efficient soft deletion for outgoing edges (preserving history) while ensuring the incoming set is immediately updated.
Get Edges
When retrieving a paginated list of active edges and all removed (soft-deleted) edges for a given domain:
- get_connections(domain_id, graph_type=outgoing, size=100, cut_off=0):
- Fetch Active Edges:
- Query the `graph_type` edge set for `domain_id` to fetch up to `size` items with version scores
- Query range `(cut_off, +inf]` (exclusive lower bound).
- Collect the list of active edge IDs and their scores.
- Determine Score Range:
- If the active list is not empty, set `max_score` to the highest score among the fetched items.
- If the active list is empty, set `max_score` to the current domain version value.
- Fetch Removed Edges:
- For `graph_type=outgoing` graphs, query the same set for items with negative scores
- Query range `[-max_score, -cut_off)` (inclusive lower, exclusive upper bound).
- Collect the list of removed (soft-deleted) edge IDs.
- Return Results:
- Return the list of active edge IDs, the list of removed edge IDs, and the `max_score` value
- use as the `cut_off` in the next page query.
This approach enables efficient pagination of active edges and retrieval of all removed edges within the same score window. The caller needs to handle the next_page logic by passing the max_score as the new cut_off. The caller may stop pagination when the next_page does not change from the previous call. Only the active edges are paginated, while all removed edges within the range are fetched in one go. Only ascending order of score is supported as the pagination order.
Build
To build the package, ensure you have setuptools, wheel, and build installed. Run the build command in the root
rm -rf build dist *.egg-info
python -m pip install --upgrade build
python -m build --wheel
To clean built files in your Python project, you should remove the following directories and files:
- build/
- dist/
- *.egg-info/ (e.g., src/redisgraph.egg-info/)
Note: as part of the rename, the egg-info directory will move from the old project name to the new one (e.g., src/redisgraph.egg-info/).
Installation
You can install the Redis Graph Connection Manager in one of the following ways:
1. Using a Wheel File
- Download the latest
.whlfile. - Install with pip:
pip install graphconnectionmanager-0.1.6-py3-none-any.whl
2. Manual Installation
- Copy the
redisgraphpackage directory into your project. - Ensure your project can import the
redisgraphmodule as needed.
Example Usage
An example code which initializes the manager, adds an edge, and retrieves edges.
from redisgraph import GraphManager
import redis
# Initialize Redis client and GraphManager
redis_client = redis.Redis(host="localhost", port=6379, db=0)
manager = GraphManager(redis_client, prefix="graph", namespace="phonebook")
# Add an edge from domain 'room:1' to member 'user:42'
manager.add_connection("user_1", "8801791223344")
# Retrieve all active edges for 'room:1'
edges, removed, next_page = manager.get_connections("user_1")
print(edges, removed, next_page)
For more examples and advanced usage, explore the tests package in this repository.
Documentation
The project includes Sphinx-based documentation under the docs/ directory.
To build the HTML documentation:
cd docs
sphinx-build -b html . _build/html
If you have a make-based environment set up, you can alternatively run:
cd docs
make html
After a successful build, open the generated documentation by pointing your browser to:
xdg-open _build/html/index.html
(or open _build/html/index.html manually in your browser).
Testing
Default (recommended): in-memory Redis via fakeredis
By default, the test suite uses an in-memory Redis implementation (fakeredis), so you don't need a Redis server running locally.
When you run the tests you'll see a one-line banner like:
[redisgraph tests] redis backend: fakeredis (in-memory, db=0)
Run tests:
pytest
Tox: run the test suite on multiple Python versions
This repository includes a tox.ini that runs the test suite on Linux for:
- Python 3.8 (
py38) - Python 3.11 (
py311) - Python 3.14 (
py314)
Run all environments (will skip any missing interpreters):
tox
Run a specific Python version:
tox -e py311
If you are using Poetry:
poetry run tox
To run tox against a real Redis server (optional), set the same env vars used by pytest:
GRAPH_TEST_USE_REAL_REDIS=1 tox -e py311
Optional: run tests against a real Redis server
If you want to validate behavior against a real Redis server, set:
GRAPH_TEST_USE_REAL_REDIS=1
By default, it connects to localhost:6379, but you can override:
GRAPH_TEST_REDIS_HOST(default:localhost)GRAPH_TEST_REDIS_PORT(default:6379)
Notes:
db=0anddecode_responses=Trueare intentionally fixed for the test suite.- Tests clean up only the keys they create (scoped to the GraphManager's key patterns).
Example:
GRAPH_TEST_USE_REAL_REDIS=1 \
GRAPH_TEST_REDIS_HOST=localhost \
GRAPH_TEST_REDIS_PORT=6379 \
pytest
Coverage
To generate coverage statistics (requires the pytest-cov plugin):
pytest --cov=redisgraph --cov-report=term-missing
To generate coverage statistics (requires the pytest-cov plugin):
pytest --cov=redisgraph --cov-report=term-missing
You can also generate an HTML coverage report. This will create an htmlcov directory with detailed coverage reports.
pytest --cov=redisgraph --cov-report=html:htmlcov
xdg-open htmlcov/index.html
For more details and advanced test scenarios, see the tests package in this repository.
Code Style
Use Black with pre-commit hooks for automatic formatting and linting. Ensure you have pre-commit installed and configured in your project. See the .pre-commit-config.yaml file for details. Trigger manually with:
git add . --all
pre-commit run # Optionally use `--all-files` flag for the first time
License
This is a demonstration project for educational purposes. It is provided "as is" without any warranties. Licensed under the MIT License. See LICENSE for details.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file graphconnectionmanager-0.1.6.tar.gz.
File metadata
- Download URL: graphconnectionmanager-0.1.6.tar.gz
- Upload date:
- Size: 8.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b6ca9a9ab7234e9018b1eef025ac92c387bd7cfdfe5cd46dee736f4860440117
|
|
| MD5 |
334f0231eabba9aa50efd1582af6da2c
|
|
| BLAKE2b-256 |
a70d2c4b04f75f1e3b8d085a6f8900319d9e0ef19d3e3a0b12886520bdf3cee6
|
Provenance
The following attestation bundles were made for graphconnectionmanager-0.1.6.tar.gz:
Publisher:
publish.yml on zobayer1/RedisGraph
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
graphconnectionmanager-0.1.6.tar.gz -
Subject digest:
b6ca9a9ab7234e9018b1eef025ac92c387bd7cfdfe5cd46dee736f4860440117 - Sigstore transparency entry: 1172084855
- Sigstore integration time:
-
Permalink:
zobayer1/RedisGraph@943b49c8ea9dec8c24c599e43b82dfe764fa0522 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/zobayer1
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@943b49c8ea9dec8c24c599e43b82dfe764fa0522 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file graphconnectionmanager-0.1.6-py3-none-any.whl.
File metadata
- Download URL: graphconnectionmanager-0.1.6-py3-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
68e7e423e8403c2c6e7130e0796ffbf6331d87e0f2714d526d02a0ab4acadca7
|
|
| MD5 |
109eed29cf3d2f85bbf6bd07dfcf9671
|
|
| BLAKE2b-256 |
dc0c3239a7716deea2ec9d83a0b2176e09aaaa732d9678c1b88edee50235812f
|
Provenance
The following attestation bundles were made for graphconnectionmanager-0.1.6-py3-none-any.whl:
Publisher:
publish.yml on zobayer1/RedisGraph
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
graphconnectionmanager-0.1.6-py3-none-any.whl -
Subject digest:
68e7e423e8403c2c6e7130e0796ffbf6331d87e0f2714d526d02a0ab4acadca7 - Sigstore transparency entry: 1172084871
- Sigstore integration time:
-
Permalink:
zobayer1/RedisGraph@943b49c8ea9dec8c24c599e43b82dfe764fa0522 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/zobayer1
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@943b49c8ea9dec8c24c599e43b82dfe764fa0522 -
Trigger Event:
workflow_dispatch
-
Statement type: