summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSiyuan Zhou <siyuan.zhou@mongodb.com>2016-07-27 23:23:43 -0400
committerSiyuan Zhou <visualzhou@gmail.com>2016-08-03 12:19:10 -0400
commit90b3966985405c450266be9b19f004f7f3a2b159 (patch)
treea1c62af7c5ba7bb773d550c7afe407870d0e4959
parentf79771f554b078c96095e9a71e6ceaf127e69077 (diff)
downloadmongo-90b3966985405c450266be9b19f004f7f3a2b159.tar.gz
SERVER-25075 Limit BSONObj buffer size used by 2dsphere index.
(cherry picked from commit 2743e906fef318763e753a67967d503b37fcdd07)
-rw-r--r--src/mongo/db/index/s2_common.cpp17
1 files changed, 13 insertions, 4 deletions
diff --git a/src/mongo/db/index/s2_common.cpp b/src/mongo/db/index/s2_common.cpp
index e1db08edae9..ff17951b044 100644
--- a/src/mongo/db/index/s2_common.cpp
+++ b/src/mongo/db/index/s2_common.cpp
@@ -85,12 +85,21 @@ BSONObj S2CellIdToIndexKey(const S2CellId& cellId, S2IndexVersion indexVersion)
// more than once face, individual intervals will
// never cross that threshold. Thus, scans will still
// produce the same results.
- BSONObjBuilder b;
if (indexVersion >= S2_INDEX_VERSION_3) {
+ // The size of an index BSONObj in S2 index version 3 is 15 bytes.
+ // total size (4 bytes) | type code 0x12 (1) | field name "" 0x00 (1) |
+ // long long cell id (8) | EOO (1)
+ BSONObjBuilder b(15);
b.append("", static_cast<long long>(cellId.id()));
- } else {
- b.append("", cellId.ToString());
+ return b.obj();
}
- return b.obj();
+
+ // The size of an index BSONObj in older versions is 10 ~ 40 bytes.
+ // total size (4 bytes) | type code 0x12 (1) | field name "" 0x00 (1) |
+ // cell id string (2 ~ 32) 0x00 (1) | EOO (1)
+ BSONObjBuilder b;
+ b.append("", cellId.ToString());
+ // Return a copy so its buffer size fits the object size.
+ return b.obj().copy();
}
} // namespace mongo