UuidGenerator.html
8.5 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>The source code</title>
<link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="../resources/prettify/prettify.js"></script>
<style type="text/css">
.highlight { display: block; background-color: #ddd; }
</style>
<script type="text/javascript">
function highlight() {
document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
}
</script>
</head>
<body onload="prettyPrint(); highlight();">
<pre class="prettyprint lang-js"><span id='Ext-data-UuidGenerator'>/**
</span> * @extend Ext.data.IdGenerator
* @author Don Griffin
*
* This class generates UUID's according to RFC 4122. This class has a default id property.
* This means that a single instance is shared unless the id property is overridden. Thus,
* two {@link Ext.data.Model} instances configured like the following share one generator:
*
* Ext.define('MyApp.data.MyModelX', {
* extend: 'Ext.data.Model',
* idgen: 'uuid'
* });
*
* Ext.define('MyApp.data.MyModelY', {
* extend: 'Ext.data.Model',
* idgen: 'uuid'
* });
*
* This allows all models using this class to share a commonly configured instance.
*
* # Using Version 1 ("Sequential") UUID's
*
* If a server can provide a proper timestamp and a "cryptographic quality random number"
* (as described in RFC 4122), the shared instance can be configured as follows:
*
* Ext.data.IdGenerator.get('uuid').reconfigure({
* version: 1,
* clockSeq: clock, // 14 random bits
* salt: salt, // 48 secure random bits (the Node field)
* timestamp: ts // timestamp per Section 4.1.4
* });
*
* // or these values can be split into 32-bit chunks:
*
* Ext.data.IdGenerator.get('uuid').reconfigure({
* version: 1,
* clockSeq: clock,
* salt: { lo: saltLow32, hi: saltHigh32 },
* timestamp: { lo: timestampLow32, hi: timestamptHigh32 }
* });
*
* This approach improves the generator's uniqueness by providing a valid timestamp and
* higher quality random data. Version 1 UUID's should not be used unless this information
* can be provided by a server and care should be taken to avoid caching of this data.
*
* See http://www.ietf.org/rfc/rfc4122.txt for details.
*/
Ext.define('Ext.data.UuidGenerator', (function () {
var twoPow14 = Math.pow(2, 14),
twoPow16 = Math.pow(2, 16),
twoPow28 = Math.pow(2, 28),
twoPow32 = Math.pow(2, 32);
function toHex (value, length) {
var ret = value.toString(16);
if (ret.length > length) {
ret = ret.substring(ret.length - length); // right-most digits
} else if (ret.length < length) {
ret = Ext.String.leftPad(ret, length, '0');
}
return ret;
}
function rand (lo, hi) {
var v = Math.random() * (hi - lo + 1);
return Math.floor(v) + lo;
}
function split (bignum) {
if (typeof(bignum) == 'number') {
var hi = Math.floor(bignum / twoPow32);
return {
lo: Math.floor(bignum - hi * twoPow32),
hi: hi
};
}
return bignum;
}
return {
extend: 'Ext.data.IdGenerator',
alias: 'idgen.uuid',
id: 'uuid', // shared by default
<span id='Ext-data-UuidGenerator-property-salt'> /**
</span> * @property {Number/Object} salt
* When created, this value is a 48-bit number. For computation, this value is split
* into 32-bit parts and stored in an object with `hi` and `lo` properties.
*/
<span id='Ext-data-UuidGenerator-property-timestamp'> /**
</span> * @property {Number/Object} timestamp
* When created, this value is a 60-bit number. For computation, this value is split
* into 32-bit parts and stored in an object with `hi` and `lo` properties.
*/
<span id='Ext-data-UuidGenerator-cfg-version'> /**
</span> * @cfg {Number} version
* The Version of UUID. Supported values are:
*
* * 1 : Time-based, "sequential" UUID.
* * 4 : Pseudo-random UUID.
*
* The default is 4.
*/
version: 4,
constructor: function() {
var me = this;
me.callParent(arguments);
me.parts = [];
me.init();
},
generate: function () {
var me = this,
parts = me.parts,
ts = me.timestamp;
/*
The magic decoder ring (derived from RFC 4122 Section 4.2.2):
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| time_low |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| time_mid | ver | time_hi |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|res| clock_hi | clock_low | salt 0 |M| salt 1 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| salt (2-5) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
time_mid clock_hi (low 6 bits)
time_low | time_hi |clock_lo
| | | || salt[0]
| | | || | salt[1..5]
v v v vv v v
0badf00d-aced-1def-b123-dfad0badbeef
^ ^ ^
version | multicast (low bit)
|
reserved (upper 2 bits)
*/
parts[0] = toHex(ts.lo, 8);
parts[1] = toHex(ts.hi & 0xFFFF, 4);
parts[2] = toHex(((ts.hi >>> 16) & 0xFFF) | (me.version << 12), 4);
parts[3] = toHex(0x80 | ((me.clockSeq >>> 8) & 0x3F), 2) +
toHex(me.clockSeq & 0xFF, 2);
parts[4] = toHex(me.salt.hi, 4) + toHex(me.salt.lo, 8);
if (me.version == 4) {
me.init(); // just regenerate all the random values...
} else {
// sequentially increment the timestamp...
++ts.lo;
if (ts.lo >= twoPow32) { // if (overflow)
ts.lo = 0;
++ts.hi;
}
}
return parts.join('-').toLowerCase();
},
getRecId: function (rec) {
return rec.getId();
},
<span id='Ext-data-UuidGenerator-method-init'> /**
</span> * @private
*/
init: function () {
var me = this,
salt, time;
if (me.version == 4) {
// See RFC 4122 (Secion 4.4)
// o If the state was unavailable (e.g., non-existent or corrupted),
// or the saved node ID is different than the current node ID,
// generate a random clock sequence value.
me.clockSeq = rand(0, twoPow14-1);
// we run this on every id generation...
salt = me.salt || (me.salt = {});
time = me.timestamp || (me.timestamp = {});
// See RFC 4122 (Secion 4.4)
salt.lo = rand(0, twoPow32-1);
salt.hi = rand(0, twoPow16-1);
time.lo = rand(0, twoPow32-1);
time.hi = rand(0, twoPow28-1);
} else {
// this is run only once per-instance
me.salt = split(me.salt);
me.timestamp = split(me.timestamp);
// Set multicast bit: "the least significant bit of the first octet of the
// node ID" (nodeId = salt for this implementation):
me.salt.hi |= 0x100;
}
},
<span id='Ext-data-UuidGenerator-method-reconfigure'> /**
</span> * Reconfigures this generator given new config properties.
*/
reconfigure: function (config) {
Ext.apply(this, config);
this.init();
}
};
}()));
</pre>
</body>
</html>