Skip to content

Commit b7362c2

Browse files
DmitryLukyanovJamesKovacs
authored andcommitted
CSHARP-3241: Zstandard compression key in the code must be kept in sync with the spec.
1 parent 90bb172 commit b7362c2

File tree

10 files changed

+94
-12
lines changed

10 files changed

+94
-12
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/* Copyright 2020–present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System;
17+
18+
namespace MongoDB.Driver.Core.Compression
19+
{
20+
internal static class CompressorTypeMapper
21+
{
22+
public static string ToServerName(CompressorType compressorType)
23+
{
24+
switch (compressorType)
25+
{
26+
case CompressorType.Noop:
27+
return "noop";
28+
case CompressorType.Zlib:
29+
return "zlib";
30+
case CompressorType.Snappy:
31+
return "snappy";
32+
case CompressorType.ZStandard:
33+
return "zstd";
34+
default:
35+
throw new ArgumentOutOfRangeException(nameof(compressorType));
36+
}
37+
}
38+
39+
public static bool TryFromServerName(string serverName, out CompressorType compressorType)
40+
{
41+
compressorType = default;
42+
switch (serverName.ToLowerInvariant())
43+
{
44+
case "noop": compressorType = CompressorType.Noop; break;
45+
case "zlib": compressorType = CompressorType.Zlib; break;
46+
case "snappy": compressorType = CompressorType.Snappy; break;
47+
case "zstd": compressorType = CompressorType.ZStandard; break;
48+
default: return false;
49+
}
50+
return true;
51+
}
52+
}
53+
}

src/MongoDB.Driver.Core/Core/Configuration/ConnectionString.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1380,7 +1380,7 @@ public void SaveCompressors(string[] compressorNames)
13801380
foreach (var compressor in compressorNames)
13811381
{
13821382
// NOTE: the 'noop' is also expected by the server
1383-
if (!Enum.TryParse(compressor, true, out CompressorType compressorType) || !CompressorSource.IsCompressorSupported(compressorType))
1383+
if (!CompressorTypeMapper.TryFromServerName(compressor, out CompressorType compressorType) || !CompressorSource.IsCompressorSupported(compressorType))
13841384
{
13851385
// Keys that aren't supported by a driver MUST be ignored.
13861386
continue;

src/MongoDB.Driver.Core/Core/Connections/IsMasterHelper.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using MongoDB.Bson;
2222
using MongoDB.Bson.Serialization.Serializers;
2323
using MongoDB.Driver.Core.Authentication;
24+
using MongoDB.Driver.Core.Compression;
2425
using MongoDB.Driver.Core.Configuration;
2526
using MongoDB.Driver.Core.Misc;
2627
using MongoDB.Driver.Core.Servers;
@@ -37,7 +38,7 @@ internal static BsonDocument AddClientDocumentToCommand(BsonDocument command, Bs
3738

3839
internal static BsonDocument AddCompressorsToCommand(BsonDocument command, IEnumerable<CompressorConfiguration> compressors)
3940
{
40-
var compressorsArray = new BsonArray(compressors.Select(x => x.Type.ToString().ToLowerInvariant()));
41+
var compressorsArray = new BsonArray(compressors.Select(x => CompressorTypeMapper.ToServerName(x.Type)));
4142

4243
return command.Add("compression", compressorsArray);
4344
}

src/MongoDB.Driver.Core/Core/Connections/IsMasterResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public IReadOnlyList<CompressorType> Compressions
5757
.AsBsonArray
5858
.Select(x =>
5959
{
60-
return Enum.TryParse<CompressorType>(x.AsString, true, out var compressorType)
60+
return CompressorTypeMapper.TryFromServerName(x.AsString, out var compressorType)
6161
? compressorType
6262
// we can have such a case only due to the server bug
6363
: throw new NotSupportedException($"The unsupported compressor name: '{x}'.");

tests/MongoDB.Driver.Core.Tests/Core/Configuration/ConnectionStringTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,14 @@ public void When_authSource_is_specified(string connectionString, string authSou
446446
}
447447

448448
[Theory]
449+
[InlineData("mongodb://localhost?compressors=noop", CompressorType.Noop)]
450+
[InlineData("mongodb://localhost?compressors=nooP", CompressorType.Noop)]
449451
[InlineData("mongodb://localhost?compressors=zlib", CompressorType.Zlib)]
452+
[InlineData("mongodb://localhost?compressors=Zlib", CompressorType.Zlib)]
453+
[InlineData("mongodb://localhost?compressors=snappy", CompressorType.Snappy)]
454+
[InlineData("mongodb://localhost?compressors=Snappy", CompressorType.Snappy)]
455+
[InlineData("mongodb://localhost?compressors=zstd", CompressorType.ZStandard)]
456+
[InlineData("mongodb://localhost?compressors=Zstd", CompressorType.ZStandard)]
450457
public void When_compressor_is_specified(string connectionString, CompressorType compressor)
451458
{
452459
var subject = new ConnectionString(connectionString);

tests/MongoDB.Driver.Core.Tests/Core/Connections/ConnectionDescriptionTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ public class ConnectionDescriptionTests
3131
"{ ok: 1, version: \"2.6.3\" }"
3232
));
3333

34-
private static readonly IEnumerable<CompressorType> __compressors = new[] { CompressorType.Zlib };
34+
private static readonly IEnumerable<CompressorType> __compressors = new[] { CompressorType.Zlib, CompressorType.ZStandard };
3535

3636
private static readonly ConnectionId __connectionId = new ConnectionId(
3737
new ServerId(new ClusterId(), new DnsEndPoint("localhost", 27017)));
3838

3939
private static readonly IsMasterResult __isMasterResult = new IsMasterResult(BsonDocument.Parse(
40-
"{ ok: 1, maxWriteBatchSize: 10, maxBsonObjectSize: 20, maxMessageSizeBytes: 30, compression: ['zlib'] }"
40+
"{ ok: 1, maxWriteBatchSize: 10, maxBsonObjectSize: 20, maxMessageSizeBytes: 30, compression: ['zlib', 'zstd'] }"
4141
));
4242

4343
private static readonly IsMasterResult __isMasterResultWithoutCompression = new IsMasterResult(BsonDocument.Parse(
@@ -57,7 +57,7 @@ public void AvailableCompressors_should_return_expected_result()
5757
{
5858
var subject = new ConnectionDescription(__connectionId, __isMasterResult, __buildInfoResult);
5959

60-
subject.AvailableCompressors.Count.Should().Be(1);
60+
subject.AvailableCompressors.Count.Should().Be(2);
6161
subject.AvailableCompressors.Should().Equal(__compressors);
6262
}
6363

tests/MongoDB.Driver.Core.Tests/Core/Connections/ConnectionInitializerTests.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,12 @@ public void InitializeConnection_should_throw_an_ArgumentNullException_if_the_co
161161

162162
[Theory]
163163
[ParameterAttributeData]
164-
public void InitializeConnectionA_should_build_the_ConnectionDescription_correctly([Values(false, true)] bool async)
164+
public void InitializeConnectionA_should_build_the_ConnectionDescription_correctly(
165+
[Values("noop", "zlib", "snappy", "zstd")] string compressorType,
166+
[Values(false, true)] bool async)
165167
{
166168
var isMasterReply = MessageHelper.BuildReply<RawBsonDocument>(
167-
RawBsonDocumentHelper.FromJson("{ ok: 1, compression: ['zlib'] }"));
169+
RawBsonDocumentHelper.FromJson($"{{ ok: 1, compression: ['{compressorType}'] }}"));
168170
var buildInfoReply = MessageHelper.BuildReply<RawBsonDocument>(
169171
RawBsonDocumentHelper.FromJson("{ ok: 1, version: \"2.6.3\" }"));
170172
var gleReply = MessageHelper.BuildReply<RawBsonDocument>(
@@ -188,7 +190,20 @@ public void InitializeConnectionA_should_build_the_ConnectionDescription_correct
188190
result.ServerVersion.Should().Be(new SemanticVersion(2, 6, 3));
189191
result.ConnectionId.ServerValue.Should().Be(10);
190192
result.AvailableCompressors.Count.Should().Be(1);
191-
result.AvailableCompressors.Should().Contain(CompressorType.Zlib);
193+
result.AvailableCompressors.Should().Contain(ToCompressorTypeEnum(compressorType));
194+
195+
CompressorType ToCompressorTypeEnum(string ct)
196+
{
197+
switch (ct)
198+
{
199+
case "noop": return CompressorType.Noop;
200+
case "zlib": return CompressorType.Zlib;
201+
case "snappy": return CompressorType.Snappy;
202+
case "zstd": return CompressorType.ZStandard;
203+
default:
204+
throw new InvalidOperationException($"Unexpected compression {compressorType}.");
205+
}
206+
}
192207
}
193208

194209
private IAuthenticator CreateAuthenticator(string authenticatorType, UsernamePasswordCredential credentials)

tests/MongoDB.Driver.Core.Tests/Core/Connections/IsMasterHelperTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ public void AddCompressorsToCommand_with_compressors_should_return_expected_resu
7171
new CompressorType[] { },
7272
new [] { CompressorType.Zlib },
7373
new [] { CompressorType.Snappy},
74-
new [] { CompressorType.Zlib, CompressorType.Snappy })]
74+
new [] { CompressorType.Zlib, CompressorType.Snappy },
75+
new [] { CompressorType.ZStandard, CompressorType.Snappy })]
7576
CompressorType[] compressorsParameters)
7677
{
7778
var command = IsMasterHelper.CreateCommand();
@@ -81,7 +82,7 @@ public void AddCompressorsToCommand_with_compressors_should_return_expected_resu
8182
.ToArray();
8283
var result = IsMasterHelper.AddCompressorsToCommand(command, compressors);
8384

84-
var expectedCompressions = string.Join(",", compressorsParameters.Select(c => $"'{c.ToString().ToLowerInvariant()}'"));
85+
var expectedCompressions = string.Join(",", compressorsParameters.Select(c => $"'{CompressorTypeMapper.ToServerName(c)}'"));
8586
result.Should().Be(BsonDocument.Parse($"{{ isMaster : 1, compression: [{expectedCompressions}] }}"));
8687
}
8788
}

tests/MongoDB.Driver.Core.Tests/Core/Connections/IsMasterResultTests.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,13 @@ public class IsMasterResultTests
2929
{
3030
[Theory]
3131
[InlineData("{ compression : ['zlib'] }", new[] { CompressorType.Zlib })]
32+
[InlineData("{ compression : ['Zlib'] }", new[] { CompressorType.Zlib })]
3233
[InlineData("{ compression : ['zlib', 'snappy'] }", new[] { CompressorType.Zlib, CompressorType.Snappy })]
34+
[InlineData("{ compression : ['zlib', 'snAppy'] }", new[] { CompressorType.Zlib, CompressorType.Snappy })]
3335
[InlineData("{ compression : ['noop'] }", new[] { CompressorType.Noop })]
36+
[InlineData("{ compression : ['nOop'] }", new[] { CompressorType.Noop })]
37+
[InlineData("{ compression : ['zstd'] }", new[] { CompressorType.ZStandard})]
38+
[InlineData("{ compression : ['zsTd'] }", new[] { CompressorType.ZStandard })]
3439
[InlineData("{ compression : [] }", new CompressorType[0])]
3540
[InlineData("{ }", new CompressorType[0])]
3641
public void Compression_should_parse_document_correctly(string json, CompressorType[] expectedCompression)

tests/MongoDB.Driver.Core.Tests/Specifications/connection-string/ConnectionStringTestRunner.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ private void AssertOptions(ConnectionString connectionString, BsonDocument defin
9191
connectionString.AuthSource.Should().Be(expectedOption.Value.AsString);
9292
break;
9393
case "compressors":
94-
var compressors = new BsonArray(connectionString.Compressors.Select(c => c.Type.ToString().ToLowerInvariant()));
94+
var compressors = new BsonArray(connectionString.Compressors.Select(c => CompressorTypeMapper.ToServerName(c.Type)));
9595
var expectedCompressors = RemoveUnsupportedCompressors(expectedOption.Value.AsBsonArray);
9696
compressors.Should().Be(expectedCompressors);
9797
break;

0 commit comments

Comments
 (0)