Search in sources :

Example 26 with DataType

use of com.scalar.db.io.DataType in project scalardb by scalar-labs.

the class KeyBytesEncoderTest method encode_TripleKeysGiven_ShouldEncodeProperlyWithPreservingSortOrder.

@Test
public void encode_TripleKeysGiven_ShouldEncodeProperlyWithPreservingSortOrder() {
    RANDOM.setSeed(seed);
    runTest(() -> {
        for (DataType col1Type : KEY_TYPES) {
            // the BLOB type is supported only for the last key
            if (col1Type == DataType.BLOB) {
                continue;
            }
            for (DataType col2Type : KEY_TYPES) {
                // the BLOB type is supported only for the last key
                if (col2Type == DataType.BLOB) {
                    continue;
                }
                for (DataType col3Type : KEY_TYPES) {
                    for (Order col1Order : ORDERS) {
                        for (Order col2Order : ORDERS) {
                            for (Order col3Order : ORDERS) {
                                encode_TripleKeysGiven_ShouldEncodeProperlyWithPreservingSortOrder(col1Type, col2Type, col3Type, col1Order, col2Order, col3Order);
                            }
                        }
                    }
                }
            }
        }
    });
}
Also used : Order(com.scalar.db.api.Scan.Ordering.Order) DataType(com.scalar.db.io.DataType) Test(org.junit.jupiter.api.Test)

Example 27 with DataType

use of com.scalar.db.io.DataType in project scalardb by scalar-labs.

the class JdbcAdmin method getTableMetadata.

@Override
public TableMetadata getTableMetadata(String namespace, String table) throws ExecutionException {
    TableMetadata.Builder builder = TableMetadata.newBuilder();
    boolean tableExists = false;
    try (Connection connection = dataSource.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(getSelectColumnsStatement())) {
        preparedStatement.setString(1, getFullTableName(namespace, table));
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                tableExists = true;
                String columnName = resultSet.getString(METADATA_COL_COLUMN_NAME);
                DataType dataType = DataType.valueOf(resultSet.getString(METADATA_COL_DATA_TYPE));
                builder.addColumn(columnName, dataType);
                boolean indexed = resultSet.getBoolean(METADATA_COL_INDEXED);
                if (indexed) {
                    builder.addSecondaryIndex(columnName);
                }
                String keyType = resultSet.getString(METADATA_COL_KEY_TYPE);
                if (keyType == null) {
                    continue;
                }
                switch(KeyType.valueOf(keyType)) {
                    case PARTITION:
                        builder.addPartitionKey(columnName);
                        break;
                    case CLUSTERING:
                        Scan.Ordering.Order clusteringOrder = Scan.Ordering.Order.valueOf(resultSet.getString(METADATA_COL_CLUSTERING_ORDER));
                        builder.addClusteringKey(columnName, clusteringOrder);
                        break;
                    default:
                        throw new AssertionError("invalid key type: " + keyType);
                }
            }
        }
    } catch (SQLException e) {
        throw new ExecutionException("getting a table metadata failed", e);
    }
    if (!tableExists) {
        return null;
    }
    return builder.build();
}
Also used : TableMetadata(com.scalar.db.api.TableMetadata) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) ResultSet(java.sql.ResultSet) Ordering(com.scalar.db.api.Scan.Ordering) DataType(com.scalar.db.io.DataType) Order(com.scalar.db.api.Scan.Ordering.Order) ExecutionException(com.scalar.db.exception.storage.ExecutionException)

Example 28 with DataType

use of com.scalar.db.io.DataType in project scalardb by scalar-labs.

the class JdbcAdmin method getVendorDbColumnType.

/**
 * Get the vendor DB data type that is equivalent to the Scalar DB data type
 *
 * @param metadata a table metadata
 * @param columnName a column name
 * @return a vendor DB data type
 */
@SuppressFBWarnings("NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE")
private String getVendorDbColumnType(TableMetadata metadata, String columnName) {
    HashSet<String> keysAndIndexes = Sets.newHashSet(Iterables.concat(metadata.getPartitionKeyNames(), metadata.getClusteringKeyNames(), metadata.getSecondaryIndexNames()));
    DataType scalarDbColumnType = metadata.getColumnDataType(columnName);
    if (keysAndIndexes.contains(columnName)) {
        return DATA_TYPE_MAPPING_FOR_KEY.get(rdbEngine).getOrDefault(scalarDbColumnType, DATA_TYPE_MAPPING.get(rdbEngine).get(scalarDbColumnType));
    } else {
        return DATA_TYPE_MAPPING.get(rdbEngine).get(scalarDbColumnType);
    }
}
Also used : DataType(com.scalar.db.io.DataType) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 29 with DataType

use of com.scalar.db.io.DataType in project scalardb by scalar-labs.

the class ResultInterpreter method convert.

private Column<?> convert(String name, ResultSet resultSet) throws SQLException {
    Column<?> ret;
    DataType dataType = metadata.getColumnDataType(name);
    switch(dataType) {
        case BOOLEAN:
            ret = BooleanColumn.of(name, resultSet.getBoolean(name));
            if (resultSet.wasNull()) {
                ret = BooleanColumn.ofNull(name);
            }
            break;
        case INT:
            ret = IntColumn.of(name, resultSet.getInt(name));
            if (resultSet.wasNull()) {
                ret = IntColumn.ofNull(name);
            }
            break;
        case BIGINT:
            ret = BigIntColumn.of(name, resultSet.getLong(name));
            if (resultSet.wasNull()) {
                ret = BigIntColumn.ofNull(name);
            }
            break;
        case FLOAT:
            // To handle Float.MAX_VALUE in MySQL, we need to get the value as double, then cast it to
            // float
            ret = FloatColumn.of(name, (float) resultSet.getDouble(name));
            if (resultSet.wasNull()) {
                ret = FloatColumn.ofNull(name);
            }
            break;
        case DOUBLE:
            ret = DoubleColumn.of(name, resultSet.getDouble(name));
            if (resultSet.wasNull()) {
                ret = DoubleColumn.ofNull(name);
            }
            break;
        case TEXT:
            ret = TextColumn.of(name, resultSet.getString(name));
            if (resultSet.wasNull()) {
                ret = TextColumn.ofNull(name);
            }
            break;
        case BLOB:
            ret = BlobColumn.of(name, resultSet.getBytes(name));
            if (resultSet.wasNull()) {
                ret = BlobColumn.ofNull(name);
            }
            break;
        default:
            throw new AssertionError();
    }
    return ret;
}
Also used : DataType(com.scalar.db.io.DataType)

Example 30 with DataType

use of com.scalar.db.io.DataType in project scalardb by scalar-labs.

the class DistributedStorageMultipleClusteringKeyScanIntegrationTestBase method dropTables.

private void dropTables() throws java.util.concurrent.ExecutionException, InterruptedException {
    List<Callable<Void>> testCallables = new ArrayList<>();
    for (DataType firstClusteringKeyType : clusteringKeyTypes.keySet()) {
        Callable<Void> testCallable = () -> {
            for (DataType secondClusteringKeyType : clusteringKeyTypes.get(firstClusteringKeyType)) {
                for (Order firstClusteringOrder : Order.values()) {
                    for (Order secondClusteringOrder : Order.values()) {
                        admin.dropTable(getNamespaceName(firstClusteringKeyType), getTableName(firstClusteringKeyType, firstClusteringOrder, secondClusteringKeyType, secondClusteringOrder));
                    }
                }
            }
            admin.dropNamespace(getNamespaceName(firstClusteringKeyType));
            return null;
        };
        testCallables.add(testCallable);
    }
    // We firstly execute the callables without the last one. And then we execute the last one. This
    // is because the last table deletion deletes the metadata table, and this process can't be
    // handled in multiple threads/processes at the same time.
    executeInParallel(testCallables.subList(0, testCallables.size() - 1));
    executeInParallel(testCallables.subList(testCallables.size() - 1, testCallables.size()));
}
Also used : Order(com.scalar.db.api.Scan.Ordering.Order) ArrayList(java.util.ArrayList) DataType(com.scalar.db.io.DataType) Callable(java.util.concurrent.Callable)

Aggregations

DataType (com.scalar.db.io.DataType)52 Order (com.scalar.db.api.Scan.Ordering.Order)30 Value (com.scalar.db.io.Value)18 Test (org.junit.jupiter.api.Test)16 Test (org.junit.Test)13 ArrayList (java.util.ArrayList)12 Callable (java.util.concurrent.Callable)12 Key (com.scalar.db.io.Key)6 Result (com.scalar.db.api.Result)5 Scan (com.scalar.db.api.Scan)3 Delete (com.scalar.db.api.Delete)2 Get (com.scalar.db.api.Get)2 TableMetadata (com.scalar.db.api.TableMetadata)2 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 Ordering (com.scalar.db.api.Scan.Ordering)1 ExecutionException (com.scalar.db.exception.storage.ExecutionException)1 BigIntValue (com.scalar.db.io.BigIntValue)1 BlobValue (com.scalar.db.io.BlobValue)1