Skip to content

Commit 2861847

Browse files
Use DocumentSet
1 parent c8d1e53 commit 2861847

File tree

7 files changed

+28
-42
lines changed

7 files changed

+28
-42
lines changed

firebase-database-collection/src/main/java/com/google/firebase/database/collection/ImmutableSortedMap.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,6 @@ public abstract class ImmutableSortedMap<K, V> implements Iterable<Map.Entry<K,
5656

5757
public abstract Comparator<K> getComparator();
5858

59-
public ImmutableSortedMap<K, V> insertAll(ImmutableSortedMap<K, V> source) {
60-
ImmutableSortedMap<K, V> result = this;
61-
for (Map.Entry<K, V> entry : source) {
62-
result = result.insert(entry.getKey(), entry.getValue());
63-
}
64-
return result;
65-
}
66-
6759
@Override
6860
@SuppressWarnings("unchecked")
6961
public boolean equals(Object o) {

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

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,10 @@
2121
import com.google.firebase.database.collection.ImmutableSortedSet;
2222
import com.google.firebase.firestore.core.Query;
2323
import com.google.firebase.firestore.model.Document;
24-
import com.google.firebase.firestore.model.DocumentCollections;
2524
import com.google.firebase.firestore.model.DocumentKey;
2625
import com.google.firebase.firestore.model.MaybeDocument;
2726
import com.google.firebase.firestore.model.SnapshotVersion;
28-
import java.util.Comparator;
29-
import java.util.Iterator;
27+
import java.util.Collections;
3028
import java.util.Map;
3129

3230
/**
@@ -78,8 +76,7 @@ public ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingQuery(
7876
return executeFullCollectionScan(query);
7977
}
8078

81-
ImmutableSortedMap<DocumentKey, Document> previousResults =
82-
getPreviousResults(query, remoteKeys);
79+
ImmutableSortedSet<Document> previousResults = getPreviousResults(query, remoteKeys);
8380

8481
if (query.hasLimit()
8582
&& needsRefill(
@@ -92,24 +89,29 @@ && needsRefill(
9289
ImmutableSortedMap<DocumentKey, Document> updatedResults =
9390
localDocumentsView.getDocumentsMatchingQuery(
9491
query, queryData.getLastLimboFreeSnapshotVersion());
95-
return previousResults.insertAll(updatedResults);
92+
for (Document result : previousResults) {
93+
updatedResults = updatedResults.insert(result.getKey(), result);
94+
}
95+
96+
return updatedResults;
9697
}
9798

9899
/** Returns the documents for the specified remote keys if they still match the query. */
99-
private ImmutableSortedMap<DocumentKey, Document> getPreviousResults(
100+
private ImmutableSortedSet<Document> getPreviousResults(
100101
Query query, ImmutableSortedSet<DocumentKey> remoteKeys) {
101102
// Fetch the documents that matched the query at the last snapshot.
102103
ImmutableSortedMap<DocumentKey, MaybeDocument> previousResults =
103104
localDocumentsView.getDocuments(remoteKeys);
104105

105106
// Re-apply the query filter since previously matching documents do not necessarily still
106107
// match the query.
107-
ImmutableSortedMap<DocumentKey, Document> results = DocumentCollections.emptyDocumentMap();
108+
ImmutableSortedSet<Document> results =
109+
new ImmutableSortedSet<>(Collections.emptyList(), query.comparator());
108110
for (Map.Entry<DocumentKey, MaybeDocument> entry : previousResults) {
109111
MaybeDocument maybeDoc = entry.getValue();
110112
if (maybeDoc instanceof Document && query.matches((Document) maybeDoc)) {
111113
Document doc = (Document) maybeDoc;
112-
results = results.insert(entry.getKey(), doc);
114+
results = results.insert(doc);
113115
}
114116
}
115117

@@ -123,38 +125,28 @@ private ImmutableSortedMap<DocumentKey, Document> getPreviousResults(
123125
private boolean needsRefill(
124126
Query query,
125127
SnapshotVersion limboFreeSnapshotVersion,
126-
ImmutableSortedMap<DocumentKey, Document> previousResults,
128+
ImmutableSortedSet<Document> previousResults,
127129
ImmutableSortedSet<DocumentKey> remoteKeys) {
128130
hardAssert(query.hasLimit(), "Only limit queries should be refilled");
131+
// The query needs to be refilled if a previously matching document no longer matches.
132+
if (remoteKeys.size() != previousResults.size()) {
133+
return true;
134+
}
129135

130136
// The query doesn't need to be refilled if there were never any documents that matched the
131137
// query constraint.
132-
if (remoteKeys.isEmpty()) {
138+
if (previousResults.isEmpty()) {
133139
return false;
134140
}
135141

136-
// The query needs to be refilled if a previously matching document no longer matches.
137-
if (remoteKeys.size() != previousResults.size()) {
138-
return true;
139-
}
140-
141142
// Limit queries are not eligible for index-free query execution if an older document from cache
142143
// may sort before a document that was previously part of the limit. This can happen if the last
143144
// document of the limit could sort lower than it did when the query was last synchronized. To
144145
// verify this, we use the query's comparator to determine the last document that fit the
145146
// limit. If the last document was edited after the query was last synchronized, there is a
146147
// chance that another document from cache sorts higher, in which case we have to perform a full
147148
// cache scan.
148-
Comparator<Document> comparator = query.comparator();
149-
Iterator<Map.Entry<DocumentKey, Document>> it = previousResults.iterator();
150-
Document lastDocumentInLimit = it.next().getValue();
151-
while (it.hasNext()) {
152-
Document doc = it.next().getValue();
153-
if (comparator.compare(lastDocumentInLimit, doc) < 0) {
154-
lastDocumentInLimit = doc;
155-
}
156-
}
157-
149+
Document lastDocumentInLimit = previousResults.getMaxEntry();
158150
return lastDocumentInLimit.hasPendingWrites()
159151
|| lastDocumentInLimit.getVersion().compareTo(limboFreeSnapshotVersion) > 0;
160152
}

firebase-storage/src/androidTest/java/com/google/firebase/storage/IntegrationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class IntegrationTest {
4949

5050
private FirebaseStorage storageClient;
5151

52-
private final String randomPrefix = UUID.randomUUID().toString();
52+
private final String randomPrefix = "foo:bar";
5353

5454
@Before
5555
public void before() throws ExecutionException, InterruptedException {

firebase-storage/src/main/java/com/google/firebase/storage/network/ListNetworkRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ protected String getQueryParameters() throws UnsupportedEncodingException {
5656
List<String> keys = new ArrayList<>();
5757
List<String> values = new ArrayList<>();
5858

59-
String prefix = getPathWithoutBucket();
59+
String prefix = getDecodedPathWithoutBucket();
6060
if (!TextUtils.isEmpty(prefix)) {
6161
keys.add("prefix");
6262
values.add(prefix + "/");

firebase-storage/src/main/java/com/google/firebase/storage/network/NetworkRequest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public static String getDefaultURL(@NonNull Uri gsUri) {
129129
*/
130130
@Nullable
131131
public static String getPathWithoutBucket(@NonNull Uri gsUri) {
132-
String path = gsUri.getEncodedPath();
132+
String path = gsUri.getPath();
133133
if (path != null && path.startsWith("/")) {
134134
// this should always be true.
135135
path = path.substring(1);
@@ -138,13 +138,13 @@ public static String getPathWithoutBucket(@NonNull Uri gsUri) {
138138
}
139139

140140
/**
141-
* Returns the path of the object but excludes the bucket name
141+
* Returns the decoded path of the object but excludes the bucket name
142142
*
143143
* @return the path in string form.
144144
*/
145145
@Nullable
146-
public String getPathWithoutBucket() {
147-
return getPathWithoutBucket(mGsUri);
146+
public String getDecodedPathWithoutBucket() {
147+
return Uri.decode(getPathWithoutBucket(mGsUri));
148148
}
149149

150150
@NonNull
@@ -318,6 +318,7 @@ private HttpURLConnection createConnection() throws IOException {
318318
}
319319

320320
url = new URL(urlString);
321+
System.err.println("--- query: " + url.toString());
321322
conn = connectionFactory.createInstance(url);
322323
return conn;
323324
}
@@ -527,6 +528,7 @@ String getPostDataString(@Nullable List<String> keys, List<String> values, boole
527528
result.append("=");
528529
result.append(encode ? URLEncoder.encode(values.get(i), "UTF-8") : values.get(i));
529530
}
531+
System.err.println("--- " + result.toString());
530532

531533
return result.toString();
532534
}

firebase-storage/src/main/java/com/google/firebase/storage/network/ResumableUploadStartRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ protected String getQueryParameters() throws UnsupportedEncodingException {
6464
List<String> keys = new ArrayList<>();
6565
List<String> values = new ArrayList<>();
6666

67-
String pathWithoutBucket = getPathWithoutBucket();
67+
String pathWithoutBucket = getDecodedPathWithoutBucket();
6868
keys.add("name");
6969
values.add(pathWithoutBucket != null ? Slashes.unSlashize(pathWithoutBucket) : "");
7070
keys.add("uploadType");

firebase-storage/src/testUtil/java/com/google/firebase/storage/TestCommandHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ public static Task<StringBuilder> deleteBlob() {
226226
}
227227

228228
public static Task<StringBuilder> listFiles(int pageSize, int pageCount) {
229-
final StorageReference reference = FirebaseStorage.getInstance().getReference("smallDirectory");
229+
final StorageReference reference = FirebaseStorage.getInstance().getReference("small:Directory");
230230

231231
TaskCompletionSource<StringBuilder> result = new TaskCompletionSource<>();
232232
StringBuilder builder = new StringBuilder();

0 commit comments

Comments
 (0)