22
22
import org .elasticsearch .cluster .metadata .IndexMetaData ;
23
23
import org .elasticsearch .common .Strings ;
24
24
import org .elasticsearch .common .SuppressForbidden ;
25
+ import org .elasticsearch .common .collect .ImmutableOpenIntMap ;
25
26
import org .elasticsearch .common .io .stream .StreamInput ;
26
27
import org .elasticsearch .common .io .stream .StreamOutput ;
27
28
import org .elasticsearch .common .settings .Settings ;
@@ -46,52 +47,63 @@ public class Version implements Comparable<Version>, ToXContentFragment {
46
47
*/
47
48
public static final int V_EMPTY_ID = 0 ;
48
49
public static final Version V_EMPTY = new Version (V_EMPTY_ID , org .apache .lucene .util .Version .LATEST );
49
- public static final int V_7_0_0_ID = 7000099 ;
50
- public static final Version V_7_0_0 = new Version (V_7_0_0_ID , org .apache .lucene .util .Version .LUCENE_8_0_0 );
51
- public static final int V_7_0_1_ID = 7000199 ;
52
- public static final Version V_7_0_1 = new Version (V_7_0_1_ID , org .apache .lucene .util .Version .LUCENE_8_0_0 );
53
- public static final int V_7_1_0_ID = 7010099 ;
54
- public static final Version V_7_1_0 = new Version (V_7_1_0_ID , org .apache .lucene .util .Version .LUCENE_8_0_0 );
55
- public static final int V_7_1_1_ID = 7010199 ;
56
- public static final Version V_7_1_1 = new Version (V_7_1_1_ID , org .apache .lucene .util .Version .LUCENE_8_0_0 );
57
- public static final int V_7_1_2_ID = 7010299 ;
58
- public static final Version V_7_1_2 = new Version (V_7_1_2_ID , org .apache .lucene .util .Version .LUCENE_8_0_0 );
59
- public static final int V_7_2_0_ID = 7020099 ;
60
- public static final Version V_7_2_0 = new Version (V_7_2_0_ID , org .apache .lucene .util .Version .LUCENE_8_0_0 );
61
- public static final int V_7_3_0_ID = 7030099 ;
62
- public static final Version V_7_3_0 = new Version (V_7_3_0_ID , org .apache .lucene .util .Version .LUCENE_8_1_0 );
63
- public static final int V_8_0_0_ID = 8000099 ;
64
- public static final Version V_8_0_0 = new Version (V_8_0_0_ID , org .apache .lucene .util .Version .LUCENE_8_1_0 );
50
+ public static final Version V_7_0_0 = new Version (7000099 , org .apache .lucene .util .Version .LUCENE_8_0_0 );
51
+ public static final Version V_7_0_1 = new Version (7000199 , org .apache .lucene .util .Version .LUCENE_8_0_0 );
52
+ public static final Version V_7_1_0 = new Version (7010099 , org .apache .lucene .util .Version .LUCENE_8_0_0 );
53
+ public static final Version V_7_1_1 = new Version (7010199 , org .apache .lucene .util .Version .LUCENE_8_0_0 );
54
+ public static final Version V_7_1_2 = new Version (7010299 , org .apache .lucene .util .Version .LUCENE_8_0_0 );
55
+ public static final Version V_7_2_0 = new Version (7020099 , org .apache .lucene .util .Version .LUCENE_8_0_0 );
56
+ public static final Version V_7_3_0 = new Version (7030099 , org .apache .lucene .util .Version .LUCENE_8_1_0 );
57
+ public static final Version V_8_0_0 = new Version (8000099 , org .apache .lucene .util .Version .LUCENE_8_1_0 );
65
58
public static final Version CURRENT = V_8_0_0 ;
66
59
60
+ private static final ImmutableOpenIntMap <Version > idToVersion ;
67
61
68
62
static {
63
+ final ImmutableOpenIntMap .Builder <Version > builder = ImmutableOpenIntMap .builder ();
64
+
65
+ for (final Field declaredField : Version .class .getFields ()) {
66
+ if (declaredField .getType ().equals (Version .class )) {
67
+ final String fieldName = declaredField .getName ();
68
+ if (fieldName .equals ("CURRENT" ) || fieldName .equals ("V_EMPTY" )) {
69
+ continue ;
70
+ }
71
+ assert fieldName .matches ("V_\\ d+_\\ d+_\\ d+" )
72
+ : "expected Version field [" + fieldName + "] to match V_\\ d+_\\ d+_\\ d+" ;
73
+ try {
74
+ final Version version = (Version ) declaredField .get (null );
75
+ if (Assertions .ENABLED ) {
76
+ final String [] fields = fieldName .split ("_" );
77
+ final int major = Integer .valueOf (fields [1 ]) * 1000000 ;
78
+ final int minor = Integer .valueOf (fields [2 ]) * 10000 ;
79
+ final int revision = Integer .valueOf (fields [3 ]) * 100 ;
80
+ final int expectedId = major + minor + revision + 99 ;
81
+ assert version .id == expectedId :
82
+ "expected version [" + fieldName + "] to have id [" + expectedId + "] but was [" + version .id + "]" ;
83
+ }
84
+ final Version maybePrevious = builder .put (version .id , version );
85
+ assert maybePrevious == null :
86
+ "expected [" + version .id + "] to be uniquely mapped but saw [" + maybePrevious + "] and [" + version + "]" ;
87
+ } catch (final IllegalAccessException e ) {
88
+ assert false : "Version field [" + fieldName + "] should be public" ;
89
+ }
90
+ }
91
+ }
69
92
assert CURRENT .luceneVersion .equals (org .apache .lucene .util .Version .LATEST ) : "Version must be upgraded to ["
70
93
+ org .apache .lucene .util .Version .LATEST + "] is still set to [" + CURRENT .luceneVersion + "]" ;
94
+
95
+ idToVersion = builder .build ();
71
96
}
72
97
73
98
public static Version readVersion (StreamInput in ) throws IOException {
74
99
return fromId (in .readVInt ());
75
100
}
76
101
77
102
public static Version fromId (int id ) {
103
+ if (idToVersion .containsKey (id )) {
104
+ return idToVersion .get (id );
105
+ }
78
106
switch (id ) {
79
- case V_8_0_0_ID :
80
- return V_8_0_0 ;
81
- case V_7_3_0_ID :
82
- return V_7_3_0 ;
83
- case V_7_2_0_ID :
84
- return V_7_2_0 ;
85
- case V_7_1_2_ID :
86
- return V_7_1_2 ;
87
- case V_7_1_1_ID :
88
- return V_7_1_1 ;
89
- case V_7_1_0_ID :
90
- return V_7_1_0 ;
91
- case V_7_0_1_ID :
92
- return V_7_0_1 ;
93
- case V_7_0_0_ID :
94
- return V_7_0_0 ;
95
107
case V_EMPTY_ID :
96
108
return V_EMPTY ;
97
109
default :
0 commit comments