Skip to content

[DE-667]: GET /_api/collection/{name} & PUT /_api/collection/{name}/compact #286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions arango/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
from arango.exceptions import (
ArangoServerError,
CollectionChecksumError,
CollectionCompactError,
CollectionConfigureError,
CollectionInformationError,
CollectionLoadError,
CollectionPropertiesError,
CollectionRecalculateCountError,
Expand Down Expand Up @@ -335,6 +337,26 @@ def response_handler(resp: Response) -> Json:

return self._execute(request, response_handler)

def info(self) -> Result[Json]:
"""Return the collection information.

:return: Information about the collection.
:rtype: dict
:raise arango.exceptions.CollectionInformationError: If retrieval fails.
"""
request = Request(
method="get",
endpoint=f"/_api/collection/{self.name}",
read=self.name,
)

def response_handler(resp: Response) -> Json:
if resp.is_success:
return format_collection(resp.body)
raise CollectionInformationError(resp, request)

return self._execute(request, response_handler)

def configure(
self,
sync: Optional[bool] = None,
Expand Down Expand Up @@ -480,6 +502,35 @@ def response_handler(resp: Response) -> str:

return self._execute(request, response_handler)

def compact(self) -> Result[Json]:
"""Compact a collection.

Compacts the data of a collection in order to reclaim disk space.
The operation will compact the document and index data by rewriting the
underlying .sst files and only keeping the relevant entries.

Under normal circumstances, running a compact operation is not necessary, as
the collection data will eventually get compacted anyway. However, in some
situations, e.g. after running lots of update/replace or remove operations,
the disk data for a collection may contain a lot of outdated data for which the
space shall be reclaimed. In this case the compaction operation can be used.

:return: Collection compact.
:rtype: dict
:raise arango.exceptions.CollectionCompactError: If retrieval fails.
"""
request = Request(
method="put",
endpoint=f"/_api/collection/{self.name}/compact",
)

def response_handler(resp: Response) -> Json:
if resp.is_success:
return format_collection(resp.body)
raise CollectionCompactError(resp, request)

return self._execute(request, response_handler)

def load(self) -> Result[bool]:
"""Load the collection into memory.

Expand Down
8 changes: 8 additions & 0 deletions arango/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ class CollectionListError(ArangoServerError):
"""Failed to retrieve collections."""


class CollectionInformationError(ArangoServerError):
"""Failed to retrieve collection information."""


class CollectionPropertiesError(ArangoServerError):
"""Failed to retrieve collection properties."""

Expand All @@ -274,6 +278,10 @@ class CollectionChecksumError(ArangoServerError):
"""Failed to retrieve collection checksum."""


class CollectionCompactError(ArangoServerError):
"""Failed to compact collection."""


class CollectionCreateError(ArangoServerError):
"""Failed to create collection."""

Expand Down
10 changes: 10 additions & 0 deletions tests/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,16 @@ def test_collection_misc_methods(col, bad_col, cluster):
bad_col.recalculate_count()
assert err.value.error_code in {11, 1228}

# Test collection info
info = col.info()
assert set(info.keys()) == {"id", "name", "system", "type", "status", "global_id"}
assert info["name"] == col.name
assert info["system"] is False

# Test collection compact
result = col.compact()
assert result == info


def test_collection_management(db, bad_db, cluster):
# Test create collection
Expand Down