Search in sources :

Example 1 with EntityWithTtl

use of com.datastax.driver.mapping.entity.EntityWithTtl in project cassandra-driver-mapping by valchkou.

the class MappingSessionAsyncTest method batchTtlTest.

@Test
public void batchTtlTest() throws Exception {
    UUID uuid1 = UUID.randomUUID();
    Simple obj1 = new Simple();
    obj1.setTimestamp(new Date());
    obj1.setId(uuid1);
    UUID uuid2 = UUID.randomUUID();
    EntityWithTtl obj2 = new EntityWithTtl();
    obj2.setTimestamp(new Date());
    obj2.setId(uuid2);
    UUID uuid3 = UUID.randomUUID();
    EntityWithTtl obj3 = new EntityWithTtl();
    obj3.setTimestamp(new Date());
    obj3.setId(uuid3);
    ResultSetFuture f = target.withBatch().save(obj1).save(obj2).save(obj3, new WriteOptions().setTtl(10)).executeAsync();
    f.getUninterruptibly();
    Simple loaded1 = target.get(Simple.class, uuid1);
    EntityWithTtl loaded2 = target.get(EntityWithTtl.class, uuid2);
    EntityWithTtl loaded3 = target.get(EntityWithTtl.class, uuid3);
    assertNotNull(loaded1);
    assertNotNull(loaded2);
    assertNotNull(loaded3);
    Thread.sleep(5000);
    loaded1 = target.get(Simple.class, uuid1);
    loaded2 = target.get(EntityWithTtl.class, uuid2);
    loaded3 = target.get(EntityWithTtl.class, uuid3);
    assertNotNull(loaded1);
    assertNull(loaded2);
    assertNotNull(loaded3);
    Thread.sleep(5000);
    loaded1 = target.get(Simple.class, uuid1);
    loaded2 = target.get(EntityWithTtl.class, uuid2);
    loaded3 = target.get(EntityWithTtl.class, uuid3);
    assertNotNull(loaded1);
    assertNull(loaded2);
    assertNull(loaded3);
    target.delete(loaded1);
}
Also used : WriteOptions(com.datastax.driver.mapping.option.WriteOptions) ResultSetFuture(com.datastax.driver.core.ResultSetFuture) EntityWithTtl(com.datastax.driver.mapping.entity.EntityWithTtl) UUID(java.util.UUID) Date(java.util.Date) Simple(com.datastax.driver.mapping.entity.Simple) Test(org.junit.Test)

Example 2 with EntityWithTtl

use of com.datastax.driver.mapping.entity.EntityWithTtl in project cassandra-driver-mapping by valchkou.

the class MappingSessionAsyncTest method saveAndGetWithDefaultTtlTest.

@Test
public void saveAndGetWithDefaultTtlTest() throws Exception {
    UUID uuid = UUID.randomUUID();
    EntityWithTtl obj = new EntityWithTtl();
    obj.setTimestamp(new Date());
    obj.setId(uuid);
    EntityWithTtl loaded = target.get(EntityWithTtl.class, uuid);
    assertNull(loaded);
    ResultSetFuture f = target.saveAsync(obj);
    f.getUninterruptibly();
    loaded = target.get(EntityWithTtl.class, uuid);
    assertNotNull(loaded);
    Thread.sleep(4000);
    loaded = target.get(EntityWithTtl.class, uuid);
    assertNull(loaded);
}
Also used : ResultSetFuture(com.datastax.driver.core.ResultSetFuture) EntityWithTtl(com.datastax.driver.mapping.entity.EntityWithTtl) UUID(java.util.UUID) Date(java.util.Date) Test(org.junit.Test)

Example 3 with EntityWithTtl

use of com.datastax.driver.mapping.entity.EntityWithTtl in project cassandra-driver-mapping by valchkou.

the class MappingSessionAsyncTest method saveAndGetWithOverrideTtlTest.

@Test
public void saveAndGetWithOverrideTtlTest() throws Exception {
    UUID uuid = UUID.randomUUID();
    EntityWithTtl obj = new EntityWithTtl();
    obj.setTimestamp(new Date());
    obj.setId(uuid);
    EntityWithTtl loaded = target.get(EntityWithTtl.class, uuid);
    assertNull(loaded);
    ResultSetFuture f = target.saveAsync(obj, new WriteOptions().setTtl(10));
    f.getUninterruptibly();
    // ttl is 10 sec. obj still should be alive
    Thread.sleep(5000);
    loaded = target.get(EntityWithTtl.class, uuid);
    assertNotNull(loaded);
    // 10 sec passed, obj should expire
    Thread.sleep(5000);
    loaded = target.get(EntityWithTtl.class, uuid);
    assertNull(loaded);
}
Also used : WriteOptions(com.datastax.driver.mapping.option.WriteOptions) ResultSetFuture(com.datastax.driver.core.ResultSetFuture) EntityWithTtl(com.datastax.driver.mapping.entity.EntityWithTtl) UUID(java.util.UUID) Date(java.util.Date) Test(org.junit.Test)

Aggregations

ResultSetFuture (com.datastax.driver.core.ResultSetFuture)3 EntityWithTtl (com.datastax.driver.mapping.entity.EntityWithTtl)3 Date (java.util.Date)3 UUID (java.util.UUID)3 Test (org.junit.Test)3 WriteOptions (com.datastax.driver.mapping.option.WriteOptions)2 Simple (com.datastax.driver.mapping.entity.Simple)1