-
Notifications
You must be signed in to change notification settings - Fork 934
/
Copy pathTableHiLoGenerator.cs
110 lines (99 loc) · 3.84 KB
/
TableHiLoGenerator.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
107
108
109
110
using System;
using System.Collections;
using System.Runtime.CompilerServices;
using NHibernate.Engine;
using NHibernate.Type;
using NHibernate.Util;
using System.Collections.Generic;
namespace NHibernate.Id
{
/// <summary>
/// An <see cref="IIdentifierGenerator" /> that returns an <c>Int64</c>, constructed using
/// a hi/lo algorithm.
/// </summary>
/// <remarks>
/// <p>
/// This id generation strategy is specified in the mapping file as
/// <code>
/// <generator class="hilo">
/// <param name="table">table</param>
/// <param name="column">id_column</param>
/// <param name="max_lo">max_lo_value</param>
/// <param name="schema">db_schema</param>
/// <param name="catalog">db_catalog</param>
/// <param name="where">arbitrary additional where clause</param>
/// </generator>
/// </code>
/// </p>
/// <p>
/// The <c>table</c> and <c>column</c> parameters are required, the <c>max_lo</c>,
/// <c>schema</c>, <c>catalog</c> and <c>where</c> are optional.
/// </p>
/// <p>
/// The hi value MUST be fecthed in a separate transaction to the <c>ISession</c>
/// transaction so the generator must be able to obtain a new connection and
/// commit it. Hence this implementation may not be used when the user is supplying
/// connections. In that case a <see cref="SequenceHiLoGenerator"/> would be a
/// better choice (where supported).
/// </p>
/// </remarks>
public partial class TableHiLoGenerator : TableGenerator
{
private static readonly INHibernateLogger log = NHibernateLogger.For(typeof(TableHiLoGenerator));
/// <summary>
/// The name of the max lo parameter.
/// </summary>
public const string MaxLo = "max_lo";
private long hi;
private long lo;
private long maxLo;
private System.Type returnClass;
private readonly AsyncLock _asyncLock = new AsyncLock();
#region IConfigurable Members
/// <summary>
/// Configures the TableHiLoGenerator by reading the value of <c>table</c>,
/// <c>column</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.GetInt64(MaxLo, parms, Int16.MaxValue);
lo = maxLo + 1; // so we "clock over" on the first invocation
returnClass = type.ReturnedClass;
}
#endregion
#region IIdentifierGenerator Members
/// <summary>
/// Generate a <see cref="Int64"/> for the identifier by selecting and updating a value in a table.
/// </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="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);
log.Debug("New high value: {0}", hival);
}
return IdentifierGeneratorFactory.CreateNumber(hi + lo++, returnClass);
}
}
#endregion
}
}