-
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
Open
inv-jishnu
wants to merge
12
commits into
master
Choose a base branch
from
feat/data-loader/utils
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
753618b
Util classes for data loader
inv-jishnu 8d39d02
Fix spotbug issue
inv-jishnu bf94c49
Removed error message and added core error
inv-jishnu 47be388
Applied spotless
inv-jishnu 913eb1c
Fixed unit test failures
inv-jishnu 1f204b8
Merge branch 'master' into feat/data-loader/utils
ypeckstadt 67f2474
Added DECIMAL_FORMAT
inv-jishnu 14e3593
Path util class updated
inv-jishnu a096d51
Feedback changes
inv-jishnu dbf1940
Merge branch 'master' into feat/data-loader/utils
ypeckstadt cd8add9
Merge branch 'master' into feat/data-loader/utils
ypeckstadt 52890c8
Changes
inv-jishnu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
data-loader/core/src/main/java/com/scalar/db/dataloader/core/exception/Base64Exception.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
data-loader/core/src/main/java/com/scalar/db/dataloader/core/util/CollectionUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
data-loader/core/src/main/java/com/scalar/db/dataloader/core/util/CsvUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
data-loader/core/src/main/java/com/scalar/db/dataloader/core/util/DebugUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
data-loader/core/src/main/java/com/scalar/db/dataloader/core/util/DecimalUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
/** | ||
* 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; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
data-loader/core/src/main/java/com/scalar/db/dataloader/core/util/PathUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
data-loader/core/src/main/java/com/scalar/db/dataloader/core/util/RuntimeUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
If this method is frequently called, I think the DecimalFormat should be reused.
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.
@komamitsu san, I had added this change but had to revert back based on Suzuki-san's comment.
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.
Ah, it's used by multiple threads, right. Understood. Using ThreadLocal is might be an option, though.