Skip to content

Commit

Permalink
Index fields with @PrimaryKey annotation
Browse files Browse the repository at this point in the history
And update test cases to adapt this change.
  • Loading branch information
beeender committed Jul 8, 2015
1 parent 9dd22c1 commit 411320a
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 21 deletions.
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Closing realm on another thread different from where it was created now throws an exception.
* Realm will now throw a RealmError when Realm's underlying storage engine encounters an unrecoverable error.
* @Index annotation can also be applied to byte/short/int/long/boolean/Date now.
* Fields with annotation @PrimaryKey are indexed automatically now.

0.81.1
* Fixed memory leak causing Realm to never release Realm objects.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,8 @@ private boolean categorizeClassElements() {

primaryKey = variableElement;

// Also add as index if the primary key is a string
if (Utils.isString(variableElement) && !indexedFields.contains(variableElement)) {
// Also add as index. All types of primary key can be indexed.
if (!indexedFields.contains(variableElement)) {
indexedFields.add(variableElement);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,39 @@ public void compileInvalidIndexTypes() throws IOException {
final String invalidIndexFieldTypes[] = {"float", "double", "byte[]", "Simple", "RealmList"};

for (String fieldType : invalidIndexFieldTypes) {
TestRealmObjectFileObject javaFileObject = TestRealmObjectFileObject.getSingleFieldInstance(
"InvalidIndexType", "Index", fieldType, "testField");
ASSERT.about(javaSource())
.that(javaFileObject)
.processedWith(new RealmProcessor())
.failsToCompile();
}
}

// Supported "PrimaryKey" annotation types
@Test
public void compilePrimaryKeyTypes() throws IOException {
final String validPrimaryKeyFieldTypes[] = {"byte", "short", "int", "long", "String"};

for (String fieldType : validPrimaryKeyFieldTypes) {
TestRealmObjectFileObject javaFileObject = TestRealmObjectFileObject.getSingleFieldInstance(
"ValidPrimaryKeyType", "PrimaryKey", fieldType, "testField");
ASSERT.about(javaSource())
.that(javaFileObject)
.processedWith(new RealmProcessor())
.compilesWithoutError();
}
}

// Unsupported "PrimaryKey" annotation types
@Test
public void compileInvalidPrimaryKeyTypes() throws IOException {
final String invalidPrimaryKeyFieldTypes[] = {"boolean", "java.util.Date", "Simple", "RealmList"};

for (String fieldType : invalidPrimaryKeyFieldTypes) {
TestRealmObjectFileObject javaFileObject =
TestRealmObjectFileObject.getSingleFieldInstance("InvalidIndexType", "Index", fieldType, "testField");
TestRealmObjectFileObject.getSingleFieldInstance(
"InvalidPrimaryKeyType", "PrimaryKey", fieldType, "testField");
ASSERT.about(javaSource())
.that(javaFileObject)
.processedWith(new RealmProcessor())
Expand Down
18 changes: 5 additions & 13 deletions realm/src/androidTest/java/io/realm/RealmAnnotationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,6 @@ public void testIndex() {
assertFalse(table.hasSearchIndex(table.getColumnIndex("notIndexDate")));
}

public void testHasPrimaryKeyNoIntIndex() {
Table table = testRealm.getTable(AnnotationTypes.class);
assertTrue(table.hasPrimaryKey());
assertFalse(table.hasSearchIndex(table.getColumnIndex("id")));
}

public void testHasPrimaryKeyStringIndex() {
Table table = testRealm.getTable(PrimaryKeyAsString.class);
assertTrue(table.hasPrimaryKey());
assertTrue(table.hasSearchIndex(table.getColumnIndex("name")));
}

// Test migrating primary key from string to long with existing data
public void testPrimaryKeyMigration_long() {
testRealm.beginTransaction();
Expand Down Expand Up @@ -203,7 +191,11 @@ public void testPrimaryKey_errorOnInsertingSameObject() {
public void testPrimaryKeyIsIndexed() {
Table table = testRealm.getTable(PrimaryKeyAsString.class);
assertTrue(table.hasPrimaryKey());
assertTrue(table.hasSearchIndex(0));
assertTrue(table.hasSearchIndex(table.getColumnIndex("name")));

table = testRealm.getTable(PrimaryKeyAsLong.class);
assertTrue(table.hasPrimaryKey());
assertTrue(table.hasSearchIndex(table.getColumnIndex("id")));
}

// Annotation processor honors common naming conventions
Expand Down
45 changes: 40 additions & 5 deletions realm/src/androidTest/java/io/realm/RealmMigrationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,10 @@ public void testNotSettingIndexThrows() {
@Override
public long execute(Realm realm, long version) {
Table table = realm.getTable(AnnotationTypes.class);
table.addColumn(ColumnType.INTEGER, "id");
long columnIndex = table.addColumn(ColumnType.INTEGER, "id");
table.setPrimaryKey("id");
// Primary key will be indexed automatically
table.addSearchIndex(columnIndex);
table.addColumn(ColumnType.STRING, "indexString");
table.addColumn(ColumnType.STRING, "notIndexString");
// Forget to set @Index
Expand All @@ -130,9 +132,10 @@ public void testNotSettingPrimaryKeyThrows() {
@Override
public long execute(Realm realm, long version) {
Table table = realm.getTable(AnnotationTypes.class);
table.addColumn(ColumnType.INTEGER, "id");
long columnIndex = table.addColumn(ColumnType.INTEGER, "id");
table.addSearchIndex(columnIndex);
// Forget to set @PrimaryKey
long columnIndex = table.addColumn(ColumnType.STRING, "indexString");
columnIndex = table.addColumn(ColumnType.STRING, "indexString");
table.addSearchIndex(columnIndex);
table.addColumn(ColumnType.STRING, "notIndexString");
return 1;
Expand All @@ -158,9 +161,11 @@ public void testSetAnnotations() {
@Override
public long execute(Realm realm, long version) {
Table table = realm.getTable(AnnotationTypes.class);
table.addColumn(ColumnType.INTEGER, "id");
long columnIndex = table.addColumn(ColumnType.INTEGER, "id");
table.setPrimaryKey("id");
long columnIndex = table.addColumn(ColumnType.STRING, "indexString");
// Primary key will be indexed automatically
table.addSearchIndex(columnIndex);
columnIndex = table.addColumn(ColumnType.STRING, "indexString");
table.addSearchIndex(columnIndex);
table.addColumn(ColumnType.STRING, "notIndexString");
return 1;
Expand All @@ -182,6 +187,36 @@ public long execute(Realm realm, long version) {
assertTrue(table.hasSearchIndex(table.getColumnIndex("indexString")));
}

public void testNotSettingIndexForPrimaryKeyThrows() {
RealmMigration migration = new RealmMigration() {
@Override
public long execute(Realm realm, long version) {
Table table = realm.getTable(AnnotationTypes.class);
table.addColumn(ColumnType.INTEGER, "id");
table.setPrimaryKey("id");
// Forget to add search index primary key
long columnIndex = table.addColumn(ColumnType.STRING, "indexString");
table.addSearchIndex(columnIndex);
table.addColumn(ColumnType.STRING, "notIndexString");
return 1;
}
};
RealmConfiguration realmConfig = new RealmConfiguration.Builder(getContext())
.schemaVersion(1)
.schema(AnnotationTypes.class)
.migration(migration)
.build();
Realm.deleteRealm(realmConfig);
Realm.migrateRealm(realmConfig);

try {
realm = Realm.getInstance(realmConfig);
fail();
} catch (RealmMigrationNeededException expected) {
}

}

public void testGetPathFromMigrationException() throws IOException {
TestHelper.copyRealmFromAssets(getContext(), "default0.realm", Realm.DEFAULT_REALM_NAME);
File realm = new File(getContext().getFilesDir(), Realm.DEFAULT_REALM_NAME);
Expand Down

0 comments on commit 411320a

Please sign in to comment.