Skip to content
This repository was archived by the owner on Jul 12, 2020. It is now read-only.

Commit 4bcee66

Browse files
authored
Some basic v3 work (#111)
* Started migration to v3 * A bit more work migrating * Should build now
1 parent d9cb06a commit 4bcee66

36 files changed

+1448
-1908
lines changed

src/Cosmonaut/Cosmonaut.csproj

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netstandard1.6</TargetFramework>
4+
<TargetFramework>netstandard2.0</TargetFramework>
55
<Authors>Nick Chapsas</Authors>
66
<Description>A supercharged .NET SDK for Azure CosmosDB with ORM support</Description>
77
<Summary>A powerful and easy to use SDK for Azure CosmosDB.</Summary>
@@ -12,24 +12,16 @@
1212
<RepositoryUrl>https://github.com/Elfocrash/Cosmonaut</RepositoryUrl>
1313
<PackageTags>azure entitystore entity db orm microsoft cosmos cosmosdb documentdb docdb nosql azureofficial dotnetcore netcore netstandard</PackageTags>
1414
<PackageReleaseNotes>Please report any issues on Github.</PackageReleaseNotes>
15-
<Version>2.11.3</Version>
16-
<NeutralLanguage></NeutralLanguage>
15+
<Version>3.0</Version>
1716
<Company>Nick Chapsas</Company>
1817
<PackageIconUrl>https://raw.githubusercontent.com/Elfocrash/Cosmonaut/develop/logo.png</PackageIconUrl>
19-
<PackageVersion>2.11.3</PackageVersion>
20-
</PropertyGroup>
21-
22-
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
23-
<LangVersion>latest</LangVersion>
24-
</PropertyGroup>
25-
26-
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
27-
<LangVersion>latest</LangVersion>
18+
<PackageVersion>3.0</PackageVersion>
2819
</PropertyGroup>
2920

3021
<ItemGroup>
3122
<PackageReference Include="Humanizer.Core.uk" Version="2.5.16" />
32-
<PackageReference Include="Microsoft.Azure.DocumentDB.Core" Version="2.5.1" />
23+
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.1.1" />
24+
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
3325
</ItemGroup>
3426

3527
</Project>

src/Cosmonaut/Cosmonaut.csproj.DotSettings

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/Cosmonaut/CosmonautClient.cs

Lines changed: 43 additions & 311 deletions
Large diffs are not rendered by default.

src/Cosmonaut/CosmosConstants.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Microsoft.Azure.Documents;
1+
using Microsoft.Azure.Cosmos;
22

33
namespace Cosmonaut
44
{
@@ -8,8 +8,7 @@ public class CosmosConstants
88
public const int MinimumCosmosThroughput = 400;
99
public const int DefaultMaximumUpscaleThroughput = 10000;
1010
public const int TooManyRequestsStatusCode = 429;
11-
public static readonly IndexingPolicy DefaultIndexingPolicy =
12-
new IndexingPolicy(new RangeIndex(DataType.Number, -1), new RangeIndex(DataType.String, -1), new SpatialIndex(DataType.Point));
11+
public static readonly IndexingPolicy DefaultIndexingPolicy = new IndexingPolicy();
1312
public static readonly UniqueKeyPolicy DefaultUniqueKeyPolicy = new UniqueKeyPolicy();
1413
}
1514
}

src/Cosmonaut/CosmosStore.cs

Lines changed: 328 additions & 340 deletions
Large diffs are not rendered by default.

src/Cosmonaut/CosmosStoreSettings.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using Cosmonaut.Configuration;
3-
using Microsoft.Azure.Documents;
4-
using Microsoft.Azure.Documents.Client;
3+
using Microsoft.Azure.Cosmos;
54
using Newtonsoft.Json;
65

76
namespace Cosmonaut
@@ -14,7 +13,7 @@ public class CosmosStoreSettings
1413

1514
public Uri EndpointUrl { get; }
1615

17-
public ConnectionPolicy ConnectionPolicy { get; set; }
16+
public ConnectionMode ConnectionMode { get; set; }
1817

1918
public ConsistencyLevel? ConsistencyLevel { get; set; } = null;
2019

@@ -28,7 +27,7 @@ public class CosmosStoreSettings
2827

2928
public ThroughputBehaviour OnDatabaseThroughput { get; set; } = ThroughputBehaviour.UseDatabaseThroughput;
3029

31-
public JsonSerializerSettings JsonSerializerSettings { get; set; }
30+
public CosmosSerializer CosmosSerializer { get; set; }
3231

3332
public bool InfiniteRetries { get; set; } = true;
3433

@@ -58,13 +57,13 @@ public CosmosStoreSettings(
5857
string databaseName,
5958
string endpointUrl,
6059
string authKey,
61-
ConnectionPolicy connectionPolicy = null,
60+
ConnectionMode connectionMode = ConnectionMode.Direct,
6261
IndexingPolicy indexingPolicy = null,
6362
int defaultCollectionThroughput = CosmosConstants.MinimumCosmosThroughput)
6463
: this(databaseName,
6564
new Uri(endpointUrl),
6665
authKey,
67-
connectionPolicy,
66+
connectionMode,
6867
indexingPolicy,
6968
defaultCollectionThroughput)
7069
{
@@ -74,14 +73,14 @@ public CosmosStoreSettings(
7473
string databaseName,
7574
Uri endpointUrl,
7675
string authKey,
77-
ConnectionPolicy connectionPolicy = null,
76+
ConnectionMode connectionMode = ConnectionMode.Direct,
7877
IndexingPolicy indexingPolicy = null,
7978
int defaultCollectionThroughput = CosmosConstants.MinimumCosmosThroughput)
8079
{
8180
DatabaseName = databaseName ?? throw new ArgumentNullException(nameof(databaseName));
8281
EndpointUrl = endpointUrl ?? throw new ArgumentNullException(nameof(endpointUrl));
8382
AuthKey = authKey ?? throw new ArgumentNullException(nameof(authKey));
84-
ConnectionPolicy = connectionPolicy;
83+
ConnectionMode = connectionMode;
8584
DefaultCollectionThroughput = defaultCollectionThroughput;
8685

8786
IndexingPolicy = indexingPolicy ?? CosmosConstants.DefaultIndexingPolicy;

src/Cosmonaut/Diagnostics/CosmosEventCall.cs

Lines changed: 18 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
using System.Net;
66
using System.Reflection;
77
using System.Threading.Tasks;
8-
using Microsoft.Azure.Documents;
9-
using Microsoft.Azure.Documents.Client;
8+
using Microsoft.Azure.Cosmos;
109
using Newtonsoft.Json;
1110

1211
namespace Cosmonaut.Diagnostics
@@ -78,8 +77,8 @@ internal async Task<FeedResponse<TEntity>> InvokeAsync<TEntity>(Func<Task<FeedRe
7877
timer.Start();
7978
var result = await eventCall();
8079
timer.Stop();
81-
AddEventMetadataFromHeaders(result.ResponseHeaders);
82-
LogQueryMetricsIfPresent(result);
80+
AddEventMetadataFromHeaders(result.Headers);
81+
//LogQueryMetricsIfPresent(result);
8382
TrackSuccess(timer, HttpStatusCode.OK.ToString("D"));
8483
return result;
8584
}
@@ -91,7 +90,7 @@ internal async Task<FeedResponse<TEntity>> InvokeAsync<TEntity>(Func<Task<FeedRe
9190
}
9291
}
9392

94-
internal async Task<DocumentResponse<TEntity>> InvokeAsync<TEntity>(Func<Task<DocumentResponse<TEntity>>> eventCall)
93+
internal async Task<ItemResponse<TEntity>> InvokeAsync<TEntity>(Func<Task<ItemResponse<TEntity>>> eventCall)
9594
{
9695
if (!CosmosEventSource.EventSource.IsEnabled())
9796
{
@@ -105,7 +104,7 @@ internal async Task<DocumentResponse<TEntity>> InvokeAsync<TEntity>(Func<Task<Do
105104
timer.Start();
106105
var result = await eventCall();
107106
timer.Stop();
108-
AddEventMetadataFromHeaders(result.ResponseHeaders);
107+
AddEventMetadataFromHeaders(result.Headers);
109108
TrackSuccess(timer, HttpStatusCode.OK.ToString("D"));
110109
return result;
111110
}
@@ -118,7 +117,7 @@ internal async Task<DocumentResponse<TEntity>> InvokeAsync<TEntity>(Func<Task<Do
118117
}
119118
}
120119

121-
internal async Task<ResourceResponse<TEntity>> InvokeAsync<TEntity>(Func<Task<ResourceResponse<TEntity>>> eventCall) where TEntity : Resource, new()
120+
internal async Task<Response<TEntity>> InvokeAsync<TEntity>(Func<Task<Response<TEntity>>> eventCall)
122121
{
123122
if (!CosmosEventSource.EventSource.IsEnabled())
124123
{
@@ -132,33 +131,7 @@ internal async Task<DocumentResponse<TEntity>> InvokeAsync<TEntity>(Func<Task<Do
132131
timer.Start();
133132
var result = await eventCall();
134133
timer.Stop();
135-
AddEventMetadataFromHeaders(result.ResponseHeaders);
136-
TrackSuccess(timer, result.StatusCode.ToString("D"));
137-
return result;
138-
}
139-
catch (Exception ex)
140-
{
141-
timer.Stop();
142-
TrackException(ex, timer);
143-
throw;
144-
}
145-
}
146-
147-
internal async Task<StoredProcedureResponse<TEntity>> InvokeAsync<TEntity>(Func<Task<StoredProcedureResponse<TEntity>>> eventCall)
148-
{
149-
if (!CosmosEventSource.EventSource.IsEnabled())
150-
{
151-
return await eventCall();
152-
}
153-
154-
var timer = new Stopwatch();
155-
try
156-
{
157-
SetPreExecutionEventMetadata(eventCall);
158-
timer.Start();
159-
var result = await eventCall();
160-
timer.Stop();
161-
AddEventMetadataFromHeaders(result.ResponseHeaders);
134+
AddEventMetadataFromHeaders(result.Headers);
162135
TrackSuccess(timer, HttpStatusCode.OK.ToString("D"));
163136
return result;
164137
}
@@ -171,13 +144,13 @@ internal async Task<StoredProcedureResponse<TEntity>> InvokeAsync<TEntity>(Func<
171144
}
172145
}
173146

174-
private void LogQueryMetricsIfPresent<TEntity>(FeedResponse<TEntity> result)
175-
{
176-
if (result.QueryMetrics == null)
177-
return;
178-
179-
EventMetadata.Properties[nameof(result.QueryMetrics)] = JsonConvert.SerializeObject(result.QueryMetrics);
180-
}
147+
// private void LogQueryMetricsIfPresent<TEntity>(FeedResponse<TEntity> result)
148+
// {
149+
// if (result.QueryMetrics == null)
150+
// return;
151+
//
152+
// EventMetadata.Properties[nameof(result.QueryMetrics)] = JsonConvert.SerializeObject(result.QueryMetrics);
153+
// }
181154

182155
private void SetPreExecutionEventMetadata<TResult>(Func<Task<TResult>> eventCall)
183156
{
@@ -228,17 +201,17 @@ private void TrackSuccess(Stopwatch timer, string resultCode)
228201

229202
private void AddEventMetadataFromException(Exception ex)
230203
{
231-
if (!(ex is DocumentClientException documentClientException))
204+
if (!(ex is CosmosException cosmosException))
232205
{
233206
EventMetadata.ResultCode = HttpStatusCode.InternalServerError.ToString("D");
234207
return;
235208
}
236209

237-
AddEventMetadataFromHeaders(documentClientException.ResponseHeaders);
238-
EventMetadata.ResultCode = documentClientException.StatusCode?.ToString("D") ?? HttpStatusCode.InternalServerError.ToString("D");
210+
AddEventMetadataFromHeaders(cosmosException.Headers);
211+
EventMetadata.ResultCode = cosmosException.StatusCode.ToString("D");
239212
}
240213

241-
private void AddEventMetadataFromHeaders(NameValueCollection headers)
214+
private void AddEventMetadataFromHeaders(Headers headers)
242215
{
243216
if (headers == null)
244217
return;

src/Cosmonaut/Diagnostics/CosmosEventExtensions.cs

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
using System.Diagnostics.Tracing;
44
using System.Runtime.CompilerServices;
55
using System.Threading.Tasks;
6-
using Microsoft.Azure.Documents;
7-
using Microsoft.Azure.Documents.Client;
6+
using Microsoft.Azure.Cosmos;
87

98
namespace Cosmonaut.Diagnostics
109
{
@@ -21,31 +20,9 @@ internal static Task<TResult> InvokeCosmosCallAsync<TResult>(
2120
return CreateCosmosEventCall(invoker, data, properties, target, name).InvokeAsync(eventCall);
2221
}
2322

24-
internal static Task<ResourceResponse<TResource>> InvokeCosmosOperationAsync<TResource>(
23+
internal static Task<ItemResponse<TResource>> InvokeCosmosOperationAsync<TResource>(
2524
this object invoker,
26-
Func<Task<ResourceResponse<TResource>>> eventCall,
27-
string data,
28-
Dictionary<string, object> properties = null,
29-
string target = null,
30-
[CallerMemberName]string name = null) where TResource : Resource, new()
31-
{
32-
return CreateCosmosEventCall(invoker, data, properties, target, name).InvokeAsync(eventCall);
33-
}
34-
35-
internal static Task<DocumentResponse<TResource>> InvokeCosmosOperationAsync<TResource>(
36-
this object invoker,
37-
Func<Task<DocumentResponse<TResource>>> eventCall,
38-
string data,
39-
Dictionary<string, object> properties = null,
40-
string target = null,
41-
[CallerMemberName]string name = null)
42-
{
43-
return CreateCosmosEventCall(invoker, data, properties, target, name).InvokeAsync(eventCall);
44-
}
45-
46-
internal static Task<StoredProcedureResponse<TResource>> InvokeCosmosOperationAsync<TResource>(
47-
this object invoker,
48-
Func<Task<StoredProcedureResponse<TResource>>> eventCall,
25+
Func<Task<ItemResponse<TResource>>> eventCall,
4926
string data,
5027
Dictionary<string, object> properties = null,
5128
string target = null,

src/Cosmonaut/Exceptions/CosmosCollectionThroughputUpdateException.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using System;
2-
using Microsoft.Azure.Documents;
2+
using Microsoft.Azure.Cosmos;
33

44
namespace Cosmonaut.Exceptions
55
{
66
public class CosmosCollectionThroughputUpdateException : Exception
77
{
8-
public CosmosCollectionThroughputUpdateException(DocumentCollection collection) : base($"Failed to update hroughput of collection {collection.Id}")
8+
public CosmosCollectionThroughputUpdateException(Container collection) : base($"Failed to update hroughput of collection {collection.Id}")
99
{
1010

1111
}

src/Cosmonaut/Extensions/CosmonautHelpers.cs

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,33 @@
1-
using Microsoft.Azure.Documents;
1+
using Cosmonaut.Internal;
22
using Newtonsoft.Json;
33
using Newtonsoft.Json.Linq;
44

55
namespace Cosmonaut.Extensions
66
{
77
public static class CosmonautHelpers
88
{
9-
public static Document ToCosmonautDocument<TEntity>(this TEntity obj, JsonSerializerSettings settings) where TEntity : class
9+
public static CosmosDocument ToCosmonautDocument<TEntity>(this TEntity obj, JsonSerializerSettings settings) where TEntity : class
1010
{
1111
obj.ValidateEntityForCosmosDb();
1212
var document = JsonConvert.DeserializeObject<dynamic>(JsonConvert.SerializeObject(obj, settings), settings);
1313

14-
using (JsonReader reader = new JTokenReader(document))
15-
{
16-
var actualDocument = new Document();
17-
actualDocument.LoadFrom(reader);
18-
actualDocument.Id = obj.GetDocumentId();
19-
RemoveDuplicateIds(ref actualDocument);
20-
21-
if (typeof(TEntity).UsesSharedCollection())
22-
actualDocument.SetPropertyValue(nameof(ISharedCosmosEntity.CosmosEntityName), $"{typeof(TEntity).GetSharedCollectionEntityName()}");
23-
24-
return actualDocument;
25-
}
14+
// var actualDocument = new CosmosDocument(document) {Id = obj.GetDocumentId()};
15+
//
16+
// RemoveDuplicateIds(ref actualDocument);
17+
//
18+
// if (typeof(TEntity).UsesSharedCollection())
19+
// actualDocument.SetPropertyValue(nameof(ISharedCosmosEntity.CosmosEntityName), $"{typeof(TEntity).GetSharedCollectionEntityName()}");
20+
//
21+
// return actualDocument;
22+
return new CosmosDocument(null);
2623
}
2724

28-
internal static PartitionKeyDefinition GetPartitionKeyDefinition(string partitionKeyName)
25+
internal static string GetPartitionKeyDefinition(string partitionKeyName)
2926
{
30-
return new PartitionKeyDefinition
31-
{
32-
Paths =
33-
{
34-
$"/{partitionKeyName}"
35-
}
36-
};
27+
return $"/{partitionKeyName}";
3728
}
3829

39-
internal static void RemoveDuplicateIds(ref Document actualDocument)
30+
internal static void RemoveDuplicateIds(ref CosmosDocument actualDocument)
4031
{
4132
actualDocument.SetPropertyValue("Id", null);
4233
actualDocument.SetPropertyValue("ID", null);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Microsoft.Azure.Cosmos;
2+
3+
namespace Cosmonaut.Extensions
4+
{
5+
public static class CosmosClientExtensions
6+
{
7+
public static void SetupInfiniteRetries(this CosmosClient cosmosClient)
8+
{
9+
cosmosClient.ClientOptions.MaxRetryAttemptsOnRateLimitedRequests = int.MaxValue;
10+
}
11+
}
12+
}

0 commit comments

Comments
 (0)