Skip to content

Commit ba381c0

Browse files
pravinbhatmsmygit
andauthored
Implement two new codecs STRING_BLOB and ASCII_BLOB (#311)
* 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]>
1 parent 32ac93e commit ba381c0

File tree

12 files changed

+470
-1
lines changed

12 files changed

+470
-1
lines changed

RELEASE.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# Release Notes
2+
## [4.4.1] - 2024-09-20
3+
- 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+
25
## [4.4.0] - 2024-09-19
36
- 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.
47
- Switched to using Apache Cassandra® `5.0` docker image for testing
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright DataStax, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.datastax.cdm.cql.codec;
17+
18+
import java.nio.ByteBuffer;
19+
20+
import org.jetbrains.annotations.NotNull;
21+
22+
import com.datastax.cdm.properties.PropertyHelper;
23+
import com.datastax.oss.driver.api.core.ProtocolVersion;
24+
import com.datastax.oss.driver.api.core.type.DataType;
25+
import com.datastax.oss.driver.api.core.type.DataTypes;
26+
import com.datastax.oss.driver.api.core.type.codec.TypeCodecs;
27+
import com.datastax.oss.driver.api.core.type.reflect.GenericType;
28+
29+
public class ASCII_BLOBCodec extends AbstractBaseCodec<ByteBuffer> {
30+
31+
public ASCII_BLOBCodec(PropertyHelper propertyHelper) {
32+
super(propertyHelper);
33+
}
34+
35+
@Override
36+
public @NotNull GenericType<ByteBuffer> getJavaType() {
37+
return GenericType.BYTE_BUFFER;
38+
}
39+
40+
@Override
41+
public @NotNull DataType getCqlType() {
42+
return DataTypes.ASCII;
43+
}
44+
45+
@Override
46+
public ByteBuffer encode(ByteBuffer value, @NotNull ProtocolVersion protocolVersion) {
47+
if (value == null) {
48+
return null;
49+
} else {
50+
String stringVal = new String(value.array());
51+
return TypeCodecs.ASCII.encode(stringVal, protocolVersion);
52+
}
53+
}
54+
55+
@Override
56+
public ByteBuffer decode(ByteBuffer bytes, @NotNull ProtocolVersion protocolVersion) {
57+
String stringValue = TypeCodecs.ASCII.decode(bytes, protocolVersion);
58+
return ByteBuffer.wrap(stringValue.getBytes());
59+
}
60+
61+
@Override
62+
public @NotNull String format(ByteBuffer value) {
63+
String stringVal = new String(value.array());
64+
return TypeCodecs.ASCII.format(stringVal);
65+
}
66+
67+
@Override
68+
public ByteBuffer parse(String value) {
69+
return ByteBuffer.wrap(value.getBytes());
70+
}
71+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright DataStax, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.datastax.cdm.cql.codec;
17+
18+
import java.nio.ByteBuffer;
19+
20+
import org.jetbrains.annotations.NotNull;
21+
22+
import com.datastax.cdm.properties.PropertyHelper;
23+
import com.datastax.oss.driver.api.core.ProtocolVersion;
24+
import com.datastax.oss.driver.api.core.type.DataType;
25+
import com.datastax.oss.driver.api.core.type.DataTypes;
26+
import com.datastax.oss.driver.api.core.type.codec.TypeCodecs;
27+
import com.datastax.oss.driver.api.core.type.reflect.GenericType;
28+
29+
public class BLOB_ASCIICodec extends AbstractBaseCodec<String> {
30+
31+
public BLOB_ASCIICodec(PropertyHelper propertyHelper) {
32+
super(propertyHelper);
33+
}
34+
35+
@Override
36+
public @NotNull GenericType<String> getJavaType() {
37+
return GenericType.STRING;
38+
}
39+
40+
@Override
41+
public @NotNull DataType getCqlType() {
42+
return DataTypes.BLOB;
43+
}
44+
45+
@Override
46+
public ByteBuffer encode(String value, @NotNull ProtocolVersion protocolVersion) {
47+
if (value == null) {
48+
return null;
49+
} else {
50+
return TypeCodecs.BLOB.encode(ByteBuffer.wrap(value.getBytes()), protocolVersion);
51+
}
52+
}
53+
54+
@Override
55+
public String decode(ByteBuffer bytes, @NotNull ProtocolVersion protocolVersion) {
56+
return TypeCodecs.ASCII.decode(bytes, protocolVersion);
57+
}
58+
59+
@Override
60+
public @NotNull String format(String value) {
61+
ByteBuffer bb = ByteBuffer.wrap(value.getBytes());
62+
return TypeCodecs.BLOB.format(bb);
63+
}
64+
65+
@Override
66+
public String parse(String value) {
67+
ByteBuffer bb = TypeCodecs.BLOB.parse(value);
68+
return bb == null ? null : bb.toString();
69+
}
70+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright DataStax, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.datastax.cdm.cql.codec;
17+
18+
import java.nio.ByteBuffer;
19+
20+
import org.jetbrains.annotations.NotNull;
21+
22+
import com.datastax.cdm.properties.PropertyHelper;
23+
import com.datastax.oss.driver.api.core.ProtocolVersion;
24+
import com.datastax.oss.driver.api.core.type.DataType;
25+
import com.datastax.oss.driver.api.core.type.DataTypes;
26+
import com.datastax.oss.driver.api.core.type.codec.TypeCodecs;
27+
import com.datastax.oss.driver.api.core.type.reflect.GenericType;
28+
29+
public class BLOB_TEXTCodec extends AbstractBaseCodec<String> {
30+
31+
public BLOB_TEXTCodec(PropertyHelper propertyHelper) {
32+
super(propertyHelper);
33+
}
34+
35+
@Override
36+
public @NotNull GenericType<String> getJavaType() {
37+
return GenericType.STRING;
38+
}
39+
40+
@Override
41+
public @NotNull DataType getCqlType() {
42+
return DataTypes.BLOB;
43+
}
44+
45+
@Override
46+
public ByteBuffer encode(String value, @NotNull ProtocolVersion protocolVersion) {
47+
if (value == null) {
48+
return null;
49+
} else {
50+
return TypeCodecs.BLOB.encode(ByteBuffer.wrap(value.getBytes()), protocolVersion);
51+
}
52+
}
53+
54+
@Override
55+
public String decode(ByteBuffer bytes, @NotNull ProtocolVersion protocolVersion) {
56+
return TypeCodecs.TEXT.decode(bytes, protocolVersion);
57+
}
58+
59+
@Override
60+
public @NotNull String format(String value) {
61+
ByteBuffer bb = ByteBuffer.wrap(value.getBytes());
62+
return TypeCodecs.BLOB.format(bb);
63+
}
64+
65+
@Override
66+
public String parse(String value) {
67+
ByteBuffer bb = TypeCodecs.BLOB.parse(value);
68+
return bb == null ? null : bb.toString();
69+
}
70+
}

src/main/java/com/datastax/cdm/cql/codec/CodecFactory.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public static List<TypeCodec<?>> getCodecPair(PropertyHelper propertyHelper, Cod
3434
return Arrays.asList(new DOUBLE_StringCodec(propertyHelper), new TEXT_DoubleCodec(propertyHelper));
3535
case BIGINT_STRING:
3636
return Arrays.asList(new BIGINT_StringCodec(propertyHelper), new TEXT_LongCodec(propertyHelper));
37+
case STRING_BLOB:
38+
return Arrays.asList(new TEXT_BLOBCodec(propertyHelper), new BLOB_TEXTCodec(propertyHelper));
39+
case ASCII_BLOB:
40+
return Arrays.asList(new ASCII_BLOBCodec(propertyHelper), new BLOB_ASCIICodec(propertyHelper));
3741
case DECIMAL_STRING:
3842
return Arrays.asList(new DECIMAL_StringCodec(propertyHelper), new TEXT_BigDecimalCodec(propertyHelper));
3943
case TIMESTAMP_STRING_MILLIS:

src/main/java/com/datastax/cdm/cql/codec/Codecset.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@
1717

1818
public enum Codecset {
1919
INT_STRING, DOUBLE_STRING, BIGINT_STRING, DECIMAL_STRING, TIMESTAMP_STRING_MILLIS, TIMESTAMP_STRING_FORMAT,
20-
POINT_TYPE, POLYGON_TYPE, DATE_RANGE, LINE_STRING
20+
POINT_TYPE, POLYGON_TYPE, DATE_RANGE, LINE_STRING, STRING_BLOB, ASCII_BLOB
2121
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright DataStax, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.datastax.cdm.cql.codec;
17+
18+
import java.nio.ByteBuffer;
19+
20+
import org.jetbrains.annotations.NotNull;
21+
22+
import com.datastax.cdm.properties.PropertyHelper;
23+
import com.datastax.oss.driver.api.core.ProtocolVersion;
24+
import com.datastax.oss.driver.api.core.type.DataType;
25+
import com.datastax.oss.driver.api.core.type.DataTypes;
26+
import com.datastax.oss.driver.api.core.type.codec.TypeCodecs;
27+
import com.datastax.oss.driver.api.core.type.reflect.GenericType;
28+
29+
public class TEXT_BLOBCodec extends AbstractBaseCodec<ByteBuffer> {
30+
31+
public TEXT_BLOBCodec(PropertyHelper propertyHelper) {
32+
super(propertyHelper);
33+
}
34+
35+
@Override
36+
public @NotNull GenericType<ByteBuffer> getJavaType() {
37+
return GenericType.BYTE_BUFFER;
38+
}
39+
40+
@Override
41+
public @NotNull DataType getCqlType() {
42+
return DataTypes.TEXT;
43+
}
44+
45+
@Override
46+
public ByteBuffer encode(ByteBuffer value, @NotNull ProtocolVersion protocolVersion) {
47+
if (value == null) {
48+
return null;
49+
} else {
50+
String stringVal = new String(value.array());
51+
return TypeCodecs.TEXT.encode(stringVal, protocolVersion);
52+
}
53+
}
54+
55+
@Override
56+
public ByteBuffer decode(ByteBuffer bytes, @NotNull ProtocolVersion protocolVersion) {
57+
String stringValue = TypeCodecs.TEXT.decode(bytes, protocolVersion);
58+
return ByteBuffer.wrap(stringValue.getBytes());
59+
}
60+
61+
@Override
62+
public @NotNull String format(ByteBuffer value) {
63+
String stringVal = new String(value.array());
64+
return TypeCodecs.TEXT.format(stringVal);
65+
}
66+
67+
@Override
68+
public ByteBuffer parse(String value) {
69+
return ByteBuffer.wrap(value.getBytes());
70+
}
71+
}

src/resources/cdm-detailed.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,8 @@ spark.cdm.perfops.ratelimit.target 20000
268268
# DOUBLE_STRING : double stored in a String
269269
# BIGINT_STRING : bigint stored in a String
270270
# DECIMAL_STRING : decimal stored in a String
271+
# STRING_BLOB : TEXT stored in a Blob
272+
# ASCII_BLOB : ASCII stored in a Blob
271273
# TIMESTAMP_STRING_MILLIS : timestamp stored in a String,
272274
# as Epoch milliseconds
273275
# TIMESTAMP_STRING_FORMAT : timestamp stored in a String,
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright DataStax, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.datastax.cdm.cql.codec;
17+
18+
import static org.junit.jupiter.api.Assertions.assertEquals;
19+
20+
import java.nio.ByteBuffer;
21+
22+
import org.junit.jupiter.api.BeforeEach;
23+
import org.junit.jupiter.api.Test;
24+
25+
import com.datastax.oss.driver.api.core.ProtocolVersion;
26+
27+
public class ASCII_BLOBCodecTest {
28+
private final String INPUT = "Encode this Text string to Blob";
29+
30+
private ASCII_BLOBCodec codec;
31+
32+
@BeforeEach
33+
public void setup() {
34+
codec = new ASCII_BLOBCodec(null);
35+
}
36+
37+
@Test
38+
public void encodeDecode() {
39+
ByteBuffer buffer = codec.encode(ByteBuffer.wrap(INPUT.getBytes()), ProtocolVersion.V4);
40+
ByteBuffer retBuffer = codec.decode(buffer, ProtocolVersion.V4);
41+
assertEquals("'" + INPUT + "'", codec.format(retBuffer));
42+
assertEquals(retBuffer, codec.parse(INPUT));
43+
}
44+
45+
}

0 commit comments

Comments
 (0)