Skip to content

Commit b45b98c

Browse files
Tests: support String index type.
1 parent 5f40765 commit b45b98c

6 files changed

Lines changed: 43 additions & 26 deletions

File tree

tests/objectbox-java-test/src/test/java/io/objectbox/AbstractObjectBoxTest.java

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package io.objectbox;
1818

19+
import io.objectbox.annotation.IndexType;
1920
import org.junit.After;
2021
import org.junit.Before;
2122

@@ -101,25 +102,25 @@ protected File prepareTempDir(String prefix) throws IOException {
101102
}
102103

103104
protected BoxStore createBoxStore() {
104-
return createBoxStore(false);
105+
return createBoxStore(null);
105106
}
106107

107-
protected BoxStore createBoxStore(boolean withIndex) {
108-
return createBoxStoreBuilder(withIndex).build();
108+
protected BoxStore createBoxStore(@Nullable IndexType simpleStringIndexType) {
109+
return createBoxStoreBuilder(simpleStringIndexType).build();
109110
}
110111

111-
protected BoxStoreBuilder createBoxStoreBuilderWithTwoEntities(boolean withIndex) {
112-
BoxStoreBuilder builder = new BoxStoreBuilder(createTestModelWithTwoEntities(withIndex)).directory(boxStoreDir);
112+
protected BoxStoreBuilder createBoxStoreBuilder(@Nullable IndexType simpleStringIndexType) {
113+
BoxStoreBuilder builder = new BoxStoreBuilder(createTestModel(simpleStringIndexType)).directory(boxStoreDir);
113114
if (DEBUG_LOG) builder.debugFlags(DebugFlags.LOG_TRANSACTIONS_READ | DebugFlags.LOG_TRANSACTIONS_WRITE);
114115
builder.entity(new TestEntity_());
115-
builder.entity(new TestEntityMinimal_());
116116
return builder;
117117
}
118118

119-
protected BoxStoreBuilder createBoxStoreBuilder(boolean withIndex) {
120-
BoxStoreBuilder builder = new BoxStoreBuilder(createTestModel(withIndex)).directory(boxStoreDir);
119+
protected BoxStoreBuilder createBoxStoreBuilderWithTwoEntities(boolean withIndex) {
120+
BoxStoreBuilder builder = new BoxStoreBuilder(createTestModelWithTwoEntities(withIndex)).directory(boxStoreDir);
121121
if (DEBUG_LOG) builder.debugFlags(DebugFlags.LOG_TRANSACTIONS_READ | DebugFlags.LOG_TRANSACTIONS_WRITE);
122122
builder.entity(new TestEntity_());
123+
builder.entity(new TestEntityMinimal_());
123124
return builder;
124125
}
125126

@@ -189,24 +190,24 @@ protected long time() {
189190
return System.currentTimeMillis();
190191
}
191192

192-
protected byte[] createTestModel(boolean withIndex) {
193+
protected byte[] createTestModel(@Nullable IndexType simpleStringIndexType) {
193194
ModelBuilder modelBuilder = new ModelBuilder();
194-
addTestEntity(modelBuilder, withIndex);
195+
addTestEntity(modelBuilder, simpleStringIndexType);
195196
modelBuilder.lastEntityId(lastEntityId, lastEntityUid);
196197
modelBuilder.lastIndexId(lastIndexId, lastIndexUid);
197198
return modelBuilder.build();
198199
}
199200

200201
byte[] createTestModelWithTwoEntities(boolean withIndex) {
201202
ModelBuilder modelBuilder = new ModelBuilder();
202-
addTestEntity(modelBuilder, withIndex);
203+
addTestEntity(modelBuilder, withIndex ? IndexType.DEFAULT : null);
203204
addTestEntityMinimal(modelBuilder, withIndex);
204205
modelBuilder.lastEntityId(lastEntityId, lastEntityUid);
205206
modelBuilder.lastIndexId(lastIndexId, lastIndexUid);
206207
return modelBuilder.build();
207208
}
208209

209-
private void addTestEntity(ModelBuilder modelBuilder, boolean withIndex) {
210+
private void addTestEntity(ModelBuilder modelBuilder, @Nullable IndexType simpleStringIndexType) {
210211
lastEntityUid = ++lastUid;
211212
EntityBuilder entityBuilder = modelBuilder.entity("TestEntity").id(++lastEntityId, lastEntityUid);
212213
entityBuilder.property("id", PropertyType.Long).id(TestEntity_.id.id, ++lastUid)
@@ -220,9 +221,19 @@ private void addTestEntity(ModelBuilder modelBuilder, boolean withIndex) {
220221
entityBuilder.property("simpleDouble", PropertyType.Double).id(TestEntity_.simpleDouble.id, ++lastUid);
221222
PropertyBuilder pb =
222223
entityBuilder.property("simpleString", PropertyType.String).id(TestEntity_.simpleString.id, ++lastUid);
223-
if (withIndex) {
224+
if (simpleStringIndexType != null) {
224225
lastIndexUid = ++lastUid;
225-
pb.flags(PropertyFlags.INDEXED).indexId(++lastIndexId, lastIndexUid);
226+
// Since 2.0: default for Strings has changed from INDEXED to INDEX_HASH.
227+
int indexFlag;
228+
if (simpleStringIndexType == IndexType.VALUE) {
229+
indexFlag = PropertyFlags.INDEXED;
230+
} else if (simpleStringIndexType == IndexType.HASH64) {
231+
indexFlag = PropertyFlags.INDEX_HASH64;
232+
} else {
233+
indexFlag = PropertyFlags.INDEX_HASH;
234+
}
235+
log(String.format("Using %s index on TestEntity.simpleString", simpleStringIndexType));
236+
pb.flags(indexFlag).indexId(++lastIndexId, lastIndexUid);
226237
}
227238
entityBuilder.property("simpleByteArray", PropertyType.ByteVector).id(TestEntity_.simpleByteArray.id, ++lastUid);
228239
entityBuilder.property("simpleStringArray", PropertyType.StringVector).id(TestEntity_.simpleStringArray.id, ++lastUid);

tests/objectbox-java-test/src/test/java/io/objectbox/BoxStoreBuilderTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ protected BoxStore createBoxStore() {
4646
@Before
4747
public void setUpBuilder() {
4848
BoxStore.clearDefaultStore();
49-
builder = new BoxStoreBuilder(createTestModel(false)).directory(boxStoreDir);
49+
builder = new BoxStoreBuilder(createTestModel(null)).directory(boxStoreDir);
5050
}
5151

5252
@Test
@@ -85,7 +85,7 @@ public void testDefaultStoreNull() {
8585

8686
@Test
8787
public void testMaxReaders() {
88-
builder = createBoxStoreBuilder(false);
88+
builder = createBoxStoreBuilder(null);
8989
store = builder.maxReaders(1).build();
9090
final Exception[] exHolder = {null};
9191
final Thread thread = new Thread(() -> {
@@ -116,7 +116,7 @@ public void testMaxReaders() {
116116
@Test
117117
public void readOnly() {
118118
// Create a database first; we must create the model only once (ID/UID sequences would be different 2nd time)
119-
byte[] model = createTestModel(false);
119+
byte[] model = createTestModel(null);
120120
builder = new BoxStoreBuilder(model).directory(boxStoreDir);
121121
store = builder.build();
122122
store.close();
@@ -132,7 +132,7 @@ public void readOnly() {
132132
@Test
133133
public void validateOnOpen() {
134134
// Create a database first; we must create the model only once (ID/UID sequences would be different 2nd time)
135-
byte[] model = createTestModel(false);
135+
byte[] model = createTestModel(null);
136136
builder = new BoxStoreBuilder(model).directory(boxStoreDir);
137137
builder.entity(new TestEntity_());
138138
store = builder.build();

tests/objectbox-java-test/src/test/java/io/objectbox/BoxStoreTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public void testOpenSameBoxStoreAfterClose() {
9898
@Test
9999
public void testOpenTwoBoxStoreTwoFiles() {
100100
File boxStoreDir2 = new File(boxStoreDir.getAbsolutePath() + "-2");
101-
BoxStoreBuilder builder = new BoxStoreBuilder(createTestModel(false)).directory(boxStoreDir2);
101+
BoxStoreBuilder builder = new BoxStoreBuilder(createTestModel(null)).directory(boxStoreDir2);
102102
builder.entity(new TestEntity_());
103103
}
104104

@@ -111,7 +111,7 @@ public void testDeleteAllFiles() {
111111
public void testDeleteAllFiles_staticDir() {
112112
closeStoreForTest();
113113
File boxStoreDir2 = new File(boxStoreDir.getAbsolutePath() + "-2");
114-
BoxStoreBuilder builder = new BoxStoreBuilder(createTestModel(false)).directory(boxStoreDir2);
114+
BoxStoreBuilder builder = new BoxStoreBuilder(createTestModel(null)).directory(boxStoreDir2);
115115
BoxStore store2 = builder.build();
116116
store2.close();
117117

@@ -130,7 +130,7 @@ public void testDeleteAllFiles_baseDirName() {
130130
File dbDir = new File(basedir, name);
131131
assertFalse(dbDir.exists());
132132

133-
BoxStoreBuilder builder = new BoxStoreBuilder(createTestModel(false)).baseDirectory(basedir).name(name);
133+
BoxStoreBuilder builder = new BoxStoreBuilder(createTestModel(null)).baseDirectory(basedir).name(name);
134134
BoxStore store2 = builder.build();
135135
store2.close();
136136

@@ -193,7 +193,7 @@ public void testCallInReadTxWithRetry_callback() {
193193
final int[] countHolder = {0};
194194
final int[] countHolderCallback = {0};
195195

196-
BoxStoreBuilder builder = new BoxStoreBuilder(createTestModel(false)).directory(boxStoreDir)
196+
BoxStoreBuilder builder = new BoxStoreBuilder(createTestModel(null)).directory(boxStoreDir)
197197
.failedReadTxAttemptCallback((result, error) -> {
198198
assertNotNull(error);
199199
countHolderCallback[0]++;

tests/objectbox-java-test/src/test/java/io/objectbox/CursorTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package io.objectbox;
1818

19+
import io.objectbox.annotation.IndexType;
1920
import org.junit.Test;
2021

2122
import java.util.concurrent.CountDownLatch;
@@ -27,7 +28,9 @@ public class CursorTest extends AbstractObjectBoxTest {
2728

2829
@Override
2930
protected BoxStore createBoxStore() {
30-
return createBoxStore(true);
31+
// Note: can not use DEFAULT as tests use deprecated cursor.lookupKeyUsingIndex method
32+
// which expects a value based index.
33+
return createBoxStore(IndexType.VALUE);
3134
}
3235

3336
@Test

tests/objectbox-java-test/src/test/java/io/objectbox/query/AbstractQueryTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package io.objectbox.query;
1818

19+
import io.objectbox.annotation.IndexType;
1920
import org.junit.Before;
2021

2122
import java.util.ArrayList;
@@ -27,12 +28,14 @@
2728
import io.objectbox.DebugFlags;
2829
import io.objectbox.TestEntity;
2930

31+
import javax.annotation.Nullable;
32+
3033
public class AbstractQueryTest extends AbstractObjectBoxTest {
3134
protected Box<TestEntity> box;
3235

3336
@Override
34-
protected BoxStoreBuilder createBoxStoreBuilder(boolean withIndex) {
35-
BoxStoreBuilder builder = super.createBoxStoreBuilder(withIndex);
37+
protected BoxStoreBuilder createBoxStoreBuilder(@Nullable IndexType simpleStringIndexType) {
38+
BoxStoreBuilder builder = super.createBoxStoreBuilder(simpleStringIndexType);
3639
if (DEBUG_LOG) builder.debugFlags(DebugFlags.LOG_QUERY_PARAMETERS);
3740
return builder;
3841
}

tests/objectbox-java-test/src/test/java/io/objectbox/query/QueryTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@ public void testForEachBreak() {
861861
// TODO can we improve? More than just "still works"?
862862
public void testQueryAttempts() {
863863
store.close();
864-
BoxStoreBuilder builder = new BoxStoreBuilder(createTestModel(false)).directory(boxStoreDir)
864+
BoxStoreBuilder builder = new BoxStoreBuilder(createTestModel(null)).directory(boxStoreDir)
865865
.queryAttempts(5)
866866
.failedReadTxAttemptCallback((result, error) -> {
867867
if (error != null) {

0 commit comments

Comments
 (0)