Skip to content

Commit 834d0dc

Browse files
Feature/add mis endpoints in col (#695)
1 parent 73df279 commit 834d0dc

10 files changed

+588
-0
lines changed

v2/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## [master](https://github.com/arangodb/go-driver/tree/master) (N/A)
44
- Add tasks endpoints to v2
5+
- Add missing endpoints from collections to v2
56

67
## [2.1.3](https://github.com/arangodb/go-driver/tree/v2.1.3) (2025-02-21)
78
- Switch to Go 1.22.11

v2/arangodb/collection.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,37 @@ type Collection interface {
5151
// Count fetches the number of document in the collection.
5252
Count(ctx context.Context) (int64, error)
5353

54+
// Statistics returns the number of documents and additional statistical information about the collection.
55+
Statistics(ctx context.Context, details bool) (CollectionFigures, error)
56+
57+
// Revision fetches the revision ID of the collection.
58+
// The revision ID is a server-generated string that clients can use to check whether data
59+
// in a collection has changed since the last revision check.
60+
Revision(ctx context.Context) (CollectionProperties, error)
61+
62+
// Checksum returns a checksum for the specified collection
63+
// withRevisions - Whether to include document revision ids in the checksum calculation.
64+
// withData - Whether to include document body data in the checksum calculation.
65+
Checksum(ctx context.Context, withRevisions *bool, withData *bool) (CollectionChecksum, error)
66+
67+
// ResponsibleShard returns the shard responsible for the given options.
68+
ResponsibleShard(ctx context.Context, options map[string]interface{}) (string, error)
69+
70+
// LoadIndexesIntoMemory loads all indexes of the collection into memory.
71+
LoadIndexesIntoMemory(ctx context.Context) (bool, error)
72+
73+
// Renaming collections is not supported in cluster deployments.
74+
// Renaming collections is only supported in single server deployments.
75+
Rename(ctx context.Context, req RenameCollectionRequest) (CollectionInfo, error)
76+
77+
// RecalculateCount recalculates the count of documents in the collection.
78+
RecalculateCount(ctx context.Context) (bool, *int64, error)
79+
80+
//Compacts the data of a collection in order to reclaim disk space.
81+
// This operation is only supported in single server deployments.
82+
// In cluster deployments, the compaction is done automatically by the server.
83+
Compact(ctx context.Context) (CollectionInfo, error)
84+
5485
CollectionDocuments
5586
CollectionIndexes
5687
}

v2/arangodb/collection_impl.go

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,196 @@ func (c collection) Shards(ctx context.Context, details bool) (CollectionShards,
216216
}
217217
}
218218

219+
func (c collection) Statistics(ctx context.Context, details bool) (CollectionFigures, error) {
220+
urlEndpoint := c.url("collection", "figures")
221+
222+
var response struct {
223+
shared.ResponseStruct `json:",inline"`
224+
CollectionFigures `json:",inline"`
225+
}
226+
227+
resp, err := connection.CallGet(
228+
ctx, c.connection(), urlEndpoint, &response,
229+
c.withModifiers(connection.WithQuery("details", boolToString(details)))...,
230+
)
231+
if err != nil {
232+
return CollectionFigures{}, errors.WithStack(err)
233+
}
234+
235+
switch code := resp.Code(); code {
236+
case http.StatusOK:
237+
return response.CollectionFigures, nil
238+
default:
239+
return CollectionFigures{}, response.AsArangoErrorWithCode(code)
240+
}
241+
}
242+
243+
func (c collection) Revision(ctx context.Context) (CollectionProperties, error) {
244+
urlEndpoint := c.url("collection", "revision")
245+
246+
var response struct {
247+
shared.ResponseStruct `json:",inline"`
248+
CollectionProperties `json:",inline"`
249+
}
250+
251+
resp, err := connection.CallGet(
252+
ctx, c.connection(), urlEndpoint, &response, c.withModifiers()...,
253+
)
254+
255+
if err != nil {
256+
return CollectionProperties{}, errors.WithStack(err)
257+
}
258+
259+
switch code := resp.Code(); code {
260+
case http.StatusOK:
261+
return response.CollectionProperties, nil
262+
default:
263+
return CollectionProperties{}, response.AsArangoErrorWithCode(code)
264+
}
265+
}
266+
267+
func (c collection) Checksum(ctx context.Context, withRevisions *bool, withData *bool) (CollectionChecksum, error) {
268+
urlEndpoint := c.url("collection", "checksum")
269+
270+
var response struct {
271+
shared.ResponseStruct `json:",inline"`
272+
CollectionChecksum `json:",inline"`
273+
}
274+
275+
// Prepare query modifiers
276+
var modifiers []connection.RequestModifier
277+
if withRevisions != nil && *withRevisions {
278+
modifiers = append(modifiers, connection.WithQuery("withRevisions", boolToString(*withRevisions)))
279+
}
280+
if withData != nil && *withData {
281+
modifiers = append(modifiers, connection.WithQuery("withData", boolToString(*withData)))
282+
}
283+
284+
resp, err := connection.CallGet(
285+
ctx, c.connection(), urlEndpoint, &response,
286+
c.withModifiers(modifiers...)...,
287+
)
288+
if err != nil {
289+
return CollectionChecksum{}, errors.WithStack(err)
290+
}
291+
292+
switch code := resp.Code(); code {
293+
case http.StatusOK:
294+
return response.CollectionChecksum, nil
295+
default:
296+
return CollectionChecksum{}, response.AsArangoErrorWithCode(code)
297+
}
298+
}
299+
300+
func (c collection) ResponsibleShard(ctx context.Context, options map[string]interface{}) (string, error) {
301+
urlEndpoint := c.url("collection", "responsibleShard")
302+
303+
var response struct {
304+
shared.ResponseStruct `json:",inline"`
305+
ShardId string `json:"shardId,omitempty"`
306+
}
307+
308+
resp, err := connection.CallPut(ctx, c.connection(), urlEndpoint, &response, options, c.withModifiers()...)
309+
if err != nil {
310+
return "", errors.WithStack(err)
311+
}
312+
313+
switch code := resp.Code(); code {
314+
case http.StatusOK:
315+
return response.ShardId, nil
316+
default:
317+
return "", response.AsArangoErrorWithCode(code)
318+
}
319+
}
320+
321+
func (c collection) LoadIndexesIntoMemory(ctx context.Context) (bool, error) {
322+
urlEndpoint := c.url("collection", "loadIndexesIntoMemory")
323+
324+
var response struct {
325+
shared.ResponseStruct `json:",inline"`
326+
Result bool `json:"result"`
327+
}
328+
329+
resp, err := connection.CallPut(ctx, c.connection(), urlEndpoint, &response, nil, c.withModifiers()...)
330+
if err != nil {
331+
return false, errors.WithStack(err)
332+
}
333+
334+
switch code := resp.Code(); code {
335+
case http.StatusOK:
336+
return response.Result, nil
337+
default:
338+
return false, response.AsArangoErrorWithCode(code)
339+
}
340+
}
341+
342+
// Renaming collections is not supported in cluster deployments.
343+
func (c collection) Rename(ctx context.Context, req RenameCollectionRequest) (CollectionInfo, error) {
344+
urlEndpoint := c.url("collection", "rename")
345+
346+
var response struct {
347+
shared.ResponseStruct `json:",inline"`
348+
CollectionInfo `json:",inline"`
349+
}
350+
351+
resp, err := connection.CallPut(ctx, c.connection(), urlEndpoint, &response, req, c.withModifiers()...)
352+
if err != nil {
353+
return CollectionInfo{}, errors.WithStack(err)
354+
}
355+
356+
switch code := resp.Code(); code {
357+
case http.StatusOK:
358+
return response.CollectionInfo, nil
359+
default:
360+
return CollectionInfo{}, response.AsArangoErrorWithCode(code)
361+
}
362+
}
363+
364+
func (c collection) RecalculateCount(ctx context.Context) (bool, *int64, error) {
365+
urlEndpoint := c.url("collection", "recalculateCount")
366+
367+
var response struct {
368+
shared.ResponseStruct `json:",inline"`
369+
Count *int64 `json:"count,omitempty"`
370+
Result bool `json:"result"`
371+
}
372+
373+
resp, err := connection.CallPut(ctx, c.connection(), urlEndpoint, &response, nil, c.withModifiers()...)
374+
if err != nil {
375+
zero := int64(0)
376+
return false, &zero, errors.WithStack(err)
377+
}
378+
379+
switch code := resp.Code(); code {
380+
case http.StatusOK:
381+
return response.Result, response.Count, nil
382+
default:
383+
zero := int64(0)
384+
return false, &zero, response.AsArangoErrorWithCode(code)
385+
}
386+
}
387+
388+
func (c collection) Compact(ctx context.Context) (CollectionInfo, error) {
389+
urlEndpoint := c.url("collection", "compact")
390+
391+
var response struct {
392+
shared.ResponseStruct `json:",inline"`
393+
CollectionInfo `json:",inline"`
394+
}
395+
396+
resp, err := connection.CallPut(ctx, c.connection(), urlEndpoint, &response, nil, c.withModifiers()...)
397+
if err != nil {
398+
return CollectionInfo{}, errors.WithStack(err)
399+
}
400+
401+
switch code := resp.Code(); code {
402+
case http.StatusOK:
403+
return response.CollectionInfo, nil
404+
default:
405+
return CollectionInfo{}, response.AsArangoErrorWithCode(code)
406+
}
407+
}
408+
219409
type RemoveCollectionOptions struct {
220410
// IsSystem when set to true allows to remove system collections.
221411
// Use on your own risk!

v2/arangodb/collection_opts.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ type CollectionExtendedInfo struct {
7373
// If set to false, then the key generator is solely responsible for generating keys and supplying own key values in
7474
// the _key attribute of documents is considered an error.
7575
AllowUserKeys bool `json:"allowUserKeys,omitempty"`
76+
// The initial value for the key generator. This is only used for autoincrement key generators.
77+
LastValue *uint64 `json:"lastValue,omitempty"`
7678
} `json:"keyOptions,omitempty"`
7779

7880
// NumberOfShards is the number of shards of the collection.
@@ -149,6 +151,9 @@ type CollectionProperties struct {
149151

150152
// Schema for collection validation
151153
Schema *CollectionSchemaOptions `json:"schema,omitempty"`
154+
155+
// The collection revision id as a string.
156+
Revision string `json:"revision,omitempty"`
152157
}
153158

154159
// IsSatellite returns true if the collection is a SatelliteCollection
@@ -322,6 +327,13 @@ type CollectionStatistics struct {
322327
// The memory used for storing the revisions of this collection in the storage engine (in bytes). This figure does not include the document data but only mappings from document revision ids to storage engine datafile positions.
323328
Size int64 `json:"size,omitempty"`
324329
} `json:"revisions"`
330+
331+
DocumentsSize int64 `json:"documentsSize,omitempty"`
332+
333+
// RocksDB cache statistics
334+
CacheInUse *bool `json:"cacheInUse,omitempty"`
335+
CacheSize *int64 `json:"cacheSize,omitempty"`
336+
CacheUsage *int64 `json:"cacheUsage,omitempty"`
325337
} `json:"figures"`
326338
}
327339

@@ -370,3 +382,25 @@ func (r *ReplicationFactor) UnmarshalJSON(d []byte) error {
370382
Type: reflect.TypeOf(r).Elem(),
371383
}
372384
}
385+
386+
type CollectionFigures struct {
387+
CollectionProperties
388+
CollectionStatistics
389+
}
390+
391+
// CollectionChecksum contains information about a collection checksum response
392+
type CollectionChecksum struct {
393+
CollectionInfo
394+
// The collection revision id as a string.
395+
Revision string `json:"revision,omitempty"`
396+
}
397+
398+
type ResponsibleShardRequest struct {
399+
// Fill with shard key fields expected
400+
Key string `json:"_key,omitempty"`
401+
// other shard key fields as required
402+
}
403+
404+
type RenameCollectionRequest struct {
405+
Name string `json:"name"`
406+
}

v2/arangodb/database.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ type Database interface {
3838
// TransactionJS performs a javascript transaction. The result of the transaction function is returned.
3939
TransactionJS(ctx context.Context, options TransactionJSOptions) (interface{}, error)
4040

41+
// Returns the available key generators for collections.
42+
KeyGenerators(ctx context.Context) (KeyGeneratorsResponse, error)
43+
4144
DatabaseCollection
4245
DatabaseTransaction
4346
DatabaseQuery

v2/arangodb/database_impl.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,24 @@ func (d database) TransactionJS(ctx context.Context, options TransactionJSOption
128128
return nil, transactionResponse.AsArangoError()
129129
}
130130
}
131+
132+
func (d database) KeyGenerators(ctx context.Context) (KeyGeneratorsResponse, error) {
133+
urlEndpoint := d.url("_api", "key-generators")
134+
135+
var response struct {
136+
shared.ResponseStruct `json:",inline"`
137+
KeyGeneratorsResponse `json:",inline"`
138+
}
139+
140+
resp, err := connection.CallGet(ctx, d.client.connection, urlEndpoint, &response)
141+
if err != nil {
142+
return KeyGeneratorsResponse{}, errors.WithStack(err)
143+
}
144+
145+
switch code := resp.Code(); code {
146+
case http.StatusOK:
147+
return response.KeyGeneratorsResponse, nil
148+
default:
149+
return KeyGeneratorsResponse{}, response.AsArangoError()
150+
}
151+
}

v2/arangodb/database_opts.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,7 @@ func (t EngineType) String() string {
6363
type EngineInfo struct {
6464
Type EngineType `json:"name"`
6565
}
66+
67+
type KeyGeneratorsResponse struct {
68+
KeyGenerators []string `json:"keyGenerators"`
69+
}

0 commit comments

Comments
 (0)