Skip to content

Commit

Permalink
[REVERT] Commit about adding rawSessionInstant in CDP4TransactionManager
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander van Delft committed Jun 13, 2024
1 parent 11670f3 commit 72e0442
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 115 deletions.
177 changes: 86 additions & 91 deletions CometServer/Helpers/Cdp4TransactionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ namespace CometServer.Helpers
using ServiceUtils = Services.Utils;

/// <summary>
/// The purpose of the <see cref="Cdp4TransactionManager"/> is provide a <see cref="NpgsqlTransaction"/> for
/// read and write operations to the database while configuring the database to properly process
/// any temporal database interactions
/// A wrapper class for the <see cref="NpgsqlTransaction"/> class, allowing temporal database interaction.
/// </summary>
public class Cdp4TransactionManager : ICdp4TransactionManager
{
Expand Down Expand Up @@ -110,11 +108,6 @@ public class Cdp4TransactionManager : ICdp4TransactionManager
/// </summary>
private bool isFullAccessGranted;

/// <summary>
/// Backing field for the cached rawSessionInstant value
/// </summary>
private object rawSessionInstant;

/// <summary>
/// Gets or sets the iteration setup dao.
/// </summary>
Expand All @@ -130,14 +123,6 @@ public class Cdp4TransactionManager : ICdp4TransactionManager
/// </summary>
public IterationSetup IterationSetup { get; private set; }

/// <summary>
/// Initializes a new instance of the <see cref="Cdp4TransactionManager"/>
/// </summary>
public Cdp4TransactionManager()
{
this.rawSessionInstant = null;
}

/// <summary>
/// The setup transaction.
/// </summary>
Expand Down Expand Up @@ -183,49 +168,37 @@ public NpgsqlTransaction SetupTransaction(ref NpgsqlConnection connection, Crede
}

/// <summary>
/// Get the current session time instant value from the database. In case the most current data is to be
/// retrieved this value returns (+infinity) which translates to <see cref="DateTime.MaxValue"/>. In case
/// a request is made related to time-travel the <see cref="DateTime"/> corresponding to the period_end
/// is returned
/// Get the current session time instant.
/// </summary>
/// <param name="transaction">
/// The current transaction to the database.
/// </param>
/// <returns>
/// A <see cref="DateTime"/> that represents the session Instant (either <see cref="DateTime.MaxValue"/> or
/// the <see cref="DateTime"/> corresponding to the period_end
/// The <see cref="DateTime"/>.
/// </returns>
public DateTime GetSessionInstant(NpgsqlTransaction transaction)
{
return (DateTime)this.GetRawSessionInstant(transaction);
}

/// <summary>
/// Get the raw current session time instant value from the database. In case the most current data is to be
/// retrieved this value returns (+infinity) which translates to <see cref="DateTime.MaxValue"/>. In case
/// a request is made related to time-travel the <see cref="DateTime"/> corresponding to the period_end
/// is returned
/// Get the raw current session time instant value from the database.
/// </summary>
/// <param name="transaction">
/// The current transaction to the database.
/// </param>
/// <returns>
/// A <see cref="object"/> that represents the session Instant (either <see cref="DateTime.MaxValue"/> or
/// the <see cref="DateTime"/> corresponding to the period_end
/// The <see cref="object"/>.
/// </returns>
public object GetRawSessionInstant(NpgsqlTransaction transaction)
{
if (this.rawSessionInstant == null)
{
using var command = new NpgsqlCommand(
"SELECT * FROM \"SiteDirectory\".\"get_session_instant\"();",
transaction.Connection,
transaction);

this.rawSessionInstant = command.ExecuteScalar();
using (var command = new NpgsqlCommand(
"SELECT * FROM \"SiteDirectory\".\"get_session_instant\"();",
transaction.Connection,
transaction))
{
return command.ExecuteScalar();
}

return this.rawSessionInstant;
}

/// <summary>
Expand Down Expand Up @@ -283,41 +256,61 @@ public bool IsFullAccessEnabled()
}

/// <summary>
/// Get the timestamp in the form of a <see cref="DateTime"/> that the <see cref="NpgsqlTransaction"/> was
/// created using the <see cref="SetupTransaction"/> method.
/// Get the session timeframe start time.
/// </summary>
/// <param name="transaction">
/// The active <see cref="NpgsqlTransaction"/>
/// The current transaction to the database.
/// </param>
/// <returns>
/// The <see cref="DateTime"/>.
/// </returns>
public DateTime GetTransactionTime(NpgsqlTransaction transaction)
public DateTime GetSessionTimeFrameStart(NpgsqlTransaction transaction)
{
using var command = new NpgsqlCommand(
$"SELECT * FROM \"SiteDirectory\".\"{"get_transaction_time"}\"();",
using (var command = new NpgsqlCommand(
string.Format("SELECT * FROM \"SiteDirectory\".\"{0}\"();", "get_session_timeframe_start"),
transaction.Connection,
transaction);
transaction))
{
return (DateTime)command.ExecuteScalar();
}
}

return (DateTime)command.ExecuteScalar();
/// <summary>
/// Get the current transaction time.
/// </summary>
/// <param name="transaction">
/// The current transaction to the database.
/// </param>
/// <returns>
/// The <see cref="DateTime"/>.
/// </returns>
public DateTime GetTransactionTime(NpgsqlTransaction transaction)
{
using (var command = new NpgsqlCommand(
string.Format("SELECT * FROM \"SiteDirectory\".\"{0}\"();", "get_transaction_time"),
transaction.Connection,
transaction))
{
return (DateTime)command.ExecuteScalar();
}
}

/// <summary>
/// Sets the default temporal context (-infinity, +infinity)
/// The set default context.
/// </summary>
/// <param name="transaction">
/// The active <see cref="NpgsqlTransaction"/>
/// The current transaction to the database.
/// </param>
public void SetDefaultContext(NpgsqlTransaction transaction)
{
var sqlBuilder = new StringBuilder();

sqlBuilder.AppendFormat(
"UPDATE {0} SET ({1}, {2}) = ('-infinity', 'infinity');", TransactionInfoTable, PeriodStartColumn, PeriodEndColumn);

using var command = new NpgsqlCommand(sqlBuilder.ToString(), transaction.Connection, transaction);

command.ExecuteNonQuery();

using (var command = new NpgsqlCommand(sqlBuilder.ToString(), transaction.Connection, transaction))
{
command.ExecuteNonQuery();
}
}

/// <summary>
Expand All @@ -333,11 +326,12 @@ public void SetAuditLoggingState(NpgsqlTransaction transaction, bool enabled)
{
var sql = $"UPDATE {TransactionInfoTable} SET {TransactionAuditEnabled} = :{TransactionAuditEnabled};";

using var command = new NpgsqlCommand(sql, transaction.Connection, transaction);

command.Parameters.Add($"{TransactionAuditEnabled}", NpgsqlDbType.Boolean).Value = enabled;
using (var command = new NpgsqlCommand(sql, transaction.Connection, transaction))
{
command.Parameters.Add($"{TransactionAuditEnabled}", NpgsqlDbType.Boolean).Value = enabled;

command.ExecuteNonQuery();
command.ExecuteNonQuery();
}
}

/// <summary>
Expand Down Expand Up @@ -388,8 +382,6 @@ public void SetIterationContext(NpgsqlTransaction transaction, string partition,
/// </returns>
private NpgsqlTransaction GetTransaction(ref NpgsqlConnection connection, Credentials credentials)
{
this.rawSessionInstant = null;

var transaction = this.SetupNewTransaction(ref connection);
CreateTransactionInfoTable(transaction);
CreateDefaultTransactionInfoEntry(transaction, credentials);
Expand Down Expand Up @@ -425,11 +417,12 @@ private static void ApplyIterationContext(NpgsqlTransaction transaction, string
sqlBuilder.AppendFormat("FROM \"{0}\".\"IterationRevisionLog\" iteration_log LEFT JOIN \"{0}\".\"RevisionRegistry\" revision_from ON iteration_log.\"FromRevision\" = revision_from.\"Revision\" LEFT JOIN \"{0}\".\"RevisionRegistry\" revision_to ON iteration_log.\"ToRevision\" = revision_to.\"Revision\") IterationLogRevision", partition);
sqlBuilder.Append(" WHERE \"IterationIid\" = :iterationIid);");

using var command = new NpgsqlCommand(sqlBuilder.ToString(), transaction.Connection, transaction);

command.Parameters.Add("iterationIid", NpgsqlDbType.Uuid).Value = iterationSetup.IterationIid;
using (var command = new NpgsqlCommand(sqlBuilder.ToString(), transaction.Connection, transaction))
{
command.Parameters.Add("iterationIid", NpgsqlDbType.Uuid).Value = iterationSetup.IterationIid;

command.ExecuteNonQuery();
command.ExecuteNonQuery();
}
}

/// <summary>
Expand Down Expand Up @@ -479,56 +472,58 @@ private NpgsqlTransaction SetupNewTransaction(ref NpgsqlConnection connection)
}

/// <summary>
/// Create a transaction info table with transaction scope lifetime.
/// The create default transaction info entry.
/// </summary>
/// <param name="transaction">
/// The current transaction to the database.
/// </param>
private static void CreateTransactionInfoTable(NpgsqlTransaction transaction)
/// <param name="credentials">
/// The credentials.
/// </param>
private static void CreateDefaultTransactionInfoEntry(NpgsqlTransaction transaction, Credentials credentials)
{
// setup transaction_info table that is valid only for this transaction
// insert actor from the request credentials otherwise use default (null) user
var sqlBuilder = new StringBuilder();
var isCredentialSet = credentials != null;

sqlBuilder.AppendFormat("CREATE TEMPORARY TABLE {0} (", TransactionInfoTable);
sqlBuilder.AppendFormat("{0} uuid, ", UserIdColumn);
sqlBuilder.AppendFormat("{0} timestamp NOT NULL DEFAULT '-infinity', ", PeriodStartColumn);
sqlBuilder.AppendFormat("{0} timestamp NOT NULL DEFAULT 'infinity', ", PeriodEndColumn);
sqlBuilder.AppendFormat("{0} timestamp NOT NULL DEFAULT 'infinity', ", InstantColumn);
sqlBuilder.AppendFormat("{0} timestamp NOT NULL DEFAULT statement_timestamp(), ", TransactionTimeColumn);
sqlBuilder.AppendFormat("{0} boolean NOT NULL DEFAULT 'true'", TransactionAuditEnabled);
sqlBuilder.Append(") ON COMMIT DROP;");
sqlBuilder.AppendFormat("INSERT INTO {0} ({1}, {2})", TransactionInfoTable, UserIdColumn, TransactionTimeColumn);
sqlBuilder.AppendFormat(" VALUES({0}, statement_timestamp());", isCredentialSet ? $":{UserIdColumn}" : "null");

using var command = new NpgsqlCommand(sqlBuilder.ToString(), transaction.Connection, transaction);
using (var command = new NpgsqlCommand(sqlBuilder.ToString(), transaction.Connection, transaction))
{
if (isCredentialSet)
{
command.Parameters.Add(UserIdColumn, NpgsqlDbType.Uuid).Value = credentials.Person.Iid;
}

command.ExecuteNonQuery();
command.ExecuteNonQuery();
}
}

/// <summary>
/// The create default transaction info entry.
/// Create a transaction info table with transaction scope lifetime.
/// </summary>
/// <param name="transaction">
/// The current transaction to the database.
/// </param>
/// <param name="credentials">
/// The credentials.
/// </param>
private static void CreateDefaultTransactionInfoEntry(NpgsqlTransaction transaction, Credentials credentials)
private static void CreateTransactionInfoTable(NpgsqlTransaction transaction)
{
// insert actor from the request credentials otherwise use default (null) user
// setup transaction_info table that is valid only for this transaction
var sqlBuilder = new StringBuilder();
var isCredentialSet = credentials != null;

sqlBuilder.AppendFormat("INSERT INTO {0} ({1}, {2})", TransactionInfoTable, UserIdColumn, TransactionTimeColumn);
sqlBuilder.AppendFormat(" VALUES({0}, statement_timestamp());", isCredentialSet ? $":{UserIdColumn}" : "null");

using var command = new NpgsqlCommand(sqlBuilder.ToString(), transaction.Connection, transaction);
sqlBuilder.AppendFormat("CREATE TEMPORARY TABLE {0} (", TransactionInfoTable);
sqlBuilder.AppendFormat("{0} uuid, ", UserIdColumn);
sqlBuilder.AppendFormat("{0} timestamp NOT NULL DEFAULT '-infinity', ", PeriodStartColumn);
sqlBuilder.AppendFormat("{0} timestamp NOT NULL DEFAULT 'infinity', ", PeriodEndColumn);
sqlBuilder.AppendFormat("{0} timestamp NOT NULL DEFAULT 'infinity', ", InstantColumn);
sqlBuilder.AppendFormat("{0} timestamp NOT NULL DEFAULT statement_timestamp(), ", TransactionTimeColumn);
sqlBuilder.AppendFormat("{0} boolean NOT NULL DEFAULT 'true'", TransactionAuditEnabled);
sqlBuilder.Append(") ON COMMIT DROP;");

if (isCredentialSet)
using (var command = new NpgsqlCommand(sqlBuilder.ToString(), transaction.Connection, transaction))
{
command.Parameters.Add(UserIdColumn, NpgsqlDbType.Uuid).Value = credentials.Person.Iid;
command.ExecuteNonQuery();
}

command.ExecuteNonQuery();
}
}
}
Loading

0 comments on commit 72e0442

Please sign in to comment.