Skip to content

Commit

Permalink
Removed unused resource type.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jake Willey committed May 14, 2020
1 parent 91a6686 commit 4664cff
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,7 @@ private static IEnumerable<JObject> GetItemsFromResponse(ResponseMessage respons

return CosmosFeedResponseSerializer.FromFeedResponseStream<JObject>(
CosmosContainerExtensions.DefaultJsonSerializer,
response.Content,
ResourceType.Document);
response.Content);
}

private async Task<long> GetRemainingWorkAsync(DocumentServiceLease existingLease, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,7 @@ private Task DispatchChangesAsync(ResponseMessage response, CancellationToken ca
{
asFeedResponse = CosmosFeedResponseSerializer.FromFeedResponseStream<T>(
this.serializerCore,
response.Content,
Documents.ResourceType.Document);
response.Content);
}
catch (Exception serializationException)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ private async Task<IReadOnlyList<DocumentServiceLeaseCore>> ListDocumentsAsync(s
responseMessage.EnsureSuccessStatusCode();
leases.AddRange(CosmosFeedResponseSerializer.FromFeedResponseStream<DocumentServiceLeaseCore>(
CosmosContainerExtensions.DefaultJsonSerializer,
responseMessage.Content,
Documents.ResourceType.Document));
responseMessage.Content));
}
}

Expand Down
12 changes: 3 additions & 9 deletions Microsoft.Azure.Cosmos/src/Query/v3Query/ReadFeedResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ public override IEnumerator<T> GetEnumerator()

internal static ReadFeedResponse<TInput> CreateResponse<TInput>(
ResponseMessage responseMessage,
CosmosSerializerCore serializerCore,
Documents.ResourceType resourceType)
CosmosSerializerCore serializerCore)
{
using (responseMessage)
{
Expand All @@ -55,14 +54,9 @@ internal static ReadFeedResponse<TInput> CreateResponse<TInput>(
responseMessage.EnsureSuccessStatusCode();
}

IReadOnlyList<TInput> resources = null;
if (responseMessage.Content != null)
{
resources = CosmosFeedResponseSerializer.FromFeedResponseStream<TInput>(
IReadOnlyList<TInput> resources = CosmosFeedResponseSerializer.FromFeedResponseStream<TInput>(
serializerCore,
responseMessage.Content,
resourceType);
}
responseMessage.Content);

ReadFeedResponse<TInput> readFeedResponse = new ReadFeedResponse<TInput>(
httpStatusCode: responseMessage.StatusCode,
Expand Down
21 changes: 4 additions & 17 deletions Microsoft.Azure.Cosmos/src/Resource/CosmosResponseFactoryCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,7 @@ public override FeedResponse<T> CreateChangeFeedUserTypeResponse<T>(
ResponseMessage responseMessage)
{
return this.CreateChangeFeedResponseHelper<T>(
responseMessage,
Documents.ResourceType.Document);
}

public override FeedResponse<T> CreateChangeFeedUserTypeResponse<T>(
ResponseMessage responseMessage,
Documents.ResourceType resourceType)
{
return this.CreateChangeFeedResponseHelper<T>(
responseMessage,
resourceType);
responseMessage);
}

public override FeedResponse<T> CreateQueryFeedUserTypeResponse<T>(
Expand Down Expand Up @@ -75,18 +65,15 @@ private FeedResponse<T> CreateQueryFeedResponseHelper<T>(

return ReadFeedResponse<T>.CreateResponse<T>(
cosmosResponseMessage,
this.serializerCore,
resourceType);
this.serializerCore);
}

private FeedResponse<T> CreateChangeFeedResponseHelper<T>(
ResponseMessage cosmosResponseMessage,
Documents.ResourceType resourceType)
ResponseMessage cosmosResponseMessage)
{
return ReadFeedResponse<T>.CreateResponse<T>(
cosmosResponseMessage,
this.serializerCore,
resourceType);
this.serializerCore);
}

public override ItemResponse<T> CreateItemResponse<T>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ internal abstract class CosmosResponseFactoryInternal : CosmosResponseFactory
public abstract FeedResponse<T> CreateChangeFeedUserTypeResponse<T>(
ResponseMessage responseMessage);

public abstract FeedResponse<T> CreateChangeFeedUserTypeResponse<T>(
ResponseMessage responseMessage,
Documents.ResourceType resourceType);

public abstract FeedResponse<T> CreateQueryFeedUserTypeResponse<T>(
ResponseMessage responseMessage);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,23 @@ internal static class CosmosFeedResponseSerializer
/// </summary>
/// <param name="serializerCore">The cosmos serializer</param>
/// <param name="streamWithServiceEnvelope">A stream with the service envelope like: { "ContainerRid":"Test", "Documents":[{ "id":"MyItem"}], "count":1}</param>
/// <param name="resourceType">The resource type</param>
/// <returns>A read only list of the serialized items</returns>
internal static IReadOnlyList<T> FromFeedResponseStream<T>(
CosmosSerializerCore serializerCore,
Stream streamWithServiceEnvelope,
Documents.ResourceType resourceType)
Stream streamWithServiceEnvelope)
{
if (streamWithServiceEnvelope == null)
{
return new List<T>();
}

using (MemoryStream stream = GetStreamWithoutServiceEnvelope(
streamWithServiceEnvelope,
resourceType))
using (streamWithServiceEnvelope)
{
return serializerCore.FromStream<List<T>>(stream);
using (MemoryStream stream = GetStreamWithoutServiceEnvelope(
streamWithServiceEnvelope))
{
return serializerCore.FromStream<List<T>>(stream);
}
}
}

Expand All @@ -47,58 +47,48 @@ internal static IReadOnlyList<T> FromFeedResponseStream<T>(
/// so it only returns the array of items.
/// </summary>
/// <param name="streamWithServiceEnvelope">A stream with the service envelope like: { "ContainerRid":"Test", "Documents":[{ "id":"MyItem"}], "count":1}</param>
/// <param name="resourceType">The resource type</param>
/// <returns>A stream containing only the array of items</returns>
internal static MemoryStream GetStreamWithoutServiceEnvelope(
Stream streamWithServiceEnvelope,
Documents.ResourceType resourceType)
Stream streamWithServiceEnvelope)
{
ReadOnlyMemory<byte> content;
MemoryStream? memoryStreamWithEnvelope = streamWithServiceEnvelope as MemoryStream;
if (memoryStreamWithEnvelope == null)
using (streamWithServiceEnvelope)
{
memoryStreamWithEnvelope = new MemoryStream();
streamWithServiceEnvelope.CopyTo(memoryStreamWithEnvelope);
memoryStreamWithEnvelope.Position = 0;
}
ReadOnlyMemory<byte> content;
MemoryStream? memoryStreamWithEnvelope = streamWithServiceEnvelope as MemoryStream;
if (memoryStreamWithEnvelope == null)
{
memoryStreamWithEnvelope = new MemoryStream();
streamWithServiceEnvelope.CopyTo(memoryStreamWithEnvelope);
memoryStreamWithEnvelope.Position = 0;
}

if (memoryStreamWithEnvelope.TryGetBuffer(out ArraySegment<byte> buffer))
{
content = buffer;
}
else
{
content = memoryStreamWithEnvelope.ToArray();
}
if (memoryStreamWithEnvelope.TryGetBuffer(out ArraySegment<byte> buffer))
{
content = buffer;
}
else
{
content = memoryStreamWithEnvelope.ToArray();
}

int start = GetArrayStartPosition(content, resourceType);
int end = GetArrayEndPosition(content, resourceType);
int start = GetArrayStartPosition(content);
int end = GetArrayEndPosition(content);

ReadOnlyMemory<byte> spanwithOnlyArray = content.Slice(start, end - start + 1);
if (!MemoryMarshal.TryGetArray(spanwithOnlyArray, out ArraySegment<byte> resultAsArray))
{
resultAsArray = new ArraySegment<byte>(spanwithOnlyArray.ToArray());
}
ReadOnlyMemory<byte> spanwithOnlyArray = content.Slice(start, end - start + 1);
if (!MemoryMarshal.TryGetArray(spanwithOnlyArray, out ArraySegment<byte> resultAsArray))
{
resultAsArray = new ArraySegment<byte>(spanwithOnlyArray.ToArray());
}

MemoryStream arrayOnlyStream = new MemoryStream(resultAsArray.Array, resultAsArray.Offset, resultAsArray.Count);
return arrayOnlyStream;
MemoryStream arrayOnlyStream = new MemoryStream(resultAsArray.Array, resultAsArray.Offset, resultAsArray.Count, writable: false, publiclyVisible: true);
return arrayOnlyStream;
}
}

private static int GetArrayStartPosition(
ReadOnlyMemory<byte> memoryByte,
Documents.ResourceType resourceType)
ReadOnlyMemory<byte> memoryByte)
{
ReadOnlySpan<byte> span = memoryByte.Span;

// This is an optimization for documents to check the
// default envelope position for the array start
int defaultDocumentArrayStartPosition = 35;
if (resourceType == Documents.ResourceType.Document &&
span[defaultDocumentArrayStartPosition] == ArrayStart)
{
return defaultDocumentArrayStartPosition;
}

for (int i = 0; i < span.Length; i++)
{
if (span[i] == ArrayStart)
Expand All @@ -111,20 +101,9 @@ private static int GetArrayStartPosition(
}

private static int GetArrayEndPosition(
ReadOnlyMemory<byte> memoryByte,
Documents.ResourceType resourceType)
ReadOnlyMemory<byte> memoryByte)
{
ReadOnlySpan<byte> span = memoryByte.Span;

// This is an optimization for documents to check the
// default envelope position for the array start
int defaultDocumentArrayEndPosition = span.Length - 13;
if (resourceType == Documents.ResourceType.Document &&
span[defaultDocumentArrayEndPosition] == ArrayStart)
{
return defaultDocumentArrayEndPosition;
}

for (int i = span.Length - 1; i < span.Length; i--)
{
if (span[i] == ArrayEnd)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@ public void ByteFindArrayParsingJson()
using (MemoryStream ms = new MemoryStream(this.payloadBytes))
{
using (MemoryStream memoryStream = CosmosFeedResponseSerializer.GetStreamWithoutServiceEnvelope(
ms,
Documents.ResourceType.Document))
ms))
{
if (ms.Length == memoryStream.Length)
{
Expand Down
Loading

0 comments on commit 4664cff

Please sign in to comment.