xref: /OpenGrok/opengrok-indexer/src/main/java/org/opengrok/indexer/index/IndexAnalysisSettingsUpgrader.java (revision 5d9f3aa0ca3da3a714233f987fa732f62c0965f6)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * See LICENSE.txt included in this distribution for the specific
9  * language governing permissions and limitations under the License.
10  *
11  * When distributing Covered Code, include this CDDL HEADER in each
12  * file and include the License file at LICENSE.txt.
13  * If applicable, add the following below this CDDL HEADER, with the
14  * fields enclosed by brackets "[]" replaced with your own identifying
15  * information: Portions Copyright [yyyy] [name of copyright owner]
16  *
17  * CDDL HEADER END
18  */
19 
20 /*
21  * Copyright (c) 2018, 2019, Chris Fraire <cfraire@me.com>.
22  */
23 package org.opengrok.indexer.index;
24 
25 import java.io.IOException;
26 
27 /**
28  * Represents a converter to upgrade earlier binary representations of
29  * index-analysis-settings to the latest version.
30  */
31 public class IndexAnalysisSettingsUpgrader {
32 
33     /**
34      * De-serialize the specified {@code bytes}, and upgrade if necessary from
35      * an older version to the current object version which is
36      * {@link IndexAnalysisSettings3}.
37      * @param bytes a defined instance
38      * @param objectVersion a value greater than or equal to 1
39      * @return a defined instance
40      * @throws ClassNotFoundException if class of a serialized object cannot be
41      * found
42      * @throws IOException if any of the usual Input/Output related exceptions
43      */
upgrade(byte[] bytes, int objectVersion)44     public IndexAnalysisSettings3 upgrade(byte[] bytes, int objectVersion)
45             throws ClassNotFoundException, IOException {
46         switch (objectVersion) {
47             case 1:
48                 throw new IOException("Too old version " + objectVersion);
49             case 2:
50                 IndexAnalysisSettings old2 = IndexAnalysisSettings.deserialize(bytes);
51                 return convertFromV2(old2);
52             case 3:
53                 return IndexAnalysisSettings3.deserialize(bytes);
54             default:
55                 throw new IllegalArgumentException("Unknown version " + objectVersion);
56         }
57     }
58 
convertFromV2(IndexAnalysisSettings old2)59     private IndexAnalysisSettings3 convertFromV2(IndexAnalysisSettings old2) {
60         IndexAnalysisSettings3 res = new IndexAnalysisSettings3();
61         res.setAnalyzerGuruVersion(old2.getAnalyzerGuruVersion());
62         res.setAnalyzersVersions(old2.getAnalyzersVersions());
63         res.setProjectName(old2.getProjectName());
64         res.setTabSize(old2.getTabSize());
65         // Version 2 has no indexedSymlinks, so nothing more to do.
66         return res;
67     }
68 }
69