Skip to content

Commit 67a8e65

Browse files
committed
compact-a-collection
1 parent 514bc28 commit 67a8e65

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

arangoasync/collection.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
)
1818
from arangoasync.exceptions import (
1919
CollectionChecksumError,
20+
CollectionCompactError,
2021
CollectionConfigureError,
2122
CollectionPropertiesError,
2223
CollectionRecalculateCountError,
@@ -48,6 +49,7 @@
4849
from arangoasync.result import Result
4950
from arangoasync.serialization import Deserializer, Serializer
5051
from arangoasync.typings import (
52+
CollectionInfo,
5153
CollectionProperties,
5254
CollectionStatistics,
5355
IndexProperties,
@@ -632,6 +634,30 @@ def response_handler(resp: Response) -> None:
632634

633635
await self._executor.execute(request, response_handler)
634636

637+
async def compact(self) -> Result[CollectionInfo]:
638+
"""Compact a collection.
639+
640+
Returns:
641+
CollectionInfo: Collection information.
642+
643+
Raises:
644+
CollectionCompactError: If compaction fails.
645+
646+
References:
647+
- `compact-a-collection <https://docs.arangodb.com/stable/develop/http-api/collections/#compact-a-collection>`__
648+
""" # noqa: E501
649+
request = Request(
650+
method=Method.PUT,
651+
endpoint=f"/_api/collection/{self.name}/compact",
652+
)
653+
654+
def response_handler(resp: Response) -> CollectionInfo:
655+
if not resp.is_success:
656+
raise CollectionCompactError(resp, request)
657+
return CollectionInfo(self.deserializer.loads(resp.raw_body))
658+
659+
return await self._executor.execute(request, response_handler)
660+
635661
async def truncate(
636662
self,
637663
wait_for_sync: Optional[bool] = None,

arangoasync/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,10 @@ class CollectionConfigureError(ArangoServerError):
191191
"""Failed to configure collection properties."""
192192

193193

194+
class CollectionCompactError(ArangoServerError):
195+
"""Failed to compact collection."""
196+
197+
194198
class CollectionDeleteError(ArangoServerError):
195199
"""Failed to delete collection."""
196200

tests/test_collection.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from arangoasync.errno import DATA_SOURCE_NOT_FOUND, INDEX_NOT_FOUND
66
from arangoasync.exceptions import (
77
CollectionChecksumError,
8+
CollectionCompactError,
89
CollectionConfigureError,
910
CollectionPropertiesError,
1011
CollectionRecalculateCountError,
@@ -84,6 +85,12 @@ async def test_collection_misc_methods(doc_col, bad_col, docs, cluster):
8485
await bad_col.recalculate_count()
8586
await doc_col.recalculate_count()
8687

88+
# Compact
89+
with pytest.raises(CollectionCompactError):
90+
await bad_col.compact()
91+
res = await doc_col.compact()
92+
assert res.name == doc_col.name
93+
8794

8895
@pytest.mark.asyncio
8996
async def test_collection_rename(cluster, db, bad_col, docs):

0 commit comments

Comments
 (0)