|
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | 16 |
|
| 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; |
17 | 21 | using System; |
18 | 22 | using System.Collections.Generic; |
| 23 | +using System.Linq; |
| 24 | +using System.Net.Http; |
19 | 25 | using System.Threading.Tasks; |
20 | | -using DataStax.AstraDB.DataApi.Core; |
21 | | -using DataStax.AstraDB.DataApi.Core.Results; |
22 | 26 |
|
23 | 27 | namespace DataStax.AstraDB.DataApi.Admin |
24 | 28 | { |
| 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 | + |
25 | 37 | public class DatabaseAdminAstra : IDatabaseAdmin |
26 | 38 | { |
27 | 39 | private readonly Guid _id; |
28 | 40 |
|
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) |
30 | 49 | { |
| 50 | + Guard.NotNull(client, nameof(client)); |
| 51 | + _client = client; |
| 52 | + _adminOptions = adminOptions; |
| 53 | + |
| 54 | + _database = _client.GetDatabase(id); |
31 | 55 | _id = id; |
32 | 56 | } |
33 | 57 |
|
34 | | - public IEnumerable<string> ListKeyspaceNames() |
| 58 | + internal DatabaseAdminAstra(Database database, DataApiClient client, CommandOptions adminOptions) |
35 | 59 | { |
36 | | - throw new NotImplementedException(); |
| 60 | + Guard.NotNull(client, nameof(client)); |
| 61 | + _client = client; |
| 62 | + _adminOptions = adminOptions; |
| 63 | + |
| 64 | + _database = database; |
| 65 | + _id = _database.DatabaseId; |
37 | 66 | } |
38 | 67 |
|
39 | | - public FindEmbeddingProvidersResult FindEmbeddingProviders() |
| 68 | + public Database GetDatabase() |
40 | 69 | { |
41 | | - throw new NotImplementedException(); |
| 70 | + return _database; |
42 | 71 | } |
43 | 72 |
|
44 | | - public Task<IEnumerable<string>> ListKeyspaceNamesAsync() |
| 73 | + public string GetApiEndpoint() |
45 | 74 | { |
46 | | - throw new NotImplementedException(); |
| 75 | + return _database.ApiEndpoint; |
47 | 76 | } |
48 | 77 |
|
49 | | - public Database GetDatabase(string keyspace) |
| 78 | + public IEnumerable<string> ListKeyspaceNames() |
50 | 79 | { |
51 | | - throw new NotImplementedException(); |
| 80 | + return ListKeyspaceNamesAsync(true, null).ResultSync(); |
52 | 81 | } |
53 | 82 |
|
54 | | - public Database GetDatabase(string keyspace, string userToken) |
| 83 | + public Task<IEnumerable<string>> ListKeyspaceNamesAsync() |
55 | 84 | { |
56 | | - throw new NotImplementedException(); |
| 85 | + return ListKeyspaceNamesAsync(false, null); |
57 | 86 | } |
58 | 87 |
|
59 | | - public Database GetDatabase() |
| 88 | + public IEnumerable<string> ListKeyspaceNames(CommandOptions options) |
60 | 89 | { |
61 | | - throw new NotImplementedException(); |
| 90 | + return ListKeyspaceNamesAsync(true, options).ResultSync(); |
62 | 91 | } |
63 | 92 |
|
64 | | - public void DropKeyspace(string keyspace) |
| 93 | + public Task<IEnumerable<string>> ListKeyspaceNamesAsync(CommandOptions options) |
65 | 94 | { |
66 | | - throw new NotImplementedException(); |
| 95 | + return ListKeyspaceNamesAsync(false, options); |
67 | 96 | } |
68 | 97 |
|
69 | | - public void DropKeyspace(string keyspace, CommandOptions options) |
| 98 | + internal async Task<IEnumerable<string>> ListKeyspaceNamesAsync(bool runSynchronously, CommandOptions options) |
70 | 99 | { |
71 | | - throw new NotImplementedException(); |
| 100 | + var databaseInfo = await _client.GetAstraAdmin().GetDatabaseInfoAsync(_id, options, runSynchronously); |
| 101 | + return databaseInfo.Info.Keyspaces; |
72 | 102 | } |
73 | 103 |
|
74 | | - public Task DropKeyspaceAsync(string keyspace) |
| 104 | + public void CreateKeyspace(string keyspace) |
75 | 105 | { |
76 | | - throw new NotImplementedException(); |
| 106 | + CreateKeyspaceAsync(keyspace, false, null, true).ResultSync(); |
77 | 107 | } |
78 | 108 |
|
79 | 109 | public void CreateKeyspace(string keyspace, bool updateDBKeyspace) |
80 | 110 | { |
81 | | - throw new NotImplementedException(); |
| 111 | + CreateKeyspaceAsync(keyspace, updateDBKeyspace, null, true).ResultSync(); |
82 | 112 | } |
83 | 113 |
|
84 | | - public void CreateKeyspace(string keyspace) |
| 114 | + public void CreateKeyspace(string keyspace, CommandOptions options) |
85 | 115 | { |
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(); |
87 | 122 | } |
88 | 123 |
|
89 | 124 | public Task CreateKeyspaceAsync(string keyspace) |
90 | 125 | { |
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); |
92 | 200 | } |
93 | 201 |
|
94 | 202 | public bool KeyspaceExists(string keyspace) |
95 | 203 | { |
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)); |
97 | 284 | } |
98 | 285 | } |
99 | 286 | } |
0 commit comments