20
20
package org .elasticsearch .common .unit ;
21
21
22
22
import org .elasticsearch .ElasticsearchParseException ;
23
- import org .elasticsearch .Version ;
24
23
import org .elasticsearch .common .Strings ;
25
24
import org .elasticsearch .common .io .stream .StreamInput ;
26
25
import org .elasticsearch .common .io .stream .StreamOutput ;
27
26
import org .elasticsearch .common .io .stream .Writeable ;
28
- import org .elasticsearch .common .logging .DeprecationLogger ;
29
- import org .elasticsearch .common .logging .Loggers ;
30
27
31
28
import java .io .IOException ;
32
29
import java .util .Locale ;
33
30
import java .util .Objects ;
34
31
35
32
public class ByteSizeValue implements Writeable , Comparable <ByteSizeValue > {
36
- private static final DeprecationLogger DEPRECATION_LOGGER = new DeprecationLogger (Loggers .getLogger (ByteSizeValue .class ));
37
33
38
34
private final long size ;
39
35
private final ByteSizeUnit unit ;
40
36
41
37
public ByteSizeValue (StreamInput in ) throws IOException {
42
- if (in .getVersion ().before (Version .V_7_0_0_alpha1 )) {
43
- size = in .readVLong ();
44
- unit = ByteSizeUnit .BYTES ;
45
- } else {
46
- size = in .readZLong ();
47
- unit = ByteSizeUnit .readFrom (in );
48
- }
38
+ size = in .readVLong ();
39
+ unit = ByteSizeUnit .BYTES ;
49
40
}
50
41
51
42
@ Override
52
43
public void writeTo (StreamOutput out ) throws IOException {
53
- if (out .getVersion ().before (Version .V_7_0_0_alpha1 )) {
54
- out .writeVLong (getBytes ());
55
- } else {
56
- out .writeZLong (size );
57
- unit .writeTo (out );
58
- }
44
+ out .writeVLong (getBytes ());
59
45
}
60
46
61
47
public ByteSizeValue (long bytes ) {
62
48
this (bytes , ByteSizeUnit .BYTES );
63
49
}
64
50
65
51
public ByteSizeValue (long size , ByteSizeUnit unit ) {
66
- if (size < -1 || (size == -1 && unit != ByteSizeUnit .BYTES )) {
67
- throw new IllegalArgumentException ("Values less than -1 bytes are not supported: " + size + unit .getSuffix ());
68
- }
69
- if (size > Long .MAX_VALUE / unit .toBytes (1 )) {
70
- throw new IllegalArgumentException (
71
- "Values greater than " + Long .MAX_VALUE + " bytes are not supported: " + size + unit .getSuffix ());
72
- }
73
52
this .size = size ;
74
53
this .unit = unit ;
75
54
}
76
55
77
- // For testing
78
- long getSize () {
79
- return size ;
80
- }
81
-
82
- // For testing
83
- ByteSizeUnit getUnit () {
84
- return unit ;
85
- }
86
-
87
- @ Deprecated
88
56
public int bytesAsInt () {
89
57
long bytes = getBytes ();
90
58
if (bytes > Integer .MAX_VALUE ) {
@@ -137,41 +105,26 @@ public double getPbFrac() {
137
105
return ((double ) getBytes ()) / ByteSizeUnit .C5 ;
138
106
}
139
107
140
- /**
141
- * @return a string representation of this value which is guaranteed to be
142
- * able to be parsed using
143
- * {@link #parseBytesSizeValue(String, ByteSizeValue, String)}.
144
- * Unlike {@link #toString()} this method will not output fractional
145
- * or rounded values so this method should be preferred when
146
- * serialising the value to JSON.
147
- */
148
- public String getStringRep () {
149
- if (size <= 0 ) {
150
- return String .valueOf (size );
151
- }
152
- return size + unit .getSuffix ();
153
- }
154
-
155
108
@ Override
156
109
public String toString () {
157
110
long bytes = getBytes ();
158
111
double value = bytes ;
159
- String suffix = ByteSizeUnit . BYTES . getSuffix () ;
112
+ String suffix = "b" ;
160
113
if (bytes >= ByteSizeUnit .C5 ) {
161
114
value = getPbFrac ();
162
- suffix = ByteSizeUnit . PB . getSuffix () ;
115
+ suffix = "pb" ;
163
116
} else if (bytes >= ByteSizeUnit .C4 ) {
164
117
value = getTbFrac ();
165
- suffix = ByteSizeUnit . TB . getSuffix () ;
118
+ suffix = "tb" ;
166
119
} else if (bytes >= ByteSizeUnit .C3 ) {
167
120
value = getGbFrac ();
168
- suffix = ByteSizeUnit . GB . getSuffix () ;
121
+ suffix = "gb" ;
169
122
} else if (bytes >= ByteSizeUnit .C2 ) {
170
123
value = getMbFrac ();
171
- suffix = ByteSizeUnit . MB . getSuffix () ;
124
+ suffix = "mb" ;
172
125
} else if (bytes >= ByteSizeUnit .C1 ) {
173
126
value = getKbFrac ();
174
- suffix = ByteSizeUnit . KB . getSuffix () ;
127
+ suffix = "kb" ;
175
128
}
176
129
return Strings .format1Decimals (value , suffix );
177
130
}
@@ -186,64 +139,47 @@ public static ByteSizeValue parseBytesSizeValue(String sValue, ByteSizeValue def
186
139
if (sValue == null ) {
187
140
return defaultValue ;
188
141
}
189
- String lowerSValue = sValue .toLowerCase (Locale .ROOT ).trim ();
190
- if (lowerSValue .endsWith ("k" )) {
191
- return parse (sValue , lowerSValue , "k" , ByteSizeUnit .KB , settingName );
192
- } else if (lowerSValue .endsWith ("kb" )) {
193
- return parse (sValue , lowerSValue , "kb" , ByteSizeUnit .KB , settingName );
194
- } else if (lowerSValue .endsWith ("m" )) {
195
- return parse (sValue , lowerSValue , "m" , ByteSizeUnit .MB , settingName );
196
- } else if (lowerSValue .endsWith ("mb" )) {
197
- return parse (sValue , lowerSValue , "mb" , ByteSizeUnit .MB , settingName );
198
- } else if (lowerSValue .endsWith ("g" )) {
199
- return parse (sValue , lowerSValue , "g" , ByteSizeUnit .GB , settingName );
200
- } else if (lowerSValue .endsWith ("gb" )) {
201
- return parse (sValue , lowerSValue , "gb" , ByteSizeUnit .GB , settingName );
202
- } else if (lowerSValue .endsWith ("t" )) {
203
- return parse (sValue , lowerSValue , "t" , ByteSizeUnit .TB , settingName );
204
- } else if (lowerSValue .endsWith ("tb" )) {
205
- return parse (sValue , lowerSValue , "tb" , ByteSizeUnit .TB , settingName );
206
- } else if (lowerSValue .endsWith ("p" )) {
207
- return parse (sValue , lowerSValue , "p" , ByteSizeUnit .PB , settingName );
208
- } else if (lowerSValue .endsWith ("pb" )) {
209
- return parse (sValue , lowerSValue , "pb" , ByteSizeUnit .PB , settingName );
210
- } else if (lowerSValue .endsWith ("b" )) {
211
- return new ByteSizeValue (Long .parseLong (lowerSValue .substring (0 , lowerSValue .length () - 1 ).trim ()), ByteSizeUnit .BYTES );
212
- } else if (lowerSValue .equals ("-1" )) {
213
- // Allow this special value to be unit-less:
214
- return new ByteSizeValue (-1 , ByteSizeUnit .BYTES );
215
- } else if (lowerSValue .equals ("0" )) {
216
- // Allow this special value to be unit-less:
217
- return new ByteSizeValue (0 , ByteSizeUnit .BYTES );
218
- } else {
219
- // Missing units:
220
- throw new ElasticsearchParseException (
221
- "failed to parse setting [{}] with value [{}] as a size in bytes: unit is missing or unrecognized" , settingName ,
222
- sValue );
223
- }
224
- }
225
-
226
- private static ByteSizeValue parse (final String initialInput , final String normalized , final String suffix , ByteSizeUnit unit ,
227
- final String settingName ) {
228
- final String s = normalized .substring (0 , normalized .length () - suffix .length ()).trim ();
142
+ long bytes ;
229
143
try {
230
- try {
231
- return new ByteSizeValue (Long .parseLong (s ), unit );
232
- } catch (final NumberFormatException e ) {
233
- try {
234
- final double doubleValue = Double .parseDouble (s );
235
- DEPRECATION_LOGGER .deprecated (
236
- "Fractional bytes values are deprecated. Use non-fractional bytes values instead: [{}] found for setting [{}]" ,
237
- initialInput , settingName );
238
- return new ByteSizeValue ((long ) (doubleValue * unit .toBytes (1 )));
239
- } catch (final NumberFormatException ignored ) {
240
- throw new ElasticsearchParseException ("failed to parse [{}]" , e , initialInput );
241
- }
144
+ String lowerSValue = sValue .toLowerCase (Locale .ROOT ).trim ();
145
+ if (lowerSValue .endsWith ("k" )) {
146
+ bytes = (long ) (Double .parseDouble (lowerSValue .substring (0 , lowerSValue .length () - 1 )) * ByteSizeUnit .C1 );
147
+ } else if (lowerSValue .endsWith ("kb" )) {
148
+ bytes = (long ) (Double .parseDouble (lowerSValue .substring (0 , lowerSValue .length () - 2 )) * ByteSizeUnit .C1 );
149
+ } else if (lowerSValue .endsWith ("m" )) {
150
+ bytes = (long ) (Double .parseDouble (lowerSValue .substring (0 , lowerSValue .length () - 1 )) * ByteSizeUnit .C2 );
151
+ } else if (lowerSValue .endsWith ("mb" )) {
152
+ bytes = (long ) (Double .parseDouble (lowerSValue .substring (0 , lowerSValue .length () - 2 )) * ByteSizeUnit .C2 );
153
+ } else if (lowerSValue .endsWith ("g" )) {
154
+ bytes = (long ) (Double .parseDouble (lowerSValue .substring (0 , lowerSValue .length () - 1 )) * ByteSizeUnit .C3 );
155
+ } else if (lowerSValue .endsWith ("gb" )) {
156
+ bytes = (long ) (Double .parseDouble (lowerSValue .substring (0 , lowerSValue .length () - 2 )) * ByteSizeUnit .C3 );
157
+ } else if (lowerSValue .endsWith ("t" )) {
158
+ bytes = (long ) (Double .parseDouble (lowerSValue .substring (0 , lowerSValue .length () - 1 )) * ByteSizeUnit .C4 );
159
+ } else if (lowerSValue .endsWith ("tb" )) {
160
+ bytes = (long ) (Double .parseDouble (lowerSValue .substring (0 , lowerSValue .length () - 2 )) * ByteSizeUnit .C4 );
161
+ } else if (lowerSValue .endsWith ("p" )) {
162
+ bytes = (long ) (Double .parseDouble (lowerSValue .substring (0 , lowerSValue .length () - 1 )) * ByteSizeUnit .C5 );
163
+ } else if (lowerSValue .endsWith ("pb" )) {
164
+ bytes = (long ) (Double .parseDouble (lowerSValue .substring (0 , lowerSValue .length () - 2 )) * ByteSizeUnit .C5 );
165
+ } else if (lowerSValue .endsWith ("b" )) {
166
+ bytes = Long .parseLong (lowerSValue .substring (0 , lowerSValue .length () - 1 ).trim ());
167
+ } else if (lowerSValue .equals ("-1" )) {
168
+ // Allow this special value to be unit-less:
169
+ bytes = -1 ;
170
+ } else if (lowerSValue .equals ("0" )) {
171
+ // Allow this special value to be unit-less:
172
+ bytes = 0 ;
173
+ } else {
174
+ // Missing units:
175
+ throw new ElasticsearchParseException (
176
+ "failed to parse setting [{}] with value [{}] as a size in bytes: unit is missing or unrecognized" ,
177
+ settingName , sValue );
242
178
}
243
- } catch (IllegalArgumentException e ) {
244
- throw new ElasticsearchParseException ("failed to parse setting [{}] with value [{}] as a size in bytes" , e , settingName ,
245
- initialInput );
179
+ } catch (NumberFormatException e ) {
180
+ throw new ElasticsearchParseException ("failed to parse [{}]" , e , sValue );
246
181
}
182
+ return new ByteSizeValue (bytes , ByteSizeUnit .BYTES );
247
183
}
248
184
249
185
@ Override
@@ -260,13 +196,13 @@ public boolean equals(Object o) {
260
196
261
197
@ Override
262
198
public int hashCode () {
263
- return Long .hashCode (size * unit .toBytes (1 ));
199
+ return Double .hashCode ((( double ) size ) * unit .toBytes (1 ));
264
200
}
265
201
266
202
@ Override
267
203
public int compareTo (ByteSizeValue other ) {
268
- long thisValue = size * unit .toBytes (1 );
269
- long otherValue = other .size * other .unit .toBytes (1 );
270
- return Long .compare (thisValue , otherValue );
204
+ double thisValue = (( double ) size ) * unit .toBytes (1 );
205
+ double otherValue = (( double ) other .size ) * other .unit .toBytes (1 );
206
+ return Double .compare (thisValue , otherValue );
271
207
}
272
208
}
0 commit comments