37
37
* Helper class to start/stop ElasticSearch node and get ElasticSearch clients.
38
38
*/
39
39
public class Server {
40
+ /**
41
+ * Database version created by new imports with the current code.
42
+ *
43
+ * Format must be: major.minor.patch-dev
44
+ *
45
+ * Increase to next to be released version when the database layout
46
+ * changes in an incompatible way. If it is already at the next released
47
+ * version, increase the dev version.
48
+ */
49
+ public static final String DATABASE_VERSION = "0.3.6-1" ;
50
+
40
51
private static final Logger LOGGER = org .slf4j .LoggerFactory .getLogger (Server .class );
41
52
42
53
public static final String PROPERTY_DOCUMENT_ID = "DATABASE_PROPERTIES" ;
@@ -173,9 +184,7 @@ public DatabaseProperties recreateIndex(String[] languages, Date importDate, boo
173
184
174
185
createAndPutIndexMapping (languages , supportStructuredQueries );
175
186
176
- DatabaseProperties dbProperties = new DatabaseProperties ()
177
- .setLanguages (languages )
178
- .setImportDate (importDate );
187
+ DatabaseProperties dbProperties = new DatabaseProperties (languages , importDate , false );
179
188
saveToDatabase (dbProperties );
180
189
181
190
return dbProperties ;
@@ -195,8 +204,7 @@ public void updateIndexSettings(String synonymFile) throws IOException {
195
204
// Load the settings from the database to make sure it is at the right
196
205
// version. If the version is wrong, we should not be messing with the
197
206
// index.
198
- DatabaseProperties dbProperties = new DatabaseProperties ();
199
- loadFromDatabase (dbProperties );
207
+ DatabaseProperties dbProperties = loadFromDatabase ();
200
208
201
209
loadIndexSettings ().setSynonymFile (synonymFile ).updateIndex (esClient , PhotonIndex .NAME );
202
210
@@ -228,7 +236,7 @@ private void deleteIndex() {
228
236
*/
229
237
public void saveToDatabase (DatabaseProperties dbProperties ) throws IOException {
230
238
final XContentBuilder builder = XContentFactory .jsonBuilder ().startObject ().startObject (BASE_FIELD )
231
- .field (FIELD_VERSION , DatabaseProperties . DATABASE_VERSION )
239
+ .field (FIELD_VERSION , DATABASE_VERSION )
232
240
.field (FIELD_LANGUAGES , String .join ("," , dbProperties .getLanguages ()))
233
241
.field (FIELD_IMPORT_DATE , dbProperties .getImportDate () instanceof Date ? dbProperties .getImportDate ().toInstant () : null )
234
242
.endObject ().endObject ();
@@ -246,12 +254,12 @@ public void saveToDatabase(DatabaseProperties dbProperties) throws IOException
246
254
* Currently does nothing when the property entry is missing. Later versions with a higher
247
255
* database version will then fail.
248
256
*/
249
- public void loadFromDatabase (DatabaseProperties dbProperties ) {
257
+ public DatabaseProperties loadFromDatabase () {
250
258
GetResponse response = esClient .prepareGet (PhotonIndex .NAME , PhotonIndex .TYPE , PROPERTY_DOCUMENT_ID ).execute ().actionGet ();
251
259
252
260
// We are currently at the database version where versioning was introduced.
253
261
if (!response .isExists ()) {
254
- return ;
262
+ throw new UsageException ( "Cannot find database properties. Your database version is too old. Please reimport." ) ;
255
263
}
256
264
257
265
Map <String , String > properties = (Map <String , String >) response .getSource ().get (BASE_FIELD );
@@ -261,16 +269,18 @@ public void loadFromDatabase(DatabaseProperties dbProperties) {
261
269
}
262
270
263
271
String version = properties .getOrDefault (FIELD_VERSION , "" );
264
- if (!DatabaseProperties .DATABASE_VERSION .equals (version )) {
265
- LOGGER .error ("Database has incompatible version '{}'. Expected: {}" , version , DatabaseProperties .DATABASE_VERSION );
272
+ if (!DATABASE_VERSION .equals (version )) {
273
+ LOGGER .error ("Database has incompatible version '{}'. Expected: {}" ,
274
+ version , DATABASE_VERSION );
266
275
throw new UsageException ("Incompatible database." );
267
276
}
268
277
269
278
String langString = properties .get (FIELD_LANGUAGES );
270
- dbProperties .setLanguages (langString == null ? null : langString .split ("," ));
271
-
272
279
String importDateString = properties .get (FIELD_IMPORT_DATE );
273
- dbProperties .setImportDate (importDateString == null ? null : Date .from (Instant .parse (importDateString )));
280
+
281
+ return new DatabaseProperties (langString == null ? null : langString .split ("," ),
282
+ importDateString == null ? null : Date .from (Instant .parse (importDateString )),
283
+ false );
274
284
}
275
285
276
286
public Importer createImporter (String [] languages , String [] extraTags ) {
0 commit comments