forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using NHibernate.Engine;
using NHibernate.Type;
using NHibernate.Util;
namespace NHibernate.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>
public partial class SequenceHiLoGenerator : SequenceGenerator
{
private static readonly INHibernateLogger log = NHibernateLogger.For(typeof(SequenceHiLoGenerator));
/// <summary>
/// The name of the maximum low value parameter.
/// </summary>
public const string MaxLo = "max_lo";
private int maxLo;
private int lo;
private long hi;
private System.Type returnClass;
private readonly AsyncLock _asyncLock = new AsyncLock();
#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>
public override void Configure(IType type, IDictionary<string, string> parms, Dialect.Dialect dialect)
{
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>
public override object Generate(ISessionImplementor session, object obj)
{
using (_asyncLock.Lock())
{
if (maxLo < 1)
{
//keep the behavior consistent even for boundary usages
long val = Convert.ToInt64(base.Generate(session, obj));
if (val == 0)
val = Convert.ToInt64(base.Generate(session, obj));
return IdentifierGeneratorFactory.CreateNumber(val, returnClass);
}
if (lo > maxLo)
{
long hival = 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);
}
return IdentifierGeneratorFactory.CreateNumber(hi + lo++, returnClass);
}
}
#endregion
}
}