Skip to content

Commit d7f3726

Browse files
committed
Cleanup code
1 parent 3a392b0 commit d7f3726

29 files changed

+175
-213
lines changed

FoundationDB.Client/Fdb.Errors.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ internal static Exception KeyCannotBeNull(string paramName = "key")
5151

5252
internal static Exception KeyIsTooBig(Slice key, string paramName = "key")
5353
{
54-
return new ArgumentException(String.Format("Key is too big ({0} > {1}).", key.Count, Fdb.MaxKeySize), paramName);
54+
return new ArgumentException($"Key is too big ({key.Count} > {Fdb.MaxKeySize}).", paramName);
5555
}
5656

5757
internal static Exception ValueCannotBeNull(Slice value, string paramName = "value")
@@ -61,7 +61,7 @@ internal static Exception ValueCannotBeNull(Slice value, string paramName = "val
6161

6262
internal static Exception ValueIsTooBig(Slice value, string paramName = "value")
6363
{
64-
return new ArgumentException(String.Format("Value is too big ({0} > {1}).", value.Count, Fdb.MaxValueSize), paramName);
64+
return new ArgumentException($"Value is too big ({value.Count} > {Fdb.MaxValueSize}).", paramName);
6565
}
6666

6767
internal static Exception InvalidKeyOutsideDatabaseNamespace(IFdbDatabase db, Slice key)
@@ -70,9 +70,9 @@ internal static Exception InvalidKeyOutsideDatabaseNamespace(IFdbDatabase db, Sl
7070
return new FdbException(
7171
FdbError.KeyOutsideLegalRange,
7272
#if DEBUG
73-
String.Format("An attempt was made to use a key '{2}' that is outside of the global namespace {0} of database '{1}'", db.GlobalSpace, db.Name, FdbKey.Dump(key))
73+
$"An attempt was made to use a key '{FdbKey.Dump(key)}' that is outside of the global namespace {db.GlobalSpace} of database '{db.Name}'"
7474
#else
75-
String.Format("An attempt was made to use a key that is outside of the global namespace {0} of database '{1}'", db.GlobalSpace, db.Name)
75+
$"An attempt was made to use a key that is outside of the global namespace {db.GlobalSpace} of database '{db.Name}'"
7676
#endif
7777
);
7878
}
@@ -82,7 +82,7 @@ internal static Exception InvalidKeyOutsideDatabaseNamespace(IFdbDatabase db, Sl
8282
internal static Exception FailedToRegisterTransactionOnDatabase(IFdbTransaction transaction, FdbDatabase db)
8383
{
8484
Contract.Requires(transaction != null && db != null);
85-
return new InvalidOperationException(String.Format("Failed to register transaction #{0} with this instance of database {1}", transaction.Id, db.Name));
85+
return new InvalidOperationException($"Failed to register transaction #{transaction.Id} with this instance of database {db.Name}");
8686
}
8787

8888
internal static Exception CannotIncrementKey()

FoundationDB.Client/Fdb.Options.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public static void EnableNativeLibraryPreloading()
7272
/// </remarks>
7373
public static void SetNativeLibPath(string path)
7474
{
75-
if (path == null) throw new ArgumentNullException("path");
75+
if (path == null) throw new ArgumentNullException(nameof(path));
7676

7777
//TODO: throw if native library has already been loaded
7878
Fdb.Options.NativeLibPath = path;

FoundationDB.Client/Fdb.System.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ namespace FoundationDB.Client
3434
using System.Collections.Generic;
3535
using System.Threading;
3636
using System.Threading.Tasks;
37-
using Doxense.Collections.Tuples;
3837
using Doxense.Diagnostics.Contracts;
3938
using FoundationDB.Client.Status;
4039
using FoundationDB.Client.Utils;
@@ -348,7 +347,7 @@ private static async Task<List<Slice>> GetBoundaryKeysInternalAsync([NotNull] IF
348347
{
349348
results.Add(kvp.Key.Substring(KeyServers.Count));
350349
}
351-
begin = chunk.Last.Key.Substring(KeyServers.Count) + (byte)0;
350+
begin = chunk.Last.Key.Substring(KeyServers.Count) + 0;
352351
}
353352
if (!chunk.HasMore)
354353
{

FoundationDB.Client/Fdb.cs

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,15 @@ DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
3030

3131
namespace FoundationDB.Client
3232
{
33-
using FoundationDB.Client.Native;
34-
using FoundationDB.Client.Utils;
35-
using JetBrains.Annotations;
3633
using System;
3734
using System.Diagnostics;
3835
using System.Runtime.CompilerServices;
3936
using System.Runtime.ExceptionServices;
4037
using System.Threading;
4138
using System.Threading.Tasks;
4239
using SystemIO = System.IO;
40+
using FoundationDB.Client.Native;
41+
using JetBrains.Annotations;
4342

4443
/// <summary>FoundationDB binding</summary>
4544
[PublicAPI]
@@ -115,10 +114,7 @@ public static int GetMaxSafeApiVersion()
115114

116115
/// <summary>Returns the currently selected API version.</summary>
117116
/// <remarks>Unless explicitely selected by calling <see cref="UseApiVersion"/> before, the default API version level will be returned</remarks>
118-
public static int ApiVersion
119-
{
120-
get { return s_apiVersion; }
121-
}
117+
public static int ApiVersion => s_apiVersion;
122118

123119
/// <summary>Sets the desired API version of the binding.
124120
/// The selected version level may affect the availability and behavior or certain features.
@@ -138,14 +134,14 @@ public static void UseApiVersion(int value)
138134
value = DefaultApiVersion;
139135
}
140136
if (s_apiVersion == value) return; //Alreay set to same version... skip it.
141-
if (s_started) throw new InvalidOperationException(string.Format("You cannot set API version {0} because version {1} has already been selected", value, s_apiVersion));
137+
if (s_started) throw new InvalidOperationException($"You cannot set API version {value} because version {Fdb.s_apiVersion} has already been selected");
142138

143139
//note: we don't actually select the version yet, only when Start() is called.
144140

145141
int min = GetMinApiVersion();
146-
if (value < min) throw new ArgumentException(String.Format("The minimum API version supported by this binding is {0} and the default version is {1}.", min, DefaultApiVersion));
142+
if (value < min) throw new ArgumentException($"The minimum API version supported by this binding is {min} and the default version is {Fdb.DefaultApiVersion}.");
147143
int max = GetMaxApiVersion();
148-
if (value > max) throw new ArgumentException(String.Format("The maximum API version supported by this binding is {0} and the default version is {1}.", max, DefaultApiVersion));
144+
if (value > max) throw new ArgumentException($"The maximum API version supported by this binding is {max} and the default version is {Fdb.DefaultApiVersion}.");
149145

150146
s_apiVersion = value;
151147
}
@@ -182,7 +178,7 @@ public static Exception MapToException(FdbError code)
182178
if (code == FdbError.Success) return null;
183179

184180
string msg = GetErrorMessage(code);
185-
if (msg == null) throw new FdbException(code, String.Format("Unexpected error code {0}", (int)code));
181+
if (msg == null) throw new FdbException(code, $"Unexpected error code {(int) code}");
186182

187183
//TODO: create a custom FdbException to be able to store the error code and error message
188184
switch(code)
@@ -252,7 +248,7 @@ private static void StopEventLoop()
252248
var err = FdbNative.StopNetwork();
253249
if (err != FdbError.Success)
254250
{
255-
if (Logging.On) Logging.Warning(typeof(Fdb), "StopEventLoop", String.Format("Failed to stop event loop: {0}", err.ToString()));
251+
if (Logging.On) Logging.Warning(typeof(Fdb), "StopEventLoop", $"Failed to stop event loop: {err.ToString()}");
256252
}
257253
s_eventLoopStarted = false;
258254

@@ -276,7 +272,7 @@ private static void StopEventLoop()
276272

277273
if (thread.IsAlive)
278274
{
279-
if (Logging.On) Logging.Warning(typeof(Fdb), "StopEventLoop", String.Format("The fdb network thread has not stopped after {0} seconds. Forcing shutdown...", duration.Elapsed.TotalSeconds.ToString("N0")));
275+
if (Logging.On) Logging.Warning(typeof(Fdb), "StopEventLoop", $"The fdb network thread has not stopped after {duration.Elapsed.TotalSeconds:N0} seconds. Forcing shutdown...");
280276

281277
// Force a shutdown
282278
thread.Abort();
@@ -287,7 +283,7 @@ private static void StopEventLoop()
287283

288284
if (!stopped)
289285
{
290-
if (Logging.On) Logging.Warning(typeof(Fdb), "StopEventLoop", String.Format("The fdb network thread failed to stop after more than {0} seconds. Transaction integrity may not be guaranteed.", duration.Elapsed.TotalSeconds.ToString("N0")));
286+
if (Logging.On) Logging.Warning(typeof(Fdb), "StopEventLoop", $"The fdb network thread failed to stop after more than {duration.Elapsed.TotalSeconds:N0} seconds. Transaction integrity may not be guaranteed.");
291287
}
292288
}
293289
}
@@ -301,7 +297,7 @@ private static void StopEventLoop()
301297
duration.Stop();
302298
if (duration.Elapsed.TotalSeconds >= 20)
303299
{
304-
if (Logging.On) Logging.Warning(typeof(Fdb), "StopEventLoop", String.Format("The fdb network thread took a long time to stop ({0} seconds).", duration.Elapsed.TotalSeconds.ToString("N0")));
300+
if (Logging.On) Logging.Warning(typeof(Fdb), "StopEventLoop", $"The fdb network thread took a long time to stop ({duration.Elapsed.TotalSeconds:N0} seconds).");
305301
}
306302
}
307303
}
@@ -321,18 +317,18 @@ private static void EventLoop()
321317

322318
s_eventLoopThreadId = Thread.CurrentThread.ManagedThreadId;
323319

324-
if (Logging.On) Logging.Verbose(typeof(Fdb), "EventLoop", String.Format("FDB Event Loop running on thread #{0}...", s_eventLoopThreadId.Value));
320+
if (Logging.On) Logging.Verbose(typeof(Fdb), "EventLoop", $"FDB Event Loop running on thread #{Fdb.s_eventLoopThreadId.Value}...");
325321

326322
var err = FdbNative.RunNetwork();
327323
if (err != FdbError.Success)
328324
{
329325
if (s_eventLoopStopRequested || Environment.HasShutdownStarted)
330326
{ // this was requested, or can be explained by the computer shutting down...
331-
if (Logging.On) Logging.Info(typeof(Fdb), "EventLoop", String.Format("The fdb network thread returned with error code {0}: {1}", err, GetErrorMessage(err)));
327+
if (Logging.On) Logging.Info(typeof(Fdb), "EventLoop", $"The fdb network thread returned with error code {err}: {GetErrorMessage(err)}");
332328
}
333329
else
334330
{ // this was NOT expected !
335-
if (Logging.On) Logging.Error(typeof(Fdb), "EventLoop", String.Format("The fdb network thread returned with error code {0}: {1}", err, GetErrorMessage(err)));
331+
if (Logging.On) Logging.Error(typeof(Fdb), "EventLoop", $"The fdb network thread returned with error code {err}: {GetErrorMessage(err)}");
336332
#if DEBUG
337333
Console.Error.WriteLine("THE FDB NETWORK EVENT LOOP HAS FAILED!");
338334
Console.Error.WriteLine("=> " + err);
@@ -394,10 +390,7 @@ private static void EventLoop()
394390
}
395391

396392
/// <summary>Returns true if the Network thread start is executing, otherwise falsse</summary>
397-
public static bool IsNetworkRunning
398-
{
399-
get { return s_eventLoopRunning; }
400-
}
393+
public static bool IsNetworkRunning => s_eventLoopRunning;
401394

402395
/// <summary>Returns 'true' if we are currently running on the Event Loop thread</summary>
403396
internal static bool IsNetworkThread
@@ -563,8 +556,8 @@ internal static async Task<FdbDatabase> OpenInternalAsync(string clusterFile, st
563556
if (!success)
564557
{
565558
// cleanup the cluter if something went wrong
566-
if (db != null) db.Dispose();
567-
if (cluster != null) cluster.Dispose();
559+
db?.Dispose();
560+
cluster?.Dispose();
568561
}
569562
}
570563
}
@@ -597,19 +590,19 @@ public static void Start()
597590
int apiVersion = s_apiVersion;
598591
if (apiVersion <= 0) apiVersion = DefaultApiVersion;
599592

600-
if (Logging.On) Logging.Info(typeof(Fdb), "Start", String.Format("Selecting fdb API version {0}", apiVersion));
593+
if (Logging.On) Logging.Info(typeof(Fdb), "Start", $"Selecting fdb API version {apiVersion}");
601594

602595
FdbError err = FdbNative.SelectApiVersion(apiVersion);
603596
if (err != FdbError.Success)
604597
{
605-
if (Logging.On) Logging.Error(typeof(Fdb), "Start", String.Format("Failed to fdb API version {0}: {1}", apiVersion, err));
598+
if (Logging.On) Logging.Error(typeof(Fdb), "Start", $"Failed to fdb API version {apiVersion}: {err}");
606599

607600
switch (err)
608601
{
609602
case FdbError.ApiVersionNotSupported:
610603
{ // bad version was selected ?
611604
// note: we already bound check the values before, so that means that fdb_c.dll is either an older version or an incompatible new version.
612-
throw new FdbException(err, String.Format("The API version {0} is not supported by the FoundationDB client library (fdb_c.dll) installed on this system. The binding only supports versions {1} to {2}. You either need to upgrade the .NET binding or the FoundationDB client library to a newer version.", apiVersion, GetMinApiVersion(), GetMaxApiVersion()));
605+
throw new FdbException(err, $"The API version {apiVersion} is not supported by the FoundationDB client library (fdb_c.dll) installed on this system. The binding only supports versions {GetMinApiVersion()} to {GetMaxApiVersion()}. You either need to upgrade the .NET binding or the FoundationDB client library to a newer version.");
613606
}
614607
#if DEBUG
615608
case FdbError.ApiVersionAlreadySet:
@@ -626,7 +619,7 @@ public static void Start()
626619

627620
if (!string.IsNullOrWhiteSpace(Fdb.Options.TracePath))
628621
{
629-
if (Logging.On) Logging.Verbose(typeof(Fdb), "Start", String.Format("Will trace client activity in '{0}'", Fdb.Options.TracePath));
622+
if (Logging.On) Logging.Verbose(typeof(Fdb), "Start", $"Will trace client activity in '{Fdb.Options.TracePath}'");
630623
// create trace directory if missing...
631624
if (!SystemIO.Directory.Exists(Fdb.Options.TracePath)) SystemIO.Directory.CreateDirectory(Fdb.Options.TracePath);
632625

@@ -635,40 +628,40 @@ public static void Start()
635628

636629
if (!string.IsNullOrWhiteSpace(Fdb.Options.TLSPlugin))
637630
{
638-
if (Logging.On) Logging.Verbose(typeof(Fdb), "Start", String.Format("Will use custom TLS plugin '{0}'", Fdb.Options.TLSPlugin));
631+
if (Logging.On) Logging.Verbose(typeof(Fdb), "Start", $"Will use custom TLS plugin '{Fdb.Options.TLSPlugin}'");
639632

640633
DieOnError(SetNetworkOption(FdbNetworkOption.TLSPlugin, Fdb.Options.TLSPlugin));
641634
}
642635

643636
if (Fdb.Options.TLSCertificateBytes.IsPresent)
644637
{
645-
if (Logging.On) Logging.Verbose(typeof(Fdb), "Start", String.Format("Will load TLS root certificate and private key from memory ({0} bytes)", Fdb.Options.TLSCertificateBytes.Count));
638+
if (Logging.On) Logging.Verbose(typeof(Fdb), "Start", $"Will load TLS root certificate and private key from memory ({Fdb.Options.TLSCertificateBytes.Count} bytes)");
646639

647640
DieOnError(SetNetworkOption(FdbNetworkOption.TLSCertBytes, Fdb.Options.TLSCertificateBytes));
648641
}
649642
else if (!string.IsNullOrWhiteSpace(Fdb.Options.TLSCertificatePath))
650643
{
651-
if (Logging.On) Logging.Verbose(typeof(Fdb), "Start", String.Format("Will load TLS root certificate and private key from '{0}'", Fdb.Options.TLSCertificatePath));
644+
if (Logging.On) Logging.Verbose(typeof(Fdb), "Start", $"Will load TLS root certificate and private key from '{Fdb.Options.TLSCertificatePath}'");
652645

653646
DieOnError(SetNetworkOption(FdbNetworkOption.TLSCertPath, Fdb.Options.TLSCertificatePath));
654647
}
655648

656649
if (Fdb.Options.TLSPrivateKeyBytes.IsPresent)
657650
{
658-
if (Logging.On) Logging.Verbose(typeof(Fdb), "Start", String.Format("Will load TLS private key from memory ({0} bytes)", Fdb.Options.TLSPrivateKeyBytes.Count));
651+
if (Logging.On) Logging.Verbose(typeof(Fdb), "Start", $"Will load TLS private key from memory ({Fdb.Options.TLSPrivateKeyBytes.Count} bytes)");
659652

660653
DieOnError(SetNetworkOption(FdbNetworkOption.TLSKeyBytes, Fdb.Options.TLSPrivateKeyBytes));
661654
}
662655
else if (!string.IsNullOrWhiteSpace(Fdb.Options.TLSPrivateKeyPath))
663656
{
664-
if (Logging.On) Logging.Verbose(typeof(Fdb), "Start", String.Format("Will load TLS private key from '{0}'", Fdb.Options.TLSPrivateKeyPath));
657+
if (Logging.On) Logging.Verbose(typeof(Fdb), "Start", $"Will load TLS private key from '{Fdb.Options.TLSPrivateKeyPath}'");
665658

666659
DieOnError(SetNetworkOption(FdbNetworkOption.TLSKeyPath, Fdb.Options.TLSPrivateKeyPath));
667660
}
668661

669662
if (Fdb.Options.TLSVerificationPattern.IsPresent)
670663
{
671-
if (Logging.On) Logging.Verbose(typeof(Fdb), "Start", String.Format("Will verify TLS peers with pattern '{0}'", Fdb.Options.TLSVerificationPattern));
664+
if (Logging.On) Logging.Verbose(typeof(Fdb), "Start", $"Will verify TLS peers with pattern '{Fdb.Options.TLSVerificationPattern}'");
672665

673666
DieOnError(SetNetworkOption(FdbNetworkOption.TLSVerifyPeers, Fdb.Options.TLSVerificationPattern));
674667
}

0 commit comments

Comments
 (0)