Skip to content

Commit

Permalink
Add DeleteExistingDatabaseOnStartup option
Browse files Browse the repository at this point in the history
  • Loading branch information
Havret committed Apr 10, 2023
1 parent 7a5150e commit 0797431
Show file tree
Hide file tree
Showing 13 changed files with 138 additions and 5 deletions.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,22 @@ public class UsersController : ControllerBase
return Ok();
}
}
```
```

## Clean start

For certain scenarios, such as automated testing, it may be useful to remove the existing RocksDB database on startup to ensure a clean start. This can be achieved using the `DeleteExistingDatabaseOnStartup` configuration option.

To use this option, set it to true when configuring the `RocksDbOptions` object:

```csharp
var rocksDbBuilder = builder.Services.AddRocksDb(options =>
{
// Your RocksDb configuration...
options.DeleteExistingDatabaseOnStartup = true;
});
```

When this option is set to true, the existing database will be deleted on startup and a new one will be created. Note that all data in the existing database will be lost when this option is used.

By default, the `DeleteExistingDatabaseOnStartup` option is set to false to preserve the current behavior of not automatically deleting the database. If you need to ensure a clean start for your application, set this option to true in your configuration.
11 changes: 11 additions & 0 deletions src/RocksDb.Extensions/RocksDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,20 @@ public RocksDbContext(IOptions<RocksDbOptions> options)

var columnFamilies = CreateColumnFamilies(options.Value.ColumnFamilies, userSpecifiedOptions);

if (options.Value.DeleteExistingDatabaseOnStartup)
{
DestroyDatabase(options.Value.Path);
}

_rocksDb = RocksDbSharp.RocksDb.Open(dbOptions, options.Value.Path, columnFamilies);
}

private static void DestroyDatabase(string path)
{
var dbOptions = new DbOptions();
Native.Instance.rocksdb_destroy_db(dbOptions.Handle, path);
}

public RocksDbSharp.RocksDb Db => _rocksDb;

private static ColumnFamilies CreateColumnFamilies(IReadOnlyList<string> columnFamilyNames,
Expand Down
11 changes: 11 additions & 0 deletions src/RocksDb.Extensions/RocksDbOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ public class RocksDbOptions
/// </summary>
public string Path { get; set; } = null!;

/// <summary>
/// Indicates whether the existing RocksDB database should be automatically deleted on start-up.
/// </summary>
/// <remarks>
/// If true, the library will automatically delete the existing database on start-up.
/// This can be useful when testing and debugging, as it eliminates
/// the need to manually delete the database before running the code again.
/// The default value is false.
/// </remarks>
public bool DeleteExistingDatabaseOnStartup { get; set; }

/// <summary>
/// A list of <see cref="ISerializerFactory"/> instances that are used to serialize and deserialize data in the RocksDB instance.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using NScenario;
using NUnit.Framework;
using RocksDb.Extensions.Tests.Utils;
using Shouldly;

namespace RocksDb.Extensions.Tests;

public class DeleteExistingDatabaseOnStartupTests
{
[Test]
public async Task should_delete_database_on_start_up()
{
var scenario = TestScenarioFactory.Default();
var path = Path.Join(".", Guid.NewGuid().ToString("N"));

await scenario.Step("Create a new RockDb database and initialize it with some values", () =>
{
using var testFixture = TestFixture.Create(rockDb =>
{
_ = rockDb.AddStore<int, int, RocksDbGenericStore<int, int>>("my-store");
}, options =>
{
options.Path = path;
});
var store = testFixture.GetStore<RocksDbGenericStore<int, int>>();
store.Put(1, 1);
});

await scenario.Step("Re-create the database using the same path", async () =>
{
using var testFixture = TestFixture.Create(rockDb =>
{
_ = rockDb.AddStore<int, int, RocksDbGenericStore<int, int>>("my-store");
}, options =>
{
options.Path = path;
options.DeleteExistingDatabaseOnStartup = true;
});
await scenario.Step("Verify that the value is not available after the restart", () =>
{
var store = testFixture.GetStore<RocksDbGenericStore<int, int>>();
store.TryGet(1, out _).ShouldBeFalse();
});
});
}

[Test]
public async Task should_not_delete_database_on_start_up()
{
var scenario = TestScenarioFactory.Default();
var path = Path.Join(".", Guid.NewGuid().ToString("N"));

await scenario.Step("Create a new RockDb database and initialize it with some values", () =>
{
using var testFixture = TestFixture.Create(rockDb =>
{
_ = rockDb.AddStore<int, int, RocksDbGenericStore<int, int>>("my-store");
}, options =>
{
options.Path = path;
});
var store = testFixture.GetStore<RocksDbGenericStore<int, int>>();
store.Put(1, 1);
});

await scenario.Step("Re-create the database using the same path", async () =>
{
using var testFixture = TestFixture.Create(rockDb =>
{
_ = rockDb.AddStore<int, int, RocksDbGenericStore<int, int>>("my-store");
}, options =>
{
options.Path = path;
options.DeleteExistingDatabaseOnStartup = false;
});
await scenario.Step("Verify that the value is not available after the restart", () =>
{
var store = testFixture.GetStore<RocksDbGenericStore<int, int>>();
store.TryGet(1, out var value).ShouldBeTrue();
value.ShouldBe(1);
});
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
</PackageReference>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="NScenario" Version="4.3.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="NUnit.Analyzers" Version="3.3.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using NUnit.Framework;
using RocksDb.Extensions.System.Text.Json;
using RocksDb.Extensions.Tests.Protos;
using RocksDb.Extensions.Tests.Utils;
using Shouldly;

namespace RocksDb.Extensions.Tests;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NUnit.Framework;
using RocksDb.Extensions.Tests.Utils;
using Shouldly;

namespace RocksDb.Extensions.Tests;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using NUnit.Framework;
using RocksDb.Extensions.ProtoBufNet;
using RocksDb.Extensions.Tests.Protos;
using RocksDb.Extensions.Tests.Utils;
using Shouldly;

namespace RocksDb.Extensions.Tests;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using NUnit.Framework;
using RocksDb.Extensions.Protobuf;
using RocksDb.Extensions.Tests.Utils;
using Shouldly;

namespace RocksDb.Extensions.Tests;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace RocksDb.Extensions.Tests;
namespace RocksDb.Extensions.Tests.Utils;

public class RocksDbGenericStore<TKey, TValue> : RocksDbStore<TKey, TValue>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Microsoft.Extensions.DependencyInjection;
using RocksDb.Extensions.Protobuf;

namespace RocksDb.Extensions.Tests;
namespace RocksDb.Extensions.Tests.Utils;

public class TestFixture : IDisposable
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Text;
using Google.Protobuf;

namespace RocksDb.Extensions.Tests;
namespace RocksDb.Extensions.Tests.Utils;

public static class TestUtils
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace RocksDb.Extensions.Tests;
namespace RocksDb.Extensions.Tests.Utils;

public static class WellKnownValues
{
Expand Down

0 comments on commit 0797431

Please sign in to comment.