1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
// SERVER-7282 Faulty logic when testing maximum collection name length.
// constants from server
var maxNsLength = 127;
var maxNsCollectionLength = 120;
var myDb = db.getSiblingDB("ns_length");
myDb.dropDatabase(); // start empty
function mkStr(length) {
s = "";
while (s.length < length) {
s += "x";
}
return s;
}
function canMakeCollectionWithName(name) {
assert.eq(myDb.stats().fileSize, 0, "initial conditions");
myDb[name].insert({});
var success = myDb.getLastError() == null;
if (!success) {
assert.eq(myDb.stats().fileSize, 0, "no files should be created on error");
return false;
}
myDb.dropDatabase();
return true;
}
function canMakeIndexWithName(collection, name) {
var success = (collection.ensureIndex({x:1}, {name: name}) == undefined);
if (success) {
assert.commandWorked(collection.dropIndex(name));
}
return success;
}
function canRenameCollection(from, to) {
var success = myDb[from].renameCollection(to).ok;
if (success) {
// put it back
assert.commandWorked(myDb[to].renameCollection(from));
}
return success;
}
// test making collections around the name limit
var prefixOverhead = (myDb.getName() + ".").length;
var maxCollectionNameLength = maxNsCollectionLength - prefixOverhead;
for (var i = maxCollectionNameLength - 3; i <= maxCollectionNameLength + 3; i++) {
assert.eq(canMakeCollectionWithName(mkStr(i)),
i <= maxCollectionNameLength,
"ns name length = " + (prefixOverhead + i));
}
// test making indexes around the name limit
var collection = myDb.collection;
collection.insert({});
var maxIndexNameLength = maxNsLength - (collection.getFullName() + ".$").length;
for (var i = maxIndexNameLength - 3; i <= maxIndexNameLength + 3; i++) {
assert.eq(canMakeIndexWithName(collection, mkStr(i)),
i <= maxIndexNameLength,
"index ns name length = " + ((collection.getFullName() + ".$").length + i));
}
// test renaming collections with the destination around the name limit
myDb.from.insert({});
for (var i = maxCollectionNameLength - 3; i <= maxCollectionNameLength + 3; i++) {
assert.eq(canRenameCollection("from", mkStr(i)),
i <= maxCollectionNameLength,
"new ns name length = " + (prefixOverhead + i));
}
// test renaming collections with the destination around the name limit due to long indexe names
myDb.from.ensureIndex({a:1}, {name: mkStr(100)});
var indexNsNameOverhead = (myDb.getName() + "..$").length + 100; // index ns name - collection name
var maxCollectionNameWithIndex = maxNsLength - indexNsNameOverhead;
for (var i = maxCollectionNameWithIndex - 3; i <= maxCollectionNameWithIndex + 3; i++) {
assert.eq(canRenameCollection("from", mkStr(i)),
i <= maxCollectionNameWithIndex,
"index ns name length = " + (indexNsNameOverhead + i));
}
|