Search in sources :

Example 36 with ResultSetFuture

use of com.datastax.driver.core.ResultSetFuture in project cassandra-driver-mapping by valchkou.

the class MappingSessionAsyncTest method saveEntityWithEnumTest.

@Test
public void saveEntityWithEnumTest() throws Exception {
    UUID uuid = UUID.randomUUID();
    EntityWithEnum obj = new EntityWithEnum();
    obj.setId(uuid);
    obj.setMonth(Month.JUNE);
    EntityWithEnum loaded = target.get(EntityWithEnum.class, uuid);
    assertNull(loaded);
    ResultSetFuture f = target.saveAsync(obj);
    f.getUninterruptibly();
    loaded = target.get(EntityWithEnum.class, uuid);
    assertEquals(obj, loaded);
    obj.setMonth(Month.APRIL);
    f = target.saveAsync(obj);
    f.getUninterruptibly();
    loaded = target.get(EntityWithEnum.class, uuid);
    assertEquals(obj, loaded);
    f = target.deleteAsync(loaded);
    f.getUninterruptibly();
    loaded = target.get(EntityWithEnum.class, uuid);
    assertNull(loaded);
}
Also used : ResultSetFuture(com.datastax.driver.core.ResultSetFuture) EntityWithEnum(com.datastax.driver.mapping.entity.EntityWithEnum) UUID(java.util.UUID) Test(org.junit.Test)

Example 37 with ResultSetFuture

use of com.datastax.driver.core.ResultSetFuture in project cassandra-driver-mapping by valchkou.

the class MappingSessionAsyncTest method updateSelectedPropertiesTest.

@Test
public void updateSelectedPropertiesTest() throws Exception {
    UUID uuid = UUID.randomUUID();
    Simple obj = new Simple();
    obj.setName("myName");
    obj.setAge(55);
    obj.setId(uuid);
    ResultSetFuture f = target.saveAsync(obj);
    f.getUninterruptibly();
    String[] props = { "name", "age" };
    Object[] vals = { "yourName", 25 };
    f = target.updateValuesAsync(uuid, Simple.class, props, vals);
    f.getUninterruptibly();
    Simple loaded = target.get(Simple.class, uuid);
    assertEquals(25, loaded.getAge());
    assertEquals("yourName", loaded.getName());
}
Also used : ResultSetFuture(com.datastax.driver.core.ResultSetFuture) UUID(java.util.UUID) Simple(com.datastax.driver.mapping.entity.Simple) Test(org.junit.Test)

Example 38 with ResultSetFuture

use of com.datastax.driver.core.ResultSetFuture in project cassandra by apache.

the class DeleteTest method lostDeletesTest.

@Test
public void lostDeletesTest() {
    Session session = sessionNet();
    for (int i = 0; i < 500; i++) {
        session.execute(pstmtI.bind(1, 1, "inhB", "valB"));
        ResultSetFuture[] futures = load();
        Assert.assertTrue(futures[0].getUninterruptibly().isExhausted());
        Assert.assertTrue(futures[1].getUninterruptibly().isExhausted());
        Assert.assertNotNull(futures[2].getUninterruptibly().one());
        Assert.assertTrue(futures[3].getUninterruptibly().isExhausted());
        Assert.assertTrue(futures[4].getUninterruptibly().isExhausted());
        session.execute(pstmtU.bind("inhBu", "valBu", 1, 1));
        futures = load();
        Assert.assertTrue(futures[0].getUninterruptibly().isExhausted());
        Assert.assertTrue(futures[1].getUninterruptibly().isExhausted());
        Assert.assertNotNull(futures[2].getUninterruptibly().one());
        Assert.assertTrue(futures[3].getUninterruptibly().isExhausted());
        Assert.assertTrue(futures[4].getUninterruptibly().isExhausted());
        session.execute(pstmtD.bind(1, 1));
        futures = load();
        Assert.assertTrue(futures[0].getUninterruptibly().isExhausted());
        Assert.assertTrue(futures[1].getUninterruptibly().isExhausted());
        Assert.assertTrue(futures[2].getUninterruptibly().isExhausted());
        Assert.assertTrue(futures[3].getUninterruptibly().isExhausted());
        Assert.assertTrue(futures[4].getUninterruptibly().isExhausted());
    }
}
Also used : ResultSetFuture(com.datastax.driver.core.ResultSetFuture) Session(com.datastax.driver.core.Session) Test(org.junit.Test)

Example 39 with ResultSetFuture

use of com.datastax.driver.core.ResultSetFuture in project storm by apache.

the class AsyncExecutor method execAsync.

/**
 * Asynchronously executes the specified batch statement. Inputs will be passed to
 * the {@link #handler} once query succeed or failed.
 */
public SettableFuture<T> execAsync(final Statement statement, final T inputs, final AsyncResultHandler<T> handler) {
    final SettableFuture<T> settableFuture = SettableFuture.create();
    pending.incrementAndGet();
    ResultSetFuture future = session.executeAsync(statement);
    Futures.addCallback(future, new FutureCallback<ResultSet>() {

        public void release() {
            pending.decrementAndGet();
        }

        @Override
        public void onSuccess(ResultSet result) {
            release();
            settableFuture.set(inputs);
            handler.success(inputs);
        }

        @Override
        public void onFailure(Throwable t) {
            LOG.error(String.format("Failed to execute statement '%s' ", statement), t);
            release();
            settableFuture.setException(t);
            handler.failure(t, inputs);
        }
    }, executorService);
    return settableFuture;
}
Also used : ResultSetFuture(com.datastax.driver.core.ResultSetFuture) ResultSet(com.datastax.driver.core.ResultSet)

Example 40 with ResultSetFuture

use of com.datastax.driver.core.ResultSetFuture in project storm by apache.

the class AsyncExecutor method execAsync.

/**
 * Asynchronously executes the specified select statements. Results will be passed to the {@link AsyncResultSetHandler}
 * once each query has succeed or failed.
 */
public SettableFuture<List<T>> execAsync(final List<Statement> statements, final List<T> inputs, Semaphore throttle, final AsyncResultSetHandler<T> handler) {
    final SettableFuture<List<T>> settableFuture = SettableFuture.create();
    if (inputs.size() == 0) {
        settableFuture.set(new ArrayList<T>());
        return settableFuture;
    }
    final AsyncContext<T> asyncContext = new AsyncContext<>(inputs, throttle, settableFuture);
    for (int i = 0; i < statements.size(); i++) {
        // Acquire a slot
        if (asyncContext.acquire()) {
            try {
                pending.incrementAndGet();
                final T input = inputs.get(i);
                final Statement statement = statements.get(i);
                ResultSetFuture future = session.executeAsync(statement);
                Futures.addCallback(future, new FutureCallback<ResultSet>() {

                    @Override
                    public void onSuccess(ResultSet result) {
                        try {
                            handler.success(input, result);
                        } catch (Throwable throwable) {
                            asyncContext.exception(throwable);
                        } finally {
                            pending.decrementAndGet();
                            asyncContext.release();
                        }
                    }

                    @Override
                    public void onFailure(Throwable throwable) {
                        try {
                            handler.failure(throwable, input);
                        } catch (Throwable throwable2) {
                            asyncContext.exception(throwable2);
                        } finally {
                            asyncContext.exception(throwable).release();
                            pending.decrementAndGet();
                            LOG.error(String.format("Failed to execute statement '%s' ", statement), throwable);
                        }
                    }
                }, executorService);
            } catch (Throwable throwable) {
                asyncContext.exception(throwable).release();
                pending.decrementAndGet();
                break;
            }
        }
    }
    return settableFuture;
}
Also used : ResultSetFuture(com.datastax.driver.core.ResultSetFuture) Statement(com.datastax.driver.core.Statement) ResultSet(com.datastax.driver.core.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

ResultSetFuture (com.datastax.driver.core.ResultSetFuture)78 Test (org.junit.Test)35 UUID (java.util.UUID)26 ResultSet (com.datastax.driver.core.ResultSet)20 EntityWithCollections (com.datastax.driver.mapping.entity.EntityWithCollections)13 BoundStatement (com.datastax.driver.core.BoundStatement)12 PreparedStatement (com.datastax.driver.core.PreparedStatement)10 ArrayList (java.util.ArrayList)8 Date (java.util.Date)8 Row (com.datastax.driver.core.Row)7 Statement (com.datastax.driver.core.Statement)7 List (java.util.List)6 Session (com.datastax.driver.core.Session)5 Simple (com.datastax.driver.mapping.entity.Simple)5 Cell (com.palantir.atlasdb.keyvalue.api.Cell)5 BigDecimal (java.math.BigDecimal)5 HashSet (java.util.HashSet)5 Result (com.datastax.driver.mapping.Result)4 WriteOptions (com.datastax.driver.mapping.option.WriteOptions)4 Nullable (javax.annotation.Nullable)4