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

Commit

Permalink
Some basic v3 work (#111)
Browse files Browse the repository at this point in the history
* Started migration to v3

* A bit more work migrating

* Should build now
  • Loading branch information
Elfocrash authored Sep 5, 2019
1 parent d9cb06a commit 4bcee66
Show file tree
Hide file tree
Showing 36 changed files with 1,448 additions and 1,908 deletions.
18 changes: 5 additions & 13 deletions src/Cosmonaut/Cosmonaut.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard1.6</TargetFramework>
<TargetFramework>netstandard2.0</TargetFramework>
<Authors>Nick Chapsas</Authors>
<Description>A supercharged .NET SDK for Azure CosmosDB with ORM support</Description>
<Summary>A powerful and easy to use SDK for Azure CosmosDB.</Summary>
Expand All @@ -12,24 +12,16 @@
<RepositoryUrl>https://github.com/Elfocrash/Cosmonaut</RepositoryUrl>
<PackageTags>azure entitystore entity db orm microsoft cosmos cosmosdb documentdb docdb nosql azureofficial dotnetcore netcore netstandard</PackageTags>
<PackageReleaseNotes>Please report any issues on Github.</PackageReleaseNotes>
<Version>2.11.3</Version>
<NeutralLanguage></NeutralLanguage>
<Version>3.0</Version>
<Company>Nick Chapsas</Company>
<PackageIconUrl>https://raw.githubusercontent.com/Elfocrash/Cosmonaut/develop/logo.png</PackageIconUrl>
<PackageVersion>2.11.3</PackageVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<LangVersion>latest</LangVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<LangVersion>latest</LangVersion>
<PackageVersion>3.0</PackageVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Humanizer.Core.uk" Version="2.5.16" />
<PackageReference Include="Microsoft.Azure.DocumentDB.Core" Version="2.5.1" />
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.1.1" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
</ItemGroup>

</Project>
2 changes: 0 additions & 2 deletions src/Cosmonaut/Cosmonaut.csproj.DotSettings

This file was deleted.

354 changes: 43 additions & 311 deletions src/Cosmonaut/CosmonautClient.cs

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions src/Cosmonaut/CosmosConstants.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.Azure.Documents;
using Microsoft.Azure.Cosmos;

namespace Cosmonaut
{
Expand All @@ -8,8 +8,7 @@ public class CosmosConstants
public const int MinimumCosmosThroughput = 400;
public const int DefaultMaximumUpscaleThroughput = 10000;
public const int TooManyRequestsStatusCode = 429;
public static readonly IndexingPolicy DefaultIndexingPolicy =
new IndexingPolicy(new RangeIndex(DataType.Number, -1), new RangeIndex(DataType.String, -1), new SpatialIndex(DataType.Point));
public static readonly IndexingPolicy DefaultIndexingPolicy = new IndexingPolicy();
public static readonly UniqueKeyPolicy DefaultUniqueKeyPolicy = new UniqueKeyPolicy();
}
}
668 changes: 328 additions & 340 deletions src/Cosmonaut/CosmosStore.cs

Large diffs are not rendered by default.

15 changes: 7 additions & 8 deletions src/Cosmonaut/CosmosStoreSettings.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using Cosmonaut.Configuration;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using Microsoft.Azure.Cosmos;
using Newtonsoft.Json;

namespace Cosmonaut
Expand All @@ -14,7 +13,7 @@ public class CosmosStoreSettings

public Uri EndpointUrl { get; }

public ConnectionPolicy ConnectionPolicy { get; set; }
public ConnectionMode ConnectionMode { get; set; }

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

Expand All @@ -28,7 +27,7 @@ public class CosmosStoreSettings

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

public JsonSerializerSettings JsonSerializerSettings { get; set; }
public CosmosSerializer CosmosSerializer { get; set; }

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

Expand Down Expand Up @@ -58,13 +57,13 @@ public CosmosStoreSettings(
string databaseName,
string endpointUrl,
string authKey,
ConnectionPolicy connectionPolicy = null,
ConnectionMode connectionMode = ConnectionMode.Direct,
IndexingPolicy indexingPolicy = null,
int defaultCollectionThroughput = CosmosConstants.MinimumCosmosThroughput)
: this(databaseName,
new Uri(endpointUrl),
authKey,
connectionPolicy,
connectionMode,
indexingPolicy,
defaultCollectionThroughput)
{
Expand All @@ -74,14 +73,14 @@ public CosmosStoreSettings(
string databaseName,
Uri endpointUrl,
string authKey,
ConnectionPolicy connectionPolicy = null,
ConnectionMode connectionMode = ConnectionMode.Direct,
IndexingPolicy indexingPolicy = null,
int defaultCollectionThroughput = CosmosConstants.MinimumCosmosThroughput)
{
DatabaseName = databaseName ?? throw new ArgumentNullException(nameof(databaseName));
EndpointUrl = endpointUrl ?? throw new ArgumentNullException(nameof(endpointUrl));
AuthKey = authKey ?? throw new ArgumentNullException(nameof(authKey));
ConnectionPolicy = connectionPolicy;
ConnectionMode = connectionMode;
DefaultCollectionThroughput = defaultCollectionThroughput;

IndexingPolicy = indexingPolicy ?? CosmosConstants.DefaultIndexingPolicy;
Expand Down
63 changes: 18 additions & 45 deletions src/Cosmonaut/Diagnostics/CosmosEventCall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
using System.Net;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using Microsoft.Azure.Cosmos;
using Newtonsoft.Json;

namespace Cosmonaut.Diagnostics
Expand Down Expand Up @@ -78,8 +77,8 @@ internal async Task<FeedResponse<TEntity>> InvokeAsync<TEntity>(Func<Task<FeedRe
timer.Start();
var result = await eventCall();
timer.Stop();
AddEventMetadataFromHeaders(result.ResponseHeaders);
LogQueryMetricsIfPresent(result);
AddEventMetadataFromHeaders(result.Headers);
//LogQueryMetricsIfPresent(result);
TrackSuccess(timer, HttpStatusCode.OK.ToString("D"));
return result;
}
Expand All @@ -91,7 +90,7 @@ internal async Task<FeedResponse<TEntity>> InvokeAsync<TEntity>(Func<Task<FeedRe
}
}

internal async Task<DocumentResponse<TEntity>> InvokeAsync<TEntity>(Func<Task<DocumentResponse<TEntity>>> eventCall)
internal async Task<ItemResponse<TEntity>> InvokeAsync<TEntity>(Func<Task<ItemResponse<TEntity>>> eventCall)
{
if (!CosmosEventSource.EventSource.IsEnabled())
{
Expand All @@ -105,7 +104,7 @@ internal async Task<DocumentResponse<TEntity>> InvokeAsync<TEntity>(Func<Task<Do
timer.Start();
var result = await eventCall();
timer.Stop();
AddEventMetadataFromHeaders(result.ResponseHeaders);
AddEventMetadataFromHeaders(result.Headers);
TrackSuccess(timer, HttpStatusCode.OK.ToString("D"));
return result;
}
Expand All @@ -118,7 +117,7 @@ internal async Task<DocumentResponse<TEntity>> InvokeAsync<TEntity>(Func<Task<Do
}
}

internal async Task<ResourceResponse<TEntity>> InvokeAsync<TEntity>(Func<Task<ResourceResponse<TEntity>>> eventCall) where TEntity : Resource, new()
internal async Task<Response<TEntity>> InvokeAsync<TEntity>(Func<Task<Response<TEntity>>> eventCall)
{
if (!CosmosEventSource.EventSource.IsEnabled())
{
Expand All @@ -132,33 +131,7 @@ internal async Task<DocumentResponse<TEntity>> InvokeAsync<TEntity>(Func<Task<Do
timer.Start();
var result = await eventCall();
timer.Stop();
AddEventMetadataFromHeaders(result.ResponseHeaders);
TrackSuccess(timer, result.StatusCode.ToString("D"));
return result;
}
catch (Exception ex)
{
timer.Stop();
TrackException(ex, timer);
throw;
}
}

internal async Task<StoredProcedureResponse<TEntity>> InvokeAsync<TEntity>(Func<Task<StoredProcedureResponse<TEntity>>> eventCall)
{
if (!CosmosEventSource.EventSource.IsEnabled())
{
return await eventCall();
}

var timer = new Stopwatch();
try
{
SetPreExecutionEventMetadata(eventCall);
timer.Start();
var result = await eventCall();
timer.Stop();
AddEventMetadataFromHeaders(result.ResponseHeaders);
AddEventMetadataFromHeaders(result.Headers);
TrackSuccess(timer, HttpStatusCode.OK.ToString("D"));
return result;
}
Expand All @@ -171,13 +144,13 @@ internal async Task<StoredProcedureResponse<TEntity>> InvokeAsync<TEntity>(Func<
}
}

private void LogQueryMetricsIfPresent<TEntity>(FeedResponse<TEntity> result)
{
if (result.QueryMetrics == null)
return;

EventMetadata.Properties[nameof(result.QueryMetrics)] = JsonConvert.SerializeObject(result.QueryMetrics);
}
// private void LogQueryMetricsIfPresent<TEntity>(FeedResponse<TEntity> result)
// {
// if (result.QueryMetrics == null)
// return;
//
// EventMetadata.Properties[nameof(result.QueryMetrics)] = JsonConvert.SerializeObject(result.QueryMetrics);
// }

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

private void AddEventMetadataFromException(Exception ex)
{
if (!(ex is DocumentClientException documentClientException))
if (!(ex is CosmosException cosmosException))
{
EventMetadata.ResultCode = HttpStatusCode.InternalServerError.ToString("D");
return;
}

AddEventMetadataFromHeaders(documentClientException.ResponseHeaders);
EventMetadata.ResultCode = documentClientException.StatusCode?.ToString("D") ?? HttpStatusCode.InternalServerError.ToString("D");
AddEventMetadataFromHeaders(cosmosException.Headers);
EventMetadata.ResultCode = cosmosException.StatusCode.ToString("D");
}

private void AddEventMetadataFromHeaders(NameValueCollection headers)
private void AddEventMetadataFromHeaders(Headers headers)
{
if (headers == null)
return;
Expand Down
29 changes: 3 additions & 26 deletions src/Cosmonaut/Diagnostics/CosmosEventExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
using System.Diagnostics.Tracing;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using Microsoft.Azure.Cosmos;

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

internal static Task<ResourceResponse<TResource>> InvokeCosmosOperationAsync<TResource>(
internal static Task<ItemResponse<TResource>> InvokeCosmosOperationAsync<TResource>(
this object invoker,
Func<Task<ResourceResponse<TResource>>> eventCall,
string data,
Dictionary<string, object> properties = null,
string target = null,
[CallerMemberName]string name = null) where TResource : Resource, new()
{
return CreateCosmosEventCall(invoker, data, properties, target, name).InvokeAsync(eventCall);
}

internal static Task<DocumentResponse<TResource>> InvokeCosmosOperationAsync<TResource>(
this object invoker,
Func<Task<DocumentResponse<TResource>>> eventCall,
string data,
Dictionary<string, object> properties = null,
string target = null,
[CallerMemberName]string name = null)
{
return CreateCosmosEventCall(invoker, data, properties, target, name).InvokeAsync(eventCall);
}

internal static Task<StoredProcedureResponse<TResource>> InvokeCosmosOperationAsync<TResource>(
this object invoker,
Func<Task<StoredProcedureResponse<TResource>>> eventCall,
Func<Task<ItemResponse<TResource>>> eventCall,
string data,
Dictionary<string, object> properties = null,
string target = null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Cosmos;

namespace Cosmonaut.Exceptions
{
public class CosmosCollectionThroughputUpdateException : Exception
{
public CosmosCollectionThroughputUpdateException(DocumentCollection collection) : base($"Failed to update hroughput of collection {collection.Id}")
public CosmosCollectionThroughputUpdateException(Container collection) : base($"Failed to update hroughput of collection {collection.Id}")
{

}
Expand Down
37 changes: 14 additions & 23 deletions src/Cosmonaut/Extensions/CosmonautHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,42 +1,33 @@
using Microsoft.Azure.Documents;
using Cosmonaut.Internal;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Cosmonaut.Extensions
{
public static class CosmonautHelpers
{
public static Document ToCosmonautDocument<TEntity>(this TEntity obj, JsonSerializerSettings settings) where TEntity : class
public static CosmosDocument ToCosmonautDocument<TEntity>(this TEntity obj, JsonSerializerSettings settings) where TEntity : class
{
obj.ValidateEntityForCosmosDb();
var document = JsonConvert.DeserializeObject<dynamic>(JsonConvert.SerializeObject(obj, settings), settings);

using (JsonReader reader = new JTokenReader(document))
{
var actualDocument = new Document();
actualDocument.LoadFrom(reader);
actualDocument.Id = obj.GetDocumentId();
RemoveDuplicateIds(ref actualDocument);

if (typeof(TEntity).UsesSharedCollection())
actualDocument.SetPropertyValue(nameof(ISharedCosmosEntity.CosmosEntityName), $"{typeof(TEntity).GetSharedCollectionEntityName()}");

return actualDocument;
}
// var actualDocument = new CosmosDocument(document) {Id = obj.GetDocumentId()};
//
// RemoveDuplicateIds(ref actualDocument);
//
// if (typeof(TEntity).UsesSharedCollection())
// actualDocument.SetPropertyValue(nameof(ISharedCosmosEntity.CosmosEntityName), $"{typeof(TEntity).GetSharedCollectionEntityName()}");
//
// return actualDocument;
return new CosmosDocument(null);
}

internal static PartitionKeyDefinition GetPartitionKeyDefinition(string partitionKeyName)
internal static string GetPartitionKeyDefinition(string partitionKeyName)
{
return new PartitionKeyDefinition
{
Paths =
{
$"/{partitionKeyName}"
}
};
return $"/{partitionKeyName}";
}

internal static void RemoveDuplicateIds(ref Document actualDocument)
internal static void RemoveDuplicateIds(ref CosmosDocument actualDocument)
{
actualDocument.SetPropertyValue("Id", null);
actualDocument.SetPropertyValue("ID", null);
Expand Down
12 changes: 12 additions & 0 deletions src/Cosmonaut/Extensions/CosmosClientExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Microsoft.Azure.Cosmos;

namespace Cosmonaut.Extensions
{
public static class CosmosClientExtensions
{
public static void SetupInfiniteRetries(this CosmosClient cosmosClient)
{
cosmosClient.ClientOptions.MaxRetryAttemptsOnRateLimitedRequests = int.MaxValue;
}
}
}
Loading

0 comments on commit 4bcee66

Please sign in to comment.