<commit_before>using System.Collections.Generic;

using NHibernate.Engine;
using NHibernate.Mapping;
using NHibernate.Type;
using NHibernate.Util;

namespace NHibernate.Id.Enhanced
{
	/// <summary>
	/// Generates identifier values based on an sequence-style database structure.
	/// Variations range from actually using a sequence to using a table to mimic
	/// a sequence. These variations are encapsulated by the <see cref="IDatabaseStructure"/>
	/// interface internally.
	/// </summary>
	/// <remarks>
	/// General configuration parameters:
	/// <table>
	///   <tr>
	///     <td><b>NAME</b></td>
	///     <td><b>DEFAULT</b></td>
	///     <td><b>DESCRIPTION</b></td>
	///   </tr>
	///   <tr>
	///     <td><see cref="SequenceParam"/></td>
	///     <td><see cref="DefaultSequenceName"/></td>
	///     <td>The name of the sequence/table to use to store/retrieve values</td>
	///   </tr>
	///   <tr>
	///     <td><see cref="InitialParam"/></td>
	///     <td><see cref="DefaultInitialValue"/></td>
	///     <td>The initial value to be stored for the given segment; the effect in terms of storage varies based on <see cref="Optimizer"/> and <see cref="DatabaseStructure"/></td>
	///   </tr>
	///   <tr>
	///     <td><see cref="IncrementParam"/></td>
	///     <td><see cref="DefaultIncrementSize"/></td>
	///     <td>The increment size for the underlying segment; the effect in terms of storage varies based on <see cref="Optimizer"/> and <see cref="DatabaseStructure"/></td>
	///   </tr>
	///   <tr>
	///     <td><see cref="OptimizerParam"/></td>
	///     <td><i>depends on defined increment size</i></td>
	///     <td>Allows explicit definition of which optimization strategy to use</td>
	///   </tr>
	///     <td><see cref="ForceTableParam"/></td>
	///     <td><b><i>false<i/></b></td>
	///     <td>Allows explicit definition of which optimization strategy to use</td>
	///   </tr>
	/// </table>
	/// <p/>
	/// Configuration parameters used specifically when the underlying structure is a table:
	/// <table>
	///   <tr>
	///     <td><b>NAME</b></td>
	///     <td><b>DEFAULT</b></td>
	///     <td><b>DESCRIPTION</b></td>
	///   </tr>
	///   <tr>
	///     <td><see cref="ValueColumnParam"/></td>
	///     <td><see cref="DefaultValueColumnName"/></td>
	///     <td>The name of column which holds the sequence value for the given segment</td>
	///   </tr>
	/// </table>
	/// </remarks>
	public class SequenceStyleGenerator : IPersistentIdentifierGenerator, IConfigurable
	{
		private static readonly IInternalLogger Log = LoggerProvider.LoggerFor(typeof(SequenceStyleGenerator));

		public const string DefaultSequenceName = "hibernate_sequence";
		public const int DefaultInitialValue = 1;
		public const int DefaultIncrementSize = 1;

		public const string SequenceParam = "sequence_name";
		public const string InitialParam = "initial_value";
		public const string IncrementParam = "increment_size";
		public const string OptimizerParam = "optimizer";
		public const string ForceTableParam = "force_table_use";
		public const string ValueColumnParam = "value_column";
		public const string DefaultValueColumnName = "next_val";

		public IDatabaseStructure DatabaseStructure { get; private set; }
		public IOptimizer Optimizer { get; private set; }
		public IType IdentifierType { get; private set; }


		#region Implementation of IConfigurable

		public virtual void Configure(IType type, IDictionary<string, string> parms, Dialect.Dialect dialect)
		{
			IdentifierType = type;

			bool forceTableUse = PropertiesHelper.GetBoolean(ForceTableParam, parms, false);

			string sequenceName = DetermineSequenceName(parms, dialect);

			int initialValue = DetermineInitialValue(parms);
			int incrementSize = DetermineIncrementSize(parms);

			string optimizationStrategy = DetermineOptimizationStrategy(parms, incrementSize);
			incrementSize = DetermineAdjustedIncrementSize(optimizationStrategy, incrementSize);

			if (dialect.SupportsSequences && !forceTableUse)
			{
				if (OptimizerFactory.Pool.Equals(optimizationStrategy) && !dialect.SupportsPooledSequences)
				{
					forceTableUse = true;
					Log.Info("Forcing table use for sequence-style generator due to pooled optimizer selection where db does not support pooled sequences.");
				}
			}

			DatabaseStructure = BuildDatabaseStructure(type, parms, dialect, forceTableUse, sequenceName, initialValue, incrementSize);

			Optimizer = OptimizerFactory.BuildOptimizer(
				optimizationStrategy,
				IdentifierType.ReturnedClass,
				incrementSize,
				PropertiesHelper.GetInt32(InitialParam, parms, -1)); // Use -1 as default initial value here to signal that it's not set.

			DatabaseStructure.Prepare(Optimizer);
		}


		/// <summary><commit_msg>SequenceStyleGenerator.cs: Add note about possible bug in the Hibernate original.<commit_after>using System.Collections.Generic;

using NHibernate.Engine;
using NHibernate.Mapping;
using NHibernate.Type;
using NHibernate.Util;

namespace NHibernate.Id.Enhanced
{
	/// <summary>
	/// Generates identifier values based on an sequence-style database structure.
	/// Variations range from actually using a sequence to using a table to mimic
	/// a sequence. These variations are encapsulated by the <see cref="IDatabaseStructure"/>
	/// interface internally.
	/// </summary>
	/// <remarks>
	/// General configuration parameters:
	/// <table>
	///   <tr>
	///     <td><b>NAME</b></td>
	///     <td><b>DEFAULT</b></td>
	///     <td><b>DESCRIPTION</b></td>
	///   </tr>
	///   <tr>
	///     <td><see cref="SequenceParam"/></td>
	///     <td><see cref="DefaultSequenceName"/></td>
	///     <td>The name of the sequence/table to use to store/retrieve values</td>
	///   </tr>
	///   <tr>
	///     <td><see cref="InitialParam"/></td>
	///     <td><see cref="DefaultInitialValue"/></td>
	///     <td>The initial value to be stored for the given segment; the effect in terms of storage varies based on <see cref="Optimizer"/> and <see cref="DatabaseStructure"/></td>
	///   </tr>
	///   <tr>
	///     <td><see cref="IncrementParam"/></td>
	///     <td><see cref="DefaultIncrementSize"/></td>
	///     <td>The increment size for the underlying segment; the effect in terms of storage varies based on <see cref="Optimizer"/> and <see cref="DatabaseStructure"/></td>
	///   </tr>
	///   <tr>
	///     <td><see cref="OptimizerParam"/></td>
	///     <td><i>depends on defined increment size</i></td>
	///     <td>Allows explicit definition of which optimization strategy to use</td>
	///   </tr>
	///     <td><see cref="ForceTableParam"/></td>
	///     <td><b><i>false<i/></b></td>
	///     <td>Allows explicit definition of which optimization strategy to use</td>
	///   </tr>
	/// </table>
	/// <p/>
	/// Configuration parameters used specifically when the underlying structure is a table:
	/// <table>
	///   <tr>
	///     <td><b>NAME</b></td>
	///     <td><b>DEFAULT</b></td>
	///     <td><b>DESCRIPTION</b></td>
	///   </tr>
	///   <tr>
	///     <td><see cref="ValueColumnParam"/></td>
	///     <td><see cref="DefaultValueColumnName"/></td>
	///     <td>The name of column which holds the sequence value for the given segment</td>
	///   </tr>
	/// </table>
	/// </remarks>
	public class SequenceStyleGenerator : IPersistentIdentifierGenerator, IConfigurable
	{
		private static readonly IInternalLogger Log = LoggerProvider.LoggerFor(typeof(SequenceStyleGenerator));

		public const string DefaultSequenceName = "hibernate_sequence";
		public const int DefaultInitialValue = 1;
		public const int DefaultIncrementSize = 1;

		public const string SequenceParam = "sequence_name";
		public const string InitialParam = "initial_value";
		public const string IncrementParam = "increment_size";
		public const string OptimizerParam = "optimizer";
		public const string ForceTableParam = "force_table_use";
		public const string ValueColumnParam = "value_column";
		public const string DefaultValueColumnName = "next_val";

		public IDatabaseStructure DatabaseStructure { get; private set; }
		public IOptimizer Optimizer { get; private set; }
		public IType IdentifierType { get; private set; }


		#region Implementation of IConfigurable

		public virtual void Configure(IType type, IDictionary<string, string> parms, Dialect.Dialect dialect)
		{
			IdentifierType = type;

			bool forceTableUse = PropertiesHelper.GetBoolean(ForceTableParam, parms, false);

			string sequenceName = DetermineSequenceName(parms, dialect);

			int initialValue = DetermineInitialValue(parms);
			int incrementSize = DetermineIncrementSize(parms);

			string optimizationStrategy = DetermineOptimizationStrategy(parms, incrementSize);
			incrementSize = DetermineAdjustedIncrementSize(optimizationStrategy, incrementSize);

			if (dialect.SupportsSequences && !forceTableUse)
			{
				// TODO: (Added during porting) The following corresponds to what Hibernate does, but
				// TODO: if seems to me that we need to force table use also for the PooledLo optimizer.
				// TODO: Reported as HHH-6855.
				if (OptimizerFactory.Pool.Equals(optimizationStrategy) && !dialect.SupportsPooledSequences)
				{
					forceTableUse = true;
					Log.Info("Forcing table use for sequence-style generator due to pooled optimizer selection where db does not support pooled sequences.");
				}
			}

			DatabaseStructure = BuildDatabaseStructure(type, parms, dialect, forceTableUse, sequenceName, initialValue, incrementSize);

			Optimizer = OptimizerFactory.BuildOptimizer(
				optimizationStrategy,
				IdentifierType.ReturnedClass,
				incrementSize,
				PropertiesHelper.GetInt32(InitialParam, parms, -1)); // Use -1 as default initial value here to signal that it's not set.

			DatabaseStructure.Prepare(Optimizer);
		}


		/// <summary>
{'fraction_non_alphanumeric': 0.14100185528756956, 'fraction_numerical': 0.0016491445062873633, 'mean_word_length': 3.502552204176334, 'pattern_counts': {'":': 0, '<': 217, '<?xml version=': 0, '>': 217, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2398068', 'n_tokens_mistral': 3074, 'n_tokens_neox': 2822, 'n_words': 793}