Skip to content

Instantly share code, notes, and snippets.

@velvia
Created June 29, 2015 07:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save velvia/909408dc6f053d6934be to your computer and use it in GitHub Desktop.
Save velvia/909408dc6f053d6934be to your computer and use it in GitHub Desktop.
Patch to Phantom 1.8.x for ByteBuffer handling
// Primitive.scala - only change is asCql method
implicit object BlobIsPrimitive extends Primitive[ByteBuffer] {
override type PrimitiveType = java.nio.ByteBuffer
val cassandraType = CQLSyntax.Types.Blob
override def fromRow(column: String, row: Row): Try[ByteBuffer] = nullCheck(column, row) {
r => r.getBytes(column)
}
override def asCql(value: ByteBuffer): String = Bytes.toHexString(value)
override def fromString(value: String): ByteBuffer = Bytes.fromHexString(value)
override def clz: Class[java.nio.ByteBuffer] = classOf[java.nio.ByteBuffer]
}
// Addition to PrimitiveTest.scala
it should "convert ByteBuffers to valid hex bytes" in {
val buf = ByteBuffer.wrap(Array[Byte](1, 2, 3, 4, 5))
Primitive[ByteBuffer].asCql(buf) shouldEqual "0x0102030405"
buf.position(2) // Non-zero position
Primitive[ByteBuffer].asCql(buf) shouldEqual "0x030405"
val slice = buf.slice() // Slice with non-zero arrayOffset
Primitive[ByteBuffer].asCql(slice) shouldEqual "0x030405"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment