|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.kyuubi.spark.connector.common; |
| 19 | + |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.Locale; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.concurrent.TimeUnit; |
| 24 | +import java.util.regex.Matcher; |
| 25 | +import java.util.regex.Pattern; |
| 26 | +import org.apache.spark.network.util.ByteUnit; |
| 27 | + |
| 28 | +/** Copied from Apache Spark org.apache.spark.network.util.JavaUtils */ |
| 29 | +public class JavaUtils { |
| 30 | + |
| 31 | + private static final Map<String, TimeUnit> timeSuffixes; |
| 32 | + |
| 33 | + static { |
| 34 | + timeSuffixes = new HashMap<>(); |
| 35 | + timeSuffixes.put("us", TimeUnit.MICROSECONDS); |
| 36 | + timeSuffixes.put("ms", TimeUnit.MILLISECONDS); |
| 37 | + timeSuffixes.put("s", TimeUnit.SECONDS); |
| 38 | + timeSuffixes.put("m", TimeUnit.MINUTES); |
| 39 | + timeSuffixes.put("min", TimeUnit.MINUTES); |
| 40 | + timeSuffixes.put("h", TimeUnit.HOURS); |
| 41 | + timeSuffixes.put("d", TimeUnit.DAYS); |
| 42 | + } |
| 43 | + |
| 44 | + private static final Map<String, ByteUnit> byteSuffixes; |
| 45 | + |
| 46 | + static { |
| 47 | + byteSuffixes = new HashMap<>(); |
| 48 | + byteSuffixes.put("b", ByteUnit.BYTE); |
| 49 | + byteSuffixes.put("k", ByteUnit.KiB); |
| 50 | + byteSuffixes.put("kb", ByteUnit.KiB); |
| 51 | + byteSuffixes.put("m", ByteUnit.MiB); |
| 52 | + byteSuffixes.put("mb", ByteUnit.MiB); |
| 53 | + byteSuffixes.put("g", ByteUnit.GiB); |
| 54 | + byteSuffixes.put("gb", ByteUnit.GiB); |
| 55 | + byteSuffixes.put("t", ByteUnit.TiB); |
| 56 | + byteSuffixes.put("tb", ByteUnit.TiB); |
| 57 | + byteSuffixes.put("p", ByteUnit.PiB); |
| 58 | + byteSuffixes.put("pb", ByteUnit.PiB); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Convert a passed time string (e.g. 50s, 100ms, or 250us) to a time count in the given unit. The |
| 63 | + * unit is also considered the default if the given string does not specify a unit. |
| 64 | + */ |
| 65 | + public static long timeStringAs(String str, TimeUnit unit) { |
| 66 | + String lower = str.toLowerCase(Locale.ROOT).trim(); |
| 67 | + |
| 68 | + try { |
| 69 | + Matcher m = Pattern.compile("(-?[0-9]+)([a-z]+)?").matcher(lower); |
| 70 | + if (!m.matches()) { |
| 71 | + throw new NumberFormatException("Failed to parse time string: " + str); |
| 72 | + } |
| 73 | + |
| 74 | + long val = Long.parseLong(m.group(1)); |
| 75 | + String suffix = m.group(2); |
| 76 | + |
| 77 | + // Check for invalid suffixes |
| 78 | + if (suffix != null && !timeSuffixes.containsKey(suffix)) { |
| 79 | + throw new NumberFormatException("Invalid suffix: \"" + suffix + "\""); |
| 80 | + } |
| 81 | + |
| 82 | + // If suffix is valid use that, otherwise none was provided and use the default passed |
| 83 | + return unit.convert(val, suffix != null ? timeSuffixes.get(suffix) : unit); |
| 84 | + } catch (NumberFormatException e) { |
| 85 | + String timeError = |
| 86 | + "Time must be specified as seconds (s), " |
| 87 | + + "milliseconds (ms), microseconds (us), minutes (m or min), hour (h), or day (d). " |
| 88 | + + "E.g. 50s, 100ms, or 250us."; |
| 89 | + |
| 90 | + throw new NumberFormatException(timeError + "\n" + e.getMessage()); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Convert a time parameter such as (50s, 100ms, or 250us) to milliseconds for internal use. If no |
| 96 | + * suffix is provided, the passed number is assumed to be in ms. |
| 97 | + */ |
| 98 | + public static long timeStringAsMs(String str) { |
| 99 | + return timeStringAs(str, TimeUnit.MILLISECONDS); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Convert a time parameter such as (50s, 100ms, or 250us) to seconds for internal use. If no |
| 104 | + * suffix is provided, the passed number is assumed to be in seconds. |
| 105 | + */ |
| 106 | + public static long timeStringAsSec(String str) { |
| 107 | + return timeStringAs(str, TimeUnit.SECONDS); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Convert a passed byte string (e.g. 50b, 100kb, or 250mb) to the given. If no suffix is |
| 112 | + * provided, a direct conversion to the provided unit is attempted. |
| 113 | + */ |
| 114 | + public static long byteStringAs(String str, ByteUnit unit) { |
| 115 | + String lower = str.toLowerCase(Locale.ROOT).trim(); |
| 116 | + |
| 117 | + try { |
| 118 | + Matcher m = Pattern.compile("([0-9]+)([a-z]+)?").matcher(lower); |
| 119 | + Matcher fractionMatcher = Pattern.compile("([0-9]+\\.[0-9]+)([a-z]+)?").matcher(lower); |
| 120 | + |
| 121 | + if (m.matches()) { |
| 122 | + long val = Long.parseLong(m.group(1)); |
| 123 | + String suffix = m.group(2); |
| 124 | + |
| 125 | + // Check for invalid suffixes |
| 126 | + if (suffix != null && !byteSuffixes.containsKey(suffix)) { |
| 127 | + throw new NumberFormatException("Invalid suffix: \"" + suffix + "\""); |
| 128 | + } |
| 129 | + |
| 130 | + // If suffix is valid use that, otherwise none was provided and use the default passed |
| 131 | + return unit.convertFrom(val, suffix != null ? byteSuffixes.get(suffix) : unit); |
| 132 | + } else if (fractionMatcher.matches()) { |
| 133 | + throw new NumberFormatException( |
| 134 | + "Fractional values are not supported. Input was: " + fractionMatcher.group(1)); |
| 135 | + } else { |
| 136 | + throw new NumberFormatException("Failed to parse byte string: " + str); |
| 137 | + } |
| 138 | + |
| 139 | + } catch (NumberFormatException e) { |
| 140 | + String byteError = |
| 141 | + "Size must be specified as bytes (b), " |
| 142 | + + "kibibytes (k), mebibytes (m), gibibytes (g), tebibytes (t), or pebibytes(p). " |
| 143 | + + "E.g. 50b, 100k, or 250m."; |
| 144 | + |
| 145 | + throw new NumberFormatException(byteError + "\n" + e.getMessage()); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Convert a passed byte string (e.g. 50b, 100k, or 250m) to bytes for internal use. |
| 151 | + * |
| 152 | + * <p>If no suffix is provided, the passed number is assumed to be in bytes. |
| 153 | + */ |
| 154 | + public static long byteStringAsBytes(String str) { |
| 155 | + return byteStringAs(str, ByteUnit.BYTE); |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Convert a passed byte string (e.g. 50b, 100k, or 250m) to kibibytes for internal use. |
| 160 | + * |
| 161 | + * <p>If no suffix is provided, the passed number is assumed to be in kibibytes. |
| 162 | + */ |
| 163 | + public static long byteStringAsKb(String str) { |
| 164 | + return byteStringAs(str, ByteUnit.KiB); |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Convert a passed byte string (e.g. 50b, 100k, or 250m) to mebibytes for internal use. |
| 169 | + * |
| 170 | + * <p>If no suffix is provided, the passed number is assumed to be in mebibytes. |
| 171 | + */ |
| 172 | + public static long byteStringAsMb(String str) { |
| 173 | + return byteStringAs(str, ByteUnit.MiB); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Convert a passed byte string (e.g. 50b, 100k, or 250m) to gibibytes for internal use. |
| 178 | + * |
| 179 | + * <p>If no suffix is provided, the passed number is assumed to be in gibibytes. |
| 180 | + */ |
| 181 | + public static long byteStringAsGb(String str) { |
| 182 | + return byteStringAs(str, ByteUnit.GiB); |
| 183 | + } |
| 184 | +} |
0 commit comments