Skip to content

Commit 6e5c89e

Browse files
Collection: Add Compact endpoint in V2
1 parent 12f9295 commit 6e5c89e

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

v2/arangodb/collection.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,14 @@ type Collection interface {
7979
// Renaming collections is only supported in single server deployments.
8080
Rename(ctx context.Context, req RenameCollectionRequest) (CollectionInfo, error)
8181

82+
// RecalculateCount recalculates the count of documents in the collection.
8283
RecalculateCount(ctx context.Context) (bool, int64, error)
8384

85+
//Compacts the data of a collection in order to reclaim disk space.
86+
// This operation is only supported in single server deployments.
87+
// In cluster deployments, the compaction is done automatically by the server.
88+
Compact(ctx context.Context) (CollectionInfo, error)
89+
8490
CollectionDocuments
8591
CollectionIndexes
8692
}

v2/arangodb/collection_impl.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,27 @@ func (c collection) RecalculateCount(ctx context.Context) (bool, int64, error) {
405405
}
406406
}
407407

408+
func (c collection) Compact(ctx context.Context) (CollectionInfo, error) {
409+
urlEndpoint := c.url("collection", "compact")
410+
411+
var response struct {
412+
shared.ResponseStruct `json:",inline"`
413+
CollectionInfo `json:",inline"`
414+
}
415+
416+
resp, err := connection.CallPut(ctx, c.connection(), urlEndpoint, &response, nil, c.withModifiers()...)
417+
if err != nil {
418+
return CollectionInfo{}, errors.WithStack(err)
419+
}
420+
421+
switch code := resp.Code(); code {
422+
case http.StatusOK:
423+
return response.CollectionInfo, nil
424+
default:
425+
return CollectionInfo{}, response.AsArangoErrorWithCode(code)
426+
}
427+
}
428+
408429
type RemoveCollectionOptions struct {
409430
// IsSystem when set to true allows to remove system collections.
410431
// Use on your own risk!

v2/tests/database_collection_operations_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,3 +745,33 @@ func Test_CollectionRecalculateCount(t *testing.T) {
745745
})
746746
})
747747
}
748+
749+
func Test_CollectionCompact(t *testing.T) {
750+
Wrap(t, func(t *testing.T, client arangodb.Client) {
751+
WithDatabase(t, client, nil, func(db arangodb.Database) {
752+
WithCollectionV2(t, db, nil, func(col arangodb.Collection) {
753+
withContextT(t, defaultTestTimeout, func(ctx context.Context, tb testing.TB) {
754+
role, err := client.ServerRole(ctx)
755+
require.NoError(t, err)
756+
757+
if role == arangodb.ServerRoleSingle {
758+
// Create dummy data
759+
for i := 0; i < 10; i++ {
760+
_, err := col.CreateDocument(ctx, map[string]interface{}{
761+
"_key": fmt.Sprintf("key%d", i),
762+
"val": i,
763+
})
764+
require.NoError(t, err)
765+
}
766+
767+
result, err := col.Compact(ctx)
768+
require.NoError(t, err)
769+
fmt.Printf("Compacted Collection: %s, ID: %s, Status: %d\n", result.Name, result.ID, result.Status)
770+
} else {
771+
t.Skip("Compaction is not supported in cluster mode")
772+
}
773+
})
774+
})
775+
})
776+
})
777+
}

0 commit comments

Comments
 (0)