Skip to content

Commit 42ab184

Browse files
committed
Added: GZip compression feature (#621)
[changelog]
1 parent 951ea87 commit 42ab184

File tree

12 files changed

+159
-19
lines changed

12 files changed

+159
-19
lines changed

src/Algolia.Search.Test/BaseTest.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
/*
22
* Copyright (c) 2018 Algolia
33
* http://www.algolia.com/
4-
*
4+
*
55
* Permission is hereby granted, free of charge, to any person obtaining a copy
66
* of this software and associated documentation files (the "Software"), to deal
77
* in the Software without restriction, including without limitation the rights
88
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
99
* copies of the Software, and to permit persons to whom the Software is
1010
* furnished to do so, subject to the following conditions:
11-
*
11+
*
1212
* The above copyright notice and this permission notice shall be included in
1313
* all copies or substantial portions of the Software.
14-
*
14+
*
1515
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1616
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1717
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -45,7 +45,11 @@ public void Setup()
4545
{
4646
TestHelper.CheckEnvironmentVariable();
4747
SearchClient = new SearchClient(TestHelper.ApplicationId1, TestHelper.AdminKey1);
48-
SearchClient2 = new SearchClient(TestHelper.ApplicationId2, TestHelper.AdminKey2);
48+
SearchConfig configClient2 = new SearchConfig(TestHelper.ApplicationId2, TestHelper.AdminKey2)
49+
{
50+
Compression = CompressionType.NONE
51+
};
52+
SearchClient2 = new SearchClient(configClient2);
4953
McmClient = new SearchClient(TestHelper.McmApplicationId, TestHelper.McmAdminKey);
5054
AnalyticsClient = new AnalyticsClient(TestHelper.ApplicationId1, TestHelper.AdminKey1);
5155
}
@@ -83,4 +87,4 @@ protected void PreviousTestCleanUp()
8387

8488
SearchClient.MultipleBatch(operations);
8589
}
86-
}
90+
}

src/Algolia.Search/Clients/AlgoliaConfig.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* THE SOFTWARE.
2222
*/
2323

24+
using Algolia.Search.Models.Enums;
2425
using Algolia.Search.Serializer;
2526
using Algolia.Search.Transport;
2627
using System.Collections.Generic;
@@ -94,6 +95,11 @@ public AlgoliaConfig(string applicationId, string apiKey)
9495
/// </summary>
9596
public int? WriteTimeout { get; set; }
9697

98+
/// <summary>
99+
/// Compression for outgoing http requests <see cref="CompressionType"/>
100+
/// </summary>
101+
public virtual CompressionType Compression { get; protected set; }
102+
97103
/// <summary>
98104
/// Configurations hosts
99105
/// </summary>

src/Algolia.Search/Clients/AnalyticsConfig.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ public AnalyticsConfig(string applicationId, string apiKey) : base(applicationId
4848
Accept = CallType.Read | CallType.Write
4949
}
5050
};
51+
52+
Compression = CompressionType.NONE;
5153
}
5254
}
53-
}
55+
}

src/Algolia.Search/Clients/InsightsConfig.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public InsightsConfig(string applicationId, string apiKey, string region = "us")
4949
Accept = CallType.Read | CallType.Write
5050
}
5151
};
52+
53+
Compression = CompressionType.NONE;
5254
}
5355
}
54-
}
56+
}

src/Algolia.Search/Clients/SearchConfig.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ public SearchConfig(string applicationId, string apiKey) : base(applicationId, a
8888
hosts.AddRange(commonHosts);
8989

9090
DefaultHosts = hosts;
91+
92+
Compression = CompressionType.NONE;
9193
}
94+
95+
/// <inheritdoc />
96+
public new CompressionType Compression { get; set; }
9297
}
9398
}

src/Algolia.Search/Http/AlgoliaHttpRequester.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ public async Task<AlgoliaHttpResponse> SendRequestAsync(Request request, int tot
8080
Content = request.Body != null ? new StreamContent(request.Body) : null
8181
};
8282

83+
if (request.Body != null)
84+
{
85+
httpRequestMessage.Content.Headers.Fill(request);
86+
}
87+
8388
httpRequestMessage.Headers.Fill(request.Headers);
8489
httpRequestMessage.SetTimeout(TimeSpan.FromSeconds(totalTimeout));
8590

src/Algolia.Search/Http/HttpRequestHeadersExtensions.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* THE SOFTWARE.
2222
*/
2323

24+
using Algolia.Search.Models.Common;
2425
using System.Collections.Generic;
2526
using System.Net.Http.Headers;
2627

@@ -43,5 +44,25 @@ internal static HttpRequestHeaders Fill(this HttpRequestHeaders headers, Diction
4344

4445
return headers;
4546
}
47+
48+
/// <summary>
49+
/// Extension method to easily fill HttpContentHeaders with the Request object
50+
/// </summary>
51+
/// <param name="headers"></param>
52+
/// <param name="request"></param>
53+
internal static HttpContentHeaders Fill(this HttpContentHeaders headers, Request request)
54+
{
55+
if (request.Body != null)
56+
{
57+
headers.Add(Defaults.ContentType, Defaults.ApplicationJson);
58+
59+
if (request.CanCompress)
60+
{
61+
headers.ContentEncoding.Add(Defaults.GzipEncoding);
62+
}
63+
}
64+
65+
return headers;
66+
}
4667
}
47-
}
68+
}

src/Algolia.Search/Models/Common/Request.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* THE SOFTWARE.
2222
*/
2323

24+
using Algolia.Search.Models.Enums;
2425
using System;
2526
using System.Collections.Generic;
2627
using System.IO;
@@ -52,5 +53,29 @@ public class Request
5253
/// Body of the request
5354
/// </summary>
5455
public Stream Body { get; set; }
56+
57+
/// <summary>
58+
/// Compression type of the request <see cref="CompressionType"/>
59+
/// </summary>
60+
public CompressionType Compression { get; set; }
61+
62+
/// <summary>
63+
/// Tells if the request can be compressed or not
64+
/// </summary>
65+
public bool CanCompress
66+
{
67+
get
68+
{
69+
if (Method == null)
70+
{
71+
return false;
72+
}
73+
74+
bool isMethodValid = Method.Equals(HttpMethod.Post) || Method.Equals(HttpMethod.Put);
75+
bool isCompressionEnabled = Compression.Equals(CompressionType.GZIP);
76+
77+
return isMethodValid && isCompressionEnabled;
78+
}
79+
}
5580
}
56-
}
81+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (c) 2018 Algolia
3+
* http://www.algolia.com/
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
namespace Algolia.Search.Models.Enums
25+
{
26+
/// <summary>
27+
/// Compression type for outgoing HTTP requests
28+
/// </summary>
29+
public enum CompressionType
30+
{
31+
/// <summary>
32+
/// No compression
33+
/// </summary>
34+
NONE,
35+
36+
/// <summary>
37+
/// GZip Compression. Only supported by Search API.
38+
/// </summary>
39+
GZIP
40+
}
41+
}

src/Algolia.Search/Serializer/SerializerHelper.cs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
using Newtonsoft.Json;
2525
using System.IO;
26+
using System.IO.Compression;
2627
using System.Text;
2728
using System.Threading.Tasks;
2829

@@ -34,16 +35,36 @@ namespace Algolia.Search.Serializer
3435
/// </summary>
3536
internal static class SerializerHelper
3637
{
37-
private static readonly UTF8Encoding _encoding = new UTF8Encoding(false);
38+
private static readonly UTF8Encoding DefaultEncoding = new UTF8Encoding(false);
39+
private static readonly int DefaultBufferSize = 1024;
40+
// Buffer sized as recommended by Bradley Grainger, http://faithlife.codes/blog/2012/06/always-wrap-gzipstream-with-bufferedstream/
41+
private static readonly int GZipBufferSize = 8192;
3842

39-
public static void Serialize<T>(T data, Stream stream, JsonSerializerSettings settings)
43+
public static void Serialize<T>(T data, Stream stream, JsonSerializerSettings settings, bool gzipCompress)
4044
{
41-
using (var sw = new StreamWriter(stream, _encoding, 1024, true))
42-
using (var jtw = new JsonTextWriter(sw) { Formatting = Formatting.None })
45+
if (gzipCompress)
46+
{
47+
using (var gzipStream = new GZipStream(stream, CompressionMode.Compress, true))
48+
using (var sw = new StreamWriter(gzipStream, DefaultEncoding, GZipBufferSize))
49+
using (var jtw = new JsonTextWriter(sw) { Formatting = Formatting.None })
50+
{
51+
JsonSerialize(jtw);
52+
}
53+
}
54+
else
55+
{
56+
using (var sw = new StreamWriter(stream, DefaultEncoding, DefaultBufferSize, true))
57+
using (var jtw = new JsonTextWriter(sw) { Formatting = Formatting.None })
58+
{
59+
JsonSerialize(jtw);
60+
}
61+
}
62+
63+
void JsonSerialize(JsonTextWriter writer)
4364
{
4465
JsonSerializer serializer = JsonSerializer.Create(settings);
45-
serializer.Serialize(jtw, data);
46-
jtw.Flush();
66+
serializer.Serialize(writer, data);
67+
writer.Flush();
4768
}
4869
}
4970

0 commit comments

Comments
 (0)