1 | public class SequenceHiLoGenerator extends SequenceGenerator {↵ | | 1 | public class TableHiLoGenerator extends TableGenerator {↵
|
|
| | | 2 | /**↵
|
| | | 3 | * The max_lo parameter↵
|
| | | 4 | */↵
|
2 | public static final String MAX_LO = "max_lo";↵ | | 5 | public static final String MAX_LO = "max_lo";↵
|
|
3 | private static final Logger log = LoggerFactory.getLogger(SequenceHiLoGenerator.class);↵ | | 6 | private ↵
|
| | | 7 | long hi;↵
|
4 | private int maxLo;↵ | | 8 | private int lo;↵
|
5 | private int lo;↵ | | 9 | private int maxLo;↵
|
6 | private long hi;↵ | | 10 | private ↵
|
7 | private Class returnC↵ | | 11 | Class returnClass;↵
|
|
8 | lass;↵ | | 12 | private static final Logger log = LoggerFactory.getLogger(TableHiLoGenerator.class);↵
|
|
9 | public void configure(Type type, Properties params, Dialect d) throws MappingException {↵ | | 13 | public void configure(Type type, Properties params, Dialect d) {↵
|
10 | super.configure(type, params, d);↵ | | 14 | super.configure(type, params, d);↵
|
11 | maxLo = PropertiesHelper.getInt(MAX_LO, params, 9);↵ | | 15 | maxLo = PropertiesHelper.getInt(MAX_LO, params, Short.MAX_VALUE);↵
|
12 | lo = maxLo + 1; // so we "clock over" on the first invocation↵ | | 16 | lo = maxLo + 1; // so we "clock over" on the first invocation↵
|
13 | returnClass = type.getReturnedClass();↵ | | 17 | returnClass = type.getReturnedClass();↵
|
14 | }↵ | | 18 | }↵
|
|
15 | public synchronized Serializable generate(SessionImplementor session, Object obj) ↵ | | 19 | public synchronized Serializable generate(SessionImplementor session, Object obj) ↵
|
16 | throws HibernateException {↵ | | 20 | throws HibernateException {↵
|
17 | if (maxLo < 1) {↵ | | 21 | if (maxLo < 1) {↵
|
18 | //keep the behavior consistent even for boundary usages↵ | | 22 | //keep the behavior consistent even for boundary usages↵
|
19 | long val = ( (Number) super.generate(session, obj) ).longValue();↵ | | 23 | long val = ( (Number) super.generate(session, obj) ).longValue();↵
|
20 | if (val == 0) val = ( (Number) super.generate(session, obj) ).longValue();↵ | | 24 | if (val == 0) val = ( (Number) super.generate(session, obj) ).longValue();↵
|
21 | return IdentifierGeneratorFactory.createNumber( val, returnClass );↵ | | 25 | return IdentifierGeneratorFactory.createNumber( val, returnClass );↵
|
22 | }↵ | | 26 | }↵
|
23 | if ( lo>maxLo ) {↵ | | 27 | if (lo>maxLo) {↵
|
24 | long hival = ( (Number) super.generate(session, obj) ).longValue();↵ | | 28 | long hival = ( (Number) super.generate(session, obj) ).longValue();↵
|
25 | lo = (hival == 0) ? 1 : 0;↵ | | 29 | lo = (hival == 0) ? 1 : 0;↵
|
26 | hi = hival * ( maxLo+1 );↵ | | 30 | hi = hival * (maxLo+1);↵
|
27 | if ( log.isDebugEnabled() )↵ | | 31 | ↵
|
28 | log.debug("new hi value: " + hival);↵ | | 32 | log.debug("new hi value: " + hival);↵
|
29 | }↵ | | 33 | }↵
|
|
30 | return IdentifierGeneratorFactory.createNumber( hi + lo++, returnClass ) | | 34 | return IdentifierGeneratorFactory.createNumber( hi + lo++, returnClass )
|