Skip to content

Instantly share code, notes, and snippets.

@mhelmstetter
Created June 16, 2021 17:19
Show Gist options
  • Save mhelmstetter/cf0be3fe4c0613b2788326158a00c16f to your computer and use it in GitHub Desktop.
Save mhelmstetter/cf0be3fe4c0613b2788326158a00c16f to your computer and use it in GitHub Desktop.
// Number of chars of the first two groups (most-significant) in UUID string
var UUIDHeadChars = 12;
// Max number of integers that can be represented using
var UUIDHeadMax = Math.pow(16, UUIDHeadChars);
function UUIDFromInt(num) {
assert(num < UUIDHeadMax);
// Format num as hexadecimal string with enough left-zero-padding
// to reach UUIDHeadChars
head =
(('0'.repeat(UUIDHeadChars)) + num.toString(16)).substr(-UUIDHeadChars);
assert.eq(head.length, UUIDHeadChars);
// return UUID(HHHHHHHH-HHHH-0000-0000-000000000000)
return UUID(
head.substr(0, 8) + '-' + head.substr(8) + '-' +
'0'.repeat(4) + '-' +
'0'.repeat(4) + '-' +
'0'.repeat(12));
};
function genUUIDSplitPoints(numChunks) {
var splitPoints = [];
var gap = Math.round(UUIDHeadMax / numChunks);
var currHead = 0;
for (var i = 0; i < (numChunks - 1); i++) {
currHead += gap;
splitPoints.push(UUIDFromInt(currHead));
}
return splitPoints;
};
function chunkInfos(ns) {
var s = '';
confDB.chunks.find({ns: ns}).sort({ns: 1, min: 1}).forEach(function(z) {
s += ' ' + z._id + ' ' + z.shard + '\n\tmin: ' + tojson(z.min) +
'\n\tmax: ' + tojson(z.max) + '\n';
});
return s;
};
var splitPoints = genUUIDSplitPoints(16);
for (var i = 0; i < splitPoints.length; i++) {
var splitPt = {_id: {id: splitPoints[i]}};
sh.splitAt("uuidTest.test", splitPt);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment