Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
SequenceManagerInMemoryImpl |
|
| 2.2;2.2 |
1 | package org.apache.ojb.broker.util.sequence; | |
2 | ||
3 | /* Copyright 2002-2005 The Apache Software Foundation | |
4 | * | |
5 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
6 | * you may not use this file except in compliance with the License. | |
7 | * You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | */ | |
17 | ||
18 | import org.apache.ojb.broker.PersistenceBroker; | |
19 | import org.apache.ojb.broker.metadata.FieldDescriptor; | |
20 | ||
21 | import java.util.HashMap; | |
22 | import java.util.Map; | |
23 | ||
24 | /** | |
25 | * <p> | |
26 | * Very fast in memory sequence manager implementation, only the first | |
27 | * time an id was requested for a class, the manager query the database | |
28 | * for the max id in requested column - all following request were | |
29 | * performed in memory. | |
30 | * </p> | |
31 | * <b>Limitations:</b> | |
32 | * <ul type="disc"> | |
33 | * <li>do not use in clustered environments</li> | |
34 | * <li>do not use if other applications generate id's for objects</li> | |
35 | * </ul> | |
36 | * | |
37 | * | |
38 | * | |
39 | * <p> | |
40 | * Implementation configuration properties: | |
41 | * </p> | |
42 | * | |
43 | * <table cellspacing="2" cellpadding="2" border="3" frame="box"> | |
44 | * <tr> | |
45 | * <td><strong>Property Key</strong></td> | |
46 | * <td><strong>Property Values</strong></td> | |
47 | * </tr> | |
48 | * <tr> | |
49 | * <td>seq.start</td> | |
50 | * <td> | |
51 | * Set the start index of used sequences (e.g. set 100000, id generation starts with 100001). | |
52 | * Default start index is <em>1</em>. | |
53 | * </td> | |
54 | * </tr> | |
55 | * <tr> | |
56 | * <td>autoNaming</td> | |
57 | * <td> | |
58 | * Default was 'true'. If set 'true' OJB try to build a | |
59 | * sequence name automatic if none found in field-descriptor | |
60 | * and set this generated name as <code>sequence-name</code> | |
61 | * in field-descriptor. If set 'false' OJB throws an exception | |
62 | * if none sequence name was found in field-descriptor. | |
63 | * </td> | |
64 | * </tr> | |
65 | * <tr> | |
66 | * <td>sequenceStart</td> | |
67 | * <td> | |
68 | * <em>Deprecated, use property 'seq.start'.</em> Set the start index | |
69 | * of used sequences (e.g. set 100000, id generation starts with 100001). | |
70 | * Default start index is <em>1</em>. | |
71 | * </td> | |
72 | * </tr> | |
73 | * </table> | |
74 | * | |
75 | * <br/> | |
76 | * <br/> | |
77 | * <br/> | |
78 | * | |
79 | * @see org.apache.ojb.broker.util.sequence.SequenceManager | |
80 | * @see org.apache.ojb.broker.util.sequence.SequenceManagerFactory | |
81 | * @see org.apache.ojb.broker.util.sequence.SequenceManagerHelper | |
82 | * | |
83 | * | |
84 | * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a> | |
85 | * @version $Id: SequenceManagerInMemoryImpl.java,v 1.1 2007-08-24 22:17:29 ewestfal Exp $ | |
86 | */ | |
87 | public class SequenceManagerInMemoryImpl extends AbstractSequenceManager | |
88 | { | |
89 | protected static Map sequencesDBMap = new HashMap(); | |
90 | private long sequenceStart; | |
91 | ||
92 | public SequenceManagerInMemoryImpl(PersistenceBroker broker) | |
93 | { | |
94 | super(broker); | |
95 | Long start = SequenceManagerHelper.getSeqStart(getConfigurationProperties()); | |
96 | sequenceStart = start != null ? start.longValue() : 1; | |
97 | } | |
98 | ||
99 | protected long getUniqueLong(FieldDescriptor field) throws SequenceManagerException | |
100 | { | |
101 | String seqName = calculateSequenceName(field); | |
102 | // we have to be threadsafe | |
103 | synchronized (SequenceManagerInMemoryImpl.class) | |
104 | { | |
105 | // get id for given seq name | |
106 | Long currentId = getSequence(seqName); | |
107 | // check - first time we search for sequence name | |
108 | if (currentId == null) | |
109 | { | |
110 | long maxKey = SequenceManagerHelper.getMaxForExtent(getBrokerForClass(), field); | |
111 | maxKey = sequenceStart > maxKey ? sequenceStart : maxKey; | |
112 | currentId = new Long(maxKey); | |
113 | } | |
114 | currentId = new Long(currentId.longValue() + 1); | |
115 | // put new id back to map | |
116 | addSequence(seqName, currentId); | |
117 | return currentId.intValue(); | |
118 | } | |
119 | } | |
120 | ||
121 | /** | |
122 | * Returns last used sequence value or <code>null</code> if no sequence | |
123 | * was add for given sequence name. | |
124 | * | |
125 | * @param sequenceName Name of the sequence. | |
126 | * @return Last used sequence value or <code>null</code> | |
127 | */ | |
128 | private Long getSequence(String sequenceName) | |
129 | { | |
130 | Long result = null; | |
131 | // now lookup the sequence map for calling DB | |
132 | Map mapForDB = (Map) sequencesDBMap.get(getBrokerForClass() | |
133 | .serviceConnectionManager().getConnectionDescriptor().getJcdAlias()); | |
134 | if(mapForDB != null) | |
135 | { | |
136 | result = (Long) mapForDB.get(sequenceName); | |
137 | } | |
138 | return result; | |
139 | } | |
140 | ||
141 | /** | |
142 | * Add new sequence value for sequence name. | |
143 | * @param sequenceName Name of the sequence. | |
144 | * @param seq The sequence value to add. | |
145 | */ | |
146 | private void addSequence(String sequenceName, Long seq) | |
147 | { | |
148 | // lookup the sequence map for calling DB | |
149 | String jcdAlias = getBrokerForClass() | |
150 | .serviceConnectionManager().getConnectionDescriptor().getJcdAlias(); | |
151 | Map mapForDB = (Map) sequencesDBMap.get(jcdAlias); | |
152 | if(mapForDB == null) | |
153 | { | |
154 | mapForDB = new HashMap(); | |
155 | } | |
156 | mapForDB.put(sequenceName, seq); | |
157 | sequencesDBMap.put(jcdAlias, mapForDB); | |
158 | } | |
159 | ||
160 | /** | |
161 | * Remove the sequence for given sequence name. | |
162 | * | |
163 | * @param sequenceName Name of the sequence to remove. | |
164 | */ | |
165 | protected void removeSequence(String sequenceName) | |
166 | { | |
167 | // lookup the sequence map for calling DB | |
168 | Map mapForDB = (Map) sequencesDBMap.get(getBrokerForClass() | |
169 | .serviceConnectionManager().getConnectionDescriptor().getJcdAlias()); | |
170 | if(mapForDB != null) | |
171 | { | |
172 | synchronized(SequenceManagerInMemoryImpl.class) | |
173 | { | |
174 | mapForDB.remove(sequenceName); | |
175 | } | |
176 | } | |
177 | } | |
178 | } |