rantav/hector — Issue #407: BufferUnderflowException retrieving Integer values
Author: diogoeag
Created: 2012-02-01 16:02:24+00:00
State: open

Hi,

I'm using Hector 1.0.2-2 and trying to make a small abstraction over hector to retrieve values and keys wihtou knowing the schema. So I'm using ThriftColumnFamilyTemplate to retrieve the information and then deserialize it using the information that I get on connect with cluster.describeSchema.

My keyspace schema is:

create column family test
and comparator = 'UTF8Type'
and key_validation_class = 'UTF8Type'
and column_metadata = [
{column_name : 'ints', validation_class : IntegerType}];

set test['key1']['ints'] = 2;
set test['key2']['ints'] = 214748364;

Then I have the following code:

==================================================================================

Cluster cluster = HFactory.getOrCreateCluster("Test Cluster", "test-database.zai:9160");
Keyspace keyspace = HFactory.createKeyspace("pulsetests", cluster);

ThriftColumnFamilyTemplate template = new ThriftColumnFamilyTemplate(keyspace,
"test",
StringSerializer.get(),
StringSerializer.get());

ColumnFamilyResult queryColumns = template.queryColumns("key1", new ArrayList() {

{
add("ints");
}
});

HColumn value = queryColumns.getColumn("ints");
System.out.println("Buffer: "+ value.getValueBytes());
Integer intValue = IntegerSerializer.get().fromByteBuffer(value.getValueBytes());
System.out.println("Result: "+ intValue);
==================================================================================

When I run I get the following output:

Buffer: java.nio.HeapByteBuffer[pos=50 lim=51 cap=65]
Exception in thread "main" java.nio.BufferUnderflowException
at java.nio.Buffer.nextGetIndex(Buffer.java:480)
at java.nio.HeapByteBuffer.getInt(HeapByteBuffer.java:336)
at me.prettyprint.cassandra.serializers.IntegerSerializer.fromByteBuffer(IntegerSerializer.java:35)
at com.feedzai.pulse.mavenproject2.App.main(App.java:38)

When I run using the key: "key2", I get the following output:

Buffer: java.nio.HeapByteBuffer[pos=50 lim=54 cap=68]
Result: 214748364

Why is the ByteBuffer giving me pos=50 and limit=51 (1 byte only) when I ask for key1 and when I ask for key2 the number of bytes is 4?

If the ByteBuffer retrieves 1 to 4 bytes the IntegerSerializer should create a byte[] with those bytes and pad on the left with zeros to create an Integer?