} // Don't expose DocDBClient to outside! /// <summary> /// Initializes a new connection to DocDB. /// Contains four string: Url, Key, Database's ID, Collection's ID /// </summary> /// <param name="docDBEndpointUrl">The Url</param> /// <param name="docDBAuthorizationKey">The Key</param> /// <param name="docDBDatabaseID">Database's ID</param> /// <param name="docDBCollectionID">Collection's ID</param> public GraphViewConnection( string docDBEndpointUrl, string docDBAuthorizationKey, string docDBDatabaseID, string docDBCollectionID) { // TODO: Parameter checking! // Initialze the two URI for future use // They are immutable during the life of this connection this._docDBDatabaseUri = UriFactory.CreateDatabaseUri(docDBDatabaseID); this._docDBCollectionUri = UriFactory.CreateDocumentCollectionUri(docDBDatabaseID, docDBCollectionID); this.DocDBUrl = docDBEndpointUrl; this.DocDBPrimaryKey = docDBAuthorizationKey; this.DocDBDatabaseId = docDBDatabaseID; this.DocDBCollectionId = docDBCollectionID; this.DocDBClient = new DocumentClient(new Uri(this.DocDBUrl), this.DocDBPrimaryKey, new ConnectionPolicy { ConnectionMode = ConnectionMode.Direct, ConnectionProtocol = Protocol.Tcp, }); this.DocDBClient.OpenAsync().Wait(); this.Identifier = $"{docDBEndpointUrl}\0{docDBDatabaseID}\0{docDBCollectionID}"; this.VertexCache = VertexObjectCache.FromConnection(this); this.UseReverseEdges = true; // Retrieve metadata from DocDB JObject metaObject = RetrieveDocumentById("metadata"); if (metaObject != null) { Debug.Assert((int?)metaObject["_edgeSpillThreshold"] != null); this.EdgeSpillThreshold = (int)metaObject["_edgeSpillThreshold"]; } Debug.Assert(this.EdgeSpillThreshold >= 0, "The edge-spill threshold should >= 0"); }
/// <summary> /// Initializes a new connection to DocDB. /// Contains four string: Url, Key, Database's ID, Collection's ID /// </summary> /// <param name="docDBEndpointUrl">The Url</param> /// <param name="docDBAuthorizationKey">The Key</param> /// <param name="docDBDatabaseID">Database's ID</param> /// <param name="docDBCollectionID">Collection's ID</param> public GraphViewConnection( string docDBEndpointUrl, string docDBAuthorizationKey, string docDBDatabaseID, string docDBCollectionID, CollectionType collectionType = CollectionType.UNDEFINED, string preferredLocation = null) { // TODO: Parameter checking! // Initialze the two URI for future use // They are immutable during the life of this connection this._docDBDatabaseUri = UriFactory.CreateDatabaseUri(docDBDatabaseID); this._docDBCollectionUri = UriFactory.CreateDocumentCollectionUri(docDBDatabaseID, docDBCollectionID); this.DocDBUrl = docDBEndpointUrl; this.DocDBPrimaryKey = docDBAuthorizationKey; this.DocDBDatabaseId = docDBDatabaseID; this.DocDBCollectionId = docDBCollectionID; ConnectionPolicy connectionPolicy = new ConnectionPolicy { ConnectionMode = ConnectionMode.Direct, ConnectionProtocol = Protocol.Tcp, }; if (!string.IsNullOrEmpty(preferredLocation)) { connectionPolicy.PreferredLocations.Add(preferredLocation); } this.DocDBClient = new DocumentClient(new Uri(this.DocDBUrl), this.DocDBPrimaryKey, connectionPolicy); this.DocDBClient.OpenAsync().Wait(); // // Check whether it is a partition collection (if exists) // DocumentCollection docDBCollection; try { docDBCollection = this.DocDBClient.CreateDocumentCollectionQuery( UriFactory.CreateDatabaseUri(this.DocDBDatabaseId)) .Where(c => c.Id == this.DocDBCollectionId) .AsEnumerable() .FirstOrDefault(); } catch (AggregateException aggex) when((aggex.InnerException as DocumentClientException)?.Error.Code == "NotFound") { // Now the database does not exist! // NOTE: If the database exists, but the collection does not exist, it won't be an exception docDBCollection = null; } bool?isPartitionedNow = (docDBCollection?.PartitionKey.Paths.Count > 0); if (isPartitionedNow.HasValue && isPartitionedNow.Value) { if (collectionType == CollectionType.STANDARD) { throw new Exception("Can't specify CollectionType.STANDARD on an existing partitioned collection"); } } else if (isPartitionedNow.HasValue && !isPartitionedNow.Value) { if (collectionType == CollectionType.PARTITIONED) { throw new Exception("Can't specify CollectionType.PARTITIONED on an existing standard collection"); } } this.CollectionType = collectionType; if (collectionType == CollectionType.UNDEFINED) { if (docDBCollection == null) { // Do nothing } else if (docDBCollection.PartitionKey != null && docDBCollection.PartitionKey.Paths.Count < 1) { this.CollectionType = CollectionType.STANDARD; } else if (docDBCollection.PartitionKey != null && docDBCollection.PartitionKey.Paths.Count > 0 && docDBCollection.PartitionKey.Paths[0].Equals("/_partition", StringComparison.OrdinalIgnoreCase)) { this.CollectionType = CollectionType.PARTITIONED; } else { throw new Exception(string.Format("Collection not properly configured. If you wish to configure a partitioned collection, please chose /{0} as partitionKey", "_partition")); } } this.Identifier = $"{docDBEndpointUrl}\0{docDBDatabaseID}\0{docDBCollectionID}"; this.VertexCache = VertexObjectCache.FromConnection(this); this.UseReverseEdges = true; // Retrieve metadata from DocDB JObject metaObject = RetrieveDocumentById("metadata"); if (metaObject != null) { Debug.Assert((int?)metaObject["_edgeSpillThreshold"] != null); this.EdgeSpillThreshold = (int)metaObject["_edgeSpillThreshold"]; } Debug.Assert(this.EdgeSpillThreshold >= 0, "The edge-spill threshold should >= 0"); }