Skip to content

Commit

Permalink
Implement two new codecs STRING_BLOB and ASCII_BLOB (#311)
Browse files Browse the repository at this point in the history
* Implemented two new codecs `STRING_BLOB` and `ASCII_BLOB` to allow migration from `TEXT` and `ASCII` fields to `BLOB` fields
* Reuse constants for assertions in tests
---------
Co-authored-by: Madhavan Sridharan <[email protected]>
  • Loading branch information
pravinbhat authored Sep 19, 2024
1 parent 32ac93e commit ba381c0
Show file tree
Hide file tree
Showing 12 changed files with 470 additions and 1 deletion.
3 changes: 3 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Release Notes
## [4.4.1] - 2024-09-20
- Added two new codecs `STRING_BLOB` and `ASCII_BLOB` to allow migration from `TEXT` and `ASCII` fields to `BLOB` fields. These codecs can also be used to convert `BLOB` to `TEXT` or `ASCII`, but in such cases the `BLOB` value must be TEXT based in nature & fit within the applicable limits.

## [4.4.0] - 2024-09-19
- Added property `spark.cdm.connect.origin.tls.isAstra` and `spark.cdm.connect.target.tls.isAstra` to allow connecting to Astra DB without using [SCB](https://docs.datastax.com/en/astra-db-serverless/drivers/secure-connect-bundle.html). This may be needed for enterprises that may find credentials packaged within SCB as a security risk. TLS properties can now be passed as params OR wrapper scripts (not included) could be used to pull sensitive credentials from a vault service in real-time & pass them to CDM.
- Switched to using Apache Cassandra® `5.0` docker image for testing
Expand Down
71 changes: 71 additions & 0 deletions src/main/java/com/datastax/cdm/cql/codec/ASCII_BLOBCodec.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright DataStax, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.datastax.cdm.cql.codec;

import java.nio.ByteBuffer;

import org.jetbrains.annotations.NotNull;

import com.datastax.cdm.properties.PropertyHelper;
import com.datastax.oss.driver.api.core.ProtocolVersion;
import com.datastax.oss.driver.api.core.type.DataType;
import com.datastax.oss.driver.api.core.type.DataTypes;
import com.datastax.oss.driver.api.core.type.codec.TypeCodecs;
import com.datastax.oss.driver.api.core.type.reflect.GenericType;

public class ASCII_BLOBCodec extends AbstractBaseCodec<ByteBuffer> {

public ASCII_BLOBCodec(PropertyHelper propertyHelper) {
super(propertyHelper);
}

@Override
public @NotNull GenericType<ByteBuffer> getJavaType() {
return GenericType.BYTE_BUFFER;
}

@Override
public @NotNull DataType getCqlType() {
return DataTypes.ASCII;
}

@Override
public ByteBuffer encode(ByteBuffer value, @NotNull ProtocolVersion protocolVersion) {
if (value == null) {
return null;
} else {
String stringVal = new String(value.array());
return TypeCodecs.ASCII.encode(stringVal, protocolVersion);
}
}

@Override
public ByteBuffer decode(ByteBuffer bytes, @NotNull ProtocolVersion protocolVersion) {
String stringValue = TypeCodecs.ASCII.decode(bytes, protocolVersion);
return ByteBuffer.wrap(stringValue.getBytes());
}

@Override
public @NotNull String format(ByteBuffer value) {
String stringVal = new String(value.array());
return TypeCodecs.ASCII.format(stringVal);
}

@Override
public ByteBuffer parse(String value) {
return ByteBuffer.wrap(value.getBytes());
}
}
70 changes: 70 additions & 0 deletions src/main/java/com/datastax/cdm/cql/codec/BLOB_ASCIICodec.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright DataStax, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.datastax.cdm.cql.codec;

import java.nio.ByteBuffer;

import org.jetbrains.annotations.NotNull;

import com.datastax.cdm.properties.PropertyHelper;
import com.datastax.oss.driver.api.core.ProtocolVersion;
import com.datastax.oss.driver.api.core.type.DataType;
import com.datastax.oss.driver.api.core.type.DataTypes;
import com.datastax.oss.driver.api.core.type.codec.TypeCodecs;
import com.datastax.oss.driver.api.core.type.reflect.GenericType;

public class BLOB_ASCIICodec extends AbstractBaseCodec<String> {

public BLOB_ASCIICodec(PropertyHelper propertyHelper) {
super(propertyHelper);
}

@Override
public @NotNull GenericType<String> getJavaType() {
return GenericType.STRING;
}

@Override
public @NotNull DataType getCqlType() {
return DataTypes.BLOB;
}

@Override
public ByteBuffer encode(String value, @NotNull ProtocolVersion protocolVersion) {
if (value == null) {
return null;
} else {
return TypeCodecs.BLOB.encode(ByteBuffer.wrap(value.getBytes()), protocolVersion);
}
}

@Override
public String decode(ByteBuffer bytes, @NotNull ProtocolVersion protocolVersion) {
return TypeCodecs.ASCII.decode(bytes, protocolVersion);
}

@Override
public @NotNull String format(String value) {
ByteBuffer bb = ByteBuffer.wrap(value.getBytes());
return TypeCodecs.BLOB.format(bb);
}

@Override
public String parse(String value) {
ByteBuffer bb = TypeCodecs.BLOB.parse(value);
return bb == null ? null : bb.toString();
}
}
70 changes: 70 additions & 0 deletions src/main/java/com/datastax/cdm/cql/codec/BLOB_TEXTCodec.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright DataStax, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.datastax.cdm.cql.codec;

import java.nio.ByteBuffer;

import org.jetbrains.annotations.NotNull;

import com.datastax.cdm.properties.PropertyHelper;
import com.datastax.oss.driver.api.core.ProtocolVersion;
import com.datastax.oss.driver.api.core.type.DataType;
import com.datastax.oss.driver.api.core.type.DataTypes;
import com.datastax.oss.driver.api.core.type.codec.TypeCodecs;
import com.datastax.oss.driver.api.core.type.reflect.GenericType;

public class BLOB_TEXTCodec extends AbstractBaseCodec<String> {

public BLOB_TEXTCodec(PropertyHelper propertyHelper) {
super(propertyHelper);
}

@Override
public @NotNull GenericType<String> getJavaType() {
return GenericType.STRING;
}

@Override
public @NotNull DataType getCqlType() {
return DataTypes.BLOB;
}

@Override
public ByteBuffer encode(String value, @NotNull ProtocolVersion protocolVersion) {
if (value == null) {
return null;
} else {
return TypeCodecs.BLOB.encode(ByteBuffer.wrap(value.getBytes()), protocolVersion);
}
}

@Override
public String decode(ByteBuffer bytes, @NotNull ProtocolVersion protocolVersion) {
return TypeCodecs.TEXT.decode(bytes, protocolVersion);
}

@Override
public @NotNull String format(String value) {
ByteBuffer bb = ByteBuffer.wrap(value.getBytes());
return TypeCodecs.BLOB.format(bb);
}

@Override
public String parse(String value) {
ByteBuffer bb = TypeCodecs.BLOB.parse(value);
return bb == null ? null : bb.toString();
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/datastax/cdm/cql/codec/CodecFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public static List<TypeCodec<?>> getCodecPair(PropertyHelper propertyHelper, Cod
return Arrays.asList(new DOUBLE_StringCodec(propertyHelper), new TEXT_DoubleCodec(propertyHelper));
case BIGINT_STRING:
return Arrays.asList(new BIGINT_StringCodec(propertyHelper), new TEXT_LongCodec(propertyHelper));
case STRING_BLOB:
return Arrays.asList(new TEXT_BLOBCodec(propertyHelper), new BLOB_TEXTCodec(propertyHelper));
case ASCII_BLOB:
return Arrays.asList(new ASCII_BLOBCodec(propertyHelper), new BLOB_ASCIICodec(propertyHelper));
case DECIMAL_STRING:
return Arrays.asList(new DECIMAL_StringCodec(propertyHelper), new TEXT_BigDecimalCodec(propertyHelper));
case TIMESTAMP_STRING_MILLIS:
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/datastax/cdm/cql/codec/Codecset.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@

public enum Codecset {
INT_STRING, DOUBLE_STRING, BIGINT_STRING, DECIMAL_STRING, TIMESTAMP_STRING_MILLIS, TIMESTAMP_STRING_FORMAT,
POINT_TYPE, POLYGON_TYPE, DATE_RANGE, LINE_STRING
POINT_TYPE, POLYGON_TYPE, DATE_RANGE, LINE_STRING, STRING_BLOB, ASCII_BLOB
}
71 changes: 71 additions & 0 deletions src/main/java/com/datastax/cdm/cql/codec/TEXT_BLOBCodec.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright DataStax, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.datastax.cdm.cql.codec;

import java.nio.ByteBuffer;

import org.jetbrains.annotations.NotNull;

import com.datastax.cdm.properties.PropertyHelper;
import com.datastax.oss.driver.api.core.ProtocolVersion;
import com.datastax.oss.driver.api.core.type.DataType;
import com.datastax.oss.driver.api.core.type.DataTypes;
import com.datastax.oss.driver.api.core.type.codec.TypeCodecs;
import com.datastax.oss.driver.api.core.type.reflect.GenericType;

public class TEXT_BLOBCodec extends AbstractBaseCodec<ByteBuffer> {

public TEXT_BLOBCodec(PropertyHelper propertyHelper) {
super(propertyHelper);
}

@Override
public @NotNull GenericType<ByteBuffer> getJavaType() {
return GenericType.BYTE_BUFFER;
}

@Override
public @NotNull DataType getCqlType() {
return DataTypes.TEXT;
}

@Override
public ByteBuffer encode(ByteBuffer value, @NotNull ProtocolVersion protocolVersion) {
if (value == null) {
return null;
} else {
String stringVal = new String(value.array());
return TypeCodecs.TEXT.encode(stringVal, protocolVersion);
}
}

@Override
public ByteBuffer decode(ByteBuffer bytes, @NotNull ProtocolVersion protocolVersion) {
String stringValue = TypeCodecs.TEXT.decode(bytes, protocolVersion);
return ByteBuffer.wrap(stringValue.getBytes());
}

@Override
public @NotNull String format(ByteBuffer value) {
String stringVal = new String(value.array());
return TypeCodecs.TEXT.format(stringVal);
}

@Override
public ByteBuffer parse(String value) {
return ByteBuffer.wrap(value.getBytes());
}
}
2 changes: 2 additions & 0 deletions src/resources/cdm-detailed.properties
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ spark.cdm.perfops.ratelimit.target 20000
# DOUBLE_STRING : double stored in a String
# BIGINT_STRING : bigint stored in a String
# DECIMAL_STRING : decimal stored in a String
# STRING_BLOB : TEXT stored in a Blob
# ASCII_BLOB : ASCII stored in a Blob
# TIMESTAMP_STRING_MILLIS : timestamp stored in a String,
# as Epoch milliseconds
# TIMESTAMP_STRING_FORMAT : timestamp stored in a String,
Expand Down
45 changes: 45 additions & 0 deletions src/test/java/com/datastax/cdm/cql/codec/ASCII_BLOBCodecTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright DataStax, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.datastax.cdm.cql.codec;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.nio.ByteBuffer;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import com.datastax.oss.driver.api.core.ProtocolVersion;

public class ASCII_BLOBCodecTest {
private final String INPUT = "Encode this Text string to Blob";

private ASCII_BLOBCodec codec;

@BeforeEach
public void setup() {
codec = new ASCII_BLOBCodec(null);
}

@Test
public void encodeDecode() {
ByteBuffer buffer = codec.encode(ByteBuffer.wrap(INPUT.getBytes()), ProtocolVersion.V4);
ByteBuffer retBuffer = codec.decode(buffer, ProtocolVersion.V4);
assertEquals("'" + INPUT + "'", codec.format(retBuffer));
assertEquals(retBuffer, codec.parse(INPUT));
}

}
Loading

0 comments on commit ba381c0

Please sign in to comment.