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
11 changes: 11 additions & 0 deletions data-loader/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
subprojects {

Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have removed the empty line.
Thank you

ext {
jacksonVersion = '2.17.0'
}
group = "scalardb.dataloader"
dependencies {
// AssertJ
Expand All @@ -13,6 +17,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 +29,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,19 @@
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<?> a : collections) if (a.size() != N) return false;
return true;
Copy link
Collaborator

@brfrn169 brfrn169 Dec 12, 2024

Choose a reason for hiding this comment

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

Suggested change
int N = collections[0].size();
for (Collection<?> a : collections) if (a.size() != N) return false;
return true;
int n = collections[0].size();
for (Collection<?> c : collections) {
if (c.size() != n) {
return false;
}
}
return true;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have updated the change as suggested.
Thank you.

}
}
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);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please use lower camel case for the logger:

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

According to the Google Java Style Guide, loggers are not considered constants: https://google.github.io/styleguide/javaguide.html#s5.2.4-constant-names

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have updated the name to logger.
Thank you.


/**
* 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,42 @@
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 {

private static final DecimalFormat DECIMAL_FORMAT = createFormatter();
Copy link
Collaborator

Choose a reason for hiding this comment

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

DecimalFormat isn't thread-safe:
https://www.baeldung.com/java-decimalformat#thread-safety

We should not share the same instance between threads.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@brfrn169 san,

I had changed this based on an earlier comment. I have reverted this back.


/**
* 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 DECIMAL_FORMAT.format(doubleValue);
}

/**
* 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 DECIMAL_FORMAT.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.getMessage());
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
for (Object value : values) {
if (value == null) {
throw new NullPointerException(DATA_LOADER_ERROR_METHOD_NULL_ARGUMENT.getMessage());
for (Object value : values) {
if (value == null) {
throw new NullPointerException(DATA_LOADER_ERROR_METHOD_NULL_ARGUMENT.buildMessage());

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have updated the change as suggested.
Thank you.

}
}
}
}
Loading
Loading