Skip to content

Commit

Permalink
Use static readonly arrays for separators
Browse files Browse the repository at this point in the history
  • Loading branch information
gpetrou committed Dec 23, 2024
1 parent a5469f2 commit 74eede4
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
10 changes: 7 additions & 3 deletions Microsoft.Azure.Cosmos/src/DocumentClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1410,6 +1410,10 @@ public void Dispose()

internal virtual Task<QueryPartitionProvider> QueryPartitionProvider => this.queryPartitionProvider.Value;

private static readonly char[] databaseLinkSeparators = new char[] { '/' };
private static readonly char[] sourceDocumentCollectionLinkSeparators = new char[] { '/' };
private static readonly char[] resourceIdSeparators = new char[] { '/', '\\', '?', '#' };

internal virtual async Task<ConsistencyLevel> GetDefaultConsistencyLevelAsync()
{
await this.EnsureValidClientAsync(NoOpTrace.Singleton);
Expand Down Expand Up @@ -2165,7 +2169,7 @@ private async Task<ResourceResponse<DocumentCollection>> RestoreDocumentCollecti
string databaseLink = PathsHelper.GetDatabasePath(sourceDocumentCollectionLink);
if (PathsHelper.TryParsePathSegments(databaseLink, out isFeed, out resourceTypeString, out resourceIdOrFullName, out isNameBased) && isNameBased && !isFeed)
{
string[] segments = resourceIdOrFullName.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
string[] segments = resourceIdOrFullName.Split(databaseLinkSeparators, StringSplitOptions.RemoveEmptyEntries);
dbsId = segments[segments.Length - 1];
}
else
Expand All @@ -2176,7 +2180,7 @@ private async Task<ResourceResponse<DocumentCollection>> RestoreDocumentCollecti
string sourceCollId;
if (PathsHelper.TryParsePathSegments(sourceDocumentCollectionLink, out isFeed, out resourceTypeString, out resourceIdOrFullName, out isNameBased) && isNameBased && !isFeed)
{
string[] segments = resourceIdOrFullName.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
string[] segments = resourceIdOrFullName.Split(sourceDocumentCollectionLinkSeparators, StringSplitOptions.RemoveEmptyEntries);
sourceCollId = segments[segments.Length - 1];
}
else
Expand Down Expand Up @@ -6811,7 +6815,7 @@ internal void ValidateResource(string resourceId)
{
if (!string.IsNullOrEmpty(resourceId))
{
int match = resourceId.IndexOfAny(new char[] { '/', '\\', '?', '#' });
int match = resourceId.IndexOfAny(resourceIdSeparators);
if (match != -1)
{
throw new ArgumentException(string.Format(
Expand Down
2 changes: 1 addition & 1 deletion Microsoft.Azure.Cosmos/src/Linq/ExpressionToSQL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1525,7 +1525,7 @@ internal static SqlScalarExpression VisitScalarExpression(Expression expression,
{
return ExpressionToSql.VisitScalarExpression(
expression,
new ReadOnlyCollection<ParameterExpression>(new ParameterExpression[] { }),
new ReadOnlyCollection<ParameterExpression>(Array.Empty<ParameterExpression>()),
context);
}

Expand Down
4 changes: 3 additions & 1 deletion Microsoft.Azure.Cosmos/src/Routing/LocationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace Microsoft.Azure.Cosmos.Routing
/// </summary>
internal static class LocationHelper
{
private static readonly char[] hostSeparators = new char[] { '.' };

/// <summary>
/// For example, for https://contoso.documents.azure.com:443/ and "West US", this will return https://contoso-westus.documents.azure.com:443/
/// NOTE: This ONLY called by client first boot when the input endpoint is not available.
Expand All @@ -21,7 +23,7 @@ internal static Uri GetLocationEndpoint(Uri serviceEndpoint, string location)
// Split the host into 2 parts seperated by '.'
// For example, "contoso.documents.azure.com" is separated into "contoso" and "documents.azure.com"
// If the host doesn't contains '.', this will return the host as is, as the only element
string[] hostParts = builder.Host.Split(new char[] { '.' }, 2);
string[] hostParts = builder.Host.Split(hostSeparators, 2);

if (hostParts.Length != 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ internal class TelemetryCollector : ITelemetryCollector
{
private readonly ClientTelemetry clientTelemetry = null;
private readonly ConnectionPolicy connectionPolicy = null;
private static readonly char[] pathSeparators = new char[] { '/' };

internal TelemetryCollector(
ClientTelemetry clientTelemetry,
Expand Down Expand Up @@ -69,7 +70,7 @@ public void CollectOperationAndNetworkInfo(Func<TelemetryInformation> functionFo

private static void GetDatabaseAndCollectionName(string path, out string databaseName, out string collectionName)
{
string[] segments = path.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
string[] segments = path.Split(pathSeparators, StringSplitOptions.RemoveEmptyEntries);

PathsHelper.ParseDatabaseNameAndCollectionNameFromUrlSegments(segments, out databaseName, out collectionName);
}
Expand Down

0 comments on commit 74eede4

Please sign in to comment.