Skip to content

Commit 8b8ef64

Browse files
committed
Took should return int.MaxValue when value is greater than int
Add clear obsolete messages on Took int properties Add JsonIgnore attributes to Took fields so that Json serializer ignores them when enumerating properties to serializes Fix build warnings for usage of Took and check TookAsLong value is populated
1 parent 7402ab3 commit 8b8ef64

File tree

12 files changed

+115
-94
lines changed

12 files changed

+115
-94
lines changed

src/Benchmarking/PartitionExtension.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ public static IEnumerable<IEnumerable<T>> Partition<T>(this IEnumerable<T> sourc
3535
}
3636
}
3737

38-
public static decimal GetMedian(this IEnumerable<int> source)
38+
public static decimal GetMedian(this IEnumerable<long> source)
3939
{
4040
// Create a copy of the input, and sort the copy
41-
int[] temp = source.ToArray();
41+
long[] temp = source.ToArray();
4242
Array.Sort(temp);
4343

4444
int count = temp.Length;
@@ -50,13 +50,13 @@ public static decimal GetMedian(this IEnumerable<int> source)
5050
if (count % 2 == 0)
5151
{
5252
// count is even, average two middle elements
53-
int a = temp[count / 2 - 1];
54-
int b = temp[count / 2];
53+
long a = temp[count / 2 - 1];
54+
long b = temp[count / 2];
5555
return (a + b) / 2m;
5656
}
57-
57+
5858
// count is odd, return the middle element
5959
return temp[count / 2];
6060
}
6161
}
62-
}
62+
}

src/Benchmarking/Results.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class Results
1515

1616
public int IndexedDocuments { get; set; }
1717

18-
public IEnumerable<int> EsTimings { get; set; }
18+
public IEnumerable<long> EsTimings { get; set; }
1919

2020
public Metrics Before { get; set; }
2121

@@ -30,4 +30,4 @@ public void Write(TextWriter output)
3030
output.WriteLine(" memory after:{0} thread count after:{1}", After.MemorySize.Bytes(), After.ThreadCount);
3131
}
3232
}
33-
}
33+
}

src/Benchmarking/Tester.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public Results RunTests(int numMessages = NumberOfMessages, int bufferSize = Buf
7575

7676
protected ConnectionSettings CreateSettings()
7777
{
78-
var host = Process.GetProcessesByName("fiddler").Any()
78+
var host = Process.GetProcessesByName("fiddler").Any()
7979
? "ipv4.fiddler"
8080
: "localhost";
8181

@@ -99,7 +99,7 @@ protected void Connect()
9999
protected class IndexResults
100100
{
101101
public double Elapsed { get; set; }
102-
public IEnumerable<int> EsTimings { get; set; }
102+
public IEnumerable<long> EsTimings { get; set; }
103103
}
104104

105105
protected IndexResults GenerateAndIndex(int numMessages, int bufferSize)
@@ -132,7 +132,7 @@ protected IndexResults GenerateAndIndex(int numMessages, int bufferSize)
132132
{
133133
Interlocked.Add(ref NumSent, bufferSize);
134134
Console.Write("\r{2}: {0:0,0} msgs es-time: {1} ",
135-
NumSent, tt.Result.Took, this.Type);
135+
NumSent, tt.Result.TookAsLong, this.Type);
136136
return tt.Result;
137137
})
138138
).ToArray();
@@ -151,7 +151,7 @@ protected IndexResults GenerateAndIndex(int numMessages, int bufferSize)
151151
return new IndexResults
152152
{
153153
Elapsed = sw.ElapsedMilliseconds,
154-
EsTimings = array.Select(a => a.Result.Took).ToList()
154+
EsTimings = array.Select(a => a.Result.TookAsLong).ToList()
155155
};
156156
}
157157

@@ -173,4 +173,4 @@ public void SearchUsingSingleClient(int numberOfSearches)
173173
Task.WaitAll(tasks.ToArray());
174174
}
175175
}
176-
}
176+
}

src/Nest/Document/Multiple/Bulk/BulkResponse.cs

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,17 @@ namespace Nest
88
{
99
public interface IBulkResponse : IResponse
1010
{
11-
[Obsolete(@"Took field is an Int but the value in the response can exced the max value for Int.
12-
If you use this field instead of TookAsLong the value can wrap around if it is too big.")]
11+
/// <summary>
12+
/// Time in milliseconds for Elasticsearch to execute the search
13+
/// </summary>
14+
[Obsolete(@"returned value may be larger than int. In this case, value will be int.MaxValue and TookAsLong field can be checked. Took is long in 5.0.0")]
1315
int Took { get; }
16+
17+
/// <summary>
18+
/// Time in milliseconds for Elasticsearch to execute the search
19+
/// </summary>
1420
long TookAsLong { get; }
21+
1522
bool Errors { get; }
1623
IEnumerable<BulkResponseItemBase> Items { get; }
1724
IEnumerable<BulkResponseItemBase> ItemsWithErrors { get; }
@@ -24,27 +31,23 @@ public class BulkResponse : ResponseBase, IBulkResponse
2431
protected override void DebugIsValid(StringBuilder sb)
2532
{
2633
if (this.Items == null) return;
27-
sb.AppendLine($"# Invalid Bulk items:");
34+
sb.AppendLine("# Invalid Bulk items:");
2835
foreach(var i in Items.Select((item, i) => new { item, i}).Where(i=>!i.item.IsValid))
2936
sb.AppendLine($" operation[{i.i}]: {i.item}");
3037
}
3138

39+
/// <summary>
40+
/// Time in milliseconds for Elasticsearch to execute the search
41+
/// </summary>
3242
[JsonProperty("took")]
3343
public long TookAsLong { get; internal set;}
3444

35-
[Obsolete(@"Took field is an Int but the value in the response can exced the max value for Int.
36-
If you use this field instead of TookAsLong the value can wrap around if it is too big.")]
37-
public int Took
38-
{
39-
get
40-
{
41-
return unchecked((int)TookAsLong);
42-
}
43-
internal set
44-
{
45-
TookAsLong = value;
46-
}
47-
}
45+
/// <summary>
46+
/// Time in milliseconds for Elasticsearch to execute the search
47+
/// </summary>
48+
[Obsolete(@"returned value may be larger than int. In this case, value will be int.MaxValue and TookAsLong field can be checked. Took is long in 5.0.0")]
49+
[JsonIgnore]
50+
public int Took => TookAsLong > int.MaxValue ? int.MaxValue : (int)TookAsLong;
4851

4952
[JsonProperty("errors")]
5053
public bool Errors { get; internal set; }
@@ -53,12 +56,7 @@ internal set
5356
public IEnumerable<BulkResponseItemBase> Items { get; internal set; }
5457

5558
[JsonIgnore]
56-
public IEnumerable<BulkResponseItemBase> ItemsWithErrors
57-
{
58-
get
59-
{
60-
return !this.Items.HasAny() ? Enumerable.Empty<BulkResponseItemBase>() : this.Items.Where(i => !i.IsValid);
61-
}
62-
}
59+
public IEnumerable<BulkResponseItemBase> ItemsWithErrors =>
60+
!this.Items.HasAny() ? Enumerable.Empty<BulkResponseItemBase>() : this.Items.Where(i => !i.IsValid);
6361
}
6462
}

src/Nest/Document/Single/TermVectors/TermVectorsResponse.cs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,18 @@ public interface ITermVectorsResponse : IResponse
1111
string Id { get; }
1212
long Version { get; }
1313
bool Found { get; }
14-
[Obsolete(@"Took field is an Int but the value in the response can exced the max value for Int.
15-
If you use this field instead of TookAsLong the value can wrap around if it is too big.")]
14+
15+
/// <summary>
16+
/// Time in milliseconds for Elasticsearch to execute the search
17+
/// </summary>
18+
[Obsolete(@"returned value may be larger than int. In this case, value will be int.MaxValue and TookAsLong field can be checked. Took is long in 5.0.0")]
1619
int Took { get; }
20+
21+
/// <summary>
22+
/// Time in milliseconds for Elasticsearch to execute the search
23+
/// </summary>
1724
long TookAsLong { get; }
25+
1826
IDictionary<string, TermVector> TermVectors { get; }
1927
}
2028

@@ -36,22 +44,18 @@ public class TermVectorsResponse : ResponseBase, ITermVectorsResponse
3644
[JsonProperty("found")]
3745
public bool Found { get; internal set; }
3846

39-
[JsonProperty(PropertyName = "took")]
47+
/// <summary>
48+
/// Time in milliseconds for Elasticsearch to execute the search
49+
/// </summary>
50+
[JsonProperty("took")]
4051
public long TookAsLong { get; internal set; }
4152

42-
[Obsolete(@"Took field is an Int but the value in the response can exced the max value for Int.
43-
If you use this field instead of TookAsLong the value can wrap around if it is too big.")]
44-
public int Took
45-
{
46-
get
47-
{
48-
return unchecked((int)TookAsLong);
49-
}
50-
internal set
51-
{
52-
TookAsLong = value;
53-
}
54-
}
53+
/// <summary>
54+
/// Time in milliseconds for Elasticsearch to execute the search
55+
/// </summary>
56+
[Obsolete(@"returned value may be larger than int. In this case, value will be int.MaxValue and TookAsLong field can be checked. Took is long in 5.0.0")]
57+
[JsonIgnore]
58+
public int Took => TookAsLong > int.MaxValue ? int.MaxValue : (int)TookAsLong;
5559

5660
[JsonProperty("term_vectors")]
5761
public IDictionary<string, TermVector> TermVectors { get; internal set; } = new Dictionary<string, TermVector>();

src/Nest/Search/Percolator/Percolate/PercolateResponse.cs

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,18 @@
66
namespace Nest
77
{
88
public interface IPercolateCountResponse : IResponse
9-
{
10-
[Obsolete(@"Took field is an Int but the value in the response can exced the max value for Int.
11-
If you use this field instead of TookAsLong the value can wrap around if it is too big.")]
9+
{
10+
/// <summary>
11+
/// Time in milliseconds for Elasticsearch to execute the search
12+
/// </summary>
13+
[Obsolete(@"returned value may be larger than int. In this case, value will be int.MaxValue and TookAsLong field can be checked. Took is long in 5.0.0")]
1214
int Took { get; }
15+
16+
/// <summary>
17+
/// Time in milliseconds for Elasticsearch to execute the search
18+
/// </summary>
1319
long TookAsLong { get; }
20+
1421
long Total { get; }
1522
}
1623

@@ -21,34 +28,30 @@ public interface IPercolateResponse : IPercolateCountResponse
2128

2229
[JsonObject]
2330
public class PercolateCountResponse : ResponseBase, IPercolateCountResponse
24-
{
31+
{
32+
/// <summary>
33+
/// Time in milliseconds for Elasticsearch to execute the search
34+
/// </summary>
2535
[JsonProperty("took")]
2636
public long TookAsLong { get; internal set; }
2737

28-
[Obsolete(@"Took field is an Int but the value in the response can exced the max value for Int.
29-
If you use this field instead of TookAsLong the value can wrap around if it is too big.")]
30-
public int Took
31-
{
32-
get
33-
{
34-
return unchecked((int)TookAsLong);
35-
}
36-
internal set
37-
{
38-
TookAsLong = value;
39-
}
40-
}
38+
/// <summary>
39+
/// Time in milliseconds for Elasticsearch to execute the search
40+
/// </summary>
41+
[Obsolete(@"returned value may be larger than int. In this case, value will be int.MaxValue and TookAsLong field can be checked. Took is long in 5.0.0")]
42+
[JsonIgnore]
43+
public int Took => TookAsLong > int.MaxValue? int.MaxValue : (int)TookAsLong;
4144

42-
[JsonProperty(PropertyName = "total")]
45+
[JsonProperty("total")]
4346
public long Total { get; internal set; }
4447

45-
[JsonProperty(PropertyName = "_shards")]
48+
[JsonProperty("_shards")]
4649
public ShardsMetaData Shards { get; internal set; }
4750

4851
/// <summary>
4952
/// The individual error for separate requests on the _mpercolate API
5053
/// </summary>
51-
[JsonProperty(PropertyName = "error")]
54+
[JsonProperty("error")]
5255
internal ServerError Error { get; set; }
5356

5457
public override ServerError ServerError => this.Error ?? base.ServerError;
@@ -57,23 +60,22 @@ internal set
5760
[JsonObject]
5861
public class PercolateResponse : PercolateCountResponse, IPercolateResponse
5962
{
60-
61-
[JsonProperty(PropertyName = "matches")]
63+
[JsonProperty("matches")]
6264
public IEnumerable<PercolatorMatch> Matches { get; internal set; }
6365
}
6466

6567
public class PercolatorMatch
6668
{
67-
[JsonProperty(PropertyName = "highlight")]
69+
[JsonProperty("highlight")]
6870
public Dictionary<string, IList<string>> Highlight { get; set; }
6971

70-
[JsonProperty(PropertyName = "_id")]
72+
[JsonProperty("_id")]
7173
public string Id { get; set; }
7274

73-
[JsonProperty(PropertyName = "_index")]
75+
[JsonProperty("_index")]
7476
public string Index { get; set; }
7577

76-
[JsonProperty(PropertyName = "_score")]
78+
[JsonProperty("_score")]
7779
public double Score { get; set; }
7880
}
7981

src/Nest/Search/Search/SearchResponse.cs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,17 @@ public interface ISearchResponse<T> : IResponse where T : class
1515
AggregationsHelper Aggs { get; }
1616
IDictionary<string, Suggest[]> Suggest { get; }
1717

18-
[Obsolete(@"Took field is an Int but the value in the response can exced the max value for Int.
19-
If you use this field instead of TookAsLong the value can wrap around if it is too big.")]
18+
/// <summary>
19+
/// Time in milliseconds for Elasticsearch to execute the search
20+
/// </summary>
21+
[Obsolete(@"returned value may be larger than int. In this case, value will be int.MaxValue and TookAsLong field can be checked. Took is long in 5.0.0")]
2022
int Took { get; }
23+
24+
/// <summary>
25+
/// Time in milliseconds for Elasticsearch to execute the search
26+
/// </summary>
2127
long TookAsLong { get; }
28+
2229
bool TimedOut { get; }
2330
bool TerminatedEarly { get; }
2431
string ScrollId { get; }
@@ -68,22 +75,18 @@ public class SearchResponse<T> : ResponseBase, ISearchResponse<T> where T : clas
6875
[JsonProperty(PropertyName = "suggest")]
6976
public IDictionary<string, Suggest[]> Suggest { get; internal set; }
7077

78+
/// <summary>
79+
/// Time in milliseconds for Elasticsearch to execute the search
80+
/// </summary>
7181
[JsonProperty("took")]
7282
public long TookAsLong { get; internal set; }
7383

74-
[Obsolete(@"Took field is an Int but the value in the response can exced the max value for Int.
75-
If you use this field instead of TookAsLong the value can wrap around if it is too big.")]
76-
public int Took
77-
{
78-
get
79-
{
80-
return unchecked((int)TookAsLong);
81-
}
82-
internal set
83-
{
84-
TookAsLong = value;
85-
}
86-
}
84+
/// <summary>
85+
/// Time in milliseconds for Elasticsearch to execute the search
86+
/// </summary>
87+
[Obsolete(@"returned value may be larger than int. In this case, value will be int.MaxValue and TookAsLong field can be checked. Took is long in 5.0.0")]
88+
[JsonIgnore]
89+
public int Took => TookAsLong > int.MaxValue ? int.MaxValue : (int)TookAsLong;
8790

8891
[JsonProperty("timed_out")]
8992
public bool TimedOut { get; internal set; }

src/Tests/Document/Multiple/Bulk/BulkApiTests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,10 @@ protected override LazyResponses ClientUsage() => Calls(
9292

9393
protected override void ExpectResponse(IBulkResponse response)
9494
{
95+
#pragma warning disable 618
9596
response.Took.Should().BeGreaterThan(0);
97+
#pragma warning restore 618
98+
response.TookAsLong.Should().BeGreaterThan(0);
9699
response.Errors.Should().BeFalse();
97100
response.ItemsWithErrors.Should().NotBeNull().And.BeEmpty();
98101
response.Items.Should().NotBeEmpty();

src/Tests/Document/Multiple/Bulk/BulkInvalidApiTests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ protected override LazyResponses ClientUsage() => Calls(
3737

3838
protected override void ExpectResponse(IBulkResponse response)
3939
{
40+
#pragma warning disable 618
4041
response.Took.Should().BeGreaterThan(0);
42+
#pragma warning restore 618
43+
response.TookAsLong.Should().BeGreaterThan(0);
4144
response.Errors.Should().BeTrue();
4245

4346
//a delete not found is not an error (also in Elasticsearch)

0 commit comments

Comments
 (0)