-
Notifications
You must be signed in to change notification settings - Fork 1
/
CosmosDBGraphClient.cs
131 lines (114 loc) · 4.18 KB
/
CosmosDBGraphClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System;
using System.Collections;
using System.Text;
using Microsoft.Azure.Documents;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Graphs;
using Microsoft.Azure.Documents.Client;
using CuriousGremlin;
using Newtonsoft.Json;
using CuriousGremlin.Objects;
using CuriousGremlin.Client;
namespace CuriousGremlin.AzureCosmosDB
{
public class CosmosDBGraphClient : IGraphClient, IDisposable
{
private SemaphoreSlim semaphore = new SemaphoreSlim(1, 1);
public bool IsOpen { get; protected set; } = false;
private DocumentClient client;
private DocumentCollection graph;
protected CosmosDBGraphClient() { }
public CosmosDBGraphClient(string endpoint, string authKey)
{
client = new DocumentClient(new Uri(endpoint), authKey);
}
~CosmosDBGraphClient()
{
if (IsOpen)
Dispose();
}
public async Task Open(string database, string graph)
{
if (!IsOpen)
{
this.graph = await client.ReadDocumentCollectionAsync("/dbs/" + database + "/colls/" + graph);
this.IsOpen = true;
}
}
public virtual void Dispose()
{
IsOpen = false;
client.Dispose();
}
#region Database Operations
public async Task CreateDatabaseAsync(string id)
{
await client.CreateDatabaseAsync(new Database { Id = id });
}
public async Task CreateDatabaseIfNotExistsAsync(string id)
{
await client.CreateDatabaseIfNotExistsAsync(new Database { Id = id });
}
public async Task DeleteDatabaseAsync(string id)
{
await client.DeleteDatabaseAsync("/dbs/" + id);
}
#endregion
#region Collection Operations
public async Task CreateDocumentCollectionAsync(string database, string collection)
{
await client.CreateDocumentCollectionAsync("/dbs/" + database, new DocumentCollection { Id = collection });
}
public async Task CreateDocumentCollectionAsync(string database, string collection, string PartitionKey)
{
await client.CreateDocumentCollectionAsync("/dbs/" + database, new DocumentCollection
{
Id = collection,
PartitionKey = new PartitionKeyDefinition
{
Paths = new System.Collections.ObjectModel.Collection<string> { PartitionKey }
}
});
}
public async Task CreateDocumentCollectionIfNotExistsAsync(string database, string collection)
{
await client.CreateDocumentCollectionIfNotExistsAsync("/dbs/" + database, new DocumentCollection { Id = collection });
}
public async Task CreateDocumentCollectionIfNotExistsAsync(string database, string collection, string PartitionKey)
{
await client.CreateDocumentCollectionIfNotExistsAsync("/dbs/" + database, new DocumentCollection
{
Id = collection,
PartitionKey = new PartitionKeyDefinition
{
Paths = new System.Collections.ObjectModel.Collection<string> { PartitionKey }
}
});
}
public async Task DeleteCollectionAsync(string database, string collection)
{
await client.DeleteDocumentCollectionAsync("/dbs/" + database + "/colls/" + collection);
}
#endregion
#region Queries
public async Task<IEnumerable> Execute(string queryString)
{
if (!IsOpen)
throw new Exception("Client must be opened prior to executing a query");
// Generate the query from the query string
var query = GraphExtensions.CreateGremlinQuery(client, graph, queryString);
// Get the semaphore
await semaphore.WaitAsync();
try
{
return await query.ExecuteNextAsync<dynamic>();
}
finally
{
semaphore.Release();
}
}
#endregion
}
}