Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ff9b4dfc authored by Alexander Dorokhine's avatar Alexander Dorokhine
Browse files

Add another executor to setSchema() for running framework migrations.

The advice to add a second executor is based on API council guidance,
who do not recommend reusing the callback executor.

The executor is not yet used but will be used once schema migration is
implemented in framework.

Bug: 183177268
Test: Builds
Change-Id: Ic6735e696bc868884adffddd1433a198a005cad6
parent 5c02efbc
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -149,7 +149,8 @@ package android.app.appsearch {
    method public void remove(@NonNull String, @NonNull android.app.appsearch.SearchSpec, @NonNull java.util.concurrent.Executor, @NonNull java.util.function.Consumer<android.app.appsearch.AppSearchResult<java.lang.Void>>);
    method public void reportUsage(@NonNull android.app.appsearch.ReportUsageRequest, @NonNull java.util.concurrent.Executor, @NonNull java.util.function.Consumer<android.app.appsearch.AppSearchResult<java.lang.Void>>);
    method @NonNull public android.app.appsearch.SearchResults search(@NonNull String, @NonNull android.app.appsearch.SearchSpec);
    method public void setSchema(@NonNull android.app.appsearch.SetSchemaRequest, @NonNull java.util.concurrent.Executor, @NonNull java.util.function.Consumer<android.app.appsearch.AppSearchResult<android.app.appsearch.SetSchemaResponse>>);
    method @Deprecated public void setSchema(@NonNull android.app.appsearch.SetSchemaRequest, @NonNull java.util.concurrent.Executor, @NonNull java.util.function.Consumer<android.app.appsearch.AppSearchResult<android.app.appsearch.SetSchemaResponse>>);
    method public void setSchema(@NonNull android.app.appsearch.SetSchemaRequest, @NonNull java.util.concurrent.Executor, @NonNull java.util.concurrent.Executor, @NonNull java.util.function.Consumer<android.app.appsearch.AppSearchResult<android.app.appsearch.SetSchemaResponse>>);
  }

  public interface BatchResultCallback<KeyType, ValueType> {
+25 −8
Original line number Diff line number Diff line
@@ -103,6 +103,18 @@ public final class AppSearchSession implements Closeable {
        mDatabaseName = databaseName;
    }

    /**
     * TODO(b/181887768): This method exists only for dogfooder transition and must be removed.
     * @deprecated This method exists only for dogfooder transition and must be removed.
     */
    @Deprecated
    public void setSchema(
            @NonNull SetSchemaRequest request,
            @NonNull @CallbackExecutor Executor callbackExecutor,
            @NonNull Consumer<AppSearchResult<SetSchemaResponse>> callback) {
        setSchema(request, callbackExecutor, callbackExecutor, callback);
    }

    /**
     * Sets the schema that represents the organizational structure of data within the AppSearch
     * database.
@@ -113,7 +125,9 @@ public final class AppSearchSession implements Closeable {
     * no-op call.
     *
     * @param request the schema to set or update the AppSearch database to.
     * @param executor Executor on which to invoke the callback.
     * @param workExecutor Executor on which to schedule heavy client-side background work such as
     *                     transforming documents.
     * @param callbackExecutor Executor on which to invoke the callback.
     * @param callback Callback to receive errors resulting from setting the schema. If the
     *                 operation succeeds, the callback will be invoked with {@code null}.
     */
@@ -121,10 +135,12 @@ public final class AppSearchSession implements Closeable {
    //  exposed.
    public void setSchema(
            @NonNull SetSchemaRequest request,
            @NonNull @CallbackExecutor Executor executor,
            @NonNull Executor workExecutor,
            @NonNull @CallbackExecutor Executor callbackExecutor,
            @NonNull Consumer<AppSearchResult<SetSchemaResponse>> callback) {
        Objects.requireNonNull(request);
        Objects.requireNonNull(executor);
        Objects.requireNonNull(workExecutor);
        Objects.requireNonNull(callbackExecutor);
        Objects.requireNonNull(callback);
        Preconditions.checkState(!mIsClosed, "AppSearchSession has already been closed");
        List<Bundle> schemaBundles = new ArrayList<>(request.getSchemas().size());
@@ -153,10 +169,12 @@ public final class AppSearchSession implements Closeable {
                    request.getVersion(),
                    new IAppSearchResultCallback.Stub() {
                        public void onResult(AppSearchResult result) {
                            executor.execute(() -> {
                            callbackExecutor.execute(() -> {
                                if (result.isSuccess()) {
                                    callback.accept(
                                            // TODO(b/177266929) implement Migration in platform.
                                            // TODO(b/183177268): once migration is implemented, run
                                            //  it on workExecutor.
                                            AppSearchResult.newSuccessfulResult(
                                                    new SetSchemaResponse.Builder().build()));
                                } else {
@@ -332,8 +350,7 @@ public final class AppSearchSession implements Closeable {

                                // Translate successful results
                                for (Map.Entry<String, Bundle> bundleEntry :
                                        (Set<Map.Entry<String, Bundle>>)
                                                result.getSuccesses().entrySet()) {
                                        ((Map<String, Bundle>) result.getSuccesses()).entrySet()) {
                                    GenericDocument document;
                                    try {
                                        document = new GenericDocument(bundleEntry.getValue());
@@ -352,8 +369,8 @@ public final class AppSearchSession implements Closeable {

                                // Translate failed results
                                for (Map.Entry<String, AppSearchResult<Bundle>> bundleEntry :
                                        (Set<Map.Entry<String, AppSearchResult<Bundle>>>)
                                                result.getFailures().entrySet()) {
                                        ((Map<String, AppSearchResult<Bundle>>)
                                                result.getFailures()).entrySet()) {
                                    documentResultBuilder.setFailure(
                                            bundleEntry.getKey(),
                                            bundleEntry.getValue().getResultCode(),
+1 −1
Original line number Diff line number Diff line
@@ -89,7 +89,7 @@ public class AppSearchSessionShimImpl implements AppSearchSessionShim {
    @NonNull
    public ListenableFuture<SetSchemaResponse> setSchema(@NonNull SetSchemaRequest request) {
        SettableFuture<AppSearchResult<SetSchemaResponse>> future = SettableFuture.create();
        mAppSearchSession.setSchema(request, mExecutor, future::set);
        mAppSearchSession.setSchema(request, mExecutor, mExecutor, future::set);
        return Futures.transformAsync(future, this::transformResult, mExecutor);
    }

+1 −1
Original line number Diff line number Diff line
@@ -51,7 +51,7 @@ public class AppSearchSessionUnitTest {
        CompletableFuture<AppSearchResult<SetSchemaResponse>> schemaFuture =
                new CompletableFuture<>();
        mSearchSession.setSchema(
                new SetSchemaRequest.Builder().setForceOverride(true).build(), mExecutor,
                new SetSchemaRequest.Builder().setForceOverride(true).build(), mExecutor, mExecutor,
                schemaFuture::complete);

        schemaFuture.get().getResultValue();