ekaterinadimitrova2 commented on a change in pull request #1425: URL: https://github.com/apache/cassandra/pull/1425#discussion_r794009377
########## File path: src/java/org/apache/cassandra/config/DataRateSpec.java ########## @@ -0,0 +1,376 @@ +/* + * 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. + */ +package org.apache.cassandra.config; + +import java.util.Arrays; +import java.util.Objects; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + +import com.google.common.primitives.Ints; + +import org.apache.cassandra.exceptions.ConfigurationException; + +/** + * Represents a data rate type used for cassandra configuration. It supports the opportunity for the users to be able to + * add units to the confiuration parameter value. (CASSANDRA-15234) + */ +public final class DataRateSpec +{ + /** + * The Regexp used to parse the rate provided as String in cassandra.yaml. + */ + private static final Pattern BIT_RATE_UNITS_PATTERN = Pattern.compile("^(\\d+)(MiB/s|KiB/s|B/s)$"); + + private final double quantity; + + private final DataRateUnit unit; + + public DataRateSpec(String value) + { + //parse the string field value + Matcher matcher = BIT_RATE_UNITS_PATTERN.matcher(value); + + if (!matcher.find()) + throw new ConfigurationException("Invalid bit rate: " + value + " Accepted units: MiB/s, KiB/s, B/s where " + + "case matters and " + "only non-negative values are valid"); + + quantity = Long.parseLong(matcher.group(1)); + unit = DataRateUnit.fromSymbol(matcher.group(2)); + } + + DataRateSpec(double quantity, DataRateUnit unit) + { + if (quantity < 0) + throw new ConfigurationException("Invalid bit rare: value must be non-negative"); + + if (quantity > Long.MAX_VALUE) + throw new NumberFormatException("Invalid bit rate: value must be between 0 and Long.MAX_VALUE = 9223372036854775807"); + + this.quantity = quantity; + this.unit = unit; + } + + /** + * Creates a {@code DataRateSpec} of the specified amount of bits per second. + * + * @param bytesPerSecond the amount of bytes per second + * @return a {@code DataRateSpec} + */ + public static DataRateSpec inBytesPerSecond(long bytesPerSecond) + { + return new DataRateSpec(bytesPerSecond, DataRateUnit.BYTES_PER_SECOND); + } + + /** + * Creates a {@code DataRateSpec} of the specified amount of kibibytes per second. + * + * @param kibibytesPerSecond the amount of kibibytes per second + * @return a {@code DataRateSpec} + */ + public static DataRateSpec inKibibytesPerSecond(long kibibytesPerSecond) + { + return new DataRateSpec(kibibytesPerSecond, DataRateUnit.KIBIBYTES_PER_SECOND); + } + + /** + * Creates a {@code DataRateSpec} of the specified amount of mebibytes per second. + * + * @param mebibytesPerSecond the amount of mebibytes per second + * @return a {@code DataRateSpec} + */ + public static DataRateSpec inMebibytesPerSecond(long mebibytesPerSecond) + { + return new DataRateSpec(mebibytesPerSecond, DataRateUnit.MEBIBYTES_PER_SECOND); + } + + /** + * Creates a {@code DataRateSpec} of the specified amount of mebibytes per second. + * + * @param megabitsPerSecond the amount of megabits per second + * @return a {@code DataRateSpec} + */ + public static DataRateSpec megabitsPerSecondInMebibytesPerSecond(long megabitsPerSecond) + { + final double MEBIBYTES_PER_MEGABIT = 0.119209289550781; + double mebibytesPerSecond = (double)megabitsPerSecond * MEBIBYTES_PER_MEGABIT; + + return new DataRateSpec(mebibytesPerSecond, DataRateUnit.MEBIBYTES_PER_SECOND); + } + + /** + * @return the data rate unit assigned. + */ + public DataRateUnit getUnit() + { + return unit; + } + + /** + * @return the data rate in bytes per seconds + */ + public double toBytesPerSecond() + { + return unit.toBytesPerSecond(quantity); + } + + /** + * Returns the data rate in bytes per seconds as an {@code int} + * + * @return the data rate in bytes per secondss or {@code Integer.MAX_VALUE} if the rate is too large. + */ + public int toBytesPerSecondAsInt() + { + return Ints.saturatedCast(Math.round(toBytesPerSecond())); + } + + /** + * @return the data rate in kibibyts per seconds + */ + public double toKibibytesPerSecond() + { + return unit.toKibibytesPerSecond(quantity); + } + + /** + * Returns the data rate in kibibytes per seconds as an {@code int} Review comment: per second, not seconds, corrected it at a few places -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

