- Notifications
You must be signed in to change notification settings - Fork 934
/
Copy pathSequenceHiLoGenerator.cs
106 lines (95 loc) · 3.58 KB
/
SequenceHiLoGenerator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
usingSystem;
usingSystem.Collections;
usingSystem.Collections.Generic;
usingSystem.Runtime.CompilerServices;
usingNHibernate.Engine;
usingNHibernate.Type;
usingNHibernate.Util;
namespaceNHibernate.Id
{
/// <summary>
/// An <see cref="IIdentifierGenerator" /> that combines a hi/lo algorithm with an underlying
/// oracle-style sequence that generates hi values.
/// </summary>
/// <remarks>
/// <p>
/// This id generation strategy is specified in the mapping file as
/// <code>
/// <generator class="seqhilo">
/// <param name="sequence">uid_sequence</param>
/// <param name="max_lo">max_lo_value</param>
/// <param name="schema">db_schema</param>
/// </generator>
/// </code>
/// </p>
/// <p>
/// The <c>sequence</c> parameter is required, the <c>max_lo</c> and <c>schema</c> are optional.
/// </p>
/// <p>
/// The user may specify a <c>max_lo</c> value to determine how often new hi values are
/// fetched. If sequences are not avaliable, <c>TableHiLoGenerator</c> might be an
/// alternative.
/// </p>
/// </remarks>
publicpartialclassSequenceHiLoGenerator:SequenceGenerator
{
privatestaticreadonlyINHibernateLoggerlog=NHibernateLogger.For(typeof(SequenceHiLoGenerator));
/// <summary>
/// The name of the maximum low value parameter.
/// </summary>
publicconststringMaxLo="max_lo";
privateintmaxLo;
privateintlo;
privatelonghi;
privateSystem.TypereturnClass;
privatereadonlyAsyncLock_asyncLock=newAsyncLock();
#region IConfigurable Members
/// <summary>
/// Configures the SequenceHiLoGenerator by reading the value of <c>sequence</c>, <c>max_lo</c>,
/// and <c>schema</c> from the <c>parms</c> parameter.
/// </summary>
/// <param name="type">The <see cref="IType"/> the identifier should be.</param>
/// <param name="parms">An <see cref="IDictionary"/> of Param values that are keyed by parameter name.</param>
/// <param name="dialect">The <see cref="Dialect.Dialect"/> to help with Configuration.</param>
publicoverridevoidConfigure(ITypetype,IDictionary<string,string>parms,Dialect.Dialectdialect)
{
base.Configure(type,parms,dialect);
maxLo=PropertiesHelper.GetInt32(MaxLo,parms,9);
lo=maxLo+1;// so we "clock over" on the first invocation
returnClass=type.ReturnedClass;
}
#endregion
#region IIdentifierGenerator Members
/// <summary>
/// Generate an <see cref="Int16"/>, <see cref="Int32"/>, or <see cref="Int64"/>
/// for the identifier by using a database sequence.
/// </summary>
/// <param name="session">The <see cref="ISessionImplementor"/> this id is being generated in.</param>
/// <param name="obj">The entity for which the id is being generated.</param>
/// <returns>The new identifier as a <see cref="Int16"/>, <see cref="Int32"/>, or <see cref="Int64"/>.</returns>
publicoverrideobjectGenerate(ISessionImplementorsession,objectobj)
{
using(_asyncLock.Lock())
{
if(maxLo<1)
{
//keep the behavior consistent even for boundary usages
longval=Convert.ToInt64(base.Generate(session,obj));
if(val==0)
val=Convert.ToInt64(base.Generate(session,obj));
returnIdentifierGeneratorFactory.CreateNumber(val,returnClass);
}
if(lo>maxLo)
{
longhival=Convert.ToInt64(base.Generate(session,obj));
lo=(hival==0)?1:0;
hi=hival*(maxLo+1);
if(log.IsDebugEnabled())
log.Debug("new hi value: {0}",hival);
}
returnIdentifierGeneratorFactory.CreateNumber(hi+lo++,returnClass);
}
}
#endregion
}
}