From 5ede5bd3e76c5aa672ee3b8124b8e4d0287394b1 Mon Sep 17 00:00:00 2001 From: jwierzbo Date: Sun, 27 Feb 2022 20:35:24 +0100 Subject: [PATCH] Add Collection's missing fields & remove internal structures --- collection.go | 153 +++++++++------------------------------------ collection_impl.go | 130 ++++++-------------------------------- 2 files changed, 48 insertions(+), 235 deletions(-) diff --git a/collection.go b/collection.go index a3766239..92342b75 100644 --- a/collection.go +++ b/collection.go @@ -87,6 +87,8 @@ type CollectionInfo struct { Name string `json:"name,omitempty"` // The status of the collection Status CollectionStatus `json:"status,omitempty"` + // StatusString represents status as a string. + StatusString string `json:"statusString,omitempty"` // The type of the collection Type CollectionType `json:"type,omitempty"` // If true then the collection is a system collection. @@ -98,6 +100,7 @@ type CollectionInfo struct { // CollectionProperties contains extended information about a collection. type CollectionProperties struct { CollectionInfo + ArangoError // WaitForSync; If true then creating, changing or removing documents will wait until the data has been synchronized to disk. WaitForSync bool `json:"waitForSync,omitempty"` @@ -113,7 +116,8 @@ type CollectionProperties struct { // AllowUserKeys; if set to true, then it is allowed to supply own key values in the _key attribute of a document. // If set to false, then the key generator is solely responsible for generating keys and supplying own key values in // the _key attribute of documents is considered an error. - AllowUserKeys bool `json:"allowUserKeys,omitempty"` + AllowUserKeys bool `json:"allowUserKeys,omitempty"` + LastValue int64 `json:"lastValue,omitempty"` } `json:"keyOptions,omitempty"` // NumberOfShards is the number of shards of the collection. // Only available in cluster setup. @@ -123,7 +127,7 @@ type CollectionProperties struct { ShardKeys []string `json:"shardKeys,omitempty"` // ReplicationFactor contains how many copies of each shard are kept on different DBServers. // Only available in cluster setup. - ReplicationFactor int `json:"replicationFactor,omitempty"` + ReplicationFactor int `json:"-"` // Deprecated: use 'WriteConcern' instead MinReplicationFactor int `json:"minReplicationFactor,omitempty"` // WriteConcern contains how many copies must be available before a collection can be written. @@ -149,6 +153,26 @@ type CollectionProperties struct { SyncByRevision bool `json:"syncByRevision,omitempty"` // Schema for collection validation Schema *CollectionSchemaOptions `json:"schema,omitempty"` + + Revision string `json:"revision,omitempty"` + + // IsDisjoint set isDisjoint flag for Graph. Required ArangoDB 3.7+ + IsDisjoint bool `json:"isDisjoint,omitempty"` + + IsSmartChild bool `json:"isSmartChild,omitempty"` + + InternalValidatorType *int `json:"internalValidatorType, omitempty"` + + // Set to create a smart edge or vertex collection. + // This requires ArangoDB Enterprise Edition. + IsSmart bool `json:"isSmart,omitempty"` + + // StatusString represents status as a string. + StatusString string `json:"statusString,omitempty"` + + TempObjectId string `json:"tempObjectId,omitempty"` + + ObjectId string `json:"objectId,omitempty"` } const ( @@ -195,6 +219,9 @@ const ( // CollectionStatistics contains the number of documents and additional statistical information about a collection. type CollectionStatistics struct { + ArangoError + CollectionProperties + //The number of documents currently present in the collection. Count int64 `json:"count,omitempty"` // The maximal size of a journal or datafile in bytes. @@ -269,129 +296,9 @@ type CollectionStatistics struct { // CollectionShards contains shards information about a collection. type CollectionShards struct { - CollectionInfo - - // CacheEnabled set cacheEnabled option in collection properties - CacheEnabled bool `json:"cacheEnabled,omitempty"` - - // Set to create a smart edge or vertex collection. - // This requires ArangoDB Enterprise Edition. - IsSmart bool `json:"isSmart,omitempty"` - - KeyOptions struct { - // Type specifies the type of the key generator. The currently available generators are traditional and autoincrement. - Type KeyGeneratorType `json:"type,omitempty"` - // AllowUserKeys; if set to true, then it is allowed to supply own key values in the _key attribute of a document. - // If set to false, then the key generator is solely responsible for generating keys and supplying own key values in - // the _key attribute of documents is considered an error. - AllowUserKeys bool `json:"allowUserKeys,omitempty"` - } `json:"keyOptions,omitempty"` - - // Deprecated: use 'WriteConcern' instead. - MinReplicationFactor int `json:"minReplicationFactor,omitempty"` - - // NumberOfShards is the number of shards of the collection. - // Only available in cluster setup. - NumberOfShards int `json:"numberOfShards,omitempty"` - - // This attribute specifies the name of the sharding strategy to use for the collection. - // Can not be changed after creation. - ShardingStrategy ShardingStrategy `json:"shardingStrategy,omitempty"` - - // ShardKeys contains the names of document attributes that are used to determine the target shard for documents. - // Only available in cluster setup. - ShardKeys []string `json:"shardKeys,omitempty"` - - // Shards is a list of shards that belong to the collection. - // Each shard contains a list of DB servers where the first one is the leader and the rest are followers. - Shards map[ShardID][]ServerID `json:"shards,omitempty"` - - // StatusString represents status as a string. - StatusString string `json:"statusString,omitempty"` - - // ReplicationFactor contains how many copies of each shard are kept on different DBServers. - // Only available in cluster setup. - ReplicationFactor int `json:"replicationFactor,omitempty"` - - // WaitForSync; If true then creating, changing or removing documents will wait - // until the data has been synchronized to disk. - WaitForSync bool `json:"waitForSync,omitempty"` - - // WriteConcern contains how many copies must be available before a collection can be written. - // It is required that 1 <= WriteConcern <= ReplicationFactor. - // Default is 1. Not available for satellite collections. - // Available from 3.6 ArangoDB version. - WriteConcern int `json:"writeConcern,omitempty"` -} - -type collectionShardsInternal struct { - CollectionInfo - - // CacheEnabled set cacheEnabled option in collection properties - CacheEnabled bool `json:"cacheEnabled,omitempty"` - - // Set to create a smart edge or vertex collection. - // This requires ArangoDB Enterprise Edition. - IsSmart bool `json:"isSmart,omitempty"` - - KeyOptions struct { - // Type specifies the type of the key generator. The currently available generators are traditional and autoincrement. - Type KeyGeneratorType `json:"type,omitempty"` - // AllowUserKeys; if set to true, then it is allowed to supply own key values in the _key attribute of a document. - // If set to false, then the key generator is solely responsible for generating keys and supplying own key values in - // the _key attribute of documents is considered an error. - AllowUserKeys bool `json:"allowUserKeys,omitempty"` - } `json:"keyOptions,omitempty"` - - // Deprecated: use 'WriteConcern' instead. - MinReplicationFactor int `json:"minReplicationFactor,omitempty"` - - // NumberOfShards is the number of shards of the collection. - // Only available in cluster setup. - NumberOfShards int `json:"numberOfShards,omitempty"` - - // This attribute specifies the name of the sharding strategy to use for the collection. - // Can not be changed after creation. - ShardingStrategy ShardingStrategy `json:"shardingStrategy,omitempty"` - - // ShardKeys contains the names of document attributes that are used to determine the target shard for documents. - // Only available in cluster setup. - ShardKeys []string `json:"shardKeys,omitempty"` + CollectionProperties // Shards is a list of shards that belong to the collection. // Each shard contains a list of DB servers where the first one is the leader and the rest are followers. Shards map[ShardID][]ServerID `json:"shards,omitempty"` - - // StatusString represents status as a string. - StatusString string `json:"statusString,omitempty"` - - // ReplicationFactor contains how many copies of each shard are kept on different DBServers. - // Only available in cluster setup. - ReplicationFactor replicationFactor `json:"replicationFactor,omitempty"` - - // WaitForSync; If true then creating, changing or removing documents will wait - // until the data has been synchronized to disk. - WaitForSync bool `json:"waitForSync,omitempty"` - - // WriteConcern contains how many copies must be available before a collection can be written. - // It is required that 1 <= WriteConcern <= ReplicationFactor. - // Default is 1. Not available for satellite collections. - // Available from 3.6 ArangoDB version. - WriteConcern int `json:"writeConcern,omitempty"` -} - -func (p *CollectionShards) fromInternal(i collectionShardsInternal) { - p.CollectionInfo = i.CollectionInfo - p.CacheEnabled = i.CacheEnabled - p.IsSmart = i.IsSmart - p.KeyOptions = i.KeyOptions - p.MinReplicationFactor = i.MinReplicationFactor - p.NumberOfShards = i.NumberOfShards - p.ShardingStrategy = i.ShardingStrategy - p.ShardKeys = i.ShardKeys - p.Shards = i.Shards - p.StatusString = i.StatusString - p.ReplicationFactor = int(i.ReplicationFactor) - p.WaitForSync = i.WaitForSync - p.WriteConcern = i.WriteConcern } diff --git a/collection_impl.go b/collection_impl.go index 69f88ddb..4239bda3 100644 --- a/collection_impl.go +++ b/collection_impl.go @@ -143,9 +143,7 @@ func (c *collection) Revision(ctx context.Context) (string, error) { if err := resp.CheckStatus(200); err != nil { return "", WithStack(err) } - var data struct { - Revision string `json:"revision,omitempty"` - } + var data CollectionProperties if err := resp.ParseBody("", &data); err != nil { return "", WithStack(err) } @@ -165,11 +163,18 @@ func (c *collection) Properties(ctx context.Context) (CollectionProperties, erro if err := resp.CheckStatus(200); err != nil { return CollectionProperties{}, WithStack(err) } - var data collectionPropertiesInternal + + var data struct { + CollectionProperties + ReplicationFactorV2 replicationFactor `json:"replicationFactor,omitempty"` + } + if err := resp.ParseBody("", &data); err != nil { return CollectionProperties{}, WithStack(err) } - return data.asExternal(), nil + data.ReplicationFactor = int(data.ReplicationFactorV2) + + return data.CollectionProperties, nil } // SetProperties changes properties of the collection. @@ -210,15 +215,17 @@ func (c *collection) Shards(ctx context.Context, details bool) (CollectionShards return CollectionShards{}, WithStack(err) } - shardsInternal := collectionShardsInternal{} - if err := resp.ParseBody("", &shardsInternal); err != nil { - return CollectionShards{}, WithStack(err) + var data struct { + CollectionShards + ReplicationFactorV2 replicationFactor `json:"replicationFactor,omitempty"` } - var shards CollectionShards - shards.fromInternal(shardsInternal) + if err := resp.ParseBody("", &data); err != nil { + return CollectionShards{}, WithStack(err) + } + data.ReplicationFactor = int(data.ReplicationFactorV2) - return shards, nil + return data.CollectionShards, nil } // Load the collection into memory. @@ -296,107 +303,6 @@ func (c *collection) Truncate(ctx context.Context) error { return nil } -type collectionPropertiesInternal struct { - CollectionInfo - WaitForSync bool `json:"waitForSync,omitempty"` - DoCompact bool `json:"doCompact,omitempty"` - JournalSize int64 `json:"journalSize,omitempty"` - CacheEnabled bool `json:"cacheEnabled,omitempty"` - KeyOptions struct { - Type KeyGeneratorType `json:"type,omitempty"` - AllowUserKeys bool `json:"allowUserKeys,omitempty"` - } `json:"keyOptions,omitempty"` - NumberOfShards int `json:"numberOfShards,omitempty"` - ShardKeys []string `json:"shardKeys,omitempty"` - ReplicationFactor replicationFactor `json:"replicationFactor,omitempty"` - // Deprecated: use 'WriteConcern' instead - MinReplicationFactor int `json:"minReplicationFactor,omitempty"` - // Available from 3.6 arangod version. - WriteConcern int `json:"writeConcern,omitempty"` - SmartJoinAttribute string `json:"smartJoinAttribute,omitempty"` - ShardingStrategy ShardingStrategy `json:"shardingStrategy,omitempty"` - DistributeShardsLike string `json:"distributeShardsLike,omitempty"` - // Available from 3.7 arangod version. - UsesRevisionsAsDocumentIds bool `json:"usesRevisionsAsDocumentIds,omitempty"` - SyncByRevision bool `json:"syncByRevision,omitempty"` - Schema *CollectionSchemaOptions `json:"schema,omitempty"` -} - -func (p *collectionPropertiesInternal) asExternal() CollectionProperties { - return CollectionProperties{ - CollectionInfo: p.CollectionInfo, - WaitForSync: p.WaitForSync, - DoCompact: p.DoCompact, - JournalSize: p.JournalSize, - CacheEnabled: p.CacheEnabled, - KeyOptions: p.KeyOptions, - NumberOfShards: p.NumberOfShards, - ShardKeys: p.ShardKeys, - ReplicationFactor: int(p.ReplicationFactor), - MinReplicationFactor: p.MinReplicationFactor, - WriteConcern: p.WriteConcern, - SmartJoinAttribute: p.SmartJoinAttribute, - ShardingStrategy: p.ShardingStrategy, - DistributeShardsLike: p.DistributeShardsLike, - UsesRevisionsAsDocumentIds: p.UsesRevisionsAsDocumentIds, - SyncByRevision: p.SyncByRevision, - Schema: p.Schema, - } -} - -func (p *CollectionProperties) asInternal() collectionPropertiesInternal { - return collectionPropertiesInternal{ - CollectionInfo: p.CollectionInfo, - WaitForSync: p.WaitForSync, - DoCompact: p.DoCompact, - JournalSize: p.JournalSize, - CacheEnabled: p.CacheEnabled, - KeyOptions: p.KeyOptions, - NumberOfShards: p.NumberOfShards, - ShardKeys: p.ShardKeys, - ReplicationFactor: replicationFactor(p.ReplicationFactor), - MinReplicationFactor: p.MinReplicationFactor, - WriteConcern: p.WriteConcern, - SmartJoinAttribute: p.SmartJoinAttribute, - ShardingStrategy: p.ShardingStrategy, - Schema: p.Schema, - } -} - -func (p *CollectionProperties) fromInternal(i *collectionPropertiesInternal) { - p.CollectionInfo = i.CollectionInfo - p.WaitForSync = i.WaitForSync - p.DoCompact = i.DoCompact - p.JournalSize = i.JournalSize - p.CacheEnabled = i.CacheEnabled - p.KeyOptions = i.KeyOptions - p.NumberOfShards = i.NumberOfShards - p.ShardKeys = i.ShardKeys - p.ReplicationFactor = int(i.ReplicationFactor) - p.MinReplicationFactor = i.MinReplicationFactor - p.WriteConcern = i.WriteConcern - p.SmartJoinAttribute = i.SmartJoinAttribute - p.ShardingStrategy = i.ShardingStrategy - p.UsesRevisionsAsDocumentIds = i.UsesRevisionsAsDocumentIds - p.SyncByRevision = i.SyncByRevision -} - -// MarshalJSON converts CollectionProperties into json -func (p *CollectionProperties) MarshalJSON() ([]byte, error) { - return json.Marshal(p.asInternal()) -} - -// UnmarshalJSON loads CollectionProperties from json -func (p *CollectionProperties) UnmarshalJSON(d []byte) error { - var internal collectionPropertiesInternal - if err := json.Unmarshal(d, &internal); err != nil { - return err - } - - p.fromInternal(&internal) - return nil -} - type setCollectionPropertiesOptionsInternal struct { WaitForSync *bool `json:"waitForSync,omitempty"` JournalSize int64 `json:"journalSize,omitempty"`