-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1076 from MichaelDecent/comm_pkg13
Add Swarmauri Redis Document Store community package
- Loading branch information
Showing
4 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
pkgs/community/swarmauri_documentstore_communityredis/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Swarmauri Example Community Package |
57 changes: 57 additions & 0 deletions
57
pkgs/community/swarmauri_documentstore_communityredis/pyproject.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
[tool.poetry] | ||
name = "swarmauri_documentstore_communityredis" | ||
version = "0.6.0.dev1" | ||
description = "Swarmauri Psutil Tool" | ||
authors = ["Jacob Stewart <[email protected]>"] | ||
license = "Apache-2.0" | ||
readme = "README.md" | ||
repository = "http://github.com/swarmauri/swarmauri-sdk" | ||
classifiers = [ | ||
"License :: OSI Approved :: Apache Software License", | ||
"Programming Language :: Python :: 3.10", | ||
"Programming Language :: Python :: 3.11", | ||
"Programming Language :: Python :: 3.12" | ||
] | ||
|
||
[tool.poetry.dependencies] | ||
python = ">=3.10,<3.13" | ||
|
||
# Swarmauri | ||
swarmauri_core = { path = "../../core" } | ||
swarmauri_base = { path = "../../base" } | ||
|
||
# Dependencies | ||
redis = "^4.0" | ||
|
||
|
||
[tool.poetry.group.dev.dependencies] | ||
flake8 = "^7.0" | ||
pytest = "^8.0" | ||
pytest-asyncio = ">=0.24.0" | ||
pytest-xdist = "^3.6.1" | ||
pytest-json-report = "^1.5.0" | ||
python-dotenv = "*" | ||
requests = "^2.32.3" | ||
|
||
[build-system] | ||
requires = ["poetry-core>=1.0.0"] | ||
build-backend = "poetry.core.masonry.api" | ||
|
||
[tool.pytest.ini_options] | ||
norecursedirs = ["combined", "scripts"] | ||
|
||
markers = [ | ||
"test: standard test", | ||
"unit: Unit tests", | ||
"integration: Integration tests", | ||
"acceptance: Acceptance tests", | ||
"experimental: Experimental tests" | ||
] | ||
log_cli = true | ||
log_cli_level = "INFO" | ||
log_cli_format = "%(asctime)s [%(levelname)s] %(message)s" | ||
log_cli_date_format = "%Y-%m-%d %H:%M:%S" | ||
asyncio_default_fixture_loop_scope = "function" | ||
|
||
[tool.poetry.plugins."swarmauri.documentstores"] | ||
RedisDocumentStore = "swarmauri_documentstore_communityredis.RedisDocumentStore:RedisDocumentStore" |
72 changes: 72 additions & 0 deletions
72
...documentstore_communityredis/swarmauri_documentstore_communityredis/RedisDocumentStore.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from typing import List, Optional | ||
from swarmauri_base.document_stores.DocumentStoreBase import DocumentStoreBase | ||
from swarmauri_core.documents.IDocument import IDocument | ||
import redis | ||
import json | ||
from redis.commands.search.field import TextField, NumericField, TagField | ||
from redis.commands.search.indexDefinition import IndexDefinition, IndexType | ||
|
||
|
||
class RedisDocumentStore(DocumentStoreBase): | ||
def __init__(self, host, password, port, db): | ||
"""Store connection details without initializing the Redis client.""" | ||
self._host = host | ||
self._password = password | ||
self._port = port | ||
self._db = db | ||
self._redis_client = None # Delayed initialization | ||
|
||
@property | ||
def redis_client(self): | ||
"""Lazily initialize and return the Redis client using a factory method.""" | ||
if self._redis_client is None: | ||
print("here") | ||
self._redis_client = redis.Redis( | ||
host=self._host, password=self._password, port=self._port, db=self._db | ||
) | ||
print("there") | ||
return self._redis_client | ||
|
||
def add_document(self, document: IDocument) -> None: | ||
|
||
data = document.as_dict() | ||
doc_id = data["id"] | ||
del data["id"] | ||
self.redis_client.json().set(doc_id, "$", json.dumps(data)) | ||
|
||
def add_documents(self, documents: List[IDocument]) -> None: | ||
with self.redis_client.pipeline() as pipe: | ||
for document in documents: | ||
pipe.set(document.doc_id, document) | ||
pipe.execute() | ||
|
||
def get_document(self, doc_id: str) -> Optional[IDocument]: | ||
result = self.redis_client.json().get(doc_id) | ||
if result: | ||
return json.loads(result) | ||
return None | ||
|
||
def get_all_documents(self) -> List[IDocument]: | ||
keys = self.redis_client.keys("*") | ||
documents = [] | ||
for key in keys: | ||
document_data = self.redis_client.get(key) | ||
if document_data: | ||
documents.append(json.loads(document_data)) | ||
return documents | ||
|
||
def update_document(self, doc_id: str, updated_document: IDocument) -> None: | ||
self.add_document(updated_document) | ||
|
||
def delete_document(self, doc_id: str) -> None: | ||
self.redis_client.delete(doc_id) | ||
|
||
def __getstate__(self): | ||
"""Return the object state for serialization, excluding the Redis client.""" | ||
state = self.__dict__.copy() | ||
state["_redis_client"] = None # Exclude Redis client from serialization | ||
return state | ||
|
||
def __setstate__(self, state): | ||
"""Restore the object state after serialization, reinitializing the Redis client.""" | ||
self.__dict__.update(state) |
14 changes: 14 additions & 0 deletions
14
...swarmauri_documentstore_communityredis/swarmauri_documentstore_communityredis/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from .RedisDocumentStore import RedisDocumentStore | ||
|
||
__version__ = "0.6.0.dev26" | ||
__long_desc__ = """ | ||
# Swarmauri Redis document store Plugin | ||
This repository includes a Redis document store of a Swarmauri Plugin. | ||
Visit us at: https://swarmauri.com | ||
Follow us at: https://github.com/swarmauri | ||
Star us at: https://github.com/swarmauri/swarmauri-sdk | ||
""" |