Java Code Examples for com.datastax.driver.core.Session#prepare()

The following examples show how to use com.datastax.driver.core.Session#prepare() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CassandraLoadBalancerStoreTest.java    From titus-control-plane with Apache License 2.0 6 votes vote down vote up
@Test
public void testAssociationStateIsCaseInsensitive() throws Exception {
    Session session = cassandraCQLUnit.getSession();
    PreparedStatement insertStmt = session.prepare("INSERT INTO load_balancer_jobs(job_id, load_balancer_id, state) VALUES(?, ?, ?);");

    ResultSet rs1 = session.execute(insertStmt.bind("job-1", "lb-1", "Associated"));
    assertThat(rs1.isExhausted()).isTrue();
    assertThat(rs1.wasApplied()).isTrue();

    ResultSet rs2 = session.execute(insertStmt.bind("job-2", "lb-2", "Dissociated"));
    assertThat(rs2.isExhausted()).isTrue();
    assertThat(rs2.wasApplied()).isTrue();

    ResultSet rs3 = session.execute(insertStmt.bind("job-3", "lb-3", "aSsOcIaTeD"));
    assertThat(rs3.isExhausted()).isTrue();
    assertThat(rs3.wasApplied()).isTrue();

    CassandraLoadBalancerStore store = getInitdStore();
    assertThat(store.getAssociations()).containsExactlyInAnyOrder(
            new JobLoadBalancerState(new JobLoadBalancer("job-1", "lb-1"), ASSOCIATED),
            new JobLoadBalancerState(new JobLoadBalancer("job-2", "lb-2"), DISSOCIATED),
            new JobLoadBalancerState(new JobLoadBalancer("job-3", "lb-3"), ASSOCIATED)
    );
}
 
Example 2
Source File: EnqueuedMailsDAO.java    From james-project with Apache License 2.0 6 votes vote down vote up
private PreparedStatement prepareInsert(Session session) {
    return session.prepare(insertInto(TABLE_NAME)
        .value(QUEUE_NAME, bindMarker(QUEUE_NAME))
        .value(TIME_RANGE_START, bindMarker(TIME_RANGE_START))
        .value(BUCKET_ID, bindMarker(BUCKET_ID))
        .value(ENQUEUE_ID, bindMarker(ENQUEUE_ID))
        .value(NAME, bindMarker(NAME))
        .value(HEADER_BLOB_ID, bindMarker(HEADER_BLOB_ID))
        .value(BODY_BLOB_ID, bindMarker(BODY_BLOB_ID))
        .value(ENQUEUED_TIME, bindMarker(ENQUEUED_TIME))
        .value(STATE, bindMarker(STATE))
        .value(SENDER, bindMarker(SENDER))
        .value(RECIPIENTS, bindMarker(RECIPIENTS))
        .value(ATTRIBUTES, bindMarker(ATTRIBUTES))
        .value(ERROR_MESSAGE, bindMarker(ERROR_MESSAGE))
        .value(REMOTE_ADDR, bindMarker(REMOTE_ADDR))
        .value(REMOTE_HOST, bindMarker(REMOTE_HOST))
        .value(LAST_UPDATED, bindMarker(LAST_UPDATED))
        .value(PER_RECIPIENT_SPECIFIC_HEADERS, bindMarker(PER_RECIPIENT_SPECIFIC_HEADERS)));
}
 
Example 3
Source File: CassandraMessageIdToImapUidDAO.java    From james-project with Apache License 2.0 6 votes vote down vote up
private PreparedStatement prepareInsert(Session session) {
    return session.prepare(insertInto(TABLE_NAME)
            .value(MESSAGE_ID, bindMarker(MESSAGE_ID))
            .value(MAILBOX_ID, bindMarker(MAILBOX_ID))
            .value(IMAP_UID, bindMarker(IMAP_UID))
            .value(MOD_SEQ, bindMarker(MOD_SEQ))
            .value(ANSWERED, bindMarker(ANSWERED))
            .value(DELETED, bindMarker(DELETED))
            .value(DRAFT, bindMarker(DRAFT))
            .value(FLAGGED, bindMarker(FLAGGED))
            .value(RECENT, bindMarker(RECENT))
            .value(SEEN, bindMarker(SEEN))
            .value(USER, bindMarker(USER))
            .value(USER_FLAGS, bindMarker(USER_FLAGS))
            .ifNotExists());
}
 
Example 4
Source File: CassandraMailRepositoryMailDaoV2.java    From james-project with Apache License 2.0 6 votes vote down vote up
private PreparedStatement prepareInsert(Session session) {
    return session.prepare(insertInto(CONTENT_TABLE_NAME)
        .value(REPOSITORY_NAME, bindMarker(REPOSITORY_NAME))
        .value(MAIL_KEY, bindMarker(MAIL_KEY))
        .value(STATE, bindMarker(STATE))
        .value(SENDER, bindMarker(SENDER))
        .value(RECIPIENTS, bindMarker(RECIPIENTS))
        .value(ATTRIBUTES, bindMarker(ATTRIBUTES))
        .value(ERROR_MESSAGE, bindMarker(ERROR_MESSAGE))
        .value(REMOTE_ADDR, bindMarker(REMOTE_ADDR))
        .value(REMOTE_HOST, bindMarker(REMOTE_HOST))
        .value(LAST_UPDATED, bindMarker(LAST_UPDATED))
        .value(HEADER_BLOB_ID, bindMarker(HEADER_BLOB_ID))
        .value(BODY_BLOB_ID, bindMarker(BODY_BLOB_ID))
        .value(PER_RECIPIENT_SPECIFIC_HEADERS, bindMarker(PER_RECIPIENT_SPECIFIC_HEADERS)));
}
 
Example 5
Source File: CassandraNotificationRegistryDAO.java    From james-project with Apache License 2.0 6 votes vote down vote up
@Inject
public CassandraNotificationRegistryDAO(Session session) {
    this.cassandraAsyncExecutor = new CassandraAsyncExecutor(session);

    this.registerStatement = session.prepare(createInsert());

    this.registerWithTTLStatement = session.prepare(createInsert().using(ttl(bindMarker(TTL))));

    this.isRegisteredStatement = session.prepare(select()
        .from(CassandraNotificationTable.TABLE_NAME)
        .where(eq(CassandraNotificationTable.ACCOUNT_ID, bindMarker(CassandraNotificationTable.ACCOUNT_ID)))
        .and(eq(CassandraNotificationTable.RECIPIENT_ID, bindMarker(CassandraNotificationTable.RECIPIENT_ID))));

    this.flushStatement = session.prepare(delete()
        .from(CassandraNotificationTable.TABLE_NAME)
        .where(eq(CassandraNotificationTable.ACCOUNT_ID, bindMarker(CassandraNotificationTable.ACCOUNT_ID))));
}
 
Example 6
Source File: ErrorResultIntegrationTest.java    From simulacron with Apache License 2.0 5 votes vote down vote up
private ResultSet prepareAndQuery() throws Exception {
  try (com.datastax.driver.core.Cluster driverCluster =
      defaultBuilder(server.getCluster())
          .withRetryPolicy(FallthroughRetryPolicy.INSTANCE)
          .build()) {
    Session session = driverCluster.connect();
    PreparedStatement prepared = session.prepare(query);
    return session.execute(prepared.bind());
  }
}
 
Example 7
Source File: CassandraGlobalMaxQuotaDao.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Inject
public CassandraGlobalMaxQuotaDao(Session session) {
    this.queryExecutor = new CassandraAsyncExecutor(session);
    this.getGlobalMaxStatement = session.prepare(getGlobalMaxStatement());
    this.setGlobalMaxMessageStatement = session.prepare(setGlobalMaxMessageStatement());
    this.setGlobalMaxStorageStatement = session.prepare(setGlobalMaxStorageStatement());
    this.removeGlobalMaxQuotaStatement = session.prepare(removeGlobalMaxQuotaStatement());
}
 
Example 8
Source File: CassandraPersistentActorRepository.java    From elasticactors with Apache License 2.0 5 votes vote down vote up
public CassandraPersistentActorRepository(Session cassandraSession, String clusterName, ThreadBoundExecutor asyncUpdateExecutor, Serializer serializer, Deserializer deserializer, long readExecutionThresholdMillis) {
    this.cassandraSession = cassandraSession;
    this.selectStatement = cassandraSession.prepare("select value from \"PersistentActors\" where key = ? and key2 = ? AND column1 = ?");
    this.clusterName = clusterName;
    this.asyncUpdateExecutor = asyncUpdateExecutor;
    this.readExecutionThresholdMillis = readExecutionThresholdMillis;
    this.serializer = serializer;
    this.deserializer = deserializer;
}
 
Example 9
Source File: CassandraUsersDAO.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Inject
public CassandraUsersDAO(Session session) {
    this.executor = new CassandraAsyncExecutor(session);
    this.getUserStatement = prepareGetUserStatement(session);
    this.updateUserStatement = prepareUpdateUserStatement(session);
    this.removeUserStatement = prepareRemoveUserStatement(session);
    this.countUserStatement = prepareCountStatement(session);
    this.listStatement = prepareListStatement(session);
    this.insertStatement = session.prepare(insertInto(TABLE_NAME)
        .value(NAME, bindMarker(NAME))
        .value(REALNAME, bindMarker(REALNAME))
        .value(PASSWORD, bindMarker(PASSWORD))
        .value(ALGORITHM, bindMarker(ALGORITHM))
        .ifNotExists());
}
 
Example 10
Source File: CassandraDeletedMessageDAO.java    From james-project with Apache License 2.0 5 votes vote down vote up
private PreparedStatement prepareBetweenUidStatement(Session session) {
    return session.prepare(select(UID)
        .from(TABLE_NAME)
        .where(eq(MAILBOX_ID, bindMarker(MAILBOX_ID)))
        .and(gte(UID, bindMarker(UID_FROM)))
        .and(lte(UID, bindMarker(UID_TO))));
}
 
Example 11
Source File: GeneratePlacePartitionId.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
public void execute(ExecutionContext context, boolean autoRollback) throws CommandExecutionException {
   Session session = context.getSession();
   PreparedStatement update = session.prepare(UPSERT_PARTITIONID);
   update.setConsistencyLevel(ConsistencyLevel.LOCAL_QUORUM);
   
   BoundStatement select = session.prepare(SELECT).bind();
   select.setConsistencyLevel(ConsistencyLevel.ALL);
   ResultSet rs = context.getSession().execute(select);
   int count = 0;
   int [] hubsPerPartition = new int[partitionCount];
   logger.info("Preparing to partition place ids");
   long startTimeNs = System.nanoTime();
   for(Row row: rs) {
      UUID placeId = row.getUUID("id");
      int partitionId = (int) (Math.floorMod(placeId.getLeastSignificantBits(), partitionCount));

      logger.debug("Adding [{}] to partition [{}]", placeId, partitionId);
      BoundStatement bs = update.bind(partitionId, placeId);
      session.execute(bs);
      
      count++;
      hubsPerPartition[partitionId]++;
   }
   long duration = System.nanoTime() - startTimeNs;
   logger.info("Partitioned {} place in {} secs", count, duration / (float) TimeUnit.NANOSECONDS.toSeconds(1));
   for(int i=0; i<partitionCount; i++) {
      logger.info(String.format("%03d: %3d places", i, hubsPerPartition[i]));
   }
}
 
Example 12
Source File: StorageInformationDAO.java    From james-project with Apache License 2.0 5 votes vote down vote up
private PreparedStatement prepareAdd(Session session) {
    return session.prepare(insertInto(TABLE)
        .value(OWNER, bindMarker(OWNER))
        .value(MESSAGE_ID, bindMarker(MESSAGE_ID))
        .value(BUCKET_NAME, bindMarker(BUCKET_NAME))
        .value(BLOB_ID, bindMarker(BLOB_ID)));
}
 
Example 13
Source File: CassandraMailboxRecentsDAO.java    From james-project with Apache License 2.0 5 votes vote down vote up
private PreparedStatement createDeleteStatement(Session session) {
    return session.prepare(
        QueryBuilder.delete()
            .from(CassandraMailboxRecentsTable.TABLE_NAME)
            .where(eq(CassandraMailboxRecentsTable.MAILBOX_ID, bindMarker(CassandraMailboxRecentsTable.MAILBOX_ID)))
            .and(eq(CassandraMailboxRecentsTable.RECENT_MESSAGE_UID, bindMarker(CassandraMailboxRecentsTable.RECENT_MESSAGE_UID))));
}
 
Example 14
Source File: CassandraMailRepositoryCountDAO.java    From james-project with Apache License 2.0 4 votes vote down vote up
private PreparedStatement prepareDecrement(Session session) {
    return session.prepare(update(COUNT_TABLE)
        .with(decr(COUNT))
        .where(eq(REPOSITORY_NAME, bindMarker(REPOSITORY_NAME))));
}
 
Example 15
Source File: CassandraMailRepositoryKeysDAO.java    From james-project with Apache License 2.0 4 votes vote down vote up
private PreparedStatement prepareList(Session session) {
    return session.prepare(select(MAIL_KEY)
        .from(KEYS_TABLE_NAME)
        .where(eq(REPOSITORY_NAME, bindMarker(REPOSITORY_NAME))));
}
 
Example 16
Source File: CassandraMappingsSourcesDAO.java    From james-project with Apache License 2.0 4 votes vote down vote up
private PreparedStatement prepareInsertStatement(Session session) {
    return session.prepare(insertInto(TABLE_NAME)
        .value(MAPPING_TYPE, bindMarker(MAPPING_TYPE))
        .value(MAPPING_VALUE, bindMarker(MAPPING_VALUE))
        .value(SOURCE, bindMarker(SOURCE)));
}
 
Example 17
Source File: CassandraDefaultBucketDAO.java    From james-project with Apache License 2.0 4 votes vote down vote up
private PreparedStatement prepareDeleteParts(Session session) {
    return session.prepare(
        delete().from(DefaultBucketBlobParts.TABLE_NAME)
            .where(eq(DefaultBucketBlobParts.ID, bindMarker(DefaultBucketBlobParts.ID))));
}
 
Example 18
Source File: CassandraMailboxRecentsDAO.java    From james-project with Apache License 2.0 4 votes vote down vote up
private PreparedStatement createDeleteAllStatement(Session session) {
    return session.prepare(
        QueryBuilder.delete()
            .from(CassandraMailboxRecentsTable.TABLE_NAME)
            .where(eq(CassandraMailboxRecentsTable.MAILBOX_ID, bindMarker(CassandraMailboxRecentsTable.MAILBOX_ID))));
}
 
Example 19
Source File: CassandraMailboxPathDAOImpl.java    From james-project with Apache License 2.0 4 votes vote down vote up
private PreparedStatement prepareInsert(Session session) {
    return session.prepare(insertInto(TABLE_NAME)
        .value(NAMESPACE_AND_USER, bindMarker(NAMESPACE_AND_USER))
        .value(MAILBOX_NAME, bindMarker(MAILBOX_NAME))
        .value(MAILBOX_ID, bindMarker(MAILBOX_ID)));
}
 
Example 20
Source File: CassandraMailboxCounterDAO.java    From james-project with Apache License 2.0 4 votes vote down vote up
private PreparedStatement createReadStatement(Session session) {
    return session.prepare(
        select(UNSEEN, COUNT)
            .from(TABLE_NAME)
            .where(eq(MAILBOX_ID, bindMarker(MAILBOX_ID))));
}