2
0
mirror of synced 2025-02-17 08:46:43 +00:00

[android][database] Fix hanging db when querying database object with large numeric keys

This commit is contained in:
Chris Bianca 2017-06-16 16:58:28 +01:00
parent 53c02a7b73
commit 04cbb63891

View File

@ -211,16 +211,22 @@ public class Utils {
} }
/** /**
* Data should be treated as an array if:
* 1) All the keys are integers
* 2) More than half the keys between 0 and the maximum key in the object have non-empty values
*
* Definition from: https://firebase.googleblog.com/2014/04/best-practices-arrays-in-firebase.html
* *
* @param snapshot * @param snapshot
* @return * @return
*/ */
private static boolean isArray(DataSnapshot snapshot) { private static boolean isArray(DataSnapshot snapshot) {
long expectedKey = -1; long expectedKey = -1;
long maxAllowedKey = (snapshot.getChildrenCount() * 2) - 1;
for (DataSnapshot child : snapshot.getChildren()) { for (DataSnapshot child : snapshot.getChildren()) {
try { try {
long key = Long.parseLong(child.getKey()); long key = Long.parseLong(child.getKey());
if (key > expectedKey) { if (key > expectedKey && key <= maxAllowedKey) {
expectedKey = key; expectedKey = key;
} else { } else {
return false; return false;
@ -233,16 +239,22 @@ public class Utils {
} }
/** /**
* Data should be treated as an array if:
* 1) All the keys are integers
* 2) More than half the keys between 0 and the maximum key in the object have non-empty values
*
* Definition from: https://firebase.googleblog.com/2014/04/best-practices-arrays-in-firebase.html
* *
* @param mutableData * @param mutableData
* @return * @return
*/ */
private static boolean isArray(MutableData mutableData) { private static boolean isArray(MutableData mutableData) {
long expectedKey = -1; long expectedKey = -1;
long maxAllowedKey = (mutableData.getChildrenCount() * 2) - 1;
for (MutableData child : mutableData.getChildren()) { for (MutableData child : mutableData.getChildren()) {
try { try {
long key = Long.parseLong(child.getKey()); long key = Long.parseLong(child.getKey());
if (key > expectedKey) { if (key > expectedKey && key <= maxAllowedKey) {
expectedKey++; expectedKey++;
} else { } else {
return false; return false;