SchemaUtils.java 4.2 KB
Newer Older
L
ligang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
Q
qiaozhanwei 已提交
17
package org.apache.dolphinscheduler.common.utils;
L
ligang 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Metadata related common classes
 *
 */
public class SchemaUtils {
37

L
ligang 已提交
38 39 40 41 42
	private static final Logger logger = LoggerFactory.getLogger(SchemaUtils.class);
	private static Pattern p = Pattern.compile("\\s*|\t|\r|\n");

	/**
	 * Gets upgradable schemas for all upgrade directories
D
dailidong 已提交
43
	 * @return all schema list
L
ligang 已提交
44 45 46 47 48 49 50 51
	 */
	@SuppressWarnings("unchecked")
	public static List<String> getAllSchemaList() {
		List<String> schemaDirList = new ArrayList<>();
		File[] schemaDirArr = FileUtils.getAllDir("sql/upgrade");
		if(schemaDirArr == null || schemaDirArr.length == 0) {
			return null;
		}
52

L
ligang 已提交
53 54 55
		for(File file : schemaDirArr) {
			schemaDirList.add(file.getName());
		}
56

L
ligang 已提交
57 58 59 60 61 62 63 64 65 66 67
		Collections.sort(schemaDirList , new Comparator() {
			@Override
			public int compare(Object o1 , Object o2){
				try {
					String dir1 = String.valueOf(o1);
					String dir2 = String.valueOf(o2);
					String version1 = dir1.split("_")[0];
					String version2 = dir2.split("_")[0];
					if(version1.equals(version2)) {
						return 0;
					}
68

L
ligang 已提交
69 70 71
					if(SchemaUtils.isAGreatVersion(version1, version2)) {
						return 1;
					}
72

L
ligang 已提交
73
					return -1;
74

L
ligang 已提交
75 76 77 78 79 80
				} catch (Exception e) {
					logger.error(e.getMessage(),e);
					throw new RuntimeException(e);
				}
			}
		});
81

L
ligang 已提交
82 83
		return schemaDirList;
	}
84

L
ligang 已提交
85 86
	/**
	 * Determine whether schemaVersion is higher than version
D
dailidong 已提交
87 88 89
	 * @param schemaVersion schema version
	 * @param version version
	 * @return  Determine whether schemaVersion is higher than version
L
ligang 已提交
90 91 92 93 94
	 */
	public static boolean isAGreatVersion(String schemaVersion, String version) {
		if(StringUtils.isEmpty(schemaVersion) || StringUtils.isEmpty(version)) {
			throw new RuntimeException("schemaVersion or version is empty");
		}
95

L
ligang 已提交
96 97
		String[] schemaVersionArr = schemaVersion.split("\\.");
		String[] versionArr = version.split("\\.");
98
		int arrLength = Math.min(schemaVersionArr.length, versionArr.length);
L
ligang 已提交
99
		for(int i = 0 ; i < arrLength ; i++) {
100
			if(Integer.parseInt(schemaVersionArr[i]) > Integer.parseInt(versionArr[i])) {
L
ligang 已提交
101
				return true;
102
			}else if(Integer.parseInt(schemaVersionArr[i]) < Integer.parseInt(versionArr[i])) {
L
ligang 已提交
103 104 105
				return false;
			}
		}
106

L
ligang 已提交
107 108 109
		// If the version and schema version is the same from 0 up to the arrlength-1 element,whoever has a larger arrLength has a larger version number
		return schemaVersionArr.length > versionArr.length;
	}
110

L
ligang 已提交
111 112
	/**
	 * Gets the current software version number of the system
D
dailidong 已提交
113
	 * @return current software version
L
ligang 已提交
114 115 116 117 118 119 120 121 122 123 124 125
	 */
	public static String getSoftVersion() {
		String soft_version;
		try {
			soft_version = FileUtils.readFile2Str(new FileInputStream(new File("sql/soft_version")));
			soft_version = replaceBlank(soft_version);
		} catch (FileNotFoundException e) {
			logger.error(e.getMessage(),e);
			throw new RuntimeException("Failed to get the product version description file. The file could not be found", e);
		}
		return soft_version;
	}
126

L
ligang 已提交
127 128
	/**
	 * Strips the string of space carriage returns and tabs
D
dailidong 已提交
129 130
	 * @param str string
	 * @return string removed blank
L
ligang 已提交
131 132 133 134 135 136 137 138 139 140 141
	 */
	public static String replaceBlank(String str) {
		String dest = "";
		if (str!=null) {

			Matcher m = p.matcher(str);
			dest = m.replaceAll("");
		}
		return dest;
	}
}