CqlResult cqlResult = CassandraCli.client.execute_cql3_query(ByteBuffer
.wrap("Select ttl(\"PERSON_NAME\"), ttl(\"AGE\") from \"PERSONCASSANDRA\" where \"personId\" = '1'"
.getBytes()), Compression.NONE, ConsistencyLevel.ONE);
List<CqlRow> cqlRows = cqlResult.getRows();
CqlRow cqlRow = cqlRows.get(0);
boolean personNameFound = false;
boolean ageFound = false;
for (Column column : cqlRow.getColumns())
{
String columnName = new String(column.getName(), Constants.ENCODING);
if (columnName.equals("ttl(PERSON_NAME)"))
{
Assert.assertEquals(5, ByteBufferUtil.toInt(ByteBuffer.wrap(column.getValue())));
personNameFound = true;
}
else if (columnName.equals("ttl(AGE)"))
{
Assert.assertEquals(5, ByteBufferUtil.toInt(ByteBuffer.wrap(column.getValue())));
ageFound = true;
}
}
Assert.assertTrue(personNameFound && ageFound);
Thread.sleep(5000);
cqlResult = CassandraCli.client.execute_cql3_query(ByteBuffer
.wrap("Select ttl(\"PERSON_NAME\"), ttl(\"AGE\") from \"PERSONCASSANDRA\" where \"personId\" = '1'"
.getBytes()), Compression.NONE, ConsistencyLevel.ONE);
cqlRows = cqlResult.getRows();
try
{
cqlRow = cqlRows.get(0);
Assert.fail("PERSON_NAME and AGE column not deleted even though a TTL of 5 seconds was specified while writing to cassandra.");
}
catch (IndexOutOfBoundsException ioobe)
{
Assert.assertTrue(cqlRows.isEmpty());
}
// checking for update query.
Object p2 = prepareData("2", 10);
em.persist(p2);
em.clear();
ttlValues = new HashMap<String, Integer>();
ttlValues.put("PERSONCASSANDRA", new Integer(10));
em.setProperty("ttl.per.request", true);
em.setProperty("ttl.values", ttlValues);
Query q = em.createQuery("update PersonCassandra p set p.personName=''KK MISHRA'' where p.personId=2");
q.executeUpdate();
cqlResult = CassandraCli.client.execute_cql3_query(ByteBuffer
.wrap("Select ttl(\"PERSON_NAME\"), ttl(\"AGE\") from \"PERSONCASSANDRA\" where \"personId\" = '2'"
.getBytes()), Compression.NONE, ConsistencyLevel.ONE);
cqlRows = cqlResult.getRows();
cqlRow = cqlRows.get(0);
personNameFound = false;
ageFound = false;
for (Column column : cqlRow.getColumns())
{
String columnName = new String(column.getName(), Constants.ENCODING);
if (columnName.equals("ttl(PERSON_NAME)"))
{
Assert.assertEquals(10, ByteBufferUtil.toInt(ByteBuffer.wrap(column.getValue())));
personNameFound = true;
}
else if (columnName.equals("ttl(AGE)"))
{
Assert.assertEquals(null, column.getValue());
ageFound = true;
}
}
Assert.assertTrue(personNameFound && ageFound);
Thread.sleep(10000);
cqlResult = CassandraCli.client.execute_cql3_query(ByteBuffer
.wrap("Select ttl(\"PERSON_NAME\"), ttl(\"AGE\") from \"PERSONCASSANDRA\" where \"personId\" = '2'"
.getBytes()), Compression.NONE, ConsistencyLevel.ONE);
cqlRows = cqlResult.getRows();
cqlRow = cqlRows.get(0);
for (Column column : cqlRow.getColumns())
{
String columnName = new String(column.getName(), Constants.ENCODING);
if (columnName.equals("ttl(PERSON_NAME)"))
{
Assert.assertEquals(null, null);
personNameFound = true;
}
else if (columnName.equals("ttl(AGE)"))
{
Assert.assertEquals(null, column.getValue());
ageFound = true;
}
if (columnName.equals("PERSON_NAME"))
{
Assert.fail("PERSON_NAME column not deleted even though a TTL of 10 seconds was specified while writing to cassandra.");
}
}
// TTL per session.
ttlValues = new HashMap<String, Integer>();
ttlValues.put("PERSONCASSANDRA", new Integer(10));
em.setProperty("ttl.per.session", true);
em.setProperty("ttl.values", ttlValues);
Object p3 = prepareData("3", 10);
em.persist(p3);
em.clear();
cqlResult = CassandraCli.client.execute_cql3_query(ByteBuffer
.wrap("Select ttl(\"PERSON_NAME\"), ttl(\"AGE\") from \"PERSONCASSANDRA\" where \"personId\" = '3'"
.getBytes()), Compression.NONE, ConsistencyLevel.ONE);
cqlRows = cqlResult.getRows();
cqlRow = cqlRows.get(0);
personNameFound = false;
ageFound = false;
for (Column column : cqlRow.getColumns())
{
String columnName = new String(column.getName(), Constants.ENCODING);
if (columnName.equals("ttl(PERSON_NAME)"))
{
Assert.assertEquals(10, ByteBufferUtil.toInt(ByteBuffer.wrap(column.getValue())));
personNameFound = true;
}
else if (columnName.equals("ttl(AGE)"))
{
Assert.assertEquals(10, ByteBufferUtil.toInt(ByteBuffer.wrap(column.getValue())));
ageFound = true;
}
}
Assert.assertTrue(personNameFound && ageFound);
Thread.sleep(10000);
cqlResult = CassandraCli.client.execute_cql3_query(ByteBuffer
.wrap("Select ttl(\"PERSON_NAME\"), ttl(\"AGE\") from \"PERSONCASSANDRA\" where \"personId\" = '3'"
.getBytes()), Compression.NONE, ConsistencyLevel.ONE);
cqlRows = cqlResult.getRows();
try
{
cqlRow = cqlRows.get(0);
Assert.fail("PERSON_NAME and AGE column not deleted even though a TTL of 5 seconds was specified while writing to cassandra.");
}
catch (IndexOutOfBoundsException ioobe)
{
Assert.assertTrue(cqlRows.isEmpty());
}
Object p4 = prepareData("4", 10);
em.persist(p4);
em.clear();
cqlResult = CassandraCli.client.execute_cql3_query(ByteBuffer
.wrap("Select ttl(\"PERSON_NAME\"), ttl(\"AGE\") from \"PERSONCASSANDRA\" where \"personId\" = '4'"
.getBytes()), Compression.NONE, ConsistencyLevel.ONE);
cqlRows = cqlResult.getRows();
cqlRow = cqlRows.get(0);
personNameFound = false;
ageFound = false;
for (Column column : cqlRow.getColumns())
{
String columnName = new String(column.getName(), Constants.ENCODING);
if (columnName.equals("ttl(PERSON_NAME)"))
{
Assert.assertEquals(10, ByteBufferUtil.toInt(ByteBuffer.wrap(column.getValue())));