-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
32ac93e
commit ba381c0
Showing
12 changed files
with
470 additions
and
1 deletion.
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
71 changes: 71 additions & 0 deletions
71
src/main/java/com/datastax/cdm/cql/codec/ASCII_BLOBCodec.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,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
70
src/main/java/com/datastax/cdm/cql/codec/BLOB_ASCIICodec.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,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
70
src/main/java/com/datastax/cdm/cql/codec/BLOB_TEXTCodec.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,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(); | ||
} | ||
} |
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
71 changes: 71 additions & 0 deletions
71
src/main/java/com/datastax/cdm/cql/codec/TEXT_BLOBCodec.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,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()); | ||
} | ||
} |
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
45 changes: 45 additions & 0 deletions
45
src/test/java/com/datastax/cdm/cql/codec/ASCII_BLOBCodecTest.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,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)); | ||
} | ||
|
||
} |
Oops, something went wrong.