Skip to content

Commit 565d322

Browse files
committed
Merge branch 'jw/start-after' into jw/end-before
2 parents ae6ebed + 889aa88 commit 565d322

File tree

3 files changed

+67
-8
lines changed

3 files changed

+67
-8
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ public void onCancelled(DatabaseError error) {}
814814
}
815815

816816
@Test
817-
public void setLimitEnsureChildRemovedAndChildAddedHitWhenLimitIsHitFromFrontWithStartAfter()
817+
public void setLimitEnsureChildRemovedAndChildAddedHitWhenDataEntersLimitWithStartAfter()
818818
throws DatabaseException, TestFailure, ExecutionException, TimeoutException,
819819
InterruptedException {
820820
DatabaseReference ref = IntegrationTestHelpers.getRandomNode();
@@ -3922,6 +3922,7 @@ public void integerKeysBehaveNumericallyWithStartAfterOverflow()
39223922
.put("600", true)
39233923
.put("70", true)
39243924
.put("8", true)
3925+
.put("a", true)
39253926
.build(),
39263927
new DatabaseReference.CompletionListener() {
39273928
@Override
@@ -3931,7 +3932,8 @@ public void onComplete(DatabaseError error, DatabaseReference ref) {
39313932
new ValueEventListener() {
39323933
@Override
39333934
public void onDataChange(DataSnapshot snapshot) {
3934-
DeepEquals.assertEquals(null, snapshot.getValue());
3935+
DeepEquals.assertEquals(
3936+
new MapBuilder().put("a", true).build(), snapshot.getValue());
39353937
done.release();
39363938
}
39373939

firebase-database/src/main/java/com/google/firebase/database/core/utilities/PushIdGenerator.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,8 @@ public static final String successor(String key) {
9898
Validation.validateNullableKey(key);
9999
Integer num = tryParseInt(key);
100100
if (num != null) {
101-
if (Long.valueOf(num) + 1L > Integer.MAX_VALUE) {
102-
// Consult for the rationale behind this.
103-
// https://firebase.google.com/docs/database/web/lists-of-data#data-order
101+
if (num + 1L > Integer.MAX_VALUE) {
102+
// See https://firebase.google.com/docs/database/web/lists-of-data#data-order
104103
return String.valueOf(MIN_PUSH_CHAR);
105104
}
106105
return String.valueOf(num + 1);
@@ -116,15 +115,14 @@ public static final String successor(String key) {
116115
}
117116

118117
int i = next.length() - 1;
119-
120118
while (i >= 0 && next.charAt(i) == MAX_PUSH_CHAR) {
121119
i--;
122120
}
123121

124-
// `nextAfter` was called on the largest possible key, so return the
122+
// `successor` was called on the lexicographically largest possible key, so return the
125123
// maxName, which sorts larger than all keys.
126124
if (i == -1) {
127-
return ChildKey.getMaxName().toString();
125+
return ChildKey.MAX_KEY_NAME;
128126
}
129127

130128
// `i` now points to the last character in `key` that is < MAX_PUSH_CHAR,
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2021 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.database;
16+
17+
import static com.google.firebase.database.snapshot.ChildKey.MAX_KEY_NAME;
18+
import static org.junit.Assert.assertEquals;
19+
20+
import com.google.firebase.database.core.utilities.PushIdGenerator;
21+
import org.codehaus.plexus.util.StringUtils;
22+
import org.junit.Test;
23+
import org.robolectric.RobolectricTestRunner;
24+
import org.robolectric.annotation.Config;
25+
26+
@org.junit.runner.RunWith(RobolectricTestRunner.class)
27+
@Config(manifest = Config.NONE)
28+
public class PushIdGeneratorTest {
29+
30+
private static final char MIN_PUSH_CHAR = '-';
31+
32+
private static final char MAX_PUSH_CHAR = 'z';
33+
34+
private static final int MAX_KEY_LEN = 786;
35+
36+
@Test
37+
public void testSuccessorSpecialValue() {
38+
assertEquals(
39+
PushIdGenerator.successor(String.valueOf(Integer.MAX_VALUE)),
40+
Character.toString(MIN_PUSH_CHAR));
41+
assertEquals(
42+
PushIdGenerator.successor(
43+
StringUtils.repeat(Character.toString(MAX_PUSH_CHAR), MAX_KEY_LEN)),
44+
MAX_KEY_NAME);
45+
}
46+
47+
@Test
48+
public void testSuccessorBasic() {
49+
assertEquals(PushIdGenerator.successor("abc"), "abc" + MIN_PUSH_CHAR);
50+
assertEquals(
51+
PushIdGenerator.successor(
52+
"abc"
53+
+ StringUtils.repeat(
54+
Character.toString(MAX_PUSH_CHAR), MAX_KEY_LEN - "abc".length())),
55+
"abd");
56+
assertEquals(
57+
PushIdGenerator.successor("abc" + MIN_PUSH_CHAR), "abc" + MIN_PUSH_CHAR + MIN_PUSH_CHAR);
58+
}
59+
}

0 commit comments

Comments
 (0)