17d004396SChris Fraire /* 27d004396SChris Fraire * CDDL HEADER START 37d004396SChris Fraire * 47d004396SChris Fraire * The contents of this file are subject to the terms of the 57d004396SChris Fraire * Common Development and Distribution License (the "License"). 67d004396SChris Fraire * You may not use this file except in compliance with the License. 77d004396SChris Fraire * 87d004396SChris Fraire * See LICENSE.txt included in this distribution for the specific 97d004396SChris Fraire * language governing permissions and limitations under the License. 107d004396SChris Fraire * 117d004396SChris Fraire * When distributing Covered Code, include this CDDL HEADER in each 127d004396SChris Fraire * file and include the License file at LICENSE.txt. 137d004396SChris Fraire * If applicable, add the following below this CDDL HEADER, with the 147d004396SChris Fraire * fields enclosed by brackets "[]" replaced with your own identifying 157d004396SChris Fraire * information: Portions Copyright [yyyy] [name of copyright owner] 167d004396SChris Fraire * 177d004396SChris Fraire * CDDL HEADER END 187d004396SChris Fraire */ 197d004396SChris Fraire 207d004396SChris Fraire /* 21*5d9f3aa0SAdam Hornáček * Copyright (c) 2018, 2019, Chris Fraire <cfraire@me.com>. 227d004396SChris Fraire */ 237d004396SChris Fraire package org.opengrok.indexer.index; 247d004396SChris Fraire 257d004396SChris Fraire import java.io.IOException; 267d004396SChris Fraire 277d004396SChris Fraire /** 287d004396SChris Fraire * Represents a converter to upgrade earlier binary representations of 297d004396SChris Fraire * index-analysis-settings to the latest version. 307d004396SChris Fraire */ 317d004396SChris Fraire public class IndexAnalysisSettingsUpgrader { 327d004396SChris Fraire 337d004396SChris Fraire /** 347d004396SChris Fraire * De-serialize the specified {@code bytes}, and upgrade if necessary from 357d004396SChris Fraire * an older version to the current object version which is 367d004396SChris Fraire * {@link IndexAnalysisSettings3}. 377d004396SChris Fraire * @param bytes a defined instance 387d004396SChris Fraire * @param objectVersion a value greater than or equal to 1 397d004396SChris Fraire * @return a defined instance 407d004396SChris Fraire * @throws ClassNotFoundException if class of a serialized object cannot be 417d004396SChris Fraire * found 427d004396SChris Fraire * @throws IOException if any of the usual Input/Output related exceptions 437d004396SChris Fraire */ upgrade(byte[] bytes, int objectVersion)447d004396SChris Fraire public IndexAnalysisSettings3 upgrade(byte[] bytes, int objectVersion) 457d004396SChris Fraire throws ClassNotFoundException, IOException { 467d004396SChris Fraire switch (objectVersion) { 477d004396SChris Fraire case 1: 487d004396SChris Fraire throw new IOException("Too old version " + objectVersion); 497d004396SChris Fraire case 2: 507d004396SChris Fraire IndexAnalysisSettings old2 = IndexAnalysisSettings.deserialize(bytes); 517d004396SChris Fraire return convertFromV2(old2); 527d004396SChris Fraire case 3: 537d004396SChris Fraire return IndexAnalysisSettings3.deserialize(bytes); 547d004396SChris Fraire default: 557d004396SChris Fraire throw new IllegalArgumentException("Unknown version " + objectVersion); 567d004396SChris Fraire } 577d004396SChris Fraire } 587d004396SChris Fraire convertFromV2(IndexAnalysisSettings old2)597d004396SChris Fraire private IndexAnalysisSettings3 convertFromV2(IndexAnalysisSettings old2) { 607d004396SChris Fraire IndexAnalysisSettings3 res = new IndexAnalysisSettings3(); 617d004396SChris Fraire res.setAnalyzerGuruVersion(old2.getAnalyzerGuruVersion()); 627d004396SChris Fraire res.setAnalyzersVersions(old2.getAnalyzersVersions()); 637d004396SChris Fraire res.setProjectName(old2.getProjectName()); 647d004396SChris Fraire res.setTabSize(old2.getTabSize()); 657d004396SChris Fraire // Version 2 has no indexedSymlinks, so nothing more to do. 667d004396SChris Fraire return res; 677d004396SChris Fraire } 687d004396SChris Fraire } 69