summaryrefslogtreecommitdiff
path: root/jstests/core/use_power_of_2.js
diff options
context:
space:
mode:
Diffstat (limited to 'jstests/core/use_power_of_2.js')
-rw-r--r--jstests/core/use_power_of_2.js52
1 files changed, 52 insertions, 0 deletions
diff --git a/jstests/core/use_power_of_2.js b/jstests/core/use_power_of_2.js
new file mode 100644
index 00000000000..8eb24233cf1
--- /dev/null
+++ b/jstests/core/use_power_of_2.js
@@ -0,0 +1,52 @@
+/*
+ * This test ensures that the usePowerOf2 user flag effectively reuses space.
+ */
+
+// prepare a doc of 14K
+var doc = { _id: new Object(), data: "a" };
+var bigDoc = { _id: new Object(), data: "a" };
+
+while (doc.data.length < 14 * 1024) doc.data += "a";
+while (bigDoc.data.length < 15 * 1024) bigDoc.data += "a";
+
+var collName = "usepower1";
+var t = db.getCollection(collName);
+
+function checkStorageSize(expectedSize, sameLoc) {
+ t.insert(doc);
+ assert.eq(t.stats().size, expectedSize, "size should be expected");
+
+ var oldLoc = t.find()._addSpecial("$showDiskLoc" , true).toArray()[0].$diskLoc;
+
+ // Remvoe smaller doc, insert a bigger one.
+ t.remove(doc);
+ t.insert(bigDoc);
+
+ var newLoc = t.find()._addSpecial("$showDiskLoc" , true).toArray()[0].$diskLoc;
+
+ // Check the diskloc of two docs.
+ assert.eq(oldLoc.file == newLoc.file && oldLoc.offset == newLoc.offset, sameLoc);
+}
+
+t.drop();
+db.createCollection(collName);
+var res = db.runCommand( { "collMod" : collName , "usePowerOf2Sizes" : false } );
+assert( res.ok, "collMod failed" );
+checkStorageSize(15344, false); // 15344 = 14369 (bsonsize) + overhead
+
+t.drop();
+db.createCollection(collName);
+var res = db.runCommand( { "collMod" : collName , "usePowerOf2Sizes" : true } );
+assert( res.ok, "collMod failed" );
+checkStorageSize(16 * 1023, true); // power of 2
+
+
+// Create collection with flag
+t.drop();
+db.runCommand({"create" : collName, "flags" : 0 });
+checkStorageSize(15344, false);
+
+t.drop();
+db.runCommand({"create" : collName, "flags" : 1 });
+checkStorageSize(16 * 1023, true); // power of 2
+