1318 |
alexandre_ |
1 |
/*
|
|
|
2 |
Copyright (c) 2004-2006, The Dojo Foundation
|
|
|
3 |
All Rights Reserved.
|
|
|
4 |
|
|
|
5 |
Licensed under the Academic Free License version 2.1 or above OR the
|
|
|
6 |
modified BSD license. For more information on Dojo licensing, see:
|
|
|
7 |
|
|
|
8 |
http://dojotoolkit.org/community/licensing.shtml
|
|
|
9 |
*/
|
|
|
10 |
|
|
|
11 |
dojo.provide("dojo.uuid.TimeBasedGenerator");
|
|
|
12 |
dojo.require("dojo.lang.common");
|
|
|
13 |
dojo.require("dojo.lang.type");
|
|
|
14 |
dojo.require("dojo.lang.assert");
|
|
|
15 |
dojo.uuid.TimeBasedGenerator = new function () {
|
|
|
16 |
this.GREGORIAN_CHANGE_OFFSET_IN_HOURS = 3394248;
|
|
|
17 |
var _uuidPseudoNodeString = null;
|
|
|
18 |
var _uuidClockSeqString = null;
|
|
|
19 |
var _dateValueOfPreviousUuid = null;
|
|
|
20 |
var _nextIntraMillisecondIncrement = 0;
|
|
|
21 |
var _cachedMillisecondsBetween1582and1970 = null;
|
|
|
22 |
var _cachedHundredNanosecondIntervalsPerMillisecond = null;
|
|
|
23 |
var _uniformNode = null;
|
|
|
24 |
var HEX_RADIX = 16;
|
|
|
25 |
function _carry(arrayA) {
|
|
|
26 |
arrayA[2] += arrayA[3] >>> 16;
|
|
|
27 |
arrayA[3] &= 65535;
|
|
|
28 |
arrayA[1] += arrayA[2] >>> 16;
|
|
|
29 |
arrayA[2] &= 65535;
|
|
|
30 |
arrayA[0] += arrayA[1] >>> 16;
|
|
|
31 |
arrayA[1] &= 65535;
|
|
|
32 |
dojo.lang.assert((arrayA[0] >>> 16) === 0);
|
|
|
33 |
}
|
|
|
34 |
function _get64bitArrayFromFloat(x) {
|
|
|
35 |
var result = new Array(0, 0, 0, 0);
|
|
|
36 |
result[3] = x % 65536;
|
|
|
37 |
x -= result[3];
|
|
|
38 |
x /= 65536;
|
|
|
39 |
result[2] = x % 65536;
|
|
|
40 |
x -= result[2];
|
|
|
41 |
x /= 65536;
|
|
|
42 |
result[1] = x % 65536;
|
|
|
43 |
x -= result[1];
|
|
|
44 |
x /= 65536;
|
|
|
45 |
result[0] = x;
|
|
|
46 |
return result;
|
|
|
47 |
}
|
|
|
48 |
function _addTwo64bitArrays(arrayA, arrayB) {
|
|
|
49 |
dojo.lang.assertType(arrayA, Array);
|
|
|
50 |
dojo.lang.assertType(arrayB, Array);
|
|
|
51 |
dojo.lang.assert(arrayA.length == 4);
|
|
|
52 |
dojo.lang.assert(arrayB.length == 4);
|
|
|
53 |
var result = new Array(0, 0, 0, 0);
|
|
|
54 |
result[3] = arrayA[3] + arrayB[3];
|
|
|
55 |
result[2] = arrayA[2] + arrayB[2];
|
|
|
56 |
result[1] = arrayA[1] + arrayB[1];
|
|
|
57 |
result[0] = arrayA[0] + arrayB[0];
|
|
|
58 |
_carry(result);
|
|
|
59 |
return result;
|
|
|
60 |
}
|
|
|
61 |
function _multiplyTwo64bitArrays(arrayA, arrayB) {
|
|
|
62 |
dojo.lang.assertType(arrayA, Array);
|
|
|
63 |
dojo.lang.assertType(arrayB, Array);
|
|
|
64 |
dojo.lang.assert(arrayA.length == 4);
|
|
|
65 |
dojo.lang.assert(arrayB.length == 4);
|
|
|
66 |
var overflow = false;
|
|
|
67 |
if (arrayA[0] * arrayB[0] !== 0) {
|
|
|
68 |
overflow = true;
|
|
|
69 |
}
|
|
|
70 |
if (arrayA[0] * arrayB[1] !== 0) {
|
|
|
71 |
overflow = true;
|
|
|
72 |
}
|
|
|
73 |
if (arrayA[0] * arrayB[2] !== 0) {
|
|
|
74 |
overflow = true;
|
|
|
75 |
}
|
|
|
76 |
if (arrayA[1] * arrayB[0] !== 0) {
|
|
|
77 |
overflow = true;
|
|
|
78 |
}
|
|
|
79 |
if (arrayA[1] * arrayB[1] !== 0) {
|
|
|
80 |
overflow = true;
|
|
|
81 |
}
|
|
|
82 |
if (arrayA[2] * arrayB[0] !== 0) {
|
|
|
83 |
overflow = true;
|
|
|
84 |
}
|
|
|
85 |
dojo.lang.assert(!overflow);
|
|
|
86 |
var result = new Array(0, 0, 0, 0);
|
|
|
87 |
result[0] += arrayA[0] * arrayB[3];
|
|
|
88 |
_carry(result);
|
|
|
89 |
result[0] += arrayA[1] * arrayB[2];
|
|
|
90 |
_carry(result);
|
|
|
91 |
result[0] += arrayA[2] * arrayB[1];
|
|
|
92 |
_carry(result);
|
|
|
93 |
result[0] += arrayA[3] * arrayB[0];
|
|
|
94 |
_carry(result);
|
|
|
95 |
result[1] += arrayA[1] * arrayB[3];
|
|
|
96 |
_carry(result);
|
|
|
97 |
result[1] += arrayA[2] * arrayB[2];
|
|
|
98 |
_carry(result);
|
|
|
99 |
result[1] += arrayA[3] * arrayB[1];
|
|
|
100 |
_carry(result);
|
|
|
101 |
result[2] += arrayA[2] * arrayB[3];
|
|
|
102 |
_carry(result);
|
|
|
103 |
result[2] += arrayA[3] * arrayB[2];
|
|
|
104 |
_carry(result);
|
|
|
105 |
result[3] += arrayA[3] * arrayB[3];
|
|
|
106 |
_carry(result);
|
|
|
107 |
return result;
|
|
|
108 |
}
|
|
|
109 |
function _padWithLeadingZeros(string, desiredLength) {
|
|
|
110 |
while (string.length < desiredLength) {
|
|
|
111 |
string = "0" + string;
|
|
|
112 |
}
|
|
|
113 |
return string;
|
|
|
114 |
}
|
|
|
115 |
function _generateRandomEightCharacterHexString() {
|
|
|
116 |
var random32bitNumber = Math.floor((Math.random() % 1) * Math.pow(2, 32));
|
|
|
117 |
var eightCharacterString = random32bitNumber.toString(HEX_RADIX);
|
|
|
118 |
while (eightCharacterString.length < 8) {
|
|
|
119 |
eightCharacterString = "0" + eightCharacterString;
|
|
|
120 |
}
|
|
|
121 |
return eightCharacterString;
|
|
|
122 |
}
|
|
|
123 |
function _generateUuidString(node) {
|
|
|
124 |
dojo.lang.assertType(node, String, {optional:true});
|
|
|
125 |
if (node) {
|
|
|
126 |
dojo.lang.assert(node.length == 12);
|
|
|
127 |
} else {
|
|
|
128 |
if (_uniformNode) {
|
|
|
129 |
node = _uniformNode;
|
|
|
130 |
} else {
|
|
|
131 |
if (!_uuidPseudoNodeString) {
|
|
|
132 |
var pseudoNodeIndicatorBit = 32768;
|
|
|
133 |
var random15bitNumber = Math.floor((Math.random() % 1) * Math.pow(2, 15));
|
|
|
134 |
var leftmost4HexCharacters = (pseudoNodeIndicatorBit | random15bitNumber).toString(HEX_RADIX);
|
|
|
135 |
_uuidPseudoNodeString = leftmost4HexCharacters + _generateRandomEightCharacterHexString();
|
|
|
136 |
}
|
|
|
137 |
node = _uuidPseudoNodeString;
|
|
|
138 |
}
|
|
|
139 |
}
|
|
|
140 |
if (!_uuidClockSeqString) {
|
|
|
141 |
var variantCodeForDCEUuids = 32768;
|
|
|
142 |
var random14bitNumber = Math.floor((Math.random() % 1) * Math.pow(2, 14));
|
|
|
143 |
_uuidClockSeqString = (variantCodeForDCEUuids | random14bitNumber).toString(HEX_RADIX);
|
|
|
144 |
}
|
|
|
145 |
var now = new Date();
|
|
|
146 |
var millisecondsSince1970 = now.valueOf();
|
|
|
147 |
var nowArray = _get64bitArrayFromFloat(millisecondsSince1970);
|
|
|
148 |
if (!_cachedMillisecondsBetween1582and1970) {
|
|
|
149 |
var arraySecondsPerHour = _get64bitArrayFromFloat(60 * 60);
|
|
|
150 |
var arrayHoursBetween1582and1970 = _get64bitArrayFromFloat(dojo.uuid.TimeBasedGenerator.GREGORIAN_CHANGE_OFFSET_IN_HOURS);
|
|
|
151 |
var arraySecondsBetween1582and1970 = _multiplyTwo64bitArrays(arrayHoursBetween1582and1970, arraySecondsPerHour);
|
|
|
152 |
var arrayMillisecondsPerSecond = _get64bitArrayFromFloat(1000);
|
|
|
153 |
_cachedMillisecondsBetween1582and1970 = _multiplyTwo64bitArrays(arraySecondsBetween1582and1970, arrayMillisecondsPerSecond);
|
|
|
154 |
_cachedHundredNanosecondIntervalsPerMillisecond = _get64bitArrayFromFloat(10000);
|
|
|
155 |
}
|
|
|
156 |
var arrayMillisecondsSince1970 = nowArray;
|
|
|
157 |
var arrayMillisecondsSince1582 = _addTwo64bitArrays(_cachedMillisecondsBetween1582and1970, arrayMillisecondsSince1970);
|
|
|
158 |
var arrayHundredNanosecondIntervalsSince1582 = _multiplyTwo64bitArrays(arrayMillisecondsSince1582, _cachedHundredNanosecondIntervalsPerMillisecond);
|
|
|
159 |
if (now.valueOf() == _dateValueOfPreviousUuid) {
|
|
|
160 |
arrayHundredNanosecondIntervalsSince1582[3] += _nextIntraMillisecondIncrement;
|
|
|
161 |
_carry(arrayHundredNanosecondIntervalsSince1582);
|
|
|
162 |
_nextIntraMillisecondIncrement += 1;
|
|
|
163 |
if (_nextIntraMillisecondIncrement == 10000) {
|
|
|
164 |
while (now.valueOf() == _dateValueOfPreviousUuid) {
|
|
|
165 |
now = new Date();
|
|
|
166 |
}
|
|
|
167 |
}
|
|
|
168 |
} else {
|
|
|
169 |
_dateValueOfPreviousUuid = now.valueOf();
|
|
|
170 |
_nextIntraMillisecondIncrement = 1;
|
|
|
171 |
}
|
|
|
172 |
var hexTimeLowLeftHalf = arrayHundredNanosecondIntervalsSince1582[2].toString(HEX_RADIX);
|
|
|
173 |
var hexTimeLowRightHalf = arrayHundredNanosecondIntervalsSince1582[3].toString(HEX_RADIX);
|
|
|
174 |
var hexTimeLow = _padWithLeadingZeros(hexTimeLowLeftHalf, 4) + _padWithLeadingZeros(hexTimeLowRightHalf, 4);
|
|
|
175 |
var hexTimeMid = arrayHundredNanosecondIntervalsSince1582[1].toString(HEX_RADIX);
|
|
|
176 |
hexTimeMid = _padWithLeadingZeros(hexTimeMid, 4);
|
|
|
177 |
var hexTimeHigh = arrayHundredNanosecondIntervalsSince1582[0].toString(HEX_RADIX);
|
|
|
178 |
hexTimeHigh = _padWithLeadingZeros(hexTimeHigh, 3);
|
|
|
179 |
var hyphen = "-";
|
|
|
180 |
var versionCodeForTimeBasedUuids = "1";
|
|
|
181 |
var resultUuid = hexTimeLow + hyphen + hexTimeMid + hyphen + versionCodeForTimeBasedUuids + hexTimeHigh + hyphen + _uuidClockSeqString + hyphen + node;
|
|
|
182 |
resultUuid = resultUuid.toLowerCase();
|
|
|
183 |
return resultUuid;
|
|
|
184 |
}
|
|
|
185 |
this.setNode = function (node) {
|
|
|
186 |
dojo.lang.assert((node === null) || (node.length == 12));
|
|
|
187 |
_uniformNode = node;
|
|
|
188 |
};
|
|
|
189 |
this.getNode = function () {
|
|
|
190 |
return _uniformNode;
|
|
|
191 |
};
|
|
|
192 |
this.generate = function (input) {
|
|
|
193 |
var nodeString = null;
|
|
|
194 |
var returnType = null;
|
|
|
195 |
if (input) {
|
|
|
196 |
if (dojo.lang.isObject(input) && !dojo.lang.isBuiltIn(input)) {
|
|
|
197 |
var namedParameters = input;
|
|
|
198 |
dojo.lang.assertValidKeywords(namedParameters, ["node", "hardwareNode", "pseudoNode", "returnType"]);
|
|
|
199 |
var node = namedParameters["node"];
|
|
|
200 |
var hardwareNode = namedParameters["hardwareNode"];
|
|
|
201 |
var pseudoNode = namedParameters["pseudoNode"];
|
|
|
202 |
nodeString = (node || pseudoNode || hardwareNode);
|
|
|
203 |
if (nodeString) {
|
|
|
204 |
var firstCharacter = nodeString.charAt(0);
|
|
|
205 |
var firstDigit = parseInt(firstCharacter, HEX_RADIX);
|
|
|
206 |
if (hardwareNode) {
|
|
|
207 |
dojo.lang.assert((firstDigit >= 0) && (firstDigit <= 7));
|
|
|
208 |
}
|
|
|
209 |
if (pseudoNode) {
|
|
|
210 |
dojo.lang.assert((firstDigit >= 8) && (firstDigit <= 15));
|
|
|
211 |
}
|
|
|
212 |
}
|
|
|
213 |
returnType = namedParameters["returnType"];
|
|
|
214 |
dojo.lang.assertType(returnType, Function, {optional:true});
|
|
|
215 |
} else {
|
|
|
216 |
if (dojo.lang.isString(input)) {
|
|
|
217 |
nodeString = input;
|
|
|
218 |
returnType = null;
|
|
|
219 |
} else {
|
|
|
220 |
if (dojo.lang.isFunction(input)) {
|
|
|
221 |
nodeString = null;
|
|
|
222 |
returnType = input;
|
|
|
223 |
}
|
|
|
224 |
}
|
|
|
225 |
}
|
|
|
226 |
if (nodeString) {
|
|
|
227 |
dojo.lang.assert(nodeString.length == 12);
|
|
|
228 |
var integer = parseInt(nodeString, HEX_RADIX);
|
|
|
229 |
dojo.lang.assert(isFinite(integer));
|
|
|
230 |
}
|
|
|
231 |
dojo.lang.assertType(returnType, Function, {optional:true});
|
|
|
232 |
}
|
|
|
233 |
var uuidString = _generateUuidString(nodeString);
|
|
|
234 |
var returnValue;
|
|
|
235 |
if (returnType && (returnType != String)) {
|
|
|
236 |
returnValue = new returnType(uuidString);
|
|
|
237 |
} else {
|
|
|
238 |
returnValue = uuidString;
|
|
|
239 |
}
|
|
|
240 |
return returnValue;
|
|
|
241 |
};
|
|
|
242 |
}();
|
|
|
243 |
|