return setToken("partition key token", v);
return setToken("partition key token", v);
return setToken("partition key token", v);
return setToken("partition key token", v);
/** * Validates that a {@link Token} can be retrieved and parsed by using bind variables and * aliasing. * * <p> * * <p>This test does the following: * * <p> * * <ol> * <li>Retrieve the token by alias for the key '1', and ensure it matches the token by index. * <li>Select data by token using setToken by name. * </ol> */ @Test(groups = "short") @CassandraVersion("2.0") public void should_get_token_from_row_and_set_token_in_query_with_binding_and_aliasing() { Row row = session().execute("SELECT token(i) AS t FROM foo WHERE i = 1").one(); Token token = row.getToken("t"); assertThat(token.getType()).isEqualTo(expectedTokenType); PreparedStatement pst = session().prepare("SELECT * FROM foo WHERE token(i) = :myToken"); row = session().execute(pst.bind().setToken("myToken", token)).one(); assertThat(row.getInt(0)).isEqualTo(1); row = session().execute("SELECT * FROM foo WHERE token(i) = ?", token).one(); assertThat(row.getInt(0)).isEqualTo(1); }
assertThat(row.getInt(0)).isEqualTo(1); row = session().execute(pst.bind().setToken(0, token)).one(); assertThat(row.getInt(0)).isEqualTo(1);
private Map<String,String> getMappingTable() { // You only want to prepare the query once. Best to do it when you initialize the session. PreparedStatement findMappings = session.prepare( "SELECT project_id, project_name " + "FROM openshift_metrics.metrics_mappings " + "WHERE token(project_id) > ? AND token(project_id) <= ?"); Map<String,String> mappings = new HashMap<>(); if (hasMappingTable()) { for (TokenRange tokenRange : getTokenRanges()) { BoundStatement boundStatement = findMappings.bind().setToken(0, tokenRange.getStart()) .setToken(1, tokenRange.getEnd()); ResultSet resultSet = session.execute(boundStatement); resultSet.forEach(row -> mappings.put(row.getString(0), row.getString(1))); } } mappings.remove("%succeeded"); return mappings; }
/** * Fetch all the data from a temporary table for the compression job. Using TokenRanges avoids fetching first * all the metrics' partition keys and then requesting them. * * Performance can be improved by using data locality and fetching with multiple threads. * * @param timestamp A timestamp inside the wanted bucket (such as the previous starting row timestamp) * @param pageSize How many rows to fetch each time * @param maxConcurrency To how many streams should token ranges be split to * @return Observable of Observables per partition key */ @Override public Observable<Observable<Row>> findAllDataFromBucket(long timestamp, int pageSize, int maxConcurrency) { PreparedStatement ts = getTempStatement(MetricType.UNDEFINED, TempStatement.SCAN_WITH_TOKEN_RANGES, timestamp); // The table does not exists - case such as when starting Hawkular-Metrics for the first time just before // compression kicks in. if(ts == null || prepMap.floorKey(timestamp) == 0L) { return Observable.empty(); } return Observable.from(getTokenRanges()) .map(tr -> rxSession.executeAndFetch( ts .bind() .setToken(0, tr.getStart()) .setToken(1, tr.getEnd()) .setFetchSize(pageSize))); }
/** * Fetch all the data from a temporary table for the compression job. Using TokenRanges avoids fetching first * all the metrics' partition keys and then requesting them. * * Performance can be improved by using data locality and fetching with multiple threads. * * @param timestamp A timestamp inside the wanted bucket (such as the previous starting row timestamp) * @param pageSize How many rows to fetch each time * @param maxConcurrency To how many streams should token ranges be split to * @return Observable of Observables per partition key */ @Override public Observable<Observable<Row>> findAllDataFromBucket(long timestamp, int pageSize, int maxConcurrency) { PreparedStatement ts = getTempStatement(MetricType.UNDEFINED, TempStatement.SCAN_WITH_TOKEN_RANGES, timestamp); // The table does not exists - case such as when starting Hawkular-Metrics for the first time just before // compression kicks in. if(ts == null || prepMap.floorKey(timestamp) == 0L) { return Observable.empty(); } return Observable.from(getTokenRanges()) .map(tr -> rxSession.executeAndFetch( ts .bind() .setToken(0, tr.getStart()) .setToken(1, tr.getEnd()) .setFetchSize(pageSize))); }
@Override public KeyIterator getKeys(final KeyRangeQuery query, final StoreTransaction txh) throws BackendException { if (!this.storeManager.getFeatures().hasOrderedScan()) { throw new PermanentBackendException("This operation is only allowed when the byteorderedpartitioner is used."); } final Metadata metadata = this.session.getCluster().getMetadata(); return Try.of(() -> new CQLResultSetKeyIterator( query, this.getter, this.session.execute(this.getKeysRanged.bind() .setToken(KEY_START_BINDING, metadata.newToken(query.getKeyStart().asByteBuffer())) .setToken(KEY_END_BINDING, metadata.newToken(query.getKeyEnd().asByteBuffer())) .setBytes(SLICE_START_BINDING, query.getSliceStart().asByteBuffer()) .setBytes(SLICE_END_BINDING, query.getSliceEnd().asByteBuffer()) .setFetchSize(this.storeManager.getPageSize()) .setConsistencyLevel(getTransaction(txh).getReadConsistencyLevel())))) .getOrElseThrow(EXCEPTION_MAPPER); }
/** * Validates that a {@link Token} can be retrieved and parsed by using bind variables and * aliasing. * * <p> * * <p>This test does the following: * * <p> * * <ol> * <li>Retrieve the token by alias for the key '1', and ensure it matches the token by index. * <li>Select data by token using setToken by name. * </ol> */ @Test(groups = "short") @CassandraVersion("2.0") public void should_get_token_from_row_and_set_token_in_query_with_binding_and_aliasing() { Row row = session().execute("SELECT token(i) AS t FROM foo WHERE i = 1").one(); Token token = row.getToken("t"); assertThat(token.getType()).isEqualTo(expectedTokenType); PreparedStatement pst = session().prepare("SELECT * FROM foo WHERE token(i) = :myToken"); row = session().execute(pst.bind().setToken("myToken", token)).one(); assertThat(row.getInt(0)).isEqualTo(1); row = session().execute("SELECT * FROM foo WHERE token(i) = ?", token).one(); assertThat(row.getInt(0)).isEqualTo(1); }
assertThat(row.getInt(0)).isEqualTo(1); row = session().execute(pst.bind().setToken(0, token)).one(); assertThat(row.getInt(0)).isEqualTo(1);