Java Code Examples for com.datastax.driver.core.BoundStatement#bind()

The following examples show how to use com.datastax.driver.core.BoundStatement#bind() . These examples are extracted from open source projects. 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
@Override
public boolean updateMonitoringState(UUID placeId, UUID incidentId, AlarmIncident.MonitoringState state) {
   AlarmIncident.MonitoringState required = AlarmIncident.MonitoringState.NONE;
   switch(state) {
      case NONE:
      case PENDING: break;
      case DISPATCHING:
      case CANCELLED:
         required = AlarmIncident.MonitoringState.PENDING;
         break;
      case DISPATCHED: required = AlarmIncident.MonitoringState.DISPATCHING; break;
   }
   BoundStatement bound = new BoundStatement(updateMonitoringState);
   bound.bind(state.name(), placeId, incidentId, required.name());
   ResultSet rs = session.execute(bound);
   return rs.wasApplied();
}
 
Example 2
@Override
protected Session doReadSession(Serializable sessionId) {

   UUID id = toUuid(sessionId);

   PreparedStatement ps = prepareReadStatement();
   BoundStatement bs = new BoundStatement(ps);
   bs.bind(id);

   ResultSet results = cassandraSession.execute(bs);

   for(Row row : results) {
      UUID rowId = row.getUUID("id");
      if (id.equals(rowId)) {
         ByteBuffer buffer = row.getBytes("serialized_value");
         if (buffer != null) { //could be null if a tombstone due to TTL removal
            byte[] bytes = new byte[buffer.remaining()];
            buffer.get(bytes);
            return serializer.deserialize(bytes);
         }
      }
   }

   return null;
}
 
Example 3
Source Project: glowroot   File: CassandraSyncIT.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void transactionMarker() throws Exception {
    BatchStatement batchStatement = new BatchStatement();
    batchStatement.add(new SimpleStatement(
            "INSERT INTO test.users (id,  fname, lname) VALUES (100, 'f100', 'l100')"));
    batchStatement.add(new SimpleStatement(
            "INSERT INTO test.users (id,  fname, lname) VALUES (101, 'f101', 'l101')"));
    PreparedStatement preparedStatement =
            session.prepare("INSERT INTO test.users (id,  fname, lname) VALUES (?, ?, ?)");
    for (int i = 200; i < 210; i++) {
        BoundStatement boundStatement = new BoundStatement(preparedStatement);
        boundStatement.bind(i, "f" + i, "l" + i);
        batchStatement.add(boundStatement);
    }
    batchStatement.add(new SimpleStatement(
            "INSERT INTO test.users (id,  fname, lname) VALUES (300, 'f300', 'l300')"));
    session.execute(batchStatement);
}
 
Example 4
Source Project: glowroot   File: CassandraAsyncIT.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void transactionMarker() throws Exception {
    BatchStatement batchStatement = new BatchStatement();
    batchStatement.add(new SimpleStatement(
            "INSERT INTO test.users (id,  fname, lname) VALUES (100, 'f100', 'l100')"));
    batchStatement.add(new SimpleStatement(
            "INSERT INTO test.users (id,  fname, lname) VALUES (101, 'f101', 'l101')"));
    PreparedStatement preparedStatement =
            session.prepare("INSERT INTO test.users (id,  fname, lname) VALUES (?, ?, ?)");
    for (int i = 200; i < 210; i++) {
        BoundStatement boundStatement = new BoundStatement(preparedStatement);
        boundStatement.bind(i, "f" + i, "l" + i);
        batchStatement.add(boundStatement);
    }
    batchStatement.add(new SimpleStatement(
            "INSERT INTO test.users (id,  fname, lname) VALUES (300, 'f300', 'l300')"));
    session.executeAsync(batchStatement).get();
}
 
Example 5
Source Project: glowroot   File: CentralConfigDao.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Optional<Object> load(String key) throws Exception {
    BoundStatement boundStatement = readPS.bind();
    boundStatement.bind(key);
    ResultSet results = session.read(boundStatement);
    Row row = results.one();
    if (row == null) {
        return Optional.absent();
    }
    String value = row.getString(0);
    if (value == null) {
        return Optional.absent();
    }
    try {
        return Optional.of(readValue(key, value));
    } catch (IOException e) {
        logger.error("error parsing config node '{}': ", key, e);
        return Optional.absent();
    }
}
 
Example 6
@Override
public long getCommittedWindowId(String appId, int operatorId)
{
  try {
    BoundStatement boundStatement = new BoundStatement(lastWindowFetchCommand);
    lastWindowFetchStatement = boundStatement.bind(appId,operatorId);
    long lastWindow = -1;
    ResultSet resultSet = session.execute(lastWindowFetchStatement);
    if (!resultSet.isExhausted()) {
      lastWindow = resultSet.one().getLong(0);
    }
    lastWindowFetchCommand.disableTracing();
    return lastWindow;
  } catch (DriverException ex) {
    throw new RuntimeException(ex);
  }
}
 
Example 7
private void claim(String protocolAddress, UUID accountId, UUID placeId) throws IpcdDaoException {
   BoundStatement stmt = new BoundStatement(claim);
   stmt.bind(
         accountId,
         placeId,
         partitioner.getPartitionForPlaceId(placeId).getId(),
         IpcdDevice.RegistrationState.PENDING_DRIVER.name(),
         protocolAddress,
         IpcdDevice.RegistrationState.UNREGISTERED.name(),
         IpcdDevice.ConnState.ONLINE.name()
   );
   ResultSet rs = session.execute(stmt);
   parseOptimisticResult(rs, protocolAddress, null);
}
 
Example 8
@Override
public void removeCommittedWindowId(String appId, int operatorId)
{
  try {
    BoundStatement boundStatement = new BoundStatement(lastWindowDeleteCommand);
    lastWindowDeleteStatement = boundStatement.bind(appId,operatorId);
    session.execute(lastWindowDeleteStatement);
    lastWindowDeleteCommand.disableTracing();
  } catch (DriverException e) {
    throw new RuntimeException(e);
  }
}
 
Example 9
Source Project: glowroot   File: CentralConfigDao.java    License: Apache License 2.0 5 votes vote down vote up
void write(String key, Object config, String priorVersion) throws Exception {
    BoundStatement boundStatement = readPS.bind();
    boundStatement.bind(key);
    ResultSet results = session.read(boundStatement);
    Row row = results.one();
    if (row == null) {
        writeIfNotExists(key, config);
        return;
    }
    String currValue = checkNotNull(row.getString(0));
    Object currConfig = readValue(key, currValue);
    if (!Versions.getJsonVersion(currConfig).equals(priorVersion)) {
        throw new OptimisticLockException();
    }
    String newValue = mapper.writeValueAsString(config);
    boundStatement = updatePS.bind();
    int i = 0;
    boundStatement.setString(i++, newValue);
    boundStatement.setString(i++, key);
    boundStatement.setString(i++, currValue);
    results = session.update(boundStatement);
    row = checkNotNull(results.one());
    boolean applied = row.getBool("[applied]");
    if (applied) {
        centralConfigCache.invalidate(key);
    } else {
        throw new OptimisticLockException();
    }
}
 
Example 10
@Override
public void forceRegistration(String protocolAddress, UUID accountId, UUID placeId, String driverAddress) {
   BoundStatement stmt = new BoundStatement(forceRegistration);
   stmt.bind(
         accountId,
         placeId,
         partitioner.getPartitionForPlaceId(placeId).getId(),
         driverAddress,
         IpcdDevice.RegistrationState.REGISTERED.name(),
         protocolAddress
   );

   session.execute(stmt);
}
 
Example 11
@Override
public void delete(String protocolAddress, UUID placeId) throws IpcdDaoException {
   BoundStatement stmt = new BoundStatement(delete);
   stmt.bind(
         protocolAddress,
         placeId
   );

   ResultSet rs = session.execute(stmt);
   parseOptimisticResult(rs, protocolAddress, placeId);
}
 
Example 12
Source Project: arcusplatform   File: PersonDAOImpl.java    License: Apache License 2.0 5 votes vote down vote up
private BoundStatement bindUpdate(Person person, String currentEmail) {
   List<Object> values = new ArrayList<Object>(COLUMN_ORDER.length + 5);
   values.add(person.getModified());
   values.add(person.getTags());
   values.add(person.getImages());
   values.addAll(getValues(person));
   values.add(person.getId());
   values.add(currentEmail);

   // use an optimistic update to prevent inadvertently changing the
   // email address and corrupting the index
   BoundStatement bs = new BoundStatement(updatePersonOptimistic);
   return bs.bind(values.toArray());
}
 
Example 13
Source Project: arcusplatform   File: HubDAOImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ModelEntity findHubModel(String id) {
   try(Context ctxt = findHubModelTimer.time()) {
      BoundStatement boundStatement = new BoundStatement(findById);
      boundStatement.bind(id);
      Row r = session.execute(boundStatement).one();
      return toModel(r);
   }
}
 
Example 14
protected void save(SimpleSession ss) {

      //Cassandra TTL values are in seconds, so we need to convert from Shiro's millis:
      int timeoutInSeconds = (int)(ss.getTimeout() / 1000);

      PreparedStatement ps = prepareSaveStatement();
      BoundStatement bs = new BoundStatement(ps);

      byte[] serialized = serializer.serialize(ss);

      ByteBuffer bytes = ByteBuffer.wrap(serialized);

      bs.bind(
            timeoutInSeconds,
            ss.getStartTimestamp(),
            ss.getStopTimestamp() != null ? ss.getStartTimestamp() : null,
            ss.getLastAccessTime(),
            ss.getTimeout(),
            ss.isExpired(),
            ss.getHost(),
            bytes,
            ss.getId()
      );
      // TODO drop this down when we add session caching
      bs.setConsistencyLevel(ConsistencyLevel.LOCAL_QUORUM);

      cassandraSession.execute(bs);
   }
 
Example 15
Source Project: glowroot   File: CassandraAsyncIT.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void transactionMarker() throws Exception {
    PreparedStatement preparedStatement =
            session.prepare("INSERT INTO test.users (id,  fname, lname) VALUES (?, ?, ?)");
    BoundStatement boundStatement = new BoundStatement(preparedStatement);
    boundStatement.bind(100, "f100", "l100");
    session.executeAsync(boundStatement).get();
}
 
Example 16
@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);
  }
}
 
Example 17
private Long fetchKeyTokenFromDB(Object keyValue)
{
  PreparedStatement statement = store.getSession().prepare(tokenQuery);
  BoundStatement boundStatement = new BoundStatement(statement);
  boundStatement.bind(keyValue);
  ResultSet rs = store.getSession().execute(boundStatement);
  Long keyTokenValue = rs.one().getLong(0);
  return keyTokenValue;
}
 
Example 18
@Override
public void deleteKit(String hubId) {
	BoundStatement bound = new BoundStatement(deleteKit);
	bound.bind(hubId);
	session.execute(bound);
}
 
Example 19
@Override
public void delete(UUID placeId, UUID incidentId) {
   BoundStatement bound = new BoundStatement(delete);
   bound.bind(placeId, incidentId);
   session.execute(bound);
}
 
Example 20
@Override
public void offline(String protocolAddress) {
   BoundStatement stmt = new BoundStatement(offline);
   stmt.bind(IpcdDevice.ConnState.OFFLINE.name(), protocolAddress);
   session.execute(stmt);
}