Skip to content
This repository was archived by the owner on Mar 11, 2022. It is now read-only.

Commit 1ce6521

Browse files
authored
Merge pull request #520 from cloudant/289-move-query-constants
Move constants to QueryConstants
2 parents 83fd714 + 8aba398 commit 1ce6521

15 files changed

+36
-29
lines changed

cloudant-sync-datastore-core/src/main/java/com/cloudant/sync/internal/query/QueryConstants.java

+13-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package com.cloudant.sync.internal.query;
1818

19-
final class QueryConstants {
19+
public final class QueryConstants {
2020

2121
public static final String AND = "$and";
2222

@@ -54,9 +54,19 @@ private QueryConstants() {
5454
throw new AssertionError();
5555
}
5656

57+
public static final String DB_FILE_NAME = "indexes.sqlite";
58+
59+
public static final String INDEX_TABLE_PREFIX = "_t_cloudant_sync_query_index_";
60+
61+
public static final String INDEX_METADATA_TABLE_NAME = "_t_cloudant_sync_query_metadata";
62+
63+
public static final String EXTENSION_NAME = "com.cloudant.sync.query";
64+
65+
public static final String INDEX_FIELD_NAME_PATTERN = "^[a-zA-Z][a-zA-Z0-9_]*$";
66+
5767
public static String[] getSchemaVersion1() {
5868
return new String[] {
59-
"CREATE TABLE " + QueryImpl.INDEX_METADATA_TABLE_NAME + " ( " +
69+
"CREATE TABLE " + INDEX_METADATA_TABLE_NAME + " ( " +
6070
" index_name TEXT NOT NULL, " +
6171
" index_type TEXT NOT NULL, " +
6272
" field_name TEXT NOT NULL, " +
@@ -66,7 +76,7 @@ public static String[] getSchemaVersion1() {
6676

6777
public static String[] getSchemaVersion2() {
6878
return new String[] {
69-
"ALTER TABLE " + QueryImpl.INDEX_METADATA_TABLE_NAME +
79+
"ALTER TABLE " + INDEX_METADATA_TABLE_NAME +
7080
" ADD COLUMN index_settings TEXT NULL;"
7181
};
7282
}

cloudant-sync-datastore-core/src/main/java/com/cloudant/sync/internal/query/QueryImpl.java

+4-12
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,6 @@
7878
*/
7979
public class QueryImpl implements Query {
8080

81-
private static final String DB_FILE_NAME = "indexes.sqlite";
82-
83-
private static final String INDEX_TABLE_PREFIX = "_t_cloudant_sync_query_index_";
84-
public static final String INDEX_METADATA_TABLE_NAME = "_t_cloudant_sync_query_metadata";
85-
86-
private static final String EXTENSION_NAME = "com.cloudant.sync.query";
87-
private static final String INDEX_FIELD_NAME_PATTERN = "^[a-zA-Z][a-zA-Z0-9_]*$";
88-
8981
private static final Logger logger = Logger.getLogger(QueryImpl.class.getName());
9082

9183
private final Database database;
@@ -99,10 +91,10 @@ public class QueryImpl implements Query {
9991
*/
10092
public QueryImpl(Database database, File extensionsLocation, KeyProvider keyProvider) throws IOException, SQLException {
10193
this.database = database;
102-
validFieldName = Pattern.compile(INDEX_FIELD_NAME_PATTERN);
94+
validFieldName = Pattern.compile(QueryConstants.INDEX_FIELD_NAME_PATTERN);
10395

104-
File indexesLocation = new File(extensionsLocation, EXTENSION_NAME);
105-
File indexesDatabaseFile = new File(indexesLocation, DB_FILE_NAME);
96+
File indexesLocation = new File(extensionsLocation, QueryConstants.EXTENSION_NAME);
97+
File indexesDatabaseFile = new File(indexesLocation, QueryConstants.DB_FILE_NAME);
10698

10799
dbQueue = new SQLDatabaseQueue(indexesDatabaseFile, keyProvider);
108100
dbQueue.updateSchema(new SchemaOnlyMigration(QueryConstants.getSchemaVersion1()), 1);
@@ -237,7 +229,7 @@ public QueryResult find(Map<String, Object> query,
237229
}
238230

239231
public static String tableNameForIndex(String indexName) {
240-
return INDEX_TABLE_PREFIX.concat(indexName);
232+
return QueryConstants.INDEX_TABLE_PREFIX.concat(indexName);
241233
}
242234

243235
protected Database getDatabase() {

cloudant-sync-datastore-core/src/main/java/com/cloudant/sync/internal/query/callables/CreateIndexCallable.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.cloudant.sync.internal.query.callables;
1717

1818
import com.cloudant.sync.internal.android.ContentValues;
19+
import com.cloudant.sync.internal.query.QueryConstants;
1920
import com.cloudant.sync.internal.query.QueryImpl;
2021
import com.cloudant.sync.internal.query.TokenizerHelper;
2122
import com.cloudant.sync.internal.sqlite.SQLCallable;
@@ -55,7 +56,7 @@ public Void call(SQLDatabase database) throws QueryException {
5556
parameters.put("index_settings", TokenizerHelper.tokenizerToJson(index.tokenizer));
5657
parameters.put("field_name", fieldName.field);
5758
parameters.put("last_sequence", 0);
58-
long rowId = database.insert(QueryImpl.INDEX_METADATA_TABLE_NAME,
59+
long rowId = database.insert(QueryConstants.INDEX_METADATA_TABLE_NAME,
5960
parameters);
6061
if (rowId < 0) {
6162
throw new QueryException("Error inserting index metadata");

cloudant-sync-datastore-core/src/main/java/com/cloudant/sync/internal/query/callables/DeleteIndexCallable.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
package com.cloudant.sync.internal.query.callables;
1717

18+
import com.cloudant.sync.internal.query.QueryConstants;
1819
import com.cloudant.sync.internal.query.QueryImpl;
1920
import com.cloudant.sync.internal.sqlite.SQLCallable;
2021
import com.cloudant.sync.internal.sqlite.SQLDatabase;
@@ -42,7 +43,7 @@ public Void call(SQLDatabase database) throws QueryException {
4243

4344
// Delete the metadata entries
4445
String where = " index_name = ? ";
45-
database.delete(QueryImpl.INDEX_METADATA_TABLE_NAME, where, new String[]{indexName});
46+
database.delete(QueryConstants.INDEX_METADATA_TABLE_NAME, where, new String[]{indexName});
4647
} catch (SQLException e) {
4748
String msg = String.format("Failed to delete index: %s", indexName);
4849
throw new QueryException(msg, e);

cloudant-sync-datastore-core/src/main/java/com/cloudant/sync/internal/query/callables/ListIndexesCallable.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
package com.cloudant.sync.internal.query.callables;
1717

18+
import com.cloudant.sync.internal.query.QueryConstants;
1819
import com.cloudant.sync.internal.query.QueryImpl;
1920
import com.cloudant.sync.internal.query.TokenizerHelper;
2021
import com.cloudant.sync.internal.sqlite.Cursor;
@@ -38,7 +39,7 @@ public class ListIndexesCallable implements SQLCallable<List<Index>> {
3839
@Override
3940
public List<Index> call(SQLDatabase db) throws Exception {
4041
// Accumulate indexes and definitions into a map
41-
String sqlIndexNames = String.format("SELECT DISTINCT index_name FROM %s", QueryImpl
42+
String sqlIndexNames = String.format("SELECT DISTINCT index_name FROM %s", QueryConstants
4243
.INDEX_METADATA_TABLE_NAME);
4344
Cursor cursorIndexNames = null;
4445
ArrayList<Index> indexes = new ArrayList<Index>();
@@ -47,7 +48,7 @@ public List<Index> call(SQLDatabase db) throws Exception {
4748
while (cursorIndexNames.moveToNext()) {
4849
String indexName = cursorIndexNames.getString(0);
4950
String sqlIndexes = String.format("SELECT index_type, field_name, index_settings " +
50-
"FROM %s WHERE index_name = ?", QueryImpl.INDEX_METADATA_TABLE_NAME);
51+
"FROM %s WHERE index_name = ?", QueryConstants.INDEX_METADATA_TABLE_NAME);
5152
Cursor cursorIndexes = null;
5253
try {
5354
cursorIndexes = db.rawQuery(sqlIndexes, new String[]{indexName});

cloudant-sync-datastore-core/src/main/java/com/cloudant/sync/internal/query/callables/SequenceNumberForIndexCallable.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
package com.cloudant.sync.internal.query.callables;
1717

18+
import com.cloudant.sync.internal.query.QueryConstants;
1819
import com.cloudant.sync.internal.query.QueryImpl;
1920
import com.cloudant.sync.internal.sqlite.Cursor;
2021
import com.cloudant.sync.internal.sqlite.SQLCallable;
@@ -40,7 +41,7 @@ public SequenceNumberForIndexCallable(String indexName) {
4041
public Long call(SQLDatabase database) {
4142
long result = 0;
4243
String sql = String.format("SELECT last_sequence FROM %s WHERE index_name = ?",
43-
QueryImpl.INDEX_METADATA_TABLE_NAME);
44+
QueryConstants.INDEX_METADATA_TABLE_NAME);
4445
Cursor cursor = null;
4546
try {
4647
cursor = database.rawQuery(sql, new String[]{indexName});

cloudant-sync-datastore-core/src/main/java/com/cloudant/sync/internal/query/callables/UpdateMetadataForIndexCallable.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.cloudant.sync.internal.query.callables;
1717

1818
import com.cloudant.sync.internal.android.ContentValues;
19+
import com.cloudant.sync.internal.query.QueryConstants;
1920
import com.cloudant.sync.internal.query.QueryImpl;
2021
import com.cloudant.sync.internal.sqlite.SQLCallable;
2122
import com.cloudant.sync.internal.sqlite.SQLDatabase;
@@ -37,7 +38,7 @@ public UpdateMetadataForIndexCallable(long lastSequence, String indexName) {
3738
public Void call(SQLDatabase database) throws QueryException {
3839
ContentValues v = new ContentValues();
3940
v.put("last_sequence", lastSequence);
40-
int row = database.update(QueryImpl.INDEX_METADATA_TABLE_NAME,
41+
int row = database.update(QueryConstants.INDEX_METADATA_TABLE_NAME,
4142
v,
4243
" index_name = ? ",
4344
new String[]{indexName});

cloudant-sync-datastore-core/src/test/java/com/cloudant/sync/internal/query/AbstractIndexTestBase.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public void setUp() throws Exception {
4949
assertThat(im, is(notNullValue()));
5050
indexManagerDatabaseQueue = TestUtils.getDBQueue(im);
5151
assertThat(indexManagerDatabaseQueue, is(notNullValue()));
52-
String[] metadataTableList = new String[] { QueryImpl.INDEX_METADATA_TABLE_NAME };
52+
String[] metadataTableList = new String[] { QueryConstants.INDEX_METADATA_TABLE_NAME };
5353
SQLDatabaseTestUtils.assertTablesExist(indexManagerDatabaseQueue,
5454
metadataTableList);
5555
}

cloudant-sync-datastore-core/src/test/java/com/cloudant/sync/internal/query/IndexUpdaterTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1162,7 +1162,7 @@ public void indexUpdatesPersistExistingIndex() throws Exception {
11621162
private long getIndexSequenceNumber(String indexName) throws Exception {
11631163
String where = String.format("index_name = \"%s\" group by last_sequence", indexName);
11641164
final String sql = String.format("SELECT last_sequence FROM %s where %s",
1165-
QueryImpl.INDEX_METADATA_TABLE_NAME,
1165+
QueryConstants.INDEX_METADATA_TABLE_NAME,
11661166
where);
11671167

11681168
return indexManagerDatabaseQueue.submit(new SQLCallable<Long>() {

cloudant-sync-datastore-core/src/test/java/com/cloudant/sync/internal/query/QueryCoveringIndexesTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public void setUp() throws Exception{
9797
indexManagerDatabaseQueue = TestUtils.getDBQueue(im);
9898
assertThat(im, is(notNullValue()));
9999
assertThat(indexManagerDatabaseQueue, is(notNullValue()));
100-
String[] metadataTableList = new String[] { QueryImpl.INDEX_METADATA_TABLE_NAME };
100+
String[] metadataTableList = new String[] { QueryConstants.INDEX_METADATA_TABLE_NAME };
101101
SQLDatabaseTestUtils.assertTablesExist(TestUtils.getDBQueue(im), metadataTableList);
102102
}
103103

cloudant-sync-datastore-core/src/test/java/com/cloudant/sync/internal/query/QueryFilterFieldsTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void setUp() throws Exception {
5050
indexManagerDatabaseQueue = TestUtils.getDBQueue(im);
5151
assertThat(im, is(notNullValue()));
5252
assertThat(indexManagerDatabaseQueue, is(notNullValue()));
53-
String[] metadataTableList = new String[] { QueryImpl.INDEX_METADATA_TABLE_NAME };
53+
String[] metadataTableList = new String[] { QueryConstants.INDEX_METADATA_TABLE_NAME };
5454
SQLDatabaseTestUtils.assertTablesExist(indexManagerDatabaseQueue, metadataTableList);
5555

5656
setUpBasicQueryData();

cloudant-sync-datastore-core/src/test/java/com/cloudant/sync/internal/query/QueryResultTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void setUp() throws Exception {
5050
indexManagerDatabaseQueue = TestUtils.getDBQueue(im);
5151
assertThat(im, is(notNullValue()));
5252
assertThat(indexManagerDatabaseQueue, is(notNullValue()));
53-
String[] metadataTableList = new String[]{QueryImpl.INDEX_METADATA_TABLE_NAME};
53+
String[] metadataTableList = new String[]{QueryConstants.INDEX_METADATA_TABLE_NAME};
5454
SQLDatabaseTestUtils.assertTablesExist(indexManagerDatabaseQueue, metadataTableList);
5555

5656
queue = new SQLDatabaseQueue(new File(factoryPath, "/db.sync"), new NullKeyProvider());

cloudant-sync-datastore-core/src/test/java/com/cloudant/sync/internal/query/QuerySortTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void setUp() throws Exception {
5353
indexManagerDatabaseQueue = TestUtils.getDBQueue(im);
5454
assertThat(im, is(notNullValue()));
5555
assertThat(TestUtils.getDBQueue(im), is(notNullValue()));
56-
String[] metadataTableList = new String[] { QueryImpl.INDEX_METADATA_TABLE_NAME };
56+
String[] metadataTableList = new String[] { QueryConstants.INDEX_METADATA_TABLE_NAME };
5757
SQLDatabaseTestUtils.assertTablesExist(indexManagerDatabaseQueue, metadataTableList);
5858

5959
Index indexA = new Index(Arrays.<FieldSort>asList(new FieldSort("name"), new FieldSort("age"), new FieldSort("pet")), "a", IndexType.JSON, null);

cloudant-sync-datastore-core/src/test/java/com/cloudant/sync/internal/query/QueryTextSearchTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void setUp() throws Exception {
5050
indexManagerDatabaseQueue = TestUtils.getDBQueue(im);
5151
assertThat(im, is(notNullValue()));
5252
assertThat(indexManagerDatabaseQueue, is(notNullValue()));
53-
String[] metadataTableList = new String[] { QueryImpl.INDEX_METADATA_TABLE_NAME };
53+
String[] metadataTableList = new String[] { QueryConstants.INDEX_METADATA_TABLE_NAME };
5454
SQLDatabaseTestUtils.assertTablesExist(indexManagerDatabaseQueue, metadataTableList);
5555

5656
DocumentRevision rev = new DocumentRevision("mike12");

cloudant-sync-datastore-core/src/test/java/com/cloudant/sync/internal/query/QueryWithoutCoveringIndexesTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public void setUp() throws Exception {
9494
indexManagerDatabaseQueue = TestUtils.getDBQueue(im);
9595
assertThat(im, is(notNullValue()));
9696
assertThat(indexManagerDatabaseQueue, is(notNullValue()));
97-
String[] metadataTableList = new String[] { QueryImpl.INDEX_METADATA_TABLE_NAME };
97+
String[] metadataTableList = new String[] { QueryConstants.INDEX_METADATA_TABLE_NAME };
9898
SQLDatabaseTestUtils.assertTablesExist(indexManagerDatabaseQueue, metadataTableList);
9999
}
100100

0 commit comments

Comments
 (0)