forked from ing-bank/cassandra-jdbc-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'refs/heads/DataGrip/add-missing-codecs' into DataGrip/r…
…elease/next
- Loading branch information
Showing
20 changed files
with
807 additions
and
98 deletions.
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
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
56 changes: 56 additions & 0 deletions
56
src/main/java/com/ing/data/cassandra/jdbc/codec/BaseLongCodec.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,56 @@ | ||
package com.ing.data.cassandra.jdbc.codec; | ||
|
||
import com.datastax.oss.driver.api.core.ProtocolVersion; | ||
import com.datastax.oss.driver.api.core.type.codec.PrimitiveLongCodec; | ||
import com.datastax.oss.driver.api.core.type.reflect.GenericType; | ||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
import edu.umd.cs.findbugs.annotations.Nullable; | ||
|
||
import java.nio.ByteBuffer; | ||
|
||
/** | ||
* @author Liudmila Kornilova | ||
**/ | ||
public abstract class BaseLongCodec implements PrimitiveLongCodec { | ||
abstract int getNumberOfBytes(); | ||
|
||
abstract void serializeNoBoxingInner(long value, ByteBuffer bb); | ||
|
||
abstract long deserializeNoBoxingInner(ByteBuffer bytes); | ||
|
||
@NonNull | ||
@Override | ||
public GenericType<Long> getJavaType() { | ||
return GenericType.LONG; | ||
} | ||
|
||
@Override | ||
public Long parse(String value) { | ||
return value == null || value.isEmpty() || value.equalsIgnoreCase("NULL") ? null : Long.parseLong(value); | ||
} | ||
|
||
@Override | ||
@NonNull | ||
public String format(@Nullable Long value) { | ||
if (value == null) return "NULL"; | ||
return Long.toString(value); | ||
} | ||
|
||
@Override | ||
public ByteBuffer encodePrimitive(long value, @NonNull ProtocolVersion protocolVersion) { | ||
ByteBuffer bb = ByteBuffer.allocate(getNumberOfBytes()); | ||
serializeNoBoxingInner(value, bb); | ||
bb.flip(); | ||
return bb; | ||
} | ||
|
||
@Override | ||
public long decodePrimitive(ByteBuffer bytes, @NonNull ProtocolVersion protocolVersion) { | ||
if (bytes == null || bytes.remaining() == 0) return 0; | ||
if (bytes.remaining() != getNumberOfBytes()) { | ||
throw new IllegalStateException("Invalid value, expecting " + getNumberOfBytes() + " bytes but got " + bytes.remaining()); | ||
} | ||
|
||
return deserializeNoBoxingInner(bytes); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/com/ing/data/cassandra/jdbc/codec/BlobToByteArrayCodec.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,53 @@ | ||
package com.ing.data.cassandra.jdbc.codec; | ||
|
||
|
||
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.TypeCodec; | ||
import com.datastax.oss.driver.api.core.type.reflect.GenericType; | ||
import com.datastax.oss.protocol.internal.util.Bytes; | ||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
|
||
import java.nio.ByteBuffer; | ||
|
||
/** | ||
* @author Liudmila Kornilova | ||
**/ | ||
public class BlobToByteArrayCodec implements TypeCodec<byte[]> { | ||
@NonNull | ||
@Override | ||
public DataType getCqlType() { | ||
return DataTypes.BLOB; | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public GenericType<byte[]> getJavaType() { | ||
return GenericType.of(byte[].class); | ||
} | ||
|
||
@Override | ||
public ByteBuffer encode(byte[] value, @NonNull ProtocolVersion protocolVersion) { | ||
return value == null ? null : ByteBuffer.wrap(value); | ||
} | ||
|
||
@Override | ||
public byte[] decode(ByteBuffer bytes, @NonNull ProtocolVersion protocolVersion) { | ||
return bytes == null ? null : Bytes.getArray(bytes); | ||
} | ||
|
||
@Override | ||
public byte[] parse(String value) { | ||
return value == null || value.isEmpty() || value.equalsIgnoreCase("NULL") | ||
? null | ||
: Bytes.getArray(Bytes.fromHexString(value)); | ||
} | ||
|
||
@Override | ||
@NonNull | ||
public String format(byte[] value) { | ||
if (value == null) return "NULL"; | ||
return Bytes.toHexString(value); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/main/java/com/ing/data/cassandra/jdbc/codec/DateToDateCodec.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,58 @@ | ||
package com.ing.data.cassandra.jdbc.codec; | ||
|
||
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.TypeCodec; | ||
import com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry; | ||
import com.datastax.oss.driver.api.core.type.reflect.GenericType; | ||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.sql.Date; | ||
import java.time.Instant; | ||
import java.time.LocalDate; | ||
|
||
/** | ||
* @author Liudmila Kornilova | ||
**/ | ||
public class DateToDateCodec implements TypeCodec<Date> { | ||
private final TypeCodec<LocalDate> dateCodec = CodecRegistry.DEFAULT.codecFor(DataTypes.DATE, LocalDate.class); | ||
|
||
@NonNull | ||
@Override | ||
public DataType getCqlType() { | ||
return DataTypes.DATE; | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public GenericType<Date> getJavaType() { | ||
return GenericType.of(Date.class); | ||
} | ||
|
||
@Override | ||
public ByteBuffer encode(Date value, @NonNull ProtocolVersion protocolVersion) { | ||
if (value == null) return null; | ||
LocalDate localDate = LocalDate.from(Instant.ofEpochMilli(value.getTime())); | ||
return dateCodec.encode(localDate, protocolVersion); | ||
} | ||
|
||
@Override | ||
public Date decode(ByteBuffer bytes, @NonNull ProtocolVersion protocolVersion) { | ||
if (bytes == null) return null; | ||
LocalDate localDate = dateCodec.decode(bytes, protocolVersion); | ||
return localDate == null ? null : new Date(localDate.toEpochDay() * 86400L); | ||
} | ||
|
||
@Override | ||
public Date parse(String value) { | ||
throw new RuntimeException("Not supported"); | ||
} | ||
|
||
@Override | ||
@NonNull | ||
public String format(Date value) { | ||
throw new RuntimeException("Not supported"); | ||
} | ||
} |
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
Oops, something went wrong.