1 | /* |
2 | |
3 | Derby - Class org.apache.derby.impl.services.uuid.BasicUUIDFactory |
4 | |
5 | Copyright 1997, 2004 The Apache Software Foundation or its licensors, as applicable. |
6 | |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
9 | You may obtain a copy of the License at |
10 | |
11 | http://www.apache.org/licenses/LICENSE-2.0 |
12 | |
13 | Unless required by applicable law or agreed to in writing, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
18 | |
19 | */ |
20 | |
21 | package org.apache.derby.impl.services.uuid; |
22 | |
23 | import org.apache.derby.iapi.services.monitor.Monitor; |
24 | import org.apache.derby.catalog.UUID; |
25 | import org.apache.derby.iapi.services.uuid.UUIDFactory; |
26 | |
27 | /** |
28 | |
29 | A hack implementation of something similar to a DCE UUID |
30 | generator. Generates unique 128-bit numbers based on the |
31 | current machine's internet address, the current time, and |
32 | a sequence number. This implementation should be made to |
33 | conform to the DCE specification. ("DEC/HP, Network Computing |
34 | Architecture, Remote Procedure Call Runtime Extensions |
35 | Specification, version OSF TX1.0.11," Steven Miller, July |
36 | 23, 1992. This is part of the OSF DCE Documentation. |
37 | Chapter 10 describes the UUID generation algorithm.) |
38 | <P> |
39 | Some known deficiencies: |
40 | <ul> |
41 | <li> Rather than using the 48-bit hardware network address, |
42 | it uses the 32-bit IP address. IP addresses are not |
43 | guaranteed to be unique. |
44 | <li> There is no provision for generating a suitably unique |
45 | number if no IP address is available. |
46 | <li> Two processes running on this machine which start their |
47 | respective UUID services within a millisecond of one another |
48 | may generate duplicate UUIDS. |
49 | </ul> |
50 | <P> |
51 | However, the intention is that UUIDs generated from this class |
52 | will be unique with respect to UUIDs generated by other DCE |
53 | UUID generators. |
54 | |
55 | **/ |
56 | |
57 | public final class BasicUUIDFactory |
58 | implements UUIDFactory |
59 | { |
60 | /* |
61 | ** Fields of BasicUUIDFactory. |
62 | */ |
63 | |
64 | private long majorId; // 48 bits only |
65 | private long timemillis; |
66 | |
67 | public BasicUUIDFactory() { |
68 | Object env = Monitor.getMonitor().getEnvironment(); |
69 | if (env != null) { |
70 | String s = env.toString(); |
71 | if (s != null) |
72 | env = s; |
73 | |
74 | majorId = ((long) env.hashCode()); |
75 | |
76 | |
77 | } else { |
78 | majorId = Runtime.getRuntime().freeMemory(); |
79 | } |
80 | |
81 | majorId &= 0x0000ffffffffffffL; |
82 | resetCounters(); |
83 | } |
84 | |
85 | |
86 | // |
87 | // Constants and fields for computing the sequence number. We started out with monotonically |
88 | // increasing sequence numbers but realized that this causes collisions at the |
89 | // ends of BTREEs built on UUID columns. So now we have a random number |
90 | // generator. We generate these numbers using a technique from Knuth |
91 | // "Seminumerical Algorithms," section 3.2 (Generating Uniform Random Numbers). |
92 | // The formula is: |
93 | // |
94 | // next = ( (MULTIPLIER * current) + STEP ) % MODULUS |
95 | // |
96 | // Here |
97 | // |
98 | // MODULUS = int size. |
99 | // MULTIPLIER = fairly close to the square root of MODULUS to force the |
100 | // sequence number to jump around. satisifies the rule that |
101 | // (MULTIPLIER-1) is divisible by 4 and by all the primes which |
102 | // divide MODULUS. |
103 | // STEP = a large number that keeps the sequence number jumping around. |
104 | // must be relatively prime to MODULUS. |
105 | // INITIAL_VALUE = a number guaranteeing that the first couple sequence numbers |
106 | // won't be monotonically increasing. |
107 | // |
108 | // The sequence numbers should jump around and cycle through all numbers which fit in an int. |
109 | |
110 | private static final long MODULUS = ( 1L << 32 ); |
111 | private static final long MULTIPLIER = ( ( 1L << 14 ) + 1 ); |
112 | private static final long STEP = ( ( 1L << 27 ) + 1 ); |
113 | private static final long INITIAL_VALUE = ( 2551218188L ); |
114 | |
115 | private long currentValue; |
116 | |
117 | /* |
118 | ** Methods of UUID |
119 | */ |
120 | |
121 | /** |
122 | Generate a new UUID. |
123 | @see UUIDFactory#createUUID |
124 | **/ |
125 | public synchronized UUID createUUID() |
126 | { |
127 | long cv = currentValue = ( ( MULTIPLIER * currentValue ) + STEP ) % MODULUS; |
128 | if ( cv == INITIAL_VALUE ) { bumpMajor(); } |
129 | int sequence = (int) cv; |
130 | |
131 | return new BasicUUID(majorId, timemillis, sequence); |
132 | } |
133 | |
134 | /** |
135 | Recreate a UUID previously generated UUID value. |
136 | @see UUIDFactory#recreateUUID |
137 | **/ |
138 | public UUID recreateUUID(String uuidstring) |
139 | { |
140 | return new BasicUUID(uuidstring); |
141 | } |
142 | |
143 | /** |
144 | @see UUIDFactory#recreateUUID |
145 | **/ |
146 | public UUID recreateUUID(byte[] b) |
147 | { |
148 | return new BasicUUID(b); |
149 | } |
150 | |
151 | private void bumpMajor() { |
152 | |
153 | // 48 bits only |
154 | majorId = (majorId + 1L) & 0x0000ffffffffffffL; |
155 | if (majorId == 0L) |
156 | resetCounters(); |
157 | |
158 | } |
159 | private void resetCounters() |
160 | { |
161 | timemillis = System.currentTimeMillis(); |
162 | currentValue = INITIAL_VALUE; |
163 | } |
164 | } |
165 | |