Skip to content

Commit 90cf471

Browse files
Alpha version of collection methods and db admin (#9)
Co-authored-by: jeffrey-elliott <[email protected]>
1 parent 427586b commit 90cf471

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+4867
-438
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ test/DataStax.AstraDB.DataAPI.IntegrationTests/appsettings.sample.json
77
test/DataStax.AstraDB.DataApi.UnitTests/bin
88
test/DataStax.AstraDB.DataApi.UnitTests/obj
99
appsettings.json
10-
latest_run.log
10+
*.log

src/DataStax.AstraDB.DataApi/Admin/AstraDatabasesAdmin.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using System.Collections.Generic;
2222
using System.Linq;
2323
using System.Net.Http;
24+
using System.Text.Json;
2425
using System.Threading;
2526
using System.Threading.Tasks;
2627

@@ -29,8 +30,6 @@ namespace DataStax.AstraDB.DataApi.Admin;
2930
public class AstraDatabasesAdmin
3031
{
3132
private const int WAIT_IN_SECONDS = 600;
32-
private const CloudProviderType FREE_TIER_CLOUD = CloudProviderType.GCP;
33-
private const string FREE_TIER_CLOUD_REGION = "us-east1";
3433

3534
private readonly CommandOptions _adminOptions;
3635
private readonly DataApiClient _client;
@@ -370,7 +369,7 @@ internal async Task<bool> DropDatabaseAsync(Guid dbGuid, CommandOptions options,
370369
private IDatabaseAdmin GetDatabaseAdmin(Guid dbGuid)
371370
{
372371
Guard.NotEmpty(dbGuid, nameof(dbGuid));
373-
return new DatabaseAdminAstra(dbGuid);
372+
return new DatabaseAdminAstra(dbGuid, _client, null);
374373
}
375374

376375
public DatabaseInfo GetDatabaseInfo(Guid dbGuid)
@@ -412,6 +411,6 @@ internal Task<DatabaseInfo> GetDatabaseInfoAsync(Guid dbGuid, CommandOptions opt
412411

413412
private Command CreateCommand()
414413
{
415-
return new Command(_client, OptionsTree, new AdminCommandUrlBuilder());
414+
return new Command(_client, OptionsTree, new AdminCommandUrlBuilder(OptionsTree));
416415
}
417416
}

src/DataStax.AstraDB.DataApi/Admin/DatabaseAdminAstra.cs

Lines changed: 213 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,86 +14,273 @@
1414
* limitations under the License.
1515
*/
1616

17+
using DataStax.AstraDB.DataApi.Core;
18+
using DataStax.AstraDB.DataApi.Core.Commands;
19+
using DataStax.AstraDB.DataApi.Core.Results;
20+
using DataStax.AstraDB.DataApi.Utils;
1721
using System;
1822
using System.Collections.Generic;
23+
using System.Linq;
24+
using System.Net.Http;
1925
using System.Threading.Tasks;
20-
using DataStax.AstraDB.DataApi.Core;
21-
using DataStax.AstraDB.DataApi.Core.Results;
2226

2327
namespace DataStax.AstraDB.DataApi.Admin
2428
{
29+
public class EmbeddingProviderInfo
30+
{
31+
public string Name { get; set; }
32+
public string Description { get; set; }
33+
public string Version { get; set; }
34+
public List<string> SupportedModels { get; set; }
35+
}
36+
2537
public class DatabaseAdminAstra : IDatabaseAdmin
2638
{
2739
private readonly Guid _id;
2840

29-
public DatabaseAdminAstra(Guid id)
41+
private readonly Database _database;
42+
43+
private readonly CommandOptions _adminOptions;
44+
private readonly DataApiClient _client;
45+
46+
private CommandOptions[] _optionsTree => new CommandOptions[] { _client.ClientOptions, _adminOptions };
47+
48+
internal DatabaseAdminAstra(Guid id, DataApiClient client, CommandOptions adminOptions)
3049
{
50+
Guard.NotNull(client, nameof(client));
51+
_client = client;
52+
_adminOptions = adminOptions;
53+
54+
_database = _client.GetDatabase(id);
3155
_id = id;
3256
}
3357

34-
public IEnumerable<string> ListKeyspaceNames()
58+
internal DatabaseAdminAstra(Database database, DataApiClient client, CommandOptions adminOptions)
3559
{
36-
throw new NotImplementedException();
60+
Guard.NotNull(client, nameof(client));
61+
_client = client;
62+
_adminOptions = adminOptions;
63+
64+
_database = database;
65+
_id = _database.DatabaseId;
3766
}
3867

39-
public FindEmbeddingProvidersResult FindEmbeddingProviders()
68+
public Database GetDatabase()
4069
{
41-
throw new NotImplementedException();
70+
return _database;
4271
}
4372

44-
public Task<IEnumerable<string>> ListKeyspaceNamesAsync()
73+
public string GetApiEndpoint()
4574
{
46-
throw new NotImplementedException();
75+
return _database.ApiEndpoint;
4776
}
4877

49-
public Database GetDatabase(string keyspace)
78+
public IEnumerable<string> ListKeyspaceNames()
5079
{
51-
throw new NotImplementedException();
80+
return ListKeyspaceNamesAsync(true, null).ResultSync();
5281
}
5382

54-
public Database GetDatabase(string keyspace, string userToken)
83+
public Task<IEnumerable<string>> ListKeyspaceNamesAsync()
5584
{
56-
throw new NotImplementedException();
85+
return ListKeyspaceNamesAsync(false, null);
5786
}
5887

59-
public Database GetDatabase()
88+
public IEnumerable<string> ListKeyspaceNames(CommandOptions options)
6089
{
61-
throw new NotImplementedException();
90+
return ListKeyspaceNamesAsync(true, options).ResultSync();
6291
}
6392

64-
public void DropKeyspace(string keyspace)
93+
public Task<IEnumerable<string>> ListKeyspaceNamesAsync(CommandOptions options)
6594
{
66-
throw new NotImplementedException();
95+
return ListKeyspaceNamesAsync(false, options);
6796
}
6897

69-
public void DropKeyspace(string keyspace, CommandOptions options)
98+
internal async Task<IEnumerable<string>> ListKeyspaceNamesAsync(bool runSynchronously, CommandOptions options)
7099
{
71-
throw new NotImplementedException();
100+
var databaseInfo = await _client.GetAstraAdmin().GetDatabaseInfoAsync(_id, options, runSynchronously);
101+
return databaseInfo.Info.Keyspaces;
72102
}
73103

74-
public Task DropKeyspaceAsync(string keyspace)
104+
public void CreateKeyspace(string keyspace)
75105
{
76-
throw new NotImplementedException();
106+
CreateKeyspaceAsync(keyspace, false, null, true).ResultSync();
77107
}
78108

79109
public void CreateKeyspace(string keyspace, bool updateDBKeyspace)
80110
{
81-
throw new NotImplementedException();
111+
CreateKeyspaceAsync(keyspace, updateDBKeyspace, null, true).ResultSync();
82112
}
83113

84-
public void CreateKeyspace(string keyspace)
114+
public void CreateKeyspace(string keyspace, CommandOptions options)
85115
{
86-
throw new NotImplementedException();
116+
CreateKeyspaceAsync(keyspace, false, options, true).ResultSync();
117+
}
118+
119+
public void CreateKeyspace(string keyspace, bool updateDBKeyspace, CommandOptions options)
120+
{
121+
CreateKeyspaceAsync(keyspace, updateDBKeyspace, options, true).ResultSync();
87122
}
88123

89124
public Task CreateKeyspaceAsync(string keyspace)
90125
{
91-
throw new NotImplementedException();
126+
return CreateKeyspaceAsync(keyspace, false, null, false);
127+
}
128+
129+
public Task CreateKeyspaceAsync(string keyspace, bool updateDBKeyspace)
130+
{
131+
return CreateKeyspaceAsync(keyspace, updateDBKeyspace, null, false);
132+
}
133+
134+
public Task CreateKeyspaceAsync(string keyspace, CommandOptions options)
135+
{
136+
return CreateKeyspaceAsync(keyspace, false, options, false);
137+
}
138+
139+
public Task CreateKeyspaceAsync(string keyspace, bool updateDBKeyspace, CommandOptions options)
140+
{
141+
return CreateKeyspaceAsync(keyspace, updateDBKeyspace, options, false);
142+
}
143+
144+
internal async Task CreateKeyspaceAsync(string keyspace, bool updateDBKeyspace, CommandOptions options, bool runSynchronously)
145+
{
146+
options ??= new CommandOptions();
147+
options.IncludeKeyspaceInUrl = false;
148+
Guard.NotNullOrEmpty(keyspace, nameof(keyspace));
149+
150+
bool exists = await KeyspaceExistsAsync(keyspace, options, runSynchronously).ConfigureAwait(false);
151+
if (exists)
152+
{
153+
throw new InvalidOperationException($"Keyspace {keyspace} already exists");
154+
}
155+
156+
var command = CreateCommandAdmin()
157+
.AddUrlPath("databases")
158+
.AddUrlPath(_id.ToString())
159+
.AddUrlPath("keyspaces")
160+
.AddUrlPath(keyspace)
161+
.AddCommandOptions(options);
162+
163+
await command.RunAsyncRaw<Command.EmptyResult>(HttpMethod.Post, runSynchronously).ConfigureAwait(false);
164+
165+
if (updateDBKeyspace && options != null)
166+
{
167+
options.Keyspace = keyspace;
168+
}
169+
}
170+
171+
public void DropKeyspace(string keyspace)
172+
{
173+
DropKeyspaceAsync(keyspace, null, true).ResultSync();
174+
}
175+
176+
public Task DropKeyspaceAsync(string keyspace)
177+
{
178+
return DropKeyspaceAsync(keyspace, null, false);
179+
}
180+
public void DropKeyspace(string keyspace, CommandOptions options)
181+
{
182+
DropKeyspaceAsync(keyspace, options, true).ResultSync();
183+
}
184+
185+
public Task DropKeyspaceAsync(string keyspace, CommandOptions options)
186+
{
187+
return DropKeyspaceAsync(keyspace, options, false);
188+
}
189+
190+
internal async Task DropKeyspaceAsync(string keyspace, CommandOptions options, bool runSynchronously)
191+
{
192+
Guard.NotNullOrEmpty(keyspace, nameof(keyspace));
193+
194+
var command = CreateCommandAdmin()
195+
.AddUrlPath($"databases/{_id}/keyspaces/{keyspace}")
196+
.AddCommandOptions(options);
197+
198+
await command.RunAsyncRaw<Command.EmptyResult>(HttpMethod.Delete, runSynchronously)
199+
.ConfigureAwait(false);
92200
}
93201

94202
public bool KeyspaceExists(string keyspace)
95203
{
96-
throw new NotImplementedException();
204+
return KeyspaceExistsAsync(keyspace, null, true).ResultSync();
205+
}
206+
207+
public Task<bool> KeyspaceExistsAsync(string keyspace)
208+
{
209+
return KeyspaceExistsAsync(keyspace, null, false);
210+
}
211+
212+
internal async Task<bool> KeyspaceExistsAsync(string keyspace, CommandOptions options, bool runSynchronously)
213+
{
214+
Guard.NotNullOrEmpty(keyspace, nameof(keyspace));
215+
var keyspaces = await ListKeyspaceNamesAsync(runSynchronously, options).ConfigureAwait(false);
216+
return keyspaces.Contains(keyspace);
217+
}
218+
219+
public FindEmbeddingProvidersResult FindEmbeddingProviders()
220+
{
221+
return FindEmbeddingProvidersAsync(null, true).ResultSync();
222+
}
223+
224+
public Task<FindEmbeddingProvidersResult> FindEmbeddingProvidersAsync()
225+
{
226+
return FindEmbeddingProvidersAsync(null, false);
227+
}
228+
229+
public FindEmbeddingProvidersResult FindEmbeddingProviders(CommandOptions options)
230+
{
231+
return FindEmbeddingProvidersAsync(options, true).ResultSync();
232+
}
233+
234+
public Task<FindEmbeddingProvidersResult> FindEmbeddingProvidersAsync(CommandOptions options)
235+
{
236+
return FindEmbeddingProvidersAsync(options, false);
237+
}
238+
239+
public class FindEmbeddingProvidersResponse
240+
{
241+
public Status status { get; set; }
242+
}
243+
244+
public class Status
245+
{
246+
public Dictionary<string, EmbeddingProvider> embeddingProviders { get; set; }
247+
}
248+
internal async Task<FindEmbeddingProvidersResult> FindEmbeddingProvidersAsync(CommandOptions options, bool runSynchronously)
249+
{
250+
var command = CreateCommandEmbedding()
251+
.AddCommandOptions(options)
252+
.WithPayload(new { findEmbeddingProviders = new { } });
253+
254+
var response = await command
255+
.RunAsyncRaw<FindEmbeddingProvidersResponse>(HttpMethod.Post, runSynchronously)
256+
.ConfigureAwait(false);
257+
258+
var result = new FindEmbeddingProvidersResult();
259+
260+
if (response?.status?.embeddingProviders is Dictionary<string, EmbeddingProvider> providers)
261+
{
262+
foreach (var kvp in providers)
263+
{
264+
result.EmbeddingProviders[kvp.Key] = kvp.Value;
265+
}
266+
}
267+
268+
return result;
269+
}
270+
271+
private Command CreateCommandDb()
272+
{
273+
return new Command(_database.Client, _optionsTree, new DatabaseCommandUrlBuilder(_database, null));
274+
}
275+
276+
private Command CreateCommandAdmin()
277+
{
278+
return new Command(_database.Client, _optionsTree, new AdminCommandUrlBuilder(_optionsTree, null));
279+
}
280+
281+
private Command CreateCommandEmbedding()
282+
{
283+
return new Command(_database.Client, _optionsTree, new EmbeddingCommandUrlBuilder(_database));
97284
}
98285
}
99286
}

src/DataStax.AstraDB.DataApi/Admin/IDatabaseAdmin.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ public interface IDatabaseAdmin
2626
IEnumerable<string> ListKeyspaceNames();
2727
FindEmbeddingProvidersResult FindEmbeddingProviders();
2828
Task<IEnumerable<string>> ListKeyspaceNamesAsync();
29-
Database GetDatabase(string keyspace);
30-
Database GetDatabase(string keyspace, string userToken);
29+
3130
Database GetDatabase();
3231
void DropKeyspace(string keyspace);
3332
void DropKeyspace(string keyspace, CommandOptions options);

0 commit comments

Comments
 (0)