Skip to content

Commit

Permalink
add store listing
Browse files Browse the repository at this point in the history
  • Loading branch information
ritch committed Oct 2, 2024
1 parent 35d6324 commit d4d9683
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 7 deletions.
7 changes: 6 additions & 1 deletion fiftyone/factory/repos/execution_store.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Execution store repository for handling storage and retrieval of stores.
Execution store repository.
"""

from pymongo.collection import Collection
Expand Down Expand Up @@ -29,6 +29,11 @@ def create_store(self, store_name, permissions=None) -> StoreDocument:
self._collection.insert_one(store_doc.dict())
return store_doc

def list_stores(self) -> list[str]:
"""Lists all stores in the execution store."""
# ensure that only store_name is returned, and only unique values
return self._collection.distinct("store_name")

def set_key(self, store_name, key, value, ttl=None) -> KeyDocument:
"""Sets or updates a key in the specified store."""
expiration = KeyDocument.get_expiration(ttl)
Expand Down
2 changes: 1 addition & 1 deletion fiftyone/operators/store/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def get_expiration(ttl: Optional[int]) -> Optional[datetime.datetime]:


class StoreDocument(KeyDocument):
"""Model representing a store in the execution store."""
"""Model representing a Store."""

key: str = "__store__"
value: Optional[Dict[str, Any]] = None
7 changes: 2 additions & 5 deletions fiftyone/operators/store/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,13 @@ def update_ttl(self, store_name, key, new_ttl):
store_name=store_name, key=key, ttl=new_ttl
)

def list_stores(self, search=None, **kwargs):
def list_stores(self):
"""Lists all stores matching the given criteria.
Args:
search (None): optional search term dict
Returns:
a list of :class:`fiftyone.store.models.StoreDocument`
"""
return self._repo.list_stores(search=search, **kwargs)
return self._repo.list_stores()

def delete_store(self, store_name):
"""Deletes the specified store.
Expand Down
8 changes: 8 additions & 0 deletions fiftyone/operators/store/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ def __init__(self, store_name: str, store_service: ExecutionStoreService):
self.store_name: str = store_name
self._store_service: ExecutionStoreService = store_service

def list_all_stores(self) -> list[str]:
"""Lists all stores in the execution store.
Returns:
list: A list of store names.
"""
return self._store_service.list_stores()

def get(self, key: str) -> Optional[Any]:
"""Retrieves a value from the store by its key.
Expand Down
7 changes: 7 additions & 0 deletions tests/unittests/execution_store_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,13 @@ def test_list_keys(self):
self.mock_collection.find.assert_called_once()
self.mock_collection.find.assert_called_with({"store_name": "widgets"})

def test_list_stores(self):
self.mock_collection.distinct.return_value = ["widgets", "gadgets"]
stores = self.store_repo.list_stores()
assert stores == ["widgets", "gadgets"]
self.mock_collection.distinct.assert_called_once()
self.mock_collection.distinct.assert_called_with("store_name")


class TestExecutionStoreIntegration(unittest.TestCase):
def setUp(self) -> None:
Expand Down

0 comments on commit d4d9683

Please sign in to comment.