Skip to content

Commit 2850aec

Browse files
committed
resolve comments
1 parent cb91283 commit 2850aec

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

Firestore/Example/Tests/Integration/API/FIRQueryTests.mm

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -816,14 +816,15 @@ - (void)testSnapshotListenerSortsQueryByDocumentIdInTheSameOrderAsServer {
816816
@"__id-2__" : @{@"a" : @1},
817817
@"__id1_" : @{@"a" : @1},
818818
@"_id1__" : @{@"a" : @1},
819+
@"__id" : @{@"a" : @1},
819820
@"__id9223372036854775807__" : @{@"a" : @1},
820821
@"__id-9223372036854775808__" : @{@"a" : @1},
821822
}];
822823

823824
FIRQuery *query = [collRef queryOrderedByFieldPath:[FIRFieldPath documentID]];
824825
NSArray<NSString *> *expectedDocs = @[
825826
@"__id-9223372036854775808__", @"__id-2__", @"__id7__", @"__id12__",
826-
@"__id9223372036854775807__", @"12", @"7", @"A", @"Aa", @"__id1_", @"_id1__", @"a"
827+
@"__id9223372036854775807__", @"12", @"7", @"A", @"Aa", @"__id", @"__id1_", @"_id1__", @"a"
827828
];
828829
FIRQuerySnapshot *getSnapshot = [self readDocumentSetForRef:query];
829830
XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(getSnapshot), expectedDocs);
@@ -848,6 +849,7 @@ - (void)testSnapshotListenerSortsFilteredQueryByDocumentIdInTheSameOrderAsServer
848849
@"__id-2__" : @{@"a" : @1},
849850
@"__id1_" : @{@"a" : @1},
850851
@"_id1__" : @{@"a" : @1},
852+
@"__id" : @{@"a" : @1},
851853
@"__id9223372036854775807__" : @{@"a" : @1},
852854
@"__id-9223372036854775808__" : @{@"a" : @1},
853855
}];
@@ -881,15 +883,16 @@ - (void)testSdkOrdersQueryByDocumentIdTheSameWayOnlineAndOffline {
881883
@"__id-2__" : @{@"a" : @1},
882884
@"__id1_" : @{@"a" : @1},
883885
@"_id1__" : @{@"a" : @1},
886+
@"__id" : @{@"a" : @1},
884887
@"__id9223372036854775807__" : @{@"a" : @1},
885888
@"__id-9223372036854775808__" : @{@"a" : @1},
886889
}];
887890

888891
[self checkOnlineAndOfflineQuery:[collRef queryOrderedByFieldPath:[FIRFieldPath documentID]]
889892
matchesResult:@[
890893
@"__id-9223372036854775808__", @"__id-2__", @"__id7__", @"__id12__",
891-
@"__id9223372036854775807__", @"12", @"7", @"A", @"Aa", @"__id1_", @"_id1__",
892-
@"a"
894+
@"__id9223372036854775807__", @"12", @"7", @"A", @"Aa", @"__id", @"__id1_",
895+
@"_id1__", @"a"
893896
]];
894897
}
895898

Firestore/core/src/model/base_path.h

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ class BasePath {
158158
util::ComparisonResult CompareTo(const T& rhs) const {
159159
size_t min_size = std::min(size(), rhs.size());
160160
for (size_t i = 0; i < min_size; ++i) {
161-
auto cmp = compareSegments(segments_[i], rhs.segments_[i]);
161+
auto cmp = CompareSegments(segments_[i], rhs.segments_[i]);
162162
if (!util::Same(cmp)) return cmp;
163163
}
164164
return util::Compare(size(), rhs.size());
@@ -185,29 +185,36 @@ class BasePath {
185185
private:
186186
SegmentsT segments_;
187187

188-
static util::ComparisonResult compareSegments(const std::string& lhs,
188+
static const size_t kNumericIdPrefixLength = 4;
189+
static const size_t kNumericIdSuffixLength = 2;
190+
static const size_t kNumericIdTotalOverhead =
191+
kNumericIdPrefixLength + kNumericIdSuffixLength;
192+
193+
static util::ComparisonResult CompareSegments(const std::string& lhs,
189194
const std::string& rhs) {
190-
bool isLhsNumeric = isNumericId(lhs);
191-
bool isRhsNumeric = isNumericId(rhs);
195+
bool isLhsNumeric = IsNumericId(lhs);
196+
bool isRhsNumeric = IsNumericId(rhs);
192197

193198
if (isLhsNumeric && !isRhsNumeric) {
194199
return util::ComparisonResult::Ascending;
195200
} else if (!isLhsNumeric && isRhsNumeric) {
196201
return util::ComparisonResult::Descending;
197202
} else if (isLhsNumeric && isRhsNumeric) {
198-
return util::Compare(extractNumericId(lhs), extractNumericId(rhs));
203+
return util::Compare(ExtractNumericId(lhs), ExtractNumericId(rhs));
199204
} else {
200205
return util::Compare(lhs, rhs);
201206
}
202207
}
203208

204-
static bool isNumericId(const std::string& segment) {
205-
return segment.substr(0, 4) == "__id" &&
206-
segment.substr(segment.size() - 2) == "__";
209+
static bool IsNumericId(const std::string& segment) {
210+
return segment.size() > kNumericIdTotalOverhead &&
211+
segment.substr(0, kNumericIdPrefixLength) == "__id" &&
212+
segment.substr(segment.size() - kNumericIdSuffixLength) == "__";
207213
}
208214

209-
static int64_t extractNumericId(const std::string& segment) {
210-
return std::stol(segment.substr(4, segment.size() - 2));
215+
static int64_t ExtractNumericId(const std::string& segment) {
216+
return std::stol(segment.substr(kNumericIdPrefixLength,
217+
segment.size() - kNumericIdSuffixLength));
211218
}
212219
};
213220

0 commit comments

Comments
 (0)