Skip to content

Commit

Permalink
Merge pull request #1076 from MichaelDecent/comm_pkg13
Browse files Browse the repository at this point in the history
Add Swarmauri Redis Document Store community package
  • Loading branch information
cobycloud authored Jan 15, 2025
2 parents c3a4818 + 00f0029 commit 6deda77
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Swarmauri Example Community Package
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"
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)
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
"""

0 comments on commit 6deda77

Please sign in to comment.