Skip to content

Commit 79310e6

Browse files
committed
Have get check the pending writes cache
1 parent 4da7387 commit 79310e6

File tree

3 files changed

+45
-12
lines changed

3 files changed

+45
-12
lines changed

firebase-database/src/androidTest/java/com/google/firebase/database/QueryTest.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,14 +584,16 @@ public void setVariousLimitsWithStartAtName() throws DatabaseException, Interrup
584584

585585
@Test
586586
public void testStartAfterWithOrderByKey()
587-
throws DatabaseException, InterruptedException, ExecutionException {
587+
throws DatabaseException, InterruptedException, ExecutionException, TimeoutException,
588+
TestFailure {
588589
DatabaseReference ref = IntegrationTestHelpers.getRandomNode();
589590
DatabaseReference childOne = ref.push();
590591
DatabaseReference childTwo = ref.push();
591592
Tasks.await(childOne.setValue(1L));
592593
Tasks.await(childTwo.setValue(2L));
593594

594595
DataSnapshot snapshot = Tasks.await(ref.orderByKey().startAfter(childOne.getKey()).get());
596+
595597
Map<String, Long> values = (Map<String, Long>) snapshot.getValue();
596598

597599
assertNotNull(values);
@@ -4630,6 +4632,33 @@ public void onCancelled(@NonNull DatabaseError error) {}
46304632
}
46314633
}
46324634

4635+
@Test
4636+
public void testGetWithPendingWrites() throws ExecutionException, InterruptedException {
4637+
DatabaseReference node = IntegrationTestHelpers.getRandomNode();
4638+
node.getDatabase().goOffline();
4639+
try {
4640+
Map<String, Object> expected = new MapBuilder().put("foo", "bar").build();
4641+
node.setValue(expected);
4642+
DataSnapshot snapshot = Tasks.await(node.get());
4643+
assertEquals(snapshot.getValue(), expected);
4644+
} finally {
4645+
node.getDatabase().goOnline();
4646+
}
4647+
}
4648+
4649+
@Test
4650+
public void testGetChildOfPendingWrites() throws ExecutionException, InterruptedException {
4651+
DatabaseReference node = IntegrationTestHelpers.getRandomNode();
4652+
node.getDatabase().goOffline();
4653+
try {
4654+
node.setValue(new MapBuilder().put("foo", "bar").build());
4655+
DataSnapshot snapshot = Tasks.await(node.child("foo").get());
4656+
assertEquals(snapshot.getValue(), "bar");
4657+
} finally {
4658+
node.getDatabase().goOnline();
4659+
}
4660+
}
4661+
46334662
@Test
46344663
public void testGetSendsServerProbesPersistenceCacheWhenOfflineWithNoListener()
46354664
throws DatabaseException, InterruptedException, ExecutionException, TestFailure,

firebase-database/src/main/java/com/google/firebase/database/core/SyncPoint.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.google.firebase.database.core.view.View;
3232
import com.google.firebase.database.core.view.ViewCache;
3333
import com.google.firebase.database.snapshot.ChildKey;
34+
import com.google.firebase.database.snapshot.EmptyNode;
3435
import com.google.firebase.database.snapshot.IndexedNode;
3536
import com.google.firebase.database.snapshot.NamedNode;
3637
import com.google.firebase.database.snapshot.Node;
@@ -124,7 +125,9 @@ public View getView(QuerySpec query, WriteTreeRef writesCache, CacheNode serverC
124125
if (eventCache != null) {
125126
eventCacheComplete = true;
126127
} else {
127-
eventCache = writesCache.calcCompleteEventChildren(serverCache.getNode());
128+
eventCache =
129+
writesCache.calcCompleteEventChildren(
130+
serverCache.getNode() != null ? serverCache.getNode() : EmptyNode.Empty());
128131
eventCacheComplete = false;
129132
}
130133
IndexedNode indexed = IndexedNode.from(eventCache, query.getIndex());

firebase-database/src/main/java/com/google/firebase/database/core/SyncTree.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -524,16 +524,17 @@ public Node getServerValue(QuerySpec query) {
524524
: syncPoint.getCompleteServerCache(Path.getEmptyPath());
525525
}
526526

527-
CacheNode serverCache;
528-
if (serverCacheNode != null) {
529-
serverCache =
530-
new CacheNode(IndexedNode.from(serverCacheNode, query.getIndex()), true, false);
531-
532-
WriteTreeRef writesCache = pendingWriteTree.childWrites(path);
533-
View view = syncPoint.getView(query, writesCache, serverCache);
534-
return view.getCompleteNode();
535-
}
536-
return null;
527+
CacheNode serverCache =
528+
new CacheNode(
529+
IndexedNode.from(
530+
serverCacheNode != null ? serverCacheNode : EmptyNode.Empty(),
531+
query.getIndex()),
532+
serverCacheNode != null,
533+
false);
534+
535+
WriteTreeRef writesCache = pendingWriteTree.childWrites(path);
536+
View view = syncPoint.getView(query, writesCache, serverCache);
537+
return view.getCompleteNode();
537538
});
538539
}
539540

0 commit comments

Comments
 (0)