Skip to content

Commit

Permalink
Sync-Stack: Tables (#33143)
Browse files Browse the repository at this point in the history
* Prepare November 2022 Beta Release for Search

* Cherry-Pick updates to Changelog and version_client.txt from November non-main patch release for Search

* Autorest regeneration with Sync Stack

* Stashing my sync stack work

* Synchronous implementation of our Clients

* Update to CHANGELOG.md

* Fix PR build issues

* Fix PR build issues

* Removed TODOs left behind.

* Adjusting whitespaces

* Removing unnecessary annotation

* Adjustments to how context information:
- Added Synchronous Rest Proxy Context
- Added Trailing Context

Checkstyle adjustments

* Removed unused import

* Fixed issues identified by Spotbugs

* Bug fixes

* Checkstyle fixes

* Added Asserting Clients

* Add asserting client to clients used for playback

* Replaced method threads with a static Thread Pool

* Tables Refactoring

* Updating to only use threadpool for timeout cases.

* Additional refactoring

* Removed unused imports

* Fixed Checkstyle Issues

* Bug fix for failing Cosmos test

---------

Co-authored-by: Azure SDK Bot <53356347+azure-sdk@users.noreply.github.com>
  • Loading branch information
jairmyree and azure-sdk authored Mar 3, 2023
1 parent ad9dc29 commit 8130060
Show file tree
Hide file tree
Showing 55 changed files with 4,680 additions and 896 deletions.
1 change: 1 addition & 0 deletions sdk/tables/azure-data-tables/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### Bugs Fixed

### Other Changes
- Removed Reactor from synchronous clients

## 12.3.8 (2023-02-16)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,25 @@
import com.azure.core.annotation.ServiceClient;
import com.azure.core.annotation.ServiceMethod;
import com.azure.core.credential.AzureNamedKeyCredential;
import com.azure.core.http.HttpHeaders;
import com.azure.core.http.HttpPipeline;
import com.azure.core.http.HttpRequest;
import com.azure.core.http.rest.PagedFlux;
import com.azure.core.http.rest.PagedResponse;
import com.azure.core.http.rest.Response;
import com.azure.core.http.rest.ResponseBase;
import com.azure.core.http.rest.SimpleResponse;
import com.azure.core.util.Context;
import com.azure.core.util.FluxUtil;
import com.azure.core.util.IterableStream;
import com.azure.core.util.ServiceVersion;
import com.azure.core.util.logging.ClientLogger;
import com.azure.core.util.serializer.SerializerAdapter;
import com.azure.data.tables.implementation.AzureTableImpl;
import com.azure.data.tables.implementation.AzureTableImplBuilder;
import com.azure.data.tables.implementation.EntityPaged;
import com.azure.data.tables.implementation.ModelHelper;
import com.azure.data.tables.implementation.TableSasGenerator;
import com.azure.data.tables.implementation.TableSasUtils;
import com.azure.data.tables.implementation.TableUtils;
import com.azure.data.tables.implementation.TransactionalBatchImpl;
import com.azure.data.tables.implementation.models.AccessPolicy;
import com.azure.data.tables.implementation.models.OdataMetadataFormat;
import com.azure.data.tables.implementation.models.QueryOptions;
import com.azure.data.tables.implementation.models.ResponseFormat;
Expand All @@ -43,7 +40,6 @@
import com.azure.data.tables.implementation.models.TransactionalBatchSubmitBatchHeaders;
import com.azure.data.tables.models.ListEntitiesOptions;
import com.azure.data.tables.models.TableAccessPolicies;
import com.azure.data.tables.models.TableAccessPolicy;
import com.azure.data.tables.models.TableEntity;
import com.azure.data.tables.models.TableEntityUpdateMode;
import com.azure.data.tables.models.TableItem;
Expand Down Expand Up @@ -238,6 +234,7 @@ public String generateSas(TableSasSignatureValues tableSasSignatureValues) {
return new TableSasGenerator(tableSasSignatureValues, getTableName(), azureNamedKeyCredential).getSas();
}


/**
* Creates the table within the Tables service.
*
Expand Down Expand Up @@ -287,7 +284,7 @@ public Mono<Response<TableItem>> createTableWithResponse() {
}

Mono<Response<TableItem>> createTableWithResponse(Context context) {
context = context == null ? Context.NONE : context;
context = TableUtils.setContext(context);
final TableProperties properties = new TableProperties().setTableName(tableName);

try {
Expand Down Expand Up @@ -349,7 +346,7 @@ public Mono<Response<Void>> deleteTableWithResponse() {
}

Mono<Response<Void>> deleteTableWithResponse(Context context) {
context = context == null ? Context.NONE : context;
context = TableUtils.setContext(context);

try {
return tablesImplementation.getTables().deleteWithResponseAsync(tableName, null, context)
Expand Down Expand Up @@ -432,7 +429,7 @@ public Mono<Response<Void>> createEntityWithResponse(TableEntity entity) {
}

Mono<Response<Void>> createEntityWithResponse(TableEntity entity, Context context) {
context = context == null ? Context.NONE : context;
context = TableUtils.setContext(context);

if (entity == null) {
return monoError(logger, new IllegalArgumentException("'entity' cannot be null."));
Expand Down Expand Up @@ -532,14 +529,14 @@ public Mono<Response<Void>> upsertEntityWithResponse(TableEntity entity, TableEn

Mono<Response<Void>> upsertEntityWithResponse(TableEntity entity, TableEntityUpdateMode updateMode,
Context context) {
context = context == null ? Context.NONE : context;
context = TableUtils.setContext(context);

if (entity == null) {
return monoError(logger, new IllegalArgumentException("'entity' cannot be null."));
}

String partitionKey = escapeSingleQuotes(entity.getPartitionKey());
String rowKey = escapeSingleQuotes(entity.getRowKey());
String partitionKey = TableUtils.escapeSingleQuotes(entity.getPartitionKey());
String rowKey = TableUtils.escapeSingleQuotes(entity.getRowKey());

EntityHelper.setPropertiesFromGetters(entity, logger);

Expand Down Expand Up @@ -699,14 +696,14 @@ public Mono<Response<Void>> updateEntityWithResponse(TableEntity entity, TableEn

Mono<Response<Void>> updateEntityWithResponse(TableEntity entity, TableEntityUpdateMode updateMode,
boolean ifUnchanged, Context context) {
context = context == null ? Context.NONE : context;
context = TableUtils.setContext(context);

if (entity == null) {
return monoError(logger, new IllegalArgumentException("'entity' cannot be null."));
}

String partitionKey = escapeSingleQuotes(entity.getPartitionKey());
String rowKey = escapeSingleQuotes(entity.getRowKey());
String partitionKey = TableUtils.escapeSingleQuotes(entity.getPartitionKey());
String rowKey = TableUtils.escapeSingleQuotes(entity.getRowKey());
String eTag = ifUnchanged ? entity.getETag() : "*";

EntityHelper.setPropertiesFromGetters(entity, logger);
Expand Down Expand Up @@ -839,7 +836,7 @@ public Mono<Response<Void>> deleteEntityWithResponse(TableEntity entity, boolean

Mono<Response<Void>> deleteEntityWithResponse(String partitionKey, String rowKey, String eTag, boolean ifUnchanged,
Context context) {
context = context == null ? Context.NONE : context;
context = TableUtils.setContext(context);
eTag = ifUnchanged ? eTag : "*";

if (isNullOrEmpty(partitionKey) || isNullOrEmpty(rowKey)) {
Expand All @@ -848,7 +845,7 @@ Mono<Response<Void>> deleteEntityWithResponse(String partitionKey, String rowKey

try {
return tablesImplementation.getTables().deleteEntityWithResponseAsync(tableName,
escapeSingleQuotes(partitionKey), escapeSingleQuotes(rowKey), eTag, null, null, null, context)
TableUtils.escapeSingleQuotes(partitionKey), TableUtils.escapeSingleQuotes(rowKey), eTag, null, null, null, context)
.onErrorMap(TableUtils::mapThrowableToTableServiceException)
.map(response -> (Response<Void>) new SimpleResponse<Void>(response, null))
.onErrorResume(TableServiceException.class, e -> swallowExceptionForStatusCode(404, e, logger));
Expand Down Expand Up @@ -967,7 +964,7 @@ private <T extends TableEntity> Mono<PagedResponse<T>> listEntitiesNextPage(Stri
private <T extends TableEntity> Mono<PagedResponse<T>> listEntities(String nextPartitionKey, String nextRowKey,
Context context, ListEntitiesOptions options,
Class<T> resultType) {
context = context == null ? Context.NONE : context;
context = TableUtils.setContext(context);
String select = null;

if (options.getSelect() != null) {
Expand Down Expand Up @@ -1011,53 +1008,6 @@ private <T extends TableEntity> Mono<PagedResponse<T>> listEntities(String nextP
}
}

private static class EntityPaged<T extends TableEntity> implements PagedResponse<T> {
private final Response<TableEntityQueryResponse> httpResponse;
private final IterableStream<T> entityStream;
private final String continuationToken;

EntityPaged(Response<TableEntityQueryResponse> httpResponse, List<T> entityList,
String nextPartitionKey, String nextRowKey) {
if (nextPartitionKey == null || nextRowKey == null) {
this.continuationToken = null;
} else {
this.continuationToken = String.join(DELIMITER_CONTINUATION_TOKEN, nextPartitionKey, nextRowKey);
}

this.httpResponse = httpResponse;
this.entityStream = IterableStream.of(entityList);
}

@Override
public int getStatusCode() {
return httpResponse.getStatusCode();
}

@Override
public HttpHeaders getHeaders() {
return httpResponse.getHeaders();
}

@Override
public HttpRequest getRequest() {
return httpResponse.getRequest();
}

@Override
public IterableStream<T> getElements() {
return entityStream;
}

@Override
public String getContinuationToken() {
return continuationToken;
}

@Override
public void close() {
}
}

/**
* Gets a single {@link TableEntity entity} from the table.
*
Expand Down Expand Up @@ -1156,7 +1106,7 @@ <T extends TableEntity> Mono<Response<T>> getEntityWithResponse(String partition

try {
return tablesImplementation.getTables().queryEntityWithPartitionAndRowKeyWithResponseAsync(tableName,
escapeSingleQuotes(partitionKey), escapeSingleQuotes(rowKey), null, null, queryOptions, context)
TableUtils.escapeSingleQuotes(partitionKey), TableUtils.escapeSingleQuotes(rowKey), null, null, queryOptions, context)
.onErrorMap(TableUtils::mapThrowableToTableServiceException)
.handle((response, sink) -> {
final Map<String, Object> matchingEntity = response.getValue();
Expand Down Expand Up @@ -1248,41 +1198,21 @@ public Mono<Response<TableAccessPolicies>> getAccessPoliciesWithResponse() {
}

Mono<Response<TableAccessPolicies>> getAccessPoliciesWithResponse(Context context) {
context = context == null ? Context.NONE : context;
context = TableUtils.setContext(context);

try {
return tablesImplementation.getTables()
.getAccessPolicyWithResponseAsync(tableName, null, null, context)
.onErrorMap(TableUtils::mapThrowableToTableServiceException)
.map(response -> new SimpleResponse<>(response,
new TableAccessPolicies(response.getValue() == null ? null : response.getValue().stream()
.map(this::toTableSignedIdentifier)
.map(TableUtils::toTableSignedIdentifier)
.collect(Collectors.toList()))));
} catch (RuntimeException e) {
return monoError(logger, e);
}
}

private TableSignedIdentifier toTableSignedIdentifier(SignedIdentifier signedIdentifier) {
if (signedIdentifier == null) {
return null;
}

return new TableSignedIdentifier(signedIdentifier.getId())
.setAccessPolicy(toTableAccessPolicy(signedIdentifier.getAccessPolicy()));
}

private TableAccessPolicy toTableAccessPolicy(AccessPolicy accessPolicy) {
if (accessPolicy == null) {
return null;
}

return new TableAccessPolicy()
.setExpiresOn(accessPolicy.getExpiry())
.setStartsOn(accessPolicy.getStart())
.setPermissions(accessPolicy.getPermission());
}

/**
* Sets stored {@link TableAccessPolicies access policies} for the table that may be used with Shared Access
* Signatures.
Expand Down Expand Up @@ -1368,7 +1298,7 @@ public Mono<Response<Void>> setAccessPoliciesWithResponse(List<TableSignedIdenti

Mono<Response<Void>> setAccessPoliciesWithResponse(List<TableSignedIdentifier> tableSignedIdentifiers,
Context context) {
context = context == null ? Context.NONE : context;
context = TableUtils.setContext(context);
List<SignedIdentifier> signedIdentifiers = null;

/*
Expand All @@ -1380,7 +1310,7 @@ OffsetDateTime.now will only give back milliseconds (more precise fields are zer
if (tableSignedIdentifiers != null) {
signedIdentifiers = tableSignedIdentifiers.stream()
.map(tableSignedIdentifier -> {
SignedIdentifier signedIdentifier = toSignedIdentifier(tableSignedIdentifier);
SignedIdentifier signedIdentifier = TableUtils.toSignedIdentifier(tableSignedIdentifier);

if (signedIdentifier != null) {
if (signedIdentifier.getAccessPolicy() != null
Expand Down Expand Up @@ -1415,27 +1345,6 @@ OffsetDateTime.now will only give back milliseconds (more precise fields are zer
}
}

private SignedIdentifier toSignedIdentifier(TableSignedIdentifier tableSignedIdentifier) {
if (tableSignedIdentifier == null) {
return null;
}

return new SignedIdentifier()
.setId(tableSignedIdentifier.getId())
.setAccessPolicy(toAccessPolicy(tableSignedIdentifier.getAccessPolicy()));
}

private AccessPolicy toAccessPolicy(TableAccessPolicy tableAccessPolicy) {
if (tableAccessPolicy == null) {
return null;
}

return new AccessPolicy()
.setExpiry(tableAccessPolicy.getExpiresOn())
.setStart(tableAccessPolicy.getStartsOn())
.setPermission(tableAccessPolicy.getPermissions());
}

/**
* Executes all {@link TableTransactionAction actions} within the list inside a transaction. When the call
* completes, either all {@link TableTransactionAction actions} in the transaction will succeed, or if a failure
Expand Down Expand Up @@ -1617,7 +1526,7 @@ public Mono<Response<TableTransactionResult>> submitTransactionWithResponse(List
}

Mono<Response<TableTransactionResult>> submitTransactionWithResponse(List<TableTransactionAction> transactionActions, Context context) {
Context finalContext = context == null ? Context.NONE : context;
Context finalContext = TableUtils.setContext(context);

if (transactionActions.isEmpty()) {
return monoError(logger,
Expand Down Expand Up @@ -1750,14 +1659,4 @@ private Mono<Response<List<TableTransactionActionResponse>>> parseResponse(Trans
return Mono.just(new SimpleResponse<>(response, Arrays.asList(response.getValue())));
}
}

// Single quotes in OData queries should be escaped by using two consecutive single quotes characters.
// Source: http://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part2-url-conventions.html#sec_URLSyntax.
private String escapeSingleQuotes(String input) {
if (input == null) {
return null;
}

return input.replace("'", "''");
}
}
Loading

0 comments on commit 8130060

Please sign in to comment.