1 package org.apache.ojb.broker.util.sequence; 2 3 17 18 import org.apache.ojb.broker.PersistenceBroker; 19 import org.apache.ojb.broker.metadata.FieldDescriptor; 20 21 import java.util.HashMap ; 22 23 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 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 } 128 | Popular Tags |