Use tracker lookup table names when creating multi-instance observer.
When creating the multi-instance invalidation tracker observer use the
table lookup keys which correspond to the available table names to
observe and not the names in the internal table names array since those
are mapped to the shadow tables and in the case of an FTS table to a
non-visible table such as Foo_content.
Bug: 148969394
Test: MultiInstanceInvalidationTest
Change-Id: Ief0a906ccd6a87d285035d65f6b4bd87837006f6
diff --git a/room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/test/MultiInstanceInvalidationTest.java b/room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/test/MultiInstanceInvalidationTest.java
index 0126834..8cb26c4 100644
--- a/room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/test/MultiInstanceInvalidationTest.java
+++ b/room/integration-tests/testapp/src/androidTest/java/androidx/room/integration/testapp/test/MultiInstanceInvalidationTest.java
@@ -28,6 +28,7 @@
import android.content.Intent;
import android.os.SystemClock;
+import androidx.annotation.NonNull;
import androidx.collection.SimpleArrayMap;
import androidx.core.util.Pair;
import androidx.lifecycle.LiveData;
@@ -39,6 +40,7 @@
import androidx.room.integration.testapp.SampleDatabaseService;
import androidx.room.integration.testapp.database.Customer;
import androidx.room.integration.testapp.database.CustomerDao;
+import androidx.room.integration.testapp.database.Description;
import androidx.room.integration.testapp.database.Product;
import androidx.room.integration.testapp.database.SampleDatabase;
import androidx.test.core.app.ApplicationProvider;
@@ -124,6 +126,31 @@
}
@Test
+ public void invalidateInAnotherInstanceFts() throws Exception {
+ final SampleDatabase db1 = openDatabase(true);
+ final SampleDatabase db2 = openDatabase(true);
+
+ Product theProduct = new Product(1, "Candy");
+ db2.getProductDao().insert(theProduct);
+ db2.getProductDao().addDescription(new Description(1, "Delicious candy."));
+
+ final CountDownLatch changed = new CountDownLatch(1);
+ db1.getInvalidationTracker().addObserver(new InvalidationTracker.Observer("Description") {
+ @Override
+ public void onInvalidated(@NonNull Set<String> tables) {
+ changed.countDown();
+ }
+ });
+
+ db2.getProductDao().addDescription(new Description(1, "Wonderful candy."));
+
+ assertTrue(changed.await(3, TimeUnit.SECONDS));
+ List<Product> result = db1.getProductDao().getProductsWithDescription("candy");
+ assertThat(result.size(), is(1));
+ assertThat(result.get(0), is(theProduct));
+ }
+
+ @Test
public void invalidationInAnotherInstance_noMultiInstanceInvalidation() throws Exception {
final SampleDatabase db1 = openDatabase(false);
final SampleDatabase db2 = openDatabase(false);
diff --git a/room/integration-tests/testapp/src/main/java/androidx/room/integration/testapp/database/Description.java b/room/integration-tests/testapp/src/main/java/androidx/room/integration/testapp/database/Description.java
new file mode 100644
index 0000000..9264dbe
--- /dev/null
+++ b/room/integration-tests/testapp/src/main/java/androidx/room/integration/testapp/database/Description.java
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2020 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.room.integration.testapp.database;
+
+import androidx.room.Entity;
+import androidx.room.Fts4;
+
+/**
+ * A product description
+ */
+@Fts4
+@Entity
+public class Description {
+
+ private final int mProductId;
+ private final String mText;
+
+ public Description(int productId, String text) {
+ mProductId = productId;
+ mText = text;
+ }
+
+ public int getProductId() {
+ return mProductId;
+ }
+
+ public String getText() {
+ return mText;
+ }
+}
diff --git a/room/integration-tests/testapp/src/main/java/androidx/room/integration/testapp/database/Product.java b/room/integration-tests/testapp/src/main/java/androidx/room/integration/testapp/database/Product.java
index b75df0d..44741fd 100644
--- a/room/integration-tests/testapp/src/main/java/androidx/room/integration/testapp/database/Product.java
+++ b/room/integration-tests/testapp/src/main/java/androidx/room/integration/testapp/database/Product.java
@@ -31,6 +31,14 @@
private String mName;
+ public Product() {
+ }
+
+ public Product(int id, String name) {
+ mId = id;
+ mName = name;
+ }
+
public int getId() {
return mId;
}
diff --git a/room/integration-tests/testapp/src/main/java/androidx/room/integration/testapp/database/ProductDao.java b/room/integration-tests/testapp/src/main/java/androidx/room/integration/testapp/database/ProductDao.java
index 3107224..56e4beb 100644
--- a/room/integration-tests/testapp/src/main/java/androidx/room/integration/testapp/database/ProductDao.java
+++ b/room/integration-tests/testapp/src/main/java/androidx/room/integration/testapp/database/ProductDao.java
@@ -35,12 +35,25 @@
void insert(Product product);
/**
+ * Insert a product description.
+ */
+ @Insert
+ void addDescription(Description description);
+
+ /**
* Insert a review.
*/
@Insert
void addReview(Review review);
/**
+ * Query product with matching description.
+ */
+ @Query("SELECT DISTINCT Product.* FROM Product JOIN Description ON mId = mProductId "
+ + "WHERE mText MATCH :query")
+ List<Product> getProductsWithDescription(String query);
+
+ /**
* Query a products reviews.
*/
@Query("SELECT * FROM Review WHERE mProductId = :productId")
diff --git a/room/integration-tests/testapp/src/main/java/androidx/room/integration/testapp/database/SampleDatabase.java b/room/integration-tests/testapp/src/main/java/androidx/room/integration/testapp/database/SampleDatabase.java
index b0dba5b..0c62a26 100644
--- a/room/integration-tests/testapp/src/main/java/androidx/room/integration/testapp/database/SampleDatabase.java
+++ b/room/integration-tests/testapp/src/main/java/androidx/room/integration/testapp/database/SampleDatabase.java
@@ -22,7 +22,7 @@
/**
* Sample database of customers.
*/
-@Database(entities = {Customer.class, Product.class, Review.class},
+@Database(entities = {Customer.class, Product.class, Review.class, Description.class},
version = 1, exportSchema = false)
public abstract class SampleDatabase extends RoomDatabase {
/**
diff --git a/room/runtime/src/main/java/androidx/room/InvalidationTracker.java b/room/runtime/src/main/java/androidx/room/InvalidationTracker.java
index c7ea442..980aba5 100644
--- a/room/runtime/src/main/java/androidx/room/InvalidationTracker.java
+++ b/room/runtime/src/main/java/androidx/room/InvalidationTracker.java
@@ -83,7 +83,6 @@
+ " WHERE " + INVALIDATED_COLUMN_NAME + " = 1;";
@NonNull
- @VisibleForTesting
final HashMap<String, Integer> mTableIdLookup;
final String[] mTableNames;
diff --git a/room/runtime/src/main/java/androidx/room/MultiInstanceInvalidationClient.java b/room/runtime/src/main/java/androidx/room/MultiInstanceInvalidationClient.java
index 3aeddcc..3dbb8e0 100644
--- a/room/runtime/src/main/java/androidx/room/MultiInstanceInvalidationClient.java
+++ b/room/runtime/src/main/java/androidx/room/MultiInstanceInvalidationClient.java
@@ -167,7 +167,9 @@
mName = name;
mInvalidationTracker = invalidationTracker;
mExecutor = executor;
- mObserver = new InvalidationTracker.Observer(invalidationTracker.mTableNames) {
+ // Use all tables names for observer.
+ final Set<String> tableNames = invalidationTracker.mTableIdLookup.keySet();
+ mObserver = new InvalidationTracker.Observer(tableNames.toArray(new String[0])) {
@Override
public void onInvalidated(@NonNull Set<String> tables) {
if (mStopped.get()) {