comparison src/org/dancres/blitz/oid/OIDAllocator.java @ 0:3dc0c5604566

Initial checkin of blitz 2.0 fcs - no installer yet.
author Dan Creswell <dan.creswell@gmail.com>
date Sat, 21 Mar 2009 11:00:06 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:3dc0c5604566
1 package org.dancres.blitz.oid;
2
3 import java.io.Serializable;
4
5 import java.util.HashMap;
6
7 import net.jini.config.ConfigurationException;
8
9 import EDU.oswego.cs.dl.util.concurrent.Mutex;
10
11 import org.dancres.util.BytePacker;
12
13 import org.dancres.blitz.config.ConfigurationFactory;
14
15 /**
16 Allocation of OID's is done by an OIDAllocator. Each OIDAllocator has it's
17 own unique identifier (a zone id) so they may be "ganged together" for the
18 purposes of ensuring a large enough pool of available OIDs. This unique
19 identifier forms part of the underlying OID.
20 */
21 class OIDAllocator {
22 private Mutex theLock = new Mutex();
23 private int theZoneId;
24 private long theNextOid = 1;
25 private int theSeed;
26
27 OIDAllocator() {
28 }
29
30 /**
31 Use for FIFO only
32 */
33 OIDAllocator(int anId) {
34 theZoneId = anId;
35 theSeed = anId;
36 }
37
38 OIDAllocator(int anId, int aMaxId) {
39 theZoneId = anId;
40 theSeed = (Integer.MAX_VALUE / aMaxId) * anId;
41 }
42
43 int getId() {
44 return theZoneId;
45 }
46
47 int getSeed() {
48 return theSeed;
49 }
50
51 byte[] getKey() {
52 byte[] myKey = new byte[4];
53 BytePacker myPacker = BytePacker.getMSBPacker(myKey);
54
55 myPacker.putInt(theZoneId, 0);
56
57 return myKey;
58 }
59
60 /**
61 Take a copy of current state to save into persistent storage
62 */
63 Serializable getState() {
64 return new OIDAllocatorState(theZoneId, theNextOid, theSeed);
65 }
66
67 /**
68 Restore state from a copy recovered from persistent storage
69 */
70 void setState(Serializable aState) {
71 OIDAllocatorState myState = (OIDAllocatorState) aState;
72
73 theZoneId = myState.getId();
74 theNextOid = myState.getCurrentOid();
75 theSeed = myState.getSeed();
76 }
77
78 /**
79 Only to be called during recovery - update state based on BootContext
80 */
81 long jump(long aJump) {
82 theNextOid += aJump;
83 return theNextOid;
84 }
85
86 Mutex getLock() {
87 return theLock;
88 }
89
90 OID newOID() {
91 return OIDFactory.newOID(theSeed, theNextOid++);
92 }
93
94 boolean isExhausted() {
95 return (theNextOid < 0);
96 }
97
98 /**
99 Check for collisions between a range of allocators
100 */
101 public static void main(String args[]) {
102 int myNum = Integer.parseInt(args[0]);
103
104 HashMap mySeeds = new HashMap();
105
106 for (int i = 0; i < myNum; i++) {
107 OIDAllocator myAlloc = new OIDAllocator(i, myNum);
108
109 if (mySeeds.get(new Integer(myAlloc.getSeed())) != null)
110 System.err.println("Collision: " + myAlloc.getSeed());
111 else {
112 System.err.println(myAlloc.getSeed());
113 mySeeds.put(new Integer(myAlloc.getSeed()), myAlloc);
114 }
115 }
116 }
117
118 private static class OIDAllocatorState implements Serializable {
119 private int theZoneId;
120 private int theSeed;
121 private long theCurrentOid;
122
123 OIDAllocatorState(int anId, long aCurrentOid, int aSeed) {
124 theZoneId = anId;
125 theCurrentOid = aCurrentOid;
126 theSeed = aSeed;
127 }
128
129 int getSeed() {
130 return theSeed;
131 }
132
133 int getId() {
134 return theZoneId;
135 }
136
137 long getCurrentOid() {
138 return theCurrentOid;
139 }
140 }
141 }