Skip to content

Commit

Permalink
Expose GetApiVersion() on database and transaction context, and use t…
Browse files Browse the repository at this point in the history
…his for all api version checks
  • Loading branch information
KrzysFR committed Apr 16, 2022
1 parent ed0fd4e commit 7dd4a68
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 16 deletions.
2 changes: 1 addition & 1 deletion FoundationDB.Client/Core/IFdbDatabaseHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public interface IFdbDatabaseHandler : IDisposable

IFdbTransactionHandler CreateTransaction(FdbOperationContext context);

int GetSelectedApiVersion();
int GetApiVersion();

int GetMaxApiVersion();

Expand Down
3 changes: 3 additions & 0 deletions FoundationDB.Client/FdbDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ public static FdbDatabase Create(IFdbDatabaseHandler handler, FdbDirectoryLayer
/// <summary>Internal handler</summary>
internal IFdbDatabaseHandler Handler => m_handler;

/// <summary>Return the currently enforced API version for this database instance.</summary>
public int GetApiVersion() => m_handler.GetApiVersion();

#endregion

#region Transaction Management...
Expand Down
5 changes: 5 additions & 0 deletions FoundationDB.Client/FdbOperationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1316,8 +1316,13 @@ internal void ReleaseTransaction(FdbTransaction trans)
Interlocked.CompareExchange(ref this.Transaction, null, trans);
}

/// <summary>Return the underlying native handler for this transaction</summary>
/// <remarks>This is only intended for testing or troubleshooting purpose!</remarks>
public IFdbTransactionHandler GetTransactionHandler() => this.Transaction?.Handler ?? throw new InvalidOperationException("Transaction has already been disposed");

/// <summary>Return the currently enforced API version for the database attached to this transaction.</summary>
public int GetApiVersion() => this.Database.GetApiVersion();

public void Dispose()
{
this.Abort = true;
Expand Down
4 changes: 1 addition & 3 deletions FoundationDB.Client/FdbTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -861,9 +861,7 @@ static void ExecuteLogged(FdbTransaction self, ReadOnlySpan<byte> key, ReadOnlyS
/// <exception cref="FdbException">An error with code <see cref="FdbError.InvalidMutationType"/> if the type of mutation is not supported by this API level.</exception>
private void EnsureMutationTypeIsSupported(FdbMutationType mutation)
{
var dbHandler = this.Database.Handler;
int selectedApiVersion = dbHandler.GetSelectedApiVersion();

int selectedApiVersion = this.Database.GetApiVersion();
if (selectedApiVersion < 200)
{ // mutations were not available at this time

Expand Down
22 changes: 11 additions & 11 deletions FoundationDB.Client/FdbTransactionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static class FdbTransactionExtensions
public static TTransaction WithReadAccessToSystemKeys<TTransaction>(this TTransaction trans)
where TTransaction : IFdbReadOnlyTransaction
{
trans.SetOption(Fdb.ApiVersion >= 300 ? FdbTransactionOption.ReadSystemKeys : FdbTransactionOption.AccessSystemKeys);
trans.SetOption(trans.Context.GetApiVersion() >= 300 ? FdbTransactionOption.ReadSystemKeys : FdbTransactionOption.AccessSystemKeys);
//TODO: cache this into a local variable ?
return trans;
}
Expand Down Expand Up @@ -1030,9 +1030,9 @@ private static int GetVersionStampOffset(ReadOnlySpan<byte> buffer, ReadOnlySpan
}

[Pure, MethodImpl(MethodImplOptions.NoInlining)]
private static Exception FailVersionStampNotSupported()
private static Exception FailVersionStampNotSupported(int apiVersion)
{
return new NotSupportedException($"VersionStamps are not supported at API version {Fdb.ApiVersion}. You need to select at least API Version 400 or above.");
return new NotSupportedException($"VersionStamps are not supported at API version {apiVersion}. You need to select at least API Version 400 or above.");
}

/// <summary>Set the <paramref name="value"/> of the <paramref name="key"/> in the database, with the <see cref="VersionStamp"/> replaced by the resolved version at commit time.</summary>
Expand All @@ -1059,10 +1059,10 @@ public static void SetVersionStampedKey(this IFdbTransaction trans, ReadOnlySpan
trans.CreateVersionStamp().WriteTo(token);
var offset = GetVersionStampOffset(key, token, nameof(key));

int apiVer = Fdb.ApiVersion;
int apiVer = trans.Context.Database.GetApiVersion();
if (apiVer < 400)
{ // introduced in 400
throw FailVersionStampNotSupported();
throw FailVersionStampNotSupported(apiVer);
}

if (apiVer < 520)
Expand Down Expand Up @@ -1108,10 +1108,10 @@ public static void SetVersionStampedKey(this IFdbTransaction trans, ReadOnlySpan
Contract.Positive(stampOffset);
if (stampOffset > key.Length - 10) throw new ArgumentException("The VersionStamp overflows past the end of the key.", nameof(stampOffset));

int apiVer = Fdb.ApiVersion;
int apiVer = trans.Context.GetApiVersion();
if (apiVer < 400)
{ // introduced in 400
throw FailVersionStampNotSupported();
throw FailVersionStampNotSupported(apiVer);
}

if (apiVer < 520)
Expand Down Expand Up @@ -1154,10 +1154,10 @@ public static void SetVersionStampedValue(this IFdbTransaction trans, ReadOnlySp
Contract.NotNull(trans);
if (value.Length < 10) throw new ArgumentException("The value must be at least 10 bytes long.", nameof(value));

int apiVer = Fdb.ApiVersion;
int apiVer = trans.Context.GetApiVersion();
if (apiVer < 400)
{ // introduced in 400
throw FailVersionStampNotSupported();
throw FailVersionStampNotSupported(apiVer);
}

if (apiVer < 520)
Expand Down Expand Up @@ -1207,10 +1207,10 @@ public static void SetVersionStampedValue(this IFdbTransaction trans, ReadOnlySp
Contract.Positive(stampOffset);
if (stampOffset > key.Length - 10) throw new ArgumentException("The VersionStamp overflows past the end of the value.", nameof(stampOffset));

int apiVer = Fdb.ApiVersion;
int apiVer = trans.Context.GetApiVersion();
if (apiVer < 400)
{ // introduced in 400
throw FailVersionStampNotSupported();
throw FailVersionStampNotSupported(apiVer);
}

if (apiVer < 520)
Expand Down
3 changes: 3 additions & 0 deletions FoundationDB.Client/IFdbDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ public interface IFdbDatabase : IFdbRetryable, IDisposable
/// }</example>
ValueTask<IFdbTransaction> BeginTransactionAsync(FdbTransactionMode mode, CancellationToken ct, FdbOperationContext? context = null);

/// <summary>Return the currently enforced API version for this database instance.</summary>
int GetApiVersion();

}

}
2 changes: 1 addition & 1 deletion FoundationDB.Client/Native/FdbNativeDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ private static async ValueTask<IFdbDatabaseHandler> CreateDatabaseLegacyAsync(st

public bool IsClosed => m_handle.IsClosed;

public int GetSelectedApiVersion() => Fdb.ApiVersion;
public int GetApiVersion() => Fdb.ApiVersion;

public int GetMaxApiVersion() => FdbNative.GetMaxApiVersion();

Expand Down

0 comments on commit 7dd4a68

Please sign in to comment.