| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| SequenceManagerSeqHiLoImpl |
|
| 1.4;1.4 | ||||
| SequenceManagerSeqHiLoImpl$HiLoEntry |
|
| 1.4;1.4 |
| 1 | package org.apache.ojb.broker.util.sequence; | |
| 2 | ||
| 3 | /* Copyright 2003-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 | ||
| 23 | /** | |
| 24 | * <p> | |
| 25 | * A High/Low database sequence based implementation. | |
| 26 | * See {@link org.apache.ojb.broker.util.sequence.SequenceManagerNextValImpl} | |
| 27 | * for more information. | |
| 28 | * </p> | |
| 29 | * | |
| 30 | * <p> | |
| 31 | * Implementation configuration properties: | |
| 32 | * </p> | |
| 33 | * | |
| 34 | * <table cellspacing="2" cellpadding="2" border="3" frame="box"> | |
| 35 | * <tr> | |
| 36 | * <td><strong>Property Key</strong></td> | |
| 37 | * <td><strong>Property Values</strong></td> | |
| 38 | * </tr> | |
| 39 | * <tr> | |
| 40 | * <td>grabSize</td> | |
| 41 | * <td> | |
| 42 | * Integer entry determines the | |
| 43 | * number of IDs allocated within the | |
| 44 | * H/L sequence manager implementation. | |
| 45 | * Default was '20'. | |
| 46 | * </td> | |
| 47 | * </tr> | |
| 48 | * <tr> | |
| 49 | * <td>autoNaming</td> | |
| 50 | * <td> | |
| 51 | * Default was 'true'. If set 'true' OJB try to build a | |
| 52 | * sequence name automatic if none found in field-descriptor | |
| 53 | * and set this generated name as <code>sequence-name</code> | |
| 54 | * in field-descriptor. If set 'false' OJB throws an exception | |
| 55 | * if none sequence name was found in field-descriptor. | |
| 56 | * </td> | |
| 57 | * </tr> | |
| 58 | * </table> | |
| 59 | * <br/> | |
| 60 | * <p> | |
| 61 | * <b>Limitations:</b> | |
| 62 | * <ul> | |
| 63 | * <li>do not use when other applications use the database based sequence ditto</li> | |
| 64 | * </ul> | |
| 65 | * </p> | |
| 66 | * <br/> | |
| 67 | * <br/> | |
| 68 | * | |
| 69 | * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a> | |
| 70 | * @version $Id: SequenceManagerSeqHiLoImpl.java,v 1.1 2007-08-24 22:17:29 ewestfal Exp $ | |
| 71 | */ | |
| 72 | public class SequenceManagerSeqHiLoImpl extends SequenceManagerNextValImpl | |
| 73 | { | |
| 74 | public static final String PROPERTY_GRAB_SIZE = SequenceManagerHighLowImpl.PROPERTY_GRAB_SIZE; | |
| 75 | private static HashMap hiLoMap = new HashMap(); | |
| 76 | ||
| 77 | protected int grabSize; | |
| 78 | ||
| 79 | public SequenceManagerSeqHiLoImpl(PersistenceBroker broker) | |
| 80 | { | |
| 81 | super(broker); | |
| 82 | grabSize = Integer.parseInt(getConfigurationProperty(PROPERTY_GRAB_SIZE, "20")); | |
| 83 | } | |
| 84 | ||
| 85 | protected long getUniqueLong(FieldDescriptor field) throws SequenceManagerException | |
| 86 | { | |
| 87 | String sequenceName = calculateSequenceName(field); | |
| 88 | // we have to be threadsafe | |
| 89 | synchronized (hiLoMap) | |
| 90 | { | |
| 91 | HiLoEntry entry = (HiLoEntry) hiLoMap.get(sequenceName); | |
| 92 | if (entry == null) | |
| 93 | { | |
| 94 | entry = new HiLoEntry(grabSize, grabSize); | |
| 95 | hiLoMap.put(sequenceName, entry); | |
| 96 | } | |
| 97 | if (entry.needNewSequence()) | |
| 98 | { | |
| 99 | entry.maxVal = grabSize * (super.getUniqueLong(field) + 1); | |
| 100 | entry.counter = 0; | |
| 101 | } | |
| 102 | return entry.nextVal(); | |
| 103 | } | |
| 104 | } | |
| 105 | ||
| 106 | class HiLoEntry | |
| 107 | { | |
| 108 | long maxVal; | |
| 109 | long counter; | |
| 110 | ||
| 111 | public HiLoEntry(long maxVal, long counter) | |
| 112 | { | |
| 113 | this.maxVal = maxVal; | |
| 114 | this.counter = counter; | |
| 115 | } | |
| 116 | ||
| 117 | boolean needNewSequence() | |
| 118 | { | |
| 119 | return counter == grabSize; | |
| 120 | } | |
| 121 | ||
| 122 | long nextVal() | |
| 123 | { | |
| 124 | return maxVal + counter++; | |
| 125 | } | |
| 126 | } | |
| 127 | } |