-
Notifications
You must be signed in to change notification settings - Fork 37
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
base: master
Are you sure you want to change the base?
Changes from 8 commits
753618b
8d39d02
bf94c49
47be388
913eb1c
1f204b8
67f2474
14e3593
a096d51
dbf1940
cd8add9
52890c8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have updated the change as suggested. |
||||||||||||||||||||||
} | ||||||||||||||||||||||
} |
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); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use lower camel case for the logger:
Suggested change
According to the Google Java Style Guide, loggers are not considered constants: https://google.github.io/styleguide/javaguide.html#s5.2.4-constant-names There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have updated the name to |
||||||
|
||||||
/** | ||||||
* 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
We should not share the same instance between threads. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have updated the change as suggested. |
||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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