Skip to content

Commit bedbd72

Browse files
authored
[DE-667]: GET /_api/collection/{name} & PUT /_api/collection/{name}/compact (#286)
* initial commit * remove: breakpoint
1 parent 96335e6 commit bedbd72

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

arango/collection.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
from arango.exceptions import (
1010
ArangoServerError,
1111
CollectionChecksumError,
12+
CollectionCompactError,
1213
CollectionConfigureError,
14+
CollectionInformationError,
1315
CollectionLoadError,
1416
CollectionPropertiesError,
1517
CollectionRecalculateCountError,
@@ -335,6 +337,26 @@ def response_handler(resp: Response) -> Json:
335337

336338
return self._execute(request, response_handler)
337339

340+
def info(self) -> Result[Json]:
341+
"""Return the collection information.
342+
343+
:return: Information about the collection.
344+
:rtype: dict
345+
:raise arango.exceptions.CollectionInformationError: If retrieval fails.
346+
"""
347+
request = Request(
348+
method="get",
349+
endpoint=f"/_api/collection/{self.name}",
350+
read=self.name,
351+
)
352+
353+
def response_handler(resp: Response) -> Json:
354+
if resp.is_success:
355+
return format_collection(resp.body)
356+
raise CollectionInformationError(resp, request)
357+
358+
return self._execute(request, response_handler)
359+
338360
def configure(
339361
self,
340362
sync: Optional[bool] = None,
@@ -480,6 +502,35 @@ def response_handler(resp: Response) -> str:
480502

481503
return self._execute(request, response_handler)
482504

505+
def compact(self) -> Result[Json]:
506+
"""Compact a collection.
507+
508+
Compacts the data of a collection in order to reclaim disk space.
509+
The operation will compact the document and index data by rewriting the
510+
underlying .sst files and only keeping the relevant entries.
511+
512+
Under normal circumstances, running a compact operation is not necessary, as
513+
the collection data will eventually get compacted anyway. However, in some
514+
situations, e.g. after running lots of update/replace or remove operations,
515+
the disk data for a collection may contain a lot of outdated data for which the
516+
space shall be reclaimed. In this case the compaction operation can be used.
517+
518+
:return: Collection compact.
519+
:rtype: dict
520+
:raise arango.exceptions.CollectionCompactError: If retrieval fails.
521+
"""
522+
request = Request(
523+
method="put",
524+
endpoint=f"/_api/collection/{self.name}/compact",
525+
)
526+
527+
def response_handler(resp: Response) -> Json:
528+
if resp.is_success:
529+
return format_collection(resp.body)
530+
raise CollectionCompactError(resp, request)
531+
532+
return self._execute(request, response_handler)
533+
483534
def load(self) -> Result[bool]:
484535
"""Load the collection into memory.
485536

arango/exceptions.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,10 @@ class CollectionListError(ArangoServerError):
250250
"""Failed to retrieve collections."""
251251

252252

253+
class CollectionInformationError(ArangoServerError):
254+
"""Failed to retrieve collection information."""
255+
256+
253257
class CollectionPropertiesError(ArangoServerError):
254258
"""Failed to retrieve collection properties."""
255259

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

276280

281+
class CollectionCompactError(ArangoServerError):
282+
"""Failed to compact collection."""
283+
284+
277285
class CollectionCreateError(ArangoServerError):
278286
"""Failed to create collection."""
279287

tests/test_collection.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,16 @@ def test_collection_misc_methods(col, bad_col, cluster):
151151
bad_col.recalculate_count()
152152
assert err.value.error_code in {11, 1228}
153153

154+
# Test collection info
155+
info = col.info()
156+
assert set(info.keys()) == {"id", "name", "system", "type", "status", "global_id"}
157+
assert info["name"] == col.name
158+
assert info["system"] is False
159+
160+
# Test collection compact
161+
result = col.compact()
162+
assert result == info
163+
154164

155165
def test_collection_management(db, bad_db, cluster):
156166
# Test create collection

0 commit comments

Comments
 (0)