Skip to content

Commit 33b3666

Browse files
Cleanup
1 parent f08fddf commit 33b3666

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

firebase-firestore/src/main/java/com/google/firebase/firestore/local/SQLiteIndexManager.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
import static com.google.firebase.firestore.util.Assert.fail;
1818
import static com.google.firebase.firestore.util.Assert.hardAssert;
19+
import static com.google.firebase.firestore.util.Util.repeatSequence;
1920

20-
import android.text.TextUtils;
2121
import androidx.annotation.Nullable;
2222
import com.google.firebase.firestore.core.Bound;
2323
import com.google.firebase.firestore.core.FieldFilter;
@@ -219,17 +219,15 @@ public Set<DocumentKey> getDocumentsMatchingTarget(Target target) {
219219

220220
/** Returns a SQL query on 'index_entries' that unions all bounds. */
221221
private SQLitePersistence.Query generateQuery(int indexId, Object[] bounds, String op) {
222-
String[] statements = new String[bounds.length];
223222
Object[] bingArgs = new Object[bounds.length * 2];
224223
for (int i = 0; i < bounds.length; ++i) {
225-
statements[i] =
226-
String.format(
227-
"SELECT document_name FROM index_entries WHERE index_id = ? AND index_value %s ?",
228-
op);
229224
bingArgs[i * 2] = indexId;
230225
bingArgs[i * 2 + 1] = bounds[i];
231226
}
232-
String sql = TextUtils.join(" UNION ", statements);
227+
String statement =
228+
String.format(
229+
"SELECT document_name FROM index_entries WHERE index_id = ? AND index_value %s ?", op);
230+
String sql = repeatSequence(statement, bounds.length, " UNION ");
233231
return db.query(sql).binding(bingArgs);
234232
}
235233

@@ -240,22 +238,21 @@ private SQLitePersistence.Query generateQuery(
240238
String lowerBoundOp,
241239
Object[] upperBounds,
242240
String upperBoundOp) {
243-
String[] statements = new String[lowerBounds.length * upperBounds.length];
244241
Object[] bingArgs = new Object[lowerBounds.length * upperBounds.length * 3];
245242
int i = 0;
246243
for (Object value1 : lowerBounds) {
247244
for (Object value2 : upperBounds) {
248-
statements[i] =
249-
String.format(
250-
"SELECT document_name FROM index_entries WHERE index_id = ? AND index_value %s ? AND index_value %s ?",
251-
lowerBoundOp, upperBoundOp);
252245
bingArgs[i * 3] = indexId;
253246
bingArgs[i * 3 + 1] = value1;
254247
bingArgs[i * 3 + 2] = value2;
255248
++i;
256249
}
257250
}
258-
String sql = TextUtils.join(" UNION ", statements);
251+
String statement =
252+
String.format(
253+
"SELECT document_name FROM index_entries WHERE index_id = ? AND index_value %s ? AND index_value %s ?",
254+
lowerBoundOp, upperBoundOp);
255+
String sql = repeatSequence(statement, lowerBounds.length * upperBounds.length, " UNION ");
259256
return db.query(sql).binding(bingArgs);
260257
}
261258

firebase-firestore/src/main/java/com/google/firebase/firestore/util/Util.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,17 @@ public static int compareByteStrings(ByteString left, ByteString right) {
234234
}
235235
return Util.compareIntegers(left.size(), right.size());
236236
}
237+
238+
public static String repeatSequence(CharSequence sequence, int count, CharSequence delimiter) {
239+
if (count == 0) {
240+
return "";
241+
}
242+
final StringBuilder sb = new StringBuilder();
243+
sb.append(sequence);
244+
for (int i = 1; i < count; i++) {
245+
sb.append(delimiter);
246+
sb.append(sequence);
247+
}
248+
return sb.toString();
249+
}
237250
}

0 commit comments

Comments
 (0)