Skip to content

Commit f67492f

Browse files
authored
GT-214 Implement checksum endpoint for collections (#446)
1 parent fcf44bd commit f67492f

File tree

7 files changed

+104
-2
lines changed

7 files changed

+104
-2
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ vendor
1313
# Helper files
1414
debug/
1515
*.log
16+
17+
# direnv files
18+
.envrc

CHANGELOG.md

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

33
## [master](https://github.com/arangodb/go-driver/tree/master) (N/A)
4+
- Add support for `checksum` in Collections
45

56
## [1.4.0](https://github.com/arangodb/go-driver/tree/v1.4.0) (2022-10-04)
67
- Add `hex` property to analyzer's properties

collection.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ type Collection interface {
5050
// in a collection has changed since the last revision check.
5151
Revision(ctx context.Context) (string, error)
5252

53+
// Checksum returns a checksum for the specified collection
54+
// withRevisions - Whether to include document revision ids in the checksum calculation.
55+
// withData - Whether to include document body data in the checksum calculation.
56+
Checksum(ctx context.Context, withRevisions bool, withData bool) (CollectionChecksum, error)
57+
5358
// Properties fetches extended information about the collection.
5459
Properties(ctx context.Context) (CollectionProperties, error)
5560

@@ -79,6 +84,14 @@ type Collection interface {
7984
CollectionDocuments
8085
}
8186

87+
// CollectionChecksum contains information about a collection checksum response
88+
type CollectionChecksum struct {
89+
ArangoError
90+
CollectionInfo
91+
// The collection revision id as a string.
92+
Revision string `json:"revision,omitempty"`
93+
}
94+
8295
// CollectionInfo contains information about a collection
8396
type CollectionInfo struct {
8497
// The identifier of the collection.
@@ -95,6 +108,8 @@ type CollectionInfo struct {
95108
IsSystem bool `json:"isSystem,omitempty"`
96109
// Global unique name for the collection
97110
GloballyUniqueId string `json:"globallyUniqueId,omitempty"`
111+
// The calculated checksum as a number.
112+
Checksum string `json:"checksum,omitempty"`
98113
}
99114

100115
// CollectionProperties contains extended information about a collection.
@@ -154,11 +169,11 @@ type CollectionProperties struct {
154169
// The following attribute specifies if the new MerkleTree based sync protocol
155170
// can be used on the collection.
156171
SyncByRevision bool `json:"syncByRevision,omitempty"`
172+
// The collection revision id as a string.
173+
Revision string `json:"revision,omitempty"`
157174
// Schema for collection validation
158175
Schema *CollectionSchemaOptions `json:"schema,omitempty"`
159176

160-
Revision string `json:"revision,omitempty"`
161-
162177
// IsDisjoint set isDisjoint flag for Graph. Required ArangoDB 3.7+
163178
IsDisjoint bool `json:"isDisjoint,omitempty"`
164179

collection_impl.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,37 @@ func (c *collection) Revision(ctx context.Context) (string, error) {
150150
return data.Revision, nil
151151
}
152152

153+
// Checksum returns a checksum for the specified collection
154+
// withRevisions - Whether to include document revision ids in the checksum calculation.
155+
// withData - Whether to include document body data in the checksum calculation.
156+
func (c *collection) Checksum(ctx context.Context, withRevisions bool, withData bool) (CollectionChecksum, error) {
157+
var data CollectionChecksum
158+
159+
req, err := c.conn.NewRequest("GET", path.Join(c.relPath("collection"), "checksum"))
160+
if err != nil {
161+
return data, WithStack(err)
162+
}
163+
if withRevisions {
164+
req.SetQuery("withRevisions", "true")
165+
}
166+
if withData {
167+
req.SetQuery("withData", "true")
168+
}
169+
170+
resp, err := c.conn.Do(ctx, req)
171+
if err != nil {
172+
return data, WithStack(err)
173+
}
174+
if err := resp.CheckStatus(200); err != nil {
175+
return data, WithStack(err)
176+
}
177+
178+
if err := resp.ParseBody("", &data); err != nil {
179+
return data, WithStack(err)
180+
}
181+
return data, nil
182+
}
183+
153184
// Properties fetches extended information about the collection.
154185
func (c *collection) Properties(ctx context.Context) (CollectionProperties, error) {
155186
req, err := c.conn.NewRequest("GET", path.Join(c.relPath("collection"), "properties"))

edge_collection_impl.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@ func (c *edgeCollection) Status(ctx context.Context) (CollectionStatus, error) {
8181
return result, nil
8282
}
8383

84+
// Checksum returns a checksum for the specified collection
85+
func (c *edgeCollection) Checksum(ctx context.Context, withRevisions bool, withData bool) (CollectionChecksum, error) {
86+
result, err := c.rawCollection().Checksum(ctx, withRevisions, withData)
87+
if err != nil {
88+
return CollectionChecksum{}, WithStack(err)
89+
}
90+
return result, nil
91+
}
92+
8493
// Count fetches the number of document in the collection.
8594
func (c *edgeCollection) Count(ctx context.Context) (int64, error) {
8695
result, err := c.rawCollection().Count(ctx)

test/collection_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,40 @@ func TestCollectionRevision(t *testing.T) {
761761
}
762762
}
763763

764+
// TestCollectionChecksum creates a collection, checks checksum after adding documents.
765+
func TestCollectionChecksum(t *testing.T) {
766+
c := createClientFromEnv(t, true)
767+
db := ensureDatabase(nil, c, "collection_checksum", nil, t)
768+
name := "test_collection_checksum"
769+
col, err := db.CreateCollection(nil, name, nil)
770+
require.NoError(t, err)
771+
772+
// create some documents
773+
for i := 0; i < 5; i++ {
774+
before, err := col.Checksum(nil, false, false)
775+
require.NoError(t, err)
776+
777+
doc := Book{Title: fmt.Sprintf("Book %d", i)}
778+
_, err = col.CreateDocument(nil, doc)
779+
require.NoError(t, err)
780+
781+
after, err := col.Checksum(nil, false, false)
782+
require.NoError(t, err)
783+
require.NotEqual(t, before.Checksum, after.Checksum)
784+
785+
afterWithRevision, err := col.Checksum(nil, true, false)
786+
require.NoError(t, err)
787+
require.NotEqual(t, before.Checksum, afterWithRevision.Checksum)
788+
require.NotEqual(t, after.Checksum, afterWithRevision.Checksum)
789+
790+
afterWithData, err := col.Checksum(nil, false, true)
791+
require.NoError(t, err)
792+
require.NotEqual(t, before.Checksum, afterWithData.Checksum)
793+
require.NotEqual(t, after.Checksum, afterWithData.Checksum)
794+
require.NotEqual(t, afterWithRevision.Checksum, afterWithData.Checksum)
795+
}
796+
}
797+
764798
// TestCollectionStatistics creates a collection, checks statistics after adding documents.
765799
func TestCollectionStatistics(t *testing.T) {
766800
c := createClientFromEnv(t, true)

vertex_collection_impl.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@ func (c *vertexCollection) Status(ctx context.Context) (CollectionStatus, error)
8181
return result, nil
8282
}
8383

84+
// Checksum returns a checksum for the specified collection
85+
func (c *vertexCollection) Checksum(ctx context.Context, withRevisions bool, withData bool) (CollectionChecksum, error) {
86+
result, err := c.rawCollection().Checksum(ctx, withRevisions, withData)
87+
if err != nil {
88+
return CollectionChecksum{}, WithStack(err)
89+
}
90+
return result, nil
91+
}
92+
8493
// Count fetches the number of document in the collection.
8594
func (c *vertexCollection) Count(ctx context.Context) (int64, error) {
8695
result, err := c.rawCollection().Count(ctx)

0 commit comments

Comments
 (0)