-
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 TableMetadataService and utils #1772
Draft
ypeckstadt
wants to merge
6
commits into
master
Choose a base branch
from
feat/data-loader/tablemetadata-service
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.
Draft
Changes from all commits
Commits
Show all changes
6 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 cee97dc
Add TableMetadataService and 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
1 change: 1 addition & 0 deletions
1
...oader/cli/src/main/java/com/scalar/db/dataloader/cli/command/ColumnKeyValueConverter.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
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
2 changes: 1 addition & 1 deletion
2
...ataloader/cli/command/ColumnKeyValue.java → ...ar/db/dataloader/core/ColumnKeyValue.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
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); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...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,12 @@ | ||
package com.scalar.db.dataloader.core.exception; | ||
|
||
public class KeyParsingException extends Exception { | ||
|
||
public KeyParsingException(String message) { | ||
super(message); | ||
} | ||
|
||
public KeyParsingException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...ore/src/main/java/com/scalar/db/dataloader/core/tablemetadata/TableMetadataException.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.tablemetadata; | ||
|
||
/** A custom exception that encapsulates errors thrown by the TableMetaDataService */ | ||
public class TableMetadataException extends Exception { | ||
|
||
/** | ||
* Class constructor | ||
* | ||
* @param message error message | ||
* @param cause reason for exception | ||
*/ | ||
public TableMetadataException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
/** | ||
* Class constructor | ||
* | ||
* @param message error message | ||
*/ | ||
public TableMetadataException(String message) { | ||
super(message); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
.../core/src/main/java/com/scalar/db/dataloader/core/tablemetadata/TableMetadataRequest.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,27 @@ | ||
package com.scalar.db.dataloader.core.tablemetadata; | ||
|
||
/** Represents the request for metadata for a single ScalarDB table */ | ||
public class TableMetadataRequest { | ||
|
||
private final String namespace; | ||
private final String tableName; | ||
|
||
/** | ||
* Class constructor | ||
* | ||
* @param namespace ScalarDB namespace | ||
* @param tableName ScalarDB table name | ||
*/ | ||
public TableMetadataRequest(String namespace, String tableName) { | ||
this.namespace = namespace; | ||
this.tableName = tableName; | ||
} | ||
|
||
public String getNamespace() { | ||
return namespace; | ||
} | ||
|
||
public String getTableName() { | ||
return tableName; | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
.../core/src/main/java/com/scalar/db/dataloader/core/tablemetadata/TableMetadataService.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,73 @@ | ||
package com.scalar.db.dataloader.core.tablemetadata; | ||
|
||
import com.scalar.db.api.TableMetadata; | ||
import com.scalar.db.common.error.CoreError; | ||
import com.scalar.db.dataloader.core.util.TableMetadataUtils; | ||
import com.scalar.db.exception.storage.ExecutionException; | ||
import com.scalar.db.service.StorageFactory; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* A service class that provides methods to get TableMetadata for a given namespace and table name. | ||
*/ | ||
public class TableMetadataService { | ||
|
||
private final StorageFactory storageFactory; | ||
|
||
/** | ||
* Class constructor | ||
* | ||
* @param storageFactory a distributed storage admin | ||
*/ | ||
public TableMetadataService(StorageFactory storageFactory) { | ||
this.storageFactory = storageFactory; | ||
} | ||
|
||
/** | ||
* Returns the TableMetadata for the given namespace and table name. | ||
* | ||
* @param namespace ScalarDb namespace | ||
* @param tableName ScalarDb table name | ||
* @return TableMetadata | ||
* @throws TableMetadataException if the namespace or table is missing | ||
*/ | ||
public TableMetadata getTableMetadata(String namespace, String tableName) | ||
throws TableMetadataException { | ||
try { | ||
TableMetadata tableMetadata = | ||
storageFactory.getStorageAdmin().getTableMetadata(namespace, tableName); | ||
if (tableMetadata == null) { | ||
throw new TableMetadataException( | ||
CoreError.DATA_LOADER_MISSING_NAMESPACE_OR_TABLE.buildMessage(namespace, tableName)); | ||
} | ||
return tableMetadata; | ||
} catch (ExecutionException e) { | ||
throw new TableMetadataException( | ||
CoreError.DATA_LOADER_MISSING_NAMESPACE_OR_TABLE.buildMessage(namespace, tableName)); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the TableMetadata for the given list of TableMetadataRequest. | ||
* | ||
* @param requests List of TableMetadataRequest | ||
* @return Map of TableMetadata | ||
* @throws TableMetadataException if the namespace or table is missing | ||
*/ | ||
public Map<String, TableMetadata> getTableMetadata(Collection<TableMetadataRequest> requests) | ||
throws TableMetadataException { | ||
Map<String, TableMetadata> metadataMap = new HashMap<>(); | ||
|
||
for (TableMetadataRequest request : requests) { | ||
String namespace = request.getNamespace(); | ||
String tableName = request.getTableName(); | ||
TableMetadata tableMetadata = getTableMetadata(namespace, tableName); | ||
String key = TableMetadataUtils.getTableLookupKey(namespace, tableName); | ||
metadataMap.put(key, tableMetadata); | ||
} | ||
|
||
return metadataMap; | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
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,74 @@ | ||
package com.scalar.db.dataloader.core.util; | ||
|
||
import com.scalar.db.common.error.CoreError; | ||
import com.scalar.db.dataloader.core.exception.Base64Exception; | ||
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; | ||
|
||
/** Utility class for dealing and creating ScalarDB Columns */ | ||
public final class ColumnUtils { | ||
private ColumnUtils() { | ||
// restrict instantiation | ||
} | ||
|
||
/** | ||
* 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 columnName ScalarDB table column name | ||
* @param value Value for the ScalarDB column | ||
* @return ScalarDB column | ||
* @throws Base64Exception if an error occurs while base64 decoding | ||
*/ | ||
public static Column<?> createColumnFromValue(DataType dataType, String columnName, String value) | ||
throws Base64Exception { | ||
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 NumberFormatException( | ||
CoreError.DATA_LOADER_INVALID_NUMBER_FORMAT_FOR_COLUMN_VALUE.buildMessage(columnName)); | ||
} catch (IllegalArgumentException e) { | ||
throw new Base64Exception( | ||
CoreError.DATA_LOADER_INVALID_BASE64_ENCODING_FOR_COLUMN_VALUE.buildMessage(columnName)); | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
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,63 @@ | ||
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.ColumnKeyValue; | ||
import com.scalar.db.dataloader.core.exception.Base64Exception; | ||
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 { | ||
|
||
private KeyUtils() { | ||
// restrict instantiation | ||
} | ||
|
||
/** | ||
* Convert a keyValue, in the format of <key>=<value>, to a ScalarDB Key instance. | ||
* | ||
* @param columnKeyValue A key value in the format of <key>=<value> | ||
* @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 | ||
*/ | ||
public static Key parseKeyValue( | ||
@Nullable ColumnKeyValue columnKeyValue, 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_KEY_PARSING_FAILED.buildMessage(columnName)); | ||
} | ||
try { | ||
return createKey(columnDataType, columnName, columnKeyValue.getColumnValue()); | ||
} catch (Base64Exception e) { | ||
throw new KeyParsingException( | ||
CoreError.DATA_LOADER_INVALID_VALUE_KEY_PARSING_FAILED.buildMessage( | ||
columnKeyValue.getColumnValue(), e.getMessage())); | ||
} | ||
} | ||
|
||
/** | ||
* Create a ScalarDB key based on the provided data type, column name, and value. | ||
* | ||
* @param dataType Data type of the specified column | ||
* @param columnName ScalarDB table column name | ||
* @param value Value for ScalarDB key | ||
* @return ScalarDB Key instance | ||
* @throws Base64Exception if there is an error creating the key value | ||
*/ | ||
public static Key createKey(DataType dataType, String columnName, String value) | ||
throws Base64Exception { | ||
Column<?> keyValue = ColumnUtils.createColumnFromValue(dataType, columnName, value); | ||
return Key.newBuilder().add(keyValue).build(); | ||
} | ||
} |
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.