public abstract class

Migrator

extends java.lang.Object

 java.lang.Object

↳androidx.appsearch.app.Migrator

Gradle dependencies

compile group: 'androidx.appsearch', name: 'appsearch', version: '1.0.0-alpha04'

  • groupId: androidx.appsearch
  • artifactId: appsearch
  • version: 1.0.0-alpha04

Artifact androidx.appsearch:appsearch:1.0.0-alpha04 it located at Google repository (https://maven.google.com/)

Overview

A migrator class to translate GenericDocument from different version of AppSearchSchema

Make non-backwards-compatible changes will delete all stored documents in old schema. You can save your documents by setting Migrator via the SetSchemaRequest.Builder.setMigrator(String, Migrator) for each type and target version you want to save.

Migrator.onDowngrade(int, int, GenericDocument) or Migrator.onUpgrade(int, int, GenericDocument) will be triggered if the version number of the schema stored in AppSearch is different with the version in the request.

If any error or Exception occurred in the Migrator.onDowngrade(int, int, GenericDocument) or Migrator.onUpgrade(int, int, GenericDocument), all the setSchema request will be rejected unless the schema changes are backwards-compatible, and stored documents won't have any observable changes.

Summary

Constructors
publicMigrator()

Methods
public abstract GenericDocumentonDowngrade(int currentVersion, int finalVersion, GenericDocument document)

Migrates GenericDocument to an older version of AppSearchSchema.

public abstract GenericDocumentonUpgrade(int currentVersion, int finalVersion, GenericDocument document)

Migrates GenericDocument to a newer version of AppSearchSchema.

public abstract booleanshouldMigrate(int currentVersion, int finalVersion)

Returns true if this migrator's source type needs to be migrated to update from currentVersion to finalVersion.

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public Migrator()

Methods

public abstract boolean shouldMigrate(int currentVersion, int finalVersion)

Returns true if this migrator's source type needs to be migrated to update from currentVersion to finalVersion.

Migration won't be triggered if currentVersion is equal to finalVersion even if Migrator.shouldMigrate(int, int) return true;

public abstract GenericDocument onUpgrade(int currentVersion, int finalVersion, GenericDocument document)

Migrates GenericDocument to a newer version of AppSearchSchema.

This method will be invoked only if the SetSchemaRequest is setting a higher version number than the current AppSearchSchema saved in AppSearch.

If this Migrator is provided to cover a compatible schema change via AppSearchSession.setSchema(SetSchemaRequest), documents under the old version won't be removed unless you use the same document ID.

This method will be invoked on the background worker thread provided via AppSearchSession.setSchema(SetSchemaRequest).

Parameters:

currentVersion: The current version of the document's schema.
finalVersion: The final version that documents need to be migrated to.
document: The GenericDocument need to be translated to new version.

Returns:

A GenericDocument in new version.

public abstract GenericDocument onDowngrade(int currentVersion, int finalVersion, GenericDocument document)

Migrates GenericDocument to an older version of AppSearchSchema.

This method will be invoked only if the SetSchemaRequest is setting a lower version number than the current AppSearchSchema saved in AppSearch.

If this Migrator is provided to cover a compatible schema change via AppSearchSession.setSchema(SetSchemaRequest), documents under the old version won't be removed unless you use the same document ID.

This method will be invoked on the background worker thread.

Parameters:

currentVersion: The current version of the document's schema.
finalVersion: The final version that documents need to be migrated to.
document: The GenericDocument need to be translated to new version.

Returns:

A GenericDocument in new version.

Source

/*
 * Copyright 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.appsearch.app;

import androidx.annotation.NonNull;
import androidx.annotation.WorkerThread;

/**
 * A migrator class to translate {@link GenericDocument} from different version of
 * {@link AppSearchSchema}
 *
 * <p>Make non-backwards-compatible changes will delete all stored documents in old schema. You
 * can save your documents by setting {@link Migrator} via the
 * {@link SetSchemaRequest.Builder#setMigrator} for each type and target version you want to save.
 *
 * <p>{@link #onDowngrade} or {@link #onUpgrade} will be triggered if the version number of the
 * schema stored in AppSearch is different with the version in the request.
 *
 * <p>If any error or Exception occurred in the {@link #onDowngrade} or {@link #onUpgrade}, all the
 * setSchema request will be rejected unless the schema changes are backwards-compatible, and stored
 * documents won't have any observable changes.
 */
public abstract class Migrator {
    /**
     * Returns {@code true} if this migrator's source type needs to be migrated to update from
     * currentVersion to finalVersion.
     *
     * <p>Migration won't be triggered if currentVersion is equal to finalVersion even if
     * {@link #shouldMigrate} return true;
     */
    public abstract boolean shouldMigrate(int currentVersion, int finalVersion);

    /**
     * Migrates {@link GenericDocument} to a newer version of {@link AppSearchSchema}.
     *
     * <p>This method will be invoked only if the {@link SetSchemaRequest} is setting a
     * higher version number than the current {@link AppSearchSchema} saved in AppSearch.
     *
     * <p>If this {@link Migrator} is provided to cover a compatible schema change via
     * {@link AppSearchSession#setSchema}, documents under the old version won't be removed
     * unless you use the same document ID.
     *
     * <p>This method will be invoked on the background worker thread provided via
     * {@link AppSearchSession#setSchema}.
     *
     * @param currentVersion The current version of the document's schema.
     * @param finalVersion  The final version that documents need to be migrated to.
     * @param document       The {@link GenericDocument} need to be translated to new version.
     * @return               A {@link GenericDocument} in new version.
     */
    @WorkerThread
    @NonNull
    public abstract GenericDocument onUpgrade(int currentVersion, int finalVersion,
            @NonNull GenericDocument document);

    /**
     * Migrates {@link GenericDocument} to an older version of {@link AppSearchSchema}.
     *
     * <p>This method will be invoked only if the {@link SetSchemaRequest} is setting a
     * lower version number than the current {@link AppSearchSchema} saved in AppSearch.
     *
     * <p>If this {@link Migrator} is provided to cover a compatible schema change via
     * {@link AppSearchSession#setSchema}, documents under the old version won't be removed
     * unless you use the same document ID.
     *
     * <p>This method will be invoked on the background worker thread.
     *
     * @param currentVersion The current version of the document's schema.
     * @param finalVersion  The final version that documents need to be migrated to.
     * @param document       The {@link GenericDocument} need to be translated to new version.
     * @return               A {@link GenericDocument} in new version.
     */
    @WorkerThread
    @NonNull
    public abstract GenericDocument onDowngrade(int currentVersion, int finalVersion,
            @NonNull GenericDocument document);
}