Java Code Examples for com.google.cloud.spanner.KeySet#getKeys()

The following examples show how to use com.google.cloud.spanner.KeySet#getKeys() . 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: SpannerStatementQueryExecutor.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
/**
 * Builds a query that returns the rows associated with a key set with additional SQL-where.
 * The {@link org.springframework.cloud.gcp.data.spanner.core.mapping.Where} of the {@code persistentEntity} parameter
 * is ignored, you should pass the SQL-where as a {@code whereClause} parameter.
 * The secondary {@code index} will be used instead of the table name when the corresponding parameter is not null.
 * @param keySet the key set whose members to get.
 * @param persistentEntity the persistent entity of the table.
 * @param <T> the type of the persistent entity
 * @param writeConverter a converter to convert key values as needed to bind to the query statement.
 * @param mappingContext mapping context
 * @param whereClause SQL where clause
 * @param index the secondary index name
 * @return the Spanner statement to perform the retrieval.
 */
public static <T> Statement buildQuery(KeySet keySet,
		SpannerPersistentEntity<T> persistentEntity, SpannerCustomConverter writeConverter,
		SpannerMappingContext mappingContext, String whereClause, String index) {
	List<String> orParts = new ArrayList<>();
	List<String> tags = new ArrayList<>();
	List keyParts = new ArrayList();
	int tagNum = 0;
	List<SpannerPersistentProperty> keyProperties = persistentEntity.getFlattenedPrimaryKeyProperties();

	for (Key key : keySet.getKeys()) {
		StringJoiner andJoiner = new StringJoiner(" AND ");
		Iterator parentKeyParts = key.getParts().iterator();
		while (parentKeyParts.hasNext()) {
			SpannerPersistentProperty keyProp = keyProperties.get(tagNum % keyProperties.size());
			String tagName = "tag" + tagNum;
			andJoiner.add(keyProp.getColumnName() + " = @" + tagName);
			tags.add(tagName);
			keyParts.add(parentKeyParts.next());
			tagNum++;
		}
		orParts.add(andJoiner.toString());
	}
	String keyClause = orParts.stream().map(s -> "(" + s + ")").collect(Collectors.joining(" OR "));
	String condition = combineWithAnd(keyClause, whereClause);
	String sb = "SELECT " + getColumnsStringForSelect(persistentEntity, mappingContext, true) + " FROM "
			+ (StringUtils.isEmpty(index) ? persistentEntity.tableName() : String.format("%s@{FORCE_INDEX=%s}", persistentEntity.tableName(), index))
			+ (condition.isEmpty() ? "" : " WHERE " + condition);
	return buildStatementFromSqlWithArgs(sb, tags, null, writeConverter,
			keyParts.toArray(), null);
}
 
Example 2
Source File: MutationSizeEstimator.java    From beam with Apache License 2.0 5 votes vote down vote up
private static long sizeOf(KeySet keySet) {
  long result = 0;
  for (Key k : keySet.getKeys()) {
    result += sizeOf(k);
  }
  for (KeyRange kr : keySet.getRanges()) {
    result += sizeOf(kr);
  }
  return result;
}