-
Notifications
You must be signed in to change notification settings - Fork 18
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
Implement two new codecs STRING_BLOB
and ASCII_BLOB
#311
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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 ASCII_BLOBCodec codec; | ||
|
||
@BeforeEach | ||
public void setup() { | ||
codec = new ASCII_BLOBCodec(null); | ||
} | ||
|
||
@Test | ||
public void encodeDecode() { | ||
ByteBuffer buffer = codec.encode(ByteBuffer.wrap("Encode this Text string to Blob".getBytes()), | ||
ProtocolVersion.V4); | ||
ByteBuffer retBuffer = codec.decode(buffer, ProtocolVersion.V4); | ||
assertEquals("'Encode this Text string to Blob'", codec.format(retBuffer)); | ||
assertEquals(retBuffer, codec.parse("Encode this Text string to Blob")); | ||
} | ||
|
||
} |
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.
nitpick: should we extract
Encode this Text string to Blob
as a constant and re-use it everywhere?