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

The following examples show how to use com.datastax.driver.core.BoundStatement#setToNull() . 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: ProactiveCredsDAO.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
public void upsert(UUID placeId, String assistant, ProactiveCreds credentials) {
   try(Timer.Context ctxt = upsertTimer.time()) {
      BoundStatement stmt = new BoundStatement(upsert);
      stmt.setUUID(Columns.placeId.name(), placeId);
      stmt.setString(Columns.assistant.name(), assistant);
      stmt.setString(Columns.access.name(), credentials.getAccess());
      if(credentials.getAccessExpiry() != null) {
         stmt.setTimestamp(Columns.accessExpiry.name(), credentials.getAccessExpiry());
      } else {
         stmt.setToNull(Columns.accessExpiry.name());
      }
      if(credentials.getRefresh() != null) {
         stmt.setString(Columns.refresh.name(), credentials.getRefresh());
      } else {
         stmt.setToNull(Columns.refresh.name());
      }
      session.execute(stmt);
   }
}
 
Example 2
Source File: TraceDaoImpl.java    From glowroot with Apache License 2.0 6 votes vote down vote up
private static void bindSlowPoint(BoundStatement boundStatement, String agentRollupId,
        String agentId, String traceId, Trace.Header header, int adjustedTTL, boolean overall,
        boolean partial, boolean cassandra2x) throws IOException {
    int i = bind(boundStatement, agentRollupId, agentId, traceId, header, overall,
            partial && !cassandra2x);
    if (partial) {
        if (cassandra2x) {
            // don't set real_capture_time, so this still looks like data prior to 0.13.1
            boundStatement.setToNull(i++);
        } else {
            boundStatement.setTimestamp(i++, new Date(header.getCaptureTime()));
        }
    }
    boundStatement.setLong(i++, header.getDurationNanos());
    boundStatement.setBool(i++, header.hasError());
    boundStatement.setString(i++, header.getHeadline());
    boundStatement.setString(i++, Strings.emptyToNull(header.getUser()));
    List<Trace.Attribute> attributes = header.getAttributeList();
    if (attributes.isEmpty()) {
        boundStatement.setToNull(i++);
    } else {
        boundStatement.setBytes(i++, Messages.toByteBuffer(attributes));
    }
    boundStatement.setInt(i++, adjustedTTL);
}
 
Example 3
Source File: TraceDaoImpl.java    From glowroot with Apache License 2.0 6 votes vote down vote up
private static void bindErrorPoint(BoundStatement boundStatement, String agentRollupId,
        String agentId, String traceId, Trace.Header header, int adjustedTTL, boolean overall)
        throws IOException {
    int i = bind(boundStatement, agentRollupId, agentId, traceId, header, overall, false);
    boundStatement.setLong(i++, header.getDurationNanos());
    boundStatement.setString(i++, header.getError().getMessage());
    boundStatement.setString(i++, header.getHeadline());
    boundStatement.setString(i++, Strings.emptyToNull(header.getUser()));
    List<Trace.Attribute> attributes = header.getAttributeList();
    if (attributes.isEmpty()) {
        boundStatement.setToNull(i++);
    } else {
        boundStatement.setBytes(i++, Messages.toByteBuffer(attributes));
    }
    boundStatement.setInt(i++, adjustedTTL);
}
 
Example 4
Source File: CassandraAlarmIncidentDAO.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
@Override
public void upsert(AlarmIncident incident) {
   PreparedStatement pStmt = incident.isCleared() ? upsertWithTtl : upsert;
   BoundStatement bound = new BoundStatement(pStmt);
   bound.setUUID(Column.placeid.name(), incident.getPlaceId());
   bound.setUUID(Column.incidentid.name(), incident.getId());
   bound.setString(Column.alertstate.name(), incident.getAlertState().name());
   if(incident.getPlatformAlertState() == null) {
      bound.setString(Column.platformstate.name(), incident.getAlertState().name());
   }
   else {
      bound.setString(Column.platformstate.name(), incident.getPlatformAlertState().name());
   }
   if(incident.getHubAlertState() == null) {
      bound.setToNull(Column.hubstate.name());
   }
   else {
      bound.setString(Column.hubstate.name(), incident.getHubAlertState().name());
   }
   bound.setSet(Column.activealerts.name(), incident.getActiveAlerts());
   bound.setSet(Column.additionalalerts.name(), incident.getAdditionalAlerts().stream().map(AlertType::name).collect(Collectors.toSet()));
   bound.setString(Column.alert.name(), incident.getAlert().name());
   bound.setString(Column.cancelledby.name(), incident.getCancelledBy() == null ? null : incident.getCancelledBy().getRepresentation());
   bound.setTimestamp(Column.prealertendtime.name(), incident.getPrealertEndTime());
   bound.setTimestamp(Column.endtime.name(), incident.getEndTime());
   bound.setString(Column.monitoringstate.name(), incident.getMonitoringState().name());
   bound.setList(Column.tracker.name(), incident.getTracker().stream().map((te) -> JSON.toJson(te.toMap())).collect(Collectors.toList()));
   bound.setBool(Column.mockincident.name(), incident.isMockIncident());
   bound.setBool(Column.monitored.name(), incident.isMonitored());
   bound.setBool(Column.confirmed.name(),  incident.isConfirmed());
   session.execute(bound);
}
 
Example 5
Source File: RuleDaoImpl.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
@Override
protected Statement prepareUpsert(RuleDefinition definition, Date ts) {
   // note this method does not update lastExecuted
   BoundStatement bs = upsert.bind();
   bs.setUUID(Column.PLACE_ID.columnName(), definition.getPlaceId());
   bs.setInt(Column.ID.columnName(), definition.getSequenceId());
   bs.setTimestamp(Column.CREATED.columnName(), definition.getCreated());
   bs.setTimestamp(Column.MODIFIED.columnName(), definition.getModified());
   bs.setString(Column.NAME.columnName(), definition.getName());
   bs.setString(Column.DESCRIPTION.columnName(), definition.getDescription());
   bs.setSet(Column.TAGS.columnName(), definition.getTags());
   bs.setBool(RuleColumn.DISABLED.columnName(), definition.isDisabled());
   bs.setBool(RuleColumn.SUSPENDED.columnName(), definition.isSuspended());
   bs.setString(RuleColumn.TEMPLATE2.columnName(), definition.getRuleTemplate());
   bs.setString(RuleColumn.VARIABLES.columnName(), JSON.toJson(definition.getVariables()));

   if((definition instanceof StatefulRuleDefinition)) {
      StatefulRuleDefinition stateful = (StatefulRuleDefinition)definition;
      bs.setString(RuleColumn.ACTIONCONFIG.columnName(), JSON.toJson(stateful.getAction()));
      bs.setString(RuleColumn.CONDITIONCONFIG.columnName(),JSON.toJson(stateful.getCondition()));
      bs.setToNull(RuleColumn.EXPRESSIONS.columnName());
   }

   if((definition instanceof LegacyRuleDefinition)) {
      bs.setToNull(RuleColumn.ACTIONCONFIG.columnName());
      bs.setToNull(RuleColumn.CONDITIONCONFIG.columnName());
      bs.setString(RuleColumn.EXPRESSIONS.columnName(), JSON.toJson(((LegacyRuleDefinition) definition).getExpressions()));
   }

   return bs;
}
 
Example 6
Source File: CassandraBaseAttributesDao.java    From iotplatform with Apache License 2.0 5 votes vote down vote up
@Override
public ListenableFuture<Void> save(EntityId entityId, String attributeType, AttributeKvEntry attribute) {
    BoundStatement stmt = getSaveStmt().bind();
    stmt.setString(0, entityId.getEntityType().name());
    stmt.setUUID(1, entityId.getId());
    stmt.setString(2, attributeType);
    stmt.setString(3, attribute.getKey());
    stmt.setLong(4, attribute.getLastUpdateTs());
    stmt.setString(5, attribute.getStrValue().orElse(null));
    if (attribute.getBooleanValue().isPresent()) {
        stmt.setBool(6, attribute.getBooleanValue().get());
    } else {
        stmt.setToNull(6);
    }
    if (attribute.getLongValue().isPresent()) {
        stmt.setLong(7, attribute.getLongValue().get());
    } else {
        stmt.setToNull(7);
    }
    if (attribute.getDoubleValue().isPresent()) {
        stmt.setDouble(8, attribute.getDoubleValue().get());
    } else {
        stmt.setToNull(8);
    }
    log.trace("Generated save stmt [{}] for entityId {} and attributeType {} and attribute", stmt, entityId, attributeType, attribute);
    return getFuture(executeAsyncWrite(stmt), rs -> null);
}
 
Example 7
Source File: AggregateDaoImpl.java    From glowroot with Apache License 2.0 5 votes vote down vote up
private ListenableFuture<?> rollupThroughputFromRows(RollupParams rollup, AggregateQuery query,
        Iterable<Row> rows) throws Exception {
    long transactionCount = 0;
    // error_count is null for data inserted prior to glowroot central 0.9.18
    // rolling up any interval with null error_count should result in null error_count
    boolean hasMissingErrorCount = false;
    long errorCount = 0;
    for (Row row : rows) {
        transactionCount += row.getLong(0);
        if (row.isNull(1)) {
            hasMissingErrorCount = true;
        } else {
            errorCount += row.getLong(1);
        }
    }
    BoundStatement boundStatement;
    if (query.transactionName() == null) {
        boundStatement = getInsertOverallPS(throughputTable, rollup.rollupLevel()).bind();
    } else {
        boundStatement = getInsertTransactionPS(throughputTable, rollup.rollupLevel()).bind();
    }
    int i = 0;
    boundStatement.setString(i++, rollup.agentRollupId());
    boundStatement.setString(i++, query.transactionType());
    if (query.transactionName() != null) {
        boundStatement.setString(i++, query.transactionName());
    }
    boundStatement.setTimestamp(i++, new Date(query.to()));
    boundStatement.setLong(i++, transactionCount);
    if (hasMissingErrorCount) {
        boundStatement.setToNull(i++);
    } else {
        boundStatement.setLong(i++, errorCount);
    }
    boundStatement.setInt(i++, rollup.adjustedTTL().generalTTL());
    return session.writeAsync(boundStatement);
}
 
Example 8
Source File: AggregateDaoImpl.java    From glowroot with Apache License 2.0 5 votes vote down vote up
private ListenableFuture<?> insertQueries(List<MutableQuery> queries, int rollupLevel,
        String agentRollupId, String transactionType, @Nullable String transactionName,
        long captureTime, TTL adjustedTTL) throws Exception {
    List<ListenableFuture<?>> futures = new ArrayList<>();
    for (MutableQuery query : queries) {
        BoundStatement boundStatement;
        if (transactionName == null) {
            boundStatement = getInsertOverallPS(queryTable, rollupLevel).bind();
        } else {
            boundStatement = getInsertTransactionPS(queryTable, rollupLevel).bind();
        }
        String fullTextSha1 = query.getFullTextSha1();
        int i = 0;
        boundStatement.setString(i++, agentRollupId);
        boundStatement.setString(i++, transactionType);
        if (transactionName != null) {
            boundStatement.setString(i++, transactionName);
        }
        boundStatement.setTimestamp(i++, new Date(captureTime));
        boundStatement.setString(i++, query.getType());
        boundStatement.setString(i++, query.getTruncatedText());
        // full_query_text_sha1 cannot be null since it is used in clustering key
        boundStatement.setString(i++, Strings.nullToEmpty(fullTextSha1));
        boundStatement.setDouble(i++, query.getTotalDurationNanos());
        boundStatement.setLong(i++, query.getExecutionCount());
        if (query.hasTotalRows()) {
            boundStatement.setLong(i++, query.getTotalRows());
        } else {
            boundStatement.setToNull(i++);
        }
        boundStatement.setInt(i++, adjustedTTL.queryTTL());
        futures.add(session.writeAsync(boundStatement));
    }
    return Futures.allAsList(futures);
}
 
Example 9
Source File: TraceDaoImpl.java    From glowroot with Apache License 2.0 5 votes vote down vote up
private static void bindCount(BoundStatement boundStatement, String agentRollupId,
        String agentId, String traceId, Trace.Header header, int adjustedTTL, boolean overall,
        boolean partial, boolean cassandra2x) {
    int i = bind(boundStatement, agentRollupId, agentId, traceId, header, overall,
            partial && !cassandra2x);
    if (partial) {
        if (cassandra2x) {
            // don't set real_capture_time, so this still looks like data prior to 0.13.1
            boundStatement.setToNull(i++);
        } else {
            boundStatement.setTimestamp(i++, new Date(header.getCaptureTime()));
        }
    }
    boundStatement.setInt(i++, adjustedTTL);
}
 
Example 10
Source File: AggregateDaoImpl.java    From glowroot with Apache License 2.0 4 votes vote down vote up
private List<ListenableFuture<?>> insertQueries(List<Aggregate.Query> queries,
        List<Aggregate.SharedQueryText> sharedQueryTexts, int rollupLevel, String agentRollupId,
        String transactionType, @Nullable String transactionName, long captureTime,
        TTL adjustedTTL) throws Exception {
    List<ListenableFuture<?>> futures = new ArrayList<>();
    for (Aggregate.Query query : queries) {
        Aggregate.SharedQueryText sharedQueryText =
                sharedQueryTexts.get(query.getSharedQueryTextIndex());
        BoundStatement boundStatement;
        if (transactionName == null) {
            boundStatement = getInsertOverallPS(queryTable, rollupLevel).bind();
        } else {
            boundStatement = getInsertTransactionPS(queryTable, rollupLevel).bind();
        }
        int i = 0;
        boundStatement.setString(i++, agentRollupId);
        boundStatement.setString(i++, transactionType);
        if (transactionName != null) {
            boundStatement.setString(i++, transactionName);
        }
        boundStatement.setTimestamp(i++, new Date(captureTime));
        boundStatement.setString(i++, query.getType());
        String fullTextSha1 = sharedQueryText.getFullTextSha1();
        if (fullTextSha1.isEmpty()) {
            boundStatement.setString(i++, sharedQueryText.getFullText());
            // full_query_text_sha1 cannot be null since it is used in clustering key
            boundStatement.setString(i++, "");
        } else {
            boundStatement.setString(i++, sharedQueryText.getTruncatedText());
            boundStatement.setString(i++, fullTextSha1);
        }
        boundStatement.setDouble(i++, query.getTotalDurationNanos());
        boundStatement.setLong(i++, query.getExecutionCount());
        if (query.hasTotalRows()) {
            boundStatement.setLong(i++, query.getTotalRows().getValue());
        } else {
            boundStatement.setToNull(i++);
        }
        boundStatement.setInt(i++, adjustedTTL.queryTTL());
        futures.add(session.writeAsync(boundStatement));
    }
    return futures;
}
 
Example 11
Source File: SyntheticResultDaoImpl.java    From glowroot with Apache License 2.0 4 votes vote down vote up
@Override
public void store(String agentRollupId, String syntheticMonitorId,
        String syntheticMonitorDisplay, long captureTime, long durationNanos,
        @Nullable String errorMessage) throws Exception {
    int ttl = getTTLs().get(0);
    long maxCaptureTime = 0;
    BoundStatement boundStatement = insertResultPS.get(0).bind();
    maxCaptureTime = Math.max(captureTime, maxCaptureTime);
    int adjustedTTL = Common.getAdjustedTTL(ttl, captureTime, clock);
    int i = 0;
    boundStatement.setString(i++, agentRollupId);
    boundStatement.setString(i++, syntheticMonitorId);
    boundStatement.setTimestamp(i++, new Date(captureTime));
    boundStatement.setDouble(i++, durationNanos);
    boundStatement.setLong(i++, 1);
    if (errorMessage == null) {
        boundStatement.setToNull(i++);
    } else {
        Stored.ErrorInterval errorInterval = Stored.ErrorInterval.newBuilder()
                .setFrom(captureTime)
                .setTo(captureTime)
                .setCount(1)
                .setMessage(errorMessage)
                .setDoNotMergeToTheLeft(false)
                .setDoNotMergeToTheRight(false)
                .build();
        boundStatement.setBytes(i++, Messages.toByteBuffer(ImmutableList.of(errorInterval)));
    }
    boundStatement.setInt(i++, adjustedTTL);
    List<Future<?>> futures = new ArrayList<>();
    futures.add(session.writeAsync(boundStatement));
    futures.addAll(syntheticMonitorIdDao.insert(agentRollupId, captureTime, syntheticMonitorId,
            syntheticMonitorDisplay));

    // wait for success before inserting "needs rollup" records
    MoreFutures.waitForAll(futures);

    // insert into synthetic_needs_rollup_1
    List<RollupConfig> rollupConfigs = configRepository.getRollupConfigs();
    long intervalMillis = rollupConfigs.get(1).intervalMillis();
    long rollupCaptureTime = CaptureTimes.getRollup(captureTime, intervalMillis);
    int needsRollupAdjustedTTL =
            Common.getNeedsRollupAdjustedTTL(adjustedTTL, configRepository.getRollupConfigs());
    boundStatement = insertNeedsRollup.get(0).bind();
    i = 0;
    boundStatement.setString(i++, agentRollupId);
    boundStatement.setTimestamp(i++, new Date(rollupCaptureTime));
    boundStatement.setUUID(i++, UUIDs.timeBased());
    boundStatement.setSet(i++, ImmutableSet.of(syntheticMonitorId));
    boundStatement.setInt(i++, needsRollupAdjustedTTL);
    session.write(boundStatement);
}