Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Util classes for data loader #2388

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions core/src/main/java/com/scalar/db/common/error/CoreError.java
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,8 @@ public enum CoreError implements ScalarDbError {
"Invalid number specified for column %s in table %s in namespace %s",
"",
""),
DATA_LOADER_ERROR_METHOD_NULL_ARGUMENT(
Category.USER_ERROR, "0151", "Method null argument not allowed", "", ""),

//
// Errors for the concurrency error category
Expand Down
10 changes: 10 additions & 0 deletions data-loader/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
subprojects {
ext {
jacksonVersion = '2.17.0'
}
group = "scalardb.dataloader"
dependencies {
// AssertJ
Expand All @@ -13,6 +16,7 @@ subprojects {
// Apache Commons
implementation("org.apache.commons:commons-lang3:${commonsLangVersion}")
implementation("commons-io:commons-io:${commonsIoVersion}")
implementation("org.slf4j:slf4j-simple:${slf4jVersion}")

// Mockito
testImplementation "org.mockito:mockito-core:${mockitoVersion}"
Expand All @@ -24,5 +28,11 @@ subprojects {
annotationProcessor "org.projectlombok:lombok:${lombokVersion}"
testCompileOnly "org.projectlombok:lombok:${lombokVersion}"
testAnnotationProcessor "org.projectlombok:lombok:${lombokVersion}"

// Jackson
implementation("com.fasterxml.jackson.core:jackson-core:${jacksonVersion}")
implementation("com.fasterxml.jackson.core:jackson-databind:${jacksonVersion}")
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:${jacksonVersion}")

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.scalar.db.dataloader.core.exception;

/** Exception thrown when an error occurs while trying to encode or decode base64 values. */
public class Base64Exception extends Exception {

/**
* Class constructor
*
* @param message Exception message
*/
public Base64Exception(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.scalar.db.dataloader.core.util;

import java.util.Collection;

/** Utils for collection classes */
public class CollectionUtil {

/**
* Check if lists are of same length
*
* @param collections List of collections
* @return collections are same length or not
*/
public static boolean areSameLength(Collection<?>... collections) {
int n = collections[0].size();
for (Collection<?> c : collections) {
if (c.size() != n) {
return false;
}
}
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.scalar.db.dataloader.core.util;

/** Utils for csv data manipulation */
public class CsvUtil {

/**
* Remove the last character in the string builder if it's a delimiter
*
* @param stringBuilder String builder instance
* @param delimiter Delimiter character used in the CSV content
*/
public static void removeTrailingDelimiter(StringBuilder stringBuilder, String delimiter) {
if (stringBuilder.substring(stringBuilder.length() - 1).equals(delimiter)) {
stringBuilder.setLength(stringBuilder.length() - 1);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.scalar.db.dataloader.core.util;

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

public class DebugUtil {

private static final Logger logger = LoggerFactory.getLogger(DebugUtil.class);

/**
* log memory usage
*
* @param stage stage of process
*/
public static void logMemoryUsage(String stage) {
Runtime runtime = Runtime.getRuntime();
long usedMemory = runtime.totalMemory() - runtime.freeMemory();
long maxMemory = runtime.maxMemory();

logger.info(
"Memory usage at {}: Used Memory = {} MB, Max Memory = {} MB",
stage,
formatMemorySize(usedMemory),
formatMemorySize(maxMemory));
}

private static String formatMemorySize(long size) {
return String.format("%.2f", size / (1024.0 * 1024.0));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.scalar.db.dataloader.core.util;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;

/** Utils for decimal handling */
public class DecimalUtil {

/**
* Convert a Double to a non-scientific formatted string
*
* @param doubleValue Double value
* @return formatted double as a string
*/
public static String convertToNonScientific(Double doubleValue) {
return createFormatter().format(doubleValue);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this method is frequently called, I think the DecimalFormat should be reused.

  DecimalFormat DECIMAL_FORMAT = createFormatter();

  ...

    public static String convertToNonScientific(Double doubleValue) {
      return DECIMAL_FORMAT.format(doubleValue);
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@komamitsu san, I had added this change but had to revert back based on Suzuki-san's comment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, it's used by multiple threads, right. Understood. Using ThreadLocal is might be an option, though.

}

/**
* Convert a Float to a non-scientific formatted string
*
* @param floatValue Float value
* @return formatted float as a string
*/
public static String convertToNonScientific(Float floatValue) {
return createFormatter().format(floatValue);
}

/**
* Create a Decimal formatter
*
* @return decimal formatter instance
*/
private static DecimalFormat createFormatter() {
DecimalFormat df = new DecimalFormat("0", DecimalFormatSymbols.getInstance(Locale.ENGLISH));
df.setMaximumFractionDigits(340); // 340 = DecimalFormat.DOUBLE_FRACTION_DIGITS
return df;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.scalar.db.dataloader.core.util;

import java.io.File;

public class PathUtil {

/**
* Ensures the specified path has a trailing path separator.
*
* @param path the path
* @return the path with a trailing path separator.
*/
public static String ensureTrailingSeparator(String path) {
if (path == null || path.isEmpty()) {
return "";
}

if (!path.endsWith(File.separator)) {
return path + File.separator;
}

return path;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.scalar.db.dataloader.core.util;

import static com.scalar.db.common.error.CoreError.DATA_LOADER_ERROR_METHOD_NULL_ARGUMENT;

/** Utils for runtime checks */
public class RuntimeUtil {

/**
* Argument null check
*
* @param values List of arguments
* @throws NullPointerException when one of the arguments is null
*/
public static void checkNotNull(Object... values) {
for (Object value : values) {
if (value == null) {
throw new NullPointerException(DATA_LOADER_ERROR_METHOD_NULL_ARGUMENT.buildMessage());
}
}
}
}
Loading
Loading