Skip to content

Commit 0a880ff

Browse files
feeblefakieinv-jishnuypeckstadt
authored
Backport to branch(3.13) : Add Util classes for data loader (#2413)
Co-authored-by: inv-jishnu <[email protected]> Co-authored-by: Peckstadt Yves <[email protected]>
1 parent cf6cade commit 0a880ff

File tree

15 files changed

+540
-0
lines changed

15 files changed

+540
-0
lines changed

core/src/main/java/com/scalar/db/common/error/CoreError.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,8 @@ public enum CoreError implements ScalarDbError {
676676
"Invalid number specified for column %s in table %s in namespace %s",
677677
"",
678678
""),
679+
DATA_LOADER_ERROR_METHOD_NULL_ARGUMENT(
680+
Category.USER_ERROR, "0151", "Method null argument not allowed", "", ""),
679681

680682
//
681683
// Errors for the concurrency error category

data-loader/build.gradle

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
subprojects {
2+
ext {
3+
jacksonVersion = '2.17.0'
4+
}
25
group = "scalardb.dataloader"
36
dependencies {
47
// AssertJ
@@ -13,6 +16,7 @@ subprojects {
1316
// Apache Commons
1417
implementation("org.apache.commons:commons-lang3:${commonsLangVersion}")
1518
implementation("commons-io:commons-io:${commonsIoVersion}")
19+
implementation("org.slf4j:slf4j-simple:${slf4jVersion}")
1620

1721
// Mockito
1822
testImplementation "org.mockito:mockito-core:${mockitoVersion}"
@@ -24,5 +28,11 @@ subprojects {
2428
annotationProcessor "org.projectlombok:lombok:${lombokVersion}"
2529
testCompileOnly "org.projectlombok:lombok:${lombokVersion}"
2630
testAnnotationProcessor "org.projectlombok:lombok:${lombokVersion}"
31+
32+
// Jackson
33+
implementation("com.fasterxml.jackson.core:jackson-core:${jacksonVersion}")
34+
implementation("com.fasterxml.jackson.core:jackson-databind:${jacksonVersion}")
35+
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:${jacksonVersion}")
36+
2737
}
2838
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.scalar.db.dataloader.core.exception;
2+
3+
/** Exception thrown when an error occurs while trying to encode or decode base64 values. */
4+
public class Base64Exception extends Exception {
5+
6+
/**
7+
* Class constructor
8+
*
9+
* @param message Exception message
10+
*/
11+
public Base64Exception(String message) {
12+
super(message);
13+
}
14+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.scalar.db.dataloader.core.util;
2+
3+
import java.util.Collection;
4+
5+
/** Utils for collection classes */
6+
public class CollectionUtil {
7+
8+
/**
9+
* Check if lists are of same length
10+
*
11+
* @param collections List of collections
12+
* @return collections are same length or not
13+
*/
14+
public static boolean areSameLength(Collection<?>... collections) {
15+
int n = collections[0].size();
16+
for (Collection<?> c : collections) {
17+
if (c.size() != n) {
18+
return false;
19+
}
20+
}
21+
return true;
22+
}
23+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.scalar.db.dataloader.core.util;
2+
3+
/** Utils for csv data manipulation */
4+
public class CsvUtil {
5+
6+
/**
7+
* Remove the last character in the string builder if it's a delimiter
8+
*
9+
* @param stringBuilder String builder instance
10+
* @param delimiter Delimiter character used in the CSV content
11+
*/
12+
public static void removeTrailingDelimiter(StringBuilder stringBuilder, String delimiter) {
13+
if (stringBuilder.substring(stringBuilder.length() - 1).equals(delimiter)) {
14+
stringBuilder.setLength(stringBuilder.length() - 1);
15+
}
16+
}
17+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.scalar.db.dataloader.core.util;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
public class DebugUtil {
7+
8+
private static final Logger logger = LoggerFactory.getLogger(DebugUtil.class);
9+
10+
/**
11+
* log memory usage
12+
*
13+
* @param stage stage of process
14+
*/
15+
public static void logMemoryUsage(String stage) {
16+
Runtime runtime = Runtime.getRuntime();
17+
long usedMemory = runtime.totalMemory() - runtime.freeMemory();
18+
long maxMemory = runtime.maxMemory();
19+
20+
logger.info(
21+
"Memory usage at {}: Used Memory = {} MB, Max Memory = {} MB",
22+
stage,
23+
formatMemorySize(usedMemory),
24+
formatMemorySize(maxMemory));
25+
}
26+
27+
private static String formatMemorySize(long size) {
28+
return String.format("%.2f", size / (1024.0 * 1024.0));
29+
}
30+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.scalar.db.dataloader.core.util;
2+
3+
import java.text.DecimalFormat;
4+
import java.text.DecimalFormatSymbols;
5+
import java.util.Locale;
6+
7+
/** Utils for decimal handling */
8+
public class DecimalUtil {
9+
10+
/**
11+
* Convert a Double to a non-scientific formatted string
12+
*
13+
* @param doubleValue Double value
14+
* @return formatted double as a string
15+
*/
16+
public static String convertToNonScientific(Double doubleValue) {
17+
return createFormatter().format(doubleValue);
18+
}
19+
20+
/**
21+
* Convert a Float to a non-scientific formatted string
22+
*
23+
* @param floatValue Float value
24+
* @return formatted float as a string
25+
*/
26+
public static String convertToNonScientific(Float floatValue) {
27+
return createFormatter().format(floatValue);
28+
}
29+
30+
/**
31+
* Create a Decimal formatter
32+
*
33+
* @return decimal formatter instance
34+
*/
35+
private static DecimalFormat createFormatter() {
36+
DecimalFormat df = new DecimalFormat("0", DecimalFormatSymbols.getInstance(Locale.ENGLISH));
37+
df.setMaximumFractionDigits(340); // 340 = DecimalFormat.DOUBLE_FRACTION_DIGITS
38+
return df;
39+
}
40+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.scalar.db.dataloader.core.util;
2+
3+
import java.io.File;
4+
5+
public class PathUtil {
6+
7+
/**
8+
* Ensures the specified path has a trailing path separator.
9+
*
10+
* @param path the path
11+
* @return the path with a trailing path separator.
12+
*/
13+
public static String ensureTrailingSeparator(String path) {
14+
if (path == null || path.isEmpty()) {
15+
return "";
16+
}
17+
18+
if (!path.endsWith(File.separator)) {
19+
return path + File.separator;
20+
}
21+
22+
return path;
23+
}
24+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.scalar.db.dataloader.core.util;
2+
3+
import static com.scalar.db.common.error.CoreError.DATA_LOADER_ERROR_METHOD_NULL_ARGUMENT;
4+
5+
/** Utils for runtime checks */
6+
public class RuntimeUtil {
7+
8+
/**
9+
* Argument null check
10+
*
11+
* @param values List of arguments
12+
* @throws NullPointerException when one of the arguments is null
13+
*/
14+
public static void checkNotNull(Object... values) {
15+
for (Object value : values) {
16+
if (value == null) {
17+
throw new NullPointerException(DATA_LOADER_ERROR_METHOD_NULL_ARGUMENT.buildMessage());
18+
}
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)