-
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 data loader core key and column utils #1771
Open
ypeckstadt
wants to merge
15
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
15 commits
Select commit
Hold shift + click to select a range
2e38741
Add mockito and core project in Gradle files
ypeckstadt 3797510
WIP
ypeckstadt 637b474
Move ColumnKeyValue to data loader core project
ypeckstadt a113450
Merge branch 'feat/data-loader/columnkeyvalue' into feat/data-loader/…
ypeckstadt 89028b4
Add dataloader core key and column utils
ypeckstadt 0c34139
Merge branch 'master' into feat/data-loader/utils
ypeckstadt 9c24445
Rename to CoreError.DATA_LOADER_INVALID_COLUMN_NON_EXISTENT
ypeckstadt 79fee4b
Add @Nullable to parseKeyValue
ypeckstadt 4039083
Pass in tableName with parseKeyValue and parsing exception
ypeckstadt 2af371a
Apply error handling suggestions
ypeckstadt ee67ef7
Remove unused CoreError
ypeckstadt d06c3b5
Merge branch 'master' into feat/data-loader/utils
ypeckstadt 57738d3
Merge branch 'master' into feat/data-loader/utils
ypeckstadt c451948
Add Lombok to build.gradle for dataloader project
ypeckstadt d12526c
Merge branch 'master' into feat/data-loader/utils
ypeckstadt 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
subprojects { | ||
group = "scalardb.dataloader" | ||
|
||
|
||
ext { | ||
apacheCommonsLangVersion = '3.14.0' | ||
apacheCommonsIoVersion = '2.16.1' | ||
lombokVersion = '1.18.34' | ||
} | ||
dependencies { | ||
// AssertJ | ||
|
@@ -23,5 +25,12 @@ subprojects { | |
testImplementation "org.mockito:mockito-core:${mockitoVersion}" | ||
testImplementation "org.mockito:mockito-inline:${mockitoVersion}" | ||
testImplementation "org.mockito:mockito-junit-jupiter:${mockitoVersion}" | ||
|
||
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. Ditto. Unnecessary empty line? |
||
|
||
// Lombok | ||
compileOnly "org.projectlombok:lombok:${lombokVersion}" | ||
annotationProcessor "org.projectlombok:lombok:${lombokVersion}" | ||
testCompileOnly "org.projectlombok:lombok:${lombokVersion}" | ||
testAnnotationProcessor "org.projectlombok:lombok:${lombokVersion}" | ||
} | ||
} |
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
13 changes: 13 additions & 0 deletions
13
data-loader/core/src/main/java/com/scalar/db/dataloader/core/ColumnInfo.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,13 @@ | ||
package com.scalar.db.dataloader.core; | ||
|
||
import lombok.Builder; | ||
import lombok.Value; | ||
|
||
/** Represents a column in a table. */ | ||
@Value | ||
@Builder | ||
public class ColumnInfo { | ||
String namespace; | ||
String tableName; | ||
String columnName; | ||
} |
16 changes: 16 additions & 0 deletions
16
...er/core/src/main/java/com/scalar/db/dataloader/core/exception/ColumnParsingException.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,16 @@ | ||
package com.scalar.db.dataloader.core.exception; | ||
|
||
/** | ||
* An exception that is thrown when an error occurs while trying to create a ScalarDB column from a | ||
* value | ||
*/ | ||
public class ColumnParsingException extends Exception { | ||
|
||
public ColumnParsingException(String message) { | ||
super(message); | ||
} | ||
|
||
public ColumnParsingException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...oader/core/src/main/java/com/scalar/db/dataloader/core/exception/KeyParsingException.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,15 @@ | ||
package com.scalar.db.dataloader.core.exception; | ||
|
||
/** | ||
* An exception that is thrown when an error occurs while trying to create a ScalarDB from a value | ||
*/ | ||
public class KeyParsingException extends Exception { | ||
|
||
public KeyParsingException(String message) { | ||
super(message); | ||
} | ||
|
||
public KeyParsingException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
data-loader/core/src/main/java/com/scalar/db/dataloader/core/util/ColumnUtils.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,83 @@ | ||
package com.scalar.db.dataloader.core.util; | ||
|
||
import com.scalar.db.common.error.CoreError; | ||
import com.scalar.db.dataloader.core.ColumnInfo; | ||
import com.scalar.db.dataloader.core.exception.ColumnParsingException; | ||
import com.scalar.db.io.BigIntColumn; | ||
import com.scalar.db.io.BlobColumn; | ||
import com.scalar.db.io.BooleanColumn; | ||
import com.scalar.db.io.Column; | ||
import com.scalar.db.io.DataType; | ||
import com.scalar.db.io.DoubleColumn; | ||
import com.scalar.db.io.FloatColumn; | ||
import com.scalar.db.io.IntColumn; | ||
import com.scalar.db.io.TextColumn; | ||
import java.util.Base64; | ||
import javax.annotation.Nullable; | ||
|
||
/** Utility class for dealing and creating ScalarDB Columns */ | ||
public final class ColumnUtils { | ||
|
||
/** Restrict instantiation via private constructor */ | ||
private ColumnUtils() {} | ||
|
||
/** | ||
* Create a ScalarDB column from the given data type, column name, and value. Blob source values | ||
* need to be base64 encoded. | ||
* | ||
* @param dataType Data type of the specified column | ||
* @param columnInfo ScalarDB table column information | ||
* @param value Value for the ScalarDB column | ||
* @return ScalarDB column | ||
* @throws ColumnParsingException if an error occurs while creating the column and parsing the | ||
* value | ||
*/ | ||
public static Column<?> createColumnFromValue( | ||
DataType dataType, ColumnInfo columnInfo, @Nullable String value) | ||
throws ColumnParsingException { | ||
String columnName = columnInfo.getColumnName(); | ||
try { | ||
switch (dataType) { | ||
case BOOLEAN: | ||
return value != null | ||
? BooleanColumn.of(columnName, Boolean.parseBoolean(value)) | ||
: BooleanColumn.ofNull(columnName); | ||
case INT: | ||
return value != null | ||
? IntColumn.of(columnName, Integer.parseInt(value)) | ||
: IntColumn.ofNull(columnName); | ||
case BIGINT: | ||
return value != null | ||
? BigIntColumn.of(columnName, Long.parseLong(value)) | ||
: BigIntColumn.ofNull(columnName); | ||
case FLOAT: | ||
return value != null | ||
? FloatColumn.of(columnName, Float.parseFloat(value)) | ||
: FloatColumn.ofNull(columnName); | ||
case DOUBLE: | ||
return value != null | ||
? DoubleColumn.of(columnName, Double.parseDouble(value)) | ||
: DoubleColumn.ofNull(columnName); | ||
case TEXT: | ||
return value != null ? TextColumn.of(columnName, value) : TextColumn.ofNull(columnName); | ||
case BLOB: | ||
// Source blob values need to be base64 encoded | ||
return value != null | ||
? BlobColumn.of(columnName, Base64.getDecoder().decode(value)) | ||
: BlobColumn.ofNull(columnName); | ||
default: | ||
throw new AssertionError(); | ||
} | ||
} catch (NumberFormatException e) { | ||
throw new ColumnParsingException( | ||
CoreError.DATA_LOADER_INVALID_NUMBER_FORMAT_FOR_COLUMN_VALUE.buildMessage( | ||
columnName, columnInfo.getTableName(), columnInfo.getNamespace()), | ||
e); | ||
} catch (IllegalArgumentException e) { | ||
throw new ColumnParsingException( | ||
CoreError.DATA_LOADER_INVALID_BASE64_ENCODING_FOR_COLUMN_VALUE.buildMessage( | ||
columnName, columnInfo.getTableName(), columnInfo.getNamespace()), | ||
e); | ||
} | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
data-loader/core/src/main/java/com/scalar/db/dataloader/core/util/KeyUtils.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,75 @@ | ||
package com.scalar.db.dataloader.core.util; | ||
|
||
import com.scalar.db.api.TableMetadata; | ||
import com.scalar.db.common.error.CoreError; | ||
import com.scalar.db.dataloader.core.ColumnInfo; | ||
import com.scalar.db.dataloader.core.ColumnKeyValue; | ||
import com.scalar.db.dataloader.core.exception.ColumnParsingException; | ||
import com.scalar.db.dataloader.core.exception.KeyParsingException; | ||
import com.scalar.db.io.Column; | ||
import com.scalar.db.io.DataType; | ||
import com.scalar.db.io.Key; | ||
import javax.annotation.Nullable; | ||
|
||
/** Utility class for creating and dealing with ScalarDB keys. */ | ||
public final class KeyUtils { | ||
|
||
/** Restrict instantiation via private constructor */ | ||
private KeyUtils() {} | ||
|
||
/** | ||
* Convert a keyValue, in the format of <key>=<value>, to a ScalarDB Key instance for a specific | ||
* ScalarDB table. | ||
* | ||
* @param columnKeyValue A key value in the format of <key>=<value> | ||
* @param namespace Name of the ScalarDB namespace | ||
* @param tableName Name of the ScalarDB table | ||
* @param tableMetadata Metadata for one ScalarDB table | ||
* @return A new ScalarDB Key instance formatted by data type | ||
* @throws KeyParsingException if there is an error parsing the key value | ||
*/ | ||
@Nullable | ||
public static Key parseKeyValue( | ||
ypeckstadt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@Nullable ColumnKeyValue columnKeyValue, | ||
String namespace, | ||
String tableName, | ||
TableMetadata tableMetadata) | ||
throws KeyParsingException { | ||
if (columnKeyValue == null) { | ||
return null; | ||
} | ||
String columnName = columnKeyValue.getColumnName(); | ||
DataType columnDataType = tableMetadata.getColumnDataType(columnName); | ||
if (columnDataType == null) { | ||
throw new KeyParsingException( | ||
CoreError.DATA_LOADER_INVALID_COLUMN_NON_EXISTENT.buildMessage( | ||
columnName, tableName, namespace)); | ||
} | ||
ColumnInfo columnInfo = | ||
ColumnInfo.builder() | ||
.namespace(namespace) | ||
.tableName(tableName) | ||
.columnName(columnName) | ||
.build(); | ||
return createKey(columnDataType, columnInfo, columnKeyValue.getColumnValue()); | ||
} | ||
|
||
/** | ||
* Create a ScalarDB key based on the provided data type, column name, and value. | ||
* | ||
* @param dataType Data type of the specified column | ||
* @param columnInfo ScalarDB table column information | ||
* @param value Value for ScalarDB key | ||
* @return ScalarDB Key instance | ||
* @throws KeyParsingException if there is an error while creating a ScalarDB key | ||
*/ | ||
public static Key createKey(DataType dataType, ColumnInfo columnInfo, String value) | ||
throws KeyParsingException { | ||
try { | ||
Column<?> keyValue = ColumnUtils.createColumnFromValue(dataType, columnInfo, value); | ||
return Key.newBuilder().add(keyValue).build(); | ||
} catch (ColumnParsingException e) { | ||
throw new KeyParsingException(e.getMessage(), e); | ||
} | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
data-loader/core/src/test/java/com/scalar/db/dataloader/core/util/ColumnUtilsTest.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,108 @@ | ||
package com.scalar.db.dataloader.core.util; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import com.scalar.db.common.error.CoreError; | ||
import com.scalar.db.dataloader.core.ColumnInfo; | ||
import com.scalar.db.dataloader.core.exception.ColumnParsingException; | ||
import com.scalar.db.io.BigIntColumn; | ||
import com.scalar.db.io.BlobColumn; | ||
import com.scalar.db.io.BooleanColumn; | ||
import com.scalar.db.io.Column; | ||
import com.scalar.db.io.DataType; | ||
import com.scalar.db.io.DoubleColumn; | ||
import com.scalar.db.io.FloatColumn; | ||
import com.scalar.db.io.IntColumn; | ||
import com.scalar.db.io.TextColumn; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Base64; | ||
import java.util.stream.Stream; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
class ColumnUtilsTest { | ||
|
||
private static final float FLOAT_VALUE = 2.78f; | ||
|
||
private static Stream<Arguments> provideColumnsForCreateColumnFromValue() { | ||
return Stream.of( | ||
Arguments.of(DataType.BOOLEAN, "boolColumn", "true", BooleanColumn.of("boolColumn", true)), | ||
Arguments.of(DataType.BOOLEAN, "boolColumn", null, BooleanColumn.ofNull("boolColumn")), | ||
Arguments.of(DataType.INT, "intColumn", "42", IntColumn.of("intColumn", 42)), | ||
Arguments.of(DataType.INT, "intColumn", null, IntColumn.ofNull("intColumn")), | ||
Arguments.of( | ||
DataType.BIGINT, | ||
"bigintColumn", | ||
"123456789012", | ||
BigIntColumn.of("bigintColumn", 123456789012L)), | ||
Arguments.of(DataType.BIGINT, "bigintColumn", null, BigIntColumn.ofNull("bigintColumn")), | ||
Arguments.of( | ||
DataType.FLOAT, | ||
"floatColumn", | ||
Float.toString(FLOAT_VALUE), | ||
FloatColumn.of("floatColumn", FLOAT_VALUE)), | ||
Arguments.of(DataType.FLOAT, "floatColumn", null, FloatColumn.ofNull("floatColumn")), | ||
Arguments.of( | ||
DataType.DOUBLE, | ||
"doubleColumn", | ||
Double.toString(Math.E), | ||
DoubleColumn.of("doubleColumn", Math.E)), | ||
Arguments.of(DataType.DOUBLE, "doubleColumn", null, DoubleColumn.ofNull("doubleColumn")), | ||
Arguments.of( | ||
DataType.TEXT, | ||
"textColumn", | ||
"Hello, world!", | ||
TextColumn.of("textColumn", "Hello, world!")), | ||
Arguments.of(DataType.TEXT, "textColumn", null, TextColumn.ofNull("textColumn")), | ||
Arguments.of( | ||
DataType.BLOB, | ||
"blobColumn", | ||
Base64.getEncoder().encodeToString("binary".getBytes(StandardCharsets.UTF_8)), | ||
BlobColumn.of("blobColumn", "binary".getBytes(StandardCharsets.UTF_8))), | ||
Arguments.of(DataType.BLOB, "blobColumn", null, BlobColumn.ofNull("blobColumn"))); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("provideColumnsForCreateColumnFromValue") | ||
void createColumnFromValue_validInput_returnsColumn( | ||
DataType dataType, String columnName, String value, Column<?> expectedColumn) | ||
throws ColumnParsingException { | ||
ColumnInfo columnInfo = ColumnInfo.builder().columnName(columnName).build(); | ||
Column<?> actualColumn = ColumnUtils.createColumnFromValue(dataType, columnInfo, value); | ||
assertEquals(expectedColumn, actualColumn); | ||
} | ||
|
||
@Test | ||
void createColumnFromValue_invalidNumberFormat_throwsNumberFormatException() { | ||
String columnName = "intColumn"; | ||
String value = "not_a_number"; | ||
ColumnInfo columnInfo = | ||
ColumnInfo.builder().namespace("ns").tableName("table").columnName(columnName).build(); | ||
ColumnParsingException exception = | ||
assertThrows( | ||
ColumnParsingException.class, | ||
() -> ColumnUtils.createColumnFromValue(DataType.INT, columnInfo, value)); | ||
assertEquals( | ||
CoreError.DATA_LOADER_INVALID_NUMBER_FORMAT_FOR_COLUMN_VALUE.buildMessage( | ||
columnName, "table", "ns"), | ||
exception.getMessage()); | ||
} | ||
|
||
@Test | ||
void createColumnFromValue_invalidBase64_throwsBase64Exception() { | ||
String columnName = "blobColumn"; | ||
String value = "invalid_base64"; | ||
ColumnInfo columnInfo = | ||
ColumnInfo.builder().namespace("ns").tableName("table").columnName(columnName).build(); | ||
ColumnParsingException exception = | ||
assertThrows( | ||
ColumnParsingException.class, | ||
() -> ColumnUtils.createColumnFromValue(DataType.BLOB, columnInfo, value)); | ||
assertEquals( | ||
CoreError.DATA_LOADER_INVALID_BASE64_ENCODING_FOR_COLUMN_VALUE.buildMessage( | ||
columnName, "table", "ns"), | ||
exception.getMessage()); | ||
} | ||
} |
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.
Unnecessary empty line?