Search in sources :

Example 21 with BoundStatement

use of com.datastax.driver.core.BoundStatement in project newts by OpenNMS.

the class CassandraSampleRepository method delete.

@Override
public void delete(Context context, Resource resource) {
    /**
     * Check for ttl value > 0
     */
    if (m_ttl > 0) {
        /**
         * Delete exactly from (now - ttl) till now
         */
        final Timestamp start = Timestamp.now().minus(m_ttl, TimeUnit.SECONDS);
        final Timestamp end = Timestamp.now();
        final Duration resourceShard = m_contextConfigurations.getResourceShard(context);
        final List<Future<ResultSet>> futures = Lists.newArrayList();
        for (Timestamp partition : new IntervalGenerator(start.stepFloor(resourceShard), end.stepFloor(resourceShard), resourceShard)) {
            BoundStatement bindStatement = m_deleteStatement.bind();
            bindStatement.setString(SchemaConstants.F_CONTEXT, context.getId());
            bindStatement.setInt(SchemaConstants.F_PARTITION, (int) partition.asSeconds());
            bindStatement.setString(SchemaConstants.F_RESOURCE, resource.getId());
            futures.add(m_session.executeAsync(bindStatement));
        }
        for (final Future<ResultSet> future : futures) {
            try {
                future.get();
            } catch (final InterruptedException | ExecutionException e) {
                throw Throwables.propagate(e);
            }
        }
    } else {
        // Choose (now - one year) till now...
        Timestamp end = Timestamp.now();
        Timestamp start = end.minus(DELETION_INTERVAL, TimeUnit.DAYS);
        // ... and check whether samples exist for this period of time.
        while (cassandraSelect(context, resource, start, end).hasNext()) {
            // Now delete the samples...
            final Duration resourceShard = m_contextConfigurations.getResourceShard(context);
            final List<Future<ResultSet>> futures = Lists.newArrayList();
            for (Timestamp partition : new IntervalGenerator(start.stepFloor(resourceShard), end.stepFloor(resourceShard), resourceShard)) {
                BoundStatement bindStatement = m_deleteStatement.bind();
                bindStatement.setString(SchemaConstants.F_CONTEXT, context.getId());
                bindStatement.setInt(SchemaConstants.F_PARTITION, (int) partition.asSeconds());
                bindStatement.setString(SchemaConstants.F_RESOURCE, resource.getId());
                futures.add(m_session.executeAsync(bindStatement));
            }
            for (final Future<ResultSet> future : futures) {
                try {
                    future.get();
                } catch (final InterruptedException | ExecutionException e) {
                    throw Throwables.propagate(e);
                }
            }
            // ...set end to start and start to (end - one year)
            end = start;
            start = end.minus(DELETION_INTERVAL, TimeUnit.DAYS);
        // and start over again until no more samples are found
        }
    }
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) Future(java.util.concurrent.Future) Duration(org.opennms.newts.api.Duration) IntervalGenerator(org.opennms.newts.aggregate.IntervalGenerator) ExecutionException(java.util.concurrent.ExecutionException) Timestamp(org.opennms.newts.api.Timestamp) BoundStatement(com.datastax.driver.core.BoundStatement)

Example 22 with BoundStatement

use of com.datastax.driver.core.BoundStatement in project lagom-java-chirper-example by lagom.

the class FriendEventProcessor method processFriendChanged.

private CompletionStage<List<BoundStatement>> processFriendChanged(FriendAdded event) {
    BoundStatement bindWriteFollowers = writeFollowers.bind();
    bindWriteFollowers.setString("userId", event.friendId);
    bindWriteFollowers.setString("followedBy", event.userId);
    return completedStatement(bindWriteFollowers);
}
Also used : BoundStatement(com.datastax.driver.core.BoundStatement)

Example 23 with BoundStatement

use of com.datastax.driver.core.BoundStatement in project apex-malhar by apache.

the class CassandraTransactionalStore method storeCommittedWindowId.

@Override
public void storeCommittedWindowId(String appId, int operatorId, long windowId) {
    try {
        BoundStatement boundStatement = new BoundStatement(lastWindowUpdateCommand);
        lastWindowUpdateStatement = boundStatement.bind(windowId, appId, operatorId);
        batchCommand.add(lastWindowUpdateStatement);
    } catch (DriverException e) {
        throw new RuntimeException(e);
    }
}
Also used : DriverException(com.datastax.driver.core.exceptions.DriverException) BoundStatement(com.datastax.driver.core.BoundStatement)

Example 24 with BoundStatement

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

the class MappingSession method remove.

/**
     * Remove an item or items from the Set or List.
     * 
     * @param id Primary Key
     * @param class Entity.class
     * @param propertyName property of Entity to modify
     * @param item can be single value, a List or a Set of values to remove.
     */
public void remove(Object id, Class<?> clazz, String propertyName, Object item) {
    maybeSync(clazz);
    BoundStatement bs = MappingBuilder.prepareRemoveItemsFromSetOrList(id, clazz, propertyName, item, keyspace, session);
    execute(bs);
}
Also used : BoundStatement(com.datastax.driver.core.BoundStatement)

Example 25 with BoundStatement

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

the class MappingSession method replaceAt.

/**
     * Replace item at the specified position in the List.
     * 
     * @param id Primary Key
     * @param class Entity.class
     * @param propertyName Entity property
     * @param item
     * @param idx index of the item to replace. Starts from 0.
     * @param options WriteOptions
     */
public void replaceAt(Object id, Class<?> clazz, String propertyName, Object item, int idx, WriteOptions options) {
    maybeSync(clazz);
    BoundStatement bs = MappingBuilder.prepareReplaceAt(id, clazz, propertyName, item, idx, options, keyspace, session);
    execute(bs);
}
Also used : BoundStatement(com.datastax.driver.core.BoundStatement)

Aggregations

BoundStatement (com.datastax.driver.core.BoundStatement)48 ResultSet (com.datastax.driver.core.ResultSet)9 PreparedStatement (com.datastax.driver.core.PreparedStatement)6 ResultSetFuture (com.datastax.driver.core.ResultSetFuture)5 Row (com.datastax.driver.core.Row)5 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 RegularStatement (com.datastax.driver.core.RegularStatement)3 Session (com.datastax.driver.core.Session)3 DriverException (com.datastax.driver.core.exceptions.DriverException)3 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)3 List (java.util.List)3 Map (java.util.Map)3 UUID (java.util.UUID)3 MetricRegistry (com.codahale.metrics.MetricRegistry)2 DataType (com.datastax.driver.core.DataType)2 Statement (com.datastax.driver.core.Statement)2 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 Futures.allAsList (com.google.common.util.concurrent.Futures.allAsList)2