-
Notifications
You must be signed in to change notification settings - Fork 617
Add getOverlays(CollectionGroup) #3330
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
import com.google.firebase.firestore.model.DocumentKey; | ||
import com.google.firebase.firestore.model.ResourcePath; | ||
import com.google.firebase.firestore.model.mutation.Mutation; | ||
import com.google.firebase.firestore.model.mutation.Overlay; | ||
import com.google.firestore.v1.Write; | ||
import com.google.protobuf.InvalidProtocolBufferException; | ||
import java.util.HashMap; | ||
|
@@ -39,23 +40,14 @@ public SQLiteDocumentOverlayCache(SQLitePersistence db, LocalSerializer serializ | |
|
||
@Nullable | ||
@Override | ||
public Mutation getOverlay(DocumentKey key) { | ||
public Overlay getOverlay(DocumentKey key) { | ||
String collectionPath = EncodedPath.encode(key.getPath().popLast()); | ||
String documentId = key.getPath().getLastSegment(); | ||
return db.query( | ||
"SELECT overlay_mutation FROM document_overlays WHERE uid = ? AND collection_path = ? AND document_id = ?") | ||
"SELECT overlay_mutation, largest_batch_id FROM document_overlays " | ||
+ "WHERE uid = ? AND collection_path = ? AND document_id = ?") | ||
.binding(uid, collectionPath, documentId) | ||
.firstValue( | ||
row -> { | ||
if (row == null) return null; | ||
|
||
try { | ||
Write mutation = Write.parseFrom(row.getBlob(0)); | ||
return serializer.decodeMutation(mutation); | ||
} catch (InvalidProtocolBufferException e) { | ||
throw fail("Overlay failed to parse: %s", e); | ||
} | ||
}); | ||
.firstValue(this::decodeOverlay); | ||
} | ||
|
||
private void saveOverlay(int largestBatchId, DocumentKey key, @Nullable Mutation mutation) { | ||
|
@@ -90,28 +82,76 @@ public void removeOverlaysForBatchId(int batchId) { | |
} | ||
|
||
@Override | ||
public Map<DocumentKey, Mutation> getOverlays(ResourcePath collection, int sinceBatchId) { | ||
public Map<DocumentKey, Overlay> getOverlays(ResourcePath collection, int sinceBatchId) { | ||
String collectionPath = EncodedPath.encode(collection); | ||
|
||
Map<DocumentKey, Mutation> result = new HashMap<>(); | ||
|
||
Map<DocumentKey, Overlay> result = new HashMap<>(); | ||
db.query( | ||
"SELECT document_id, overlay_mutation FROM document_overlays " | ||
"SELECT overlay_mutation, largest_batch_id FROM document_overlays " | ||
+ "WHERE uid = ? AND collection_path = ? AND largest_batch_id > ?") | ||
.binding(uid, collectionPath, sinceBatchId) | ||
.forEach( | ||
row -> { | ||
try { | ||
String documentId = row.getString(0); | ||
Write write = Write.parseFrom(row.getBlob(1)); | ||
Mutation mutation = serializer.decodeMutation(write); | ||
|
||
result.put(DocumentKey.fromPath(collection.append(documentId)), mutation); | ||
} catch (InvalidProtocolBufferException e) { | ||
throw fail("Overlay failed to parse: %s", e); | ||
} | ||
Overlay overlay = decodeOverlay(row); | ||
result.put(overlay.getKey(), overlay); | ||
}); | ||
|
||
return result; | ||
} | ||
|
||
@Override | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Love this implementation. So much cleaner! |
||
public Map<DocumentKey, Overlay> getOverlays( | ||
String collectionGroup, int sinceBatchId, int count) { | ||
Map<DocumentKey, Overlay> result = new HashMap<>(); | ||
Overlay[] lastOverlay = new Overlay[] {null}; | ||
|
||
db.query( | ||
"SELECT overlay_mutation, largest_batch_id FROM document_overlays " | ||
+ "WHERE uid = ? AND collection_group = ? AND largest_batch_id > ? " | ||
+ "ORDER BY largest_batch_id, collection_path, document_id LIMIT ?") | ||
.binding(uid, collectionGroup, sinceBatchId, count) | ||
.forEach( | ||
row -> { | ||
lastOverlay[0] = decodeOverlay(row); | ||
result.put(lastOverlay[0].getKey(), lastOverlay[0]); | ||
}); | ||
|
||
if (lastOverlay[0] == null) { | ||
return result; | ||
} | ||
|
||
// Finish batch | ||
DocumentKey key = lastOverlay[0].getKey(); | ||
String encodedCollectionPath = EncodedPath.encode(key.getCollectionPath()); | ||
db.query( | ||
"SELECT overlay_mutation, largest_batch_id FROM document_overlays " | ||
+ "WHERE uid = ? AND collection_group = ? " | ||
+ "AND (collection_path > ? OR (collection_path = ? AND document_id > ?)) " | ||
+ "AND largest_batch_id = ?") | ||
.binding( | ||
uid, | ||
collectionGroup, | ||
encodedCollectionPath, | ||
encodedCollectionPath, | ||
key.getDocumentId(), | ||
lastOverlay[0].getLargestBatchId()) | ||
.forEach( | ||
row -> { | ||
Overlay overlay = decodeOverlay(row); | ||
result.put(overlay.getKey(), overlay); | ||
}); | ||
|
||
return result; | ||
} | ||
|
||
private Overlay decodeOverlay(android.database.Cursor row) { | ||
try { | ||
Write write = Write.parseFrom(row.getBlob(0)); | ||
int largestBatchId = row.getInt(1); | ||
Mutation mutation = serializer.decodeMutation(write); | ||
return Overlay.create(largestBatchId, mutation); | ||
} catch (InvalidProtocolBufferException e) { | ||
throw fail("Overlay failed to parse: %s", e); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
optional: include
count
as@param
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done