Skip to content

added support for OpenAsSecondary #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 13 additions & 21 deletions RocksDbNative/RocksDbNative.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Import Project="..\Versions.targets.include"/>
<PropertyGroup>
<Title>RocksDbNative</Title>
<TargetFrameworks>netstandard1.6;net40;net45</TargetFrameworks>
<TargetFrameworks>netstandard1.6;net471</TargetFrameworks>
<Version>$(RocksDbVersion).$(RocksDbNativeBuild)</Version>
<AssemblyVersion>$(Version)</AssemblyVersion>
<FileVersion>$(Version)</FileVersion>
Expand All @@ -21,9 +21,9 @@
</PropertyGroup>
<!-- "DispatchToInnerBuilds" will cause this to run in the outer build (i.e. once regardless of number of frameworks) -->
<!-- Condition="!Exists('rocksdb-$(RocksDbVersion)\runtimes\win-x64\native\rocksdb.dll') and !Exists('$(BaseIntermediateOutputPath)\rocksdb-v$(RocksDbVersion)-win-x64.zip')" -->
<Target Name="DownloadNativeLibs" BeforeTargets="DispatchToInnerBuilds">
<!--Target Name="DownloadNativeLibs" BeforeTargets="DispatchToInnerBuilds"-->
<!-- Here we download the native libraries from the rocksdb-sharp-native project releases according to the version number, if they don't exist -->
<DownloadFile
<!--DownloadFile
Condition="!Exists('$(BaseIntermediateOutputPath)rocksdb-$(RocksDbVersion)\runtimes\%(NativeLib.RuntimeId)\native\%(NativeLib.Lib)') And !Exists('$(BaseIntermediateOutputPath)rocksdb-v$(RocksDbVersion)-%(NativeLib.RuntimeId).zip')"
SourceUrl="https://github.com/warrenfalk/rocksdb-sharp-native/releases/download/v$(RocksDbVersion)/rocksdb-v$(RocksDbVersion)-%(NativeLib.RuntimeId).zip"
DestinationFolder="$(BaseIntermediateOutputPath)">
Expand All @@ -33,36 +33,28 @@
SourceFiles="$(BaseIntermediateOutputPath)rocksdb-v$(RocksDbVersion)-%(NativeLib.RuntimeId).zip"
DestinationFolder="$(BaseIntermediateOutputPath)rocksdb-$(RocksDbVersion)\runtimes\%(NativeLib.RuntimeId)\native\">
</Unzip>
</Target>
</Target-->
<ItemGroup>
<!-- Define the native libraries here -->
<!-- The "Include" attribute is a required attribute in ItemGroups outside of Targets, but I don't know why -->
<NativeLib Include="%(RuntimeId)-%(Lib)" RuntimeId="win-x64" Lib="rocksdb.dll" />
<NativeLib Include="%(RuntimeId)-%(Lib)" RuntimeId="linux-x64" Lib="librocksdb.so" />
<NativeLib Include="%(RuntimeId)-%(Lib)" RuntimeId="osx-x64" Lib="librocksdb.dylib" />
<!--NativeLib Include="%(RuntimeId)-%(Lib)" RuntimeId="win-x64" Lib="rocksdb.dll" /-->
<!--NativeLib Include="%(RuntimeId)-%(Lib)" RuntimeId="osx-x64" Lib="librocksdb.dylib" /-->
<!-- This "Content" is applied for all "NativeLib" and ensures that it is packaged into the correct folder -->
<!-- This also will cause it to be copied to the output directory, which was so far the only way to get it
to work correctly when referenced using a ProjectReference. When packaged as a nupkg and referenced
with PackageReference, then these go to the runtimes folders in the nuget cache, which is enough for
standard consumers, but framework consumers get them copied from the nuget cache to the output via
the .targets files for those frameworks.-->
<Content
Include="@(NativeLib -> '$(BaseIntermediateOutputPath)rocksdb-$(RocksDbVersion)\runtimes\%(RuntimeId)\native\%(Lib)')"
PackagePath="runtimes\%(RuntimeId)\native\%(Lib)"
Link="runtimes\%(RuntimeId)\native\%(Lib)">
<None Include="librocksdb.so">
<Pack>true</Pack>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<!-- These target files take care of copying from the nuget cache area into the output folder as required
by the .Net Framework frameworks -->
<None Remove="build\**" />
<None Include="build\net40\RocksDbNative.targets">
<Pack>true</Pack>
<PackagePath>build/net40</PackagePath>
<PackagePath>runtimes/linux-x64/native</PackagePath>
</None>
<None Include="build\net45\RocksDbNative.targets">

<None Include="rocksdb.dll">
<Pack>true</Pack>
<PackagePath>build/net45</PackagePath>
<PackagePath>runtimes/win-x64/native</PackagePath>
</None>


</ItemGroup>
</Project>
Binary file added RocksDbNative/librocksdb.so
Binary file not shown.
Binary file added RocksDbNative/rocksdb.dll
Binary file not shown.
2 changes: 1 addition & 1 deletion RocksDbSharp/Native.Load.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ static Native()
{
if (RuntimeInformation.ProcessArchitecture == Architecture.X86 && RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
throw new RocksDbSharpException("Rocksdb on windows is not supported for 32 bit applications");
Instance = NativeImport.Auto.Import<Native>("rocksdb", "6.2.2", true);
Instance = NativeImport.Auto.Import<Native>("rocksdb", "6.6.3", true);
}

public Native()
Expand Down
93 changes: 93 additions & 0 deletions RocksDbSharp/Native.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,40 @@ public rocksdb_t_ptr rocksdb_open_for_read_only(
return result;
}

public abstract rocksdb_t_ptr rocksdb_open_as_secondary(
const_rocksdb_options_t_ptr options,
const_char_ptr name,
const_char_ptr secondary_path,
out const_char_ptr errptr);

public rocksdb_t_ptr rocksdb_open_as_secondary(
const_rocksdb_options_t_ptr options,
const_char_ptr name,
const_char_ptr secondary_path)
{
var result = rocksdb_open_as_secondary(options, name, secondary_path, out char_ptr_ptr errptr);
if (errptr != IntPtr.Zero)
throw new RocksDbException(errptr);
return result;
}

public abstract rocksdb_t_ptr rocksdb_open_as_secondary(
const_rocksdb_options_t_ptr options,
string name,
string secondary_path,
out const_char_ptr errptr);

public rocksdb_t_ptr rocksdb_open_as_secondary(
const_rocksdb_options_t_ptr options,
string name,
string secondary_path)
{
var result = rocksdb_open_as_secondary(options, name, secondary_path, out char_ptr_ptr errptr);
if (errptr != IntPtr.Zero)
throw new RocksDbException(errptr);
return result;
}

public abstract rocksdb_backup_engine_t_ptr rocksdb_backup_engine_open(
const_rocksdb_options_t_ptr options,
const_char_ptr path,
Expand Down Expand Up @@ -516,6 +550,65 @@ public rocksdb_t_ptr rocksdb_open_column_families(
return result;
}

public abstract rocksdb_t_ptr rocksdb_open_as_secondary_column_families(
const_rocksdb_options_t_ptr options,
const_char_ptr name,
const_char_ptr secondary_path,
int num_column_families,
const_char_ptr_ptr column_family_names,
const_rocksdb_options_t_ptr[] column_family_options,
rocksdb_column_family_handle_t_ptr[] column_family_handles,
out char_ptr_ptr errptr);

public rocksdb_t_ptr rocksdb_open_as_secondary_column_families(
const_rocksdb_options_t_ptr options,
const_char_ptr name,
const_char_ptr secondary_path,
int num_column_families,
const_char_ptr_ptr column_family_names,
const_rocksdb_options_t_ptr[] column_family_options,
rocksdb_column_family_handle_t_ptr[] column_family_handles)
{
var result = rocksdb_open_as_secondary_column_families(options, name, secondary_path, num_column_families, column_family_names, column_family_options, column_family_handles, out char_ptr_ptr errptr);
if (errptr != IntPtr.Zero)
throw new RocksDbException(errptr);
return result;
}

public abstract rocksdb_t_ptr rocksdb_open_as_secondary_column_families(
const_rocksdb_options_t_ptr options,
string name,
string secondary_path,
int num_column_families,
string[] column_family_names,
const_rocksdb_options_t_ptr[] column_family_options,
rocksdb_column_family_handle_t_ptr[] column_family_handles,
out char_ptr_ptr errptr);

public rocksdb_t_ptr rocksdb_open_as_secondary_column_families(
const_rocksdb_options_t_ptr options,
string name,
string secondary_path,
int num_column_families,
string[] column_family_names,
const_rocksdb_options_t_ptr[] column_family_options,
rocksdb_column_family_handle_t_ptr[] column_family_handles)
{
var result = rocksdb_open_as_secondary_column_families(options, name, secondary_path, num_column_families, column_family_names, column_family_options, column_family_handles, out char_ptr_ptr errptr);
if (errptr != IntPtr.Zero)
throw new RocksDbException(errptr);
return result;
}

public abstract void rocksdb_try_catch_up_with_primary(rocksdb_t_ptr db, out char_ptr_ptr errptr);

public void rocksdb_try_catch_up_with_primary(rocksdb_t_ptr db)
{
rocksdb_try_catch_up_with_primary(db, out char_ptr_ptr errptr);
if (errptr != IntPtr.Zero)
throw new RocksDbException(errptr);
}

public abstract rocksdb_t_ptr rocksdb_open_for_read_only_column_families(
const_rocksdb_options_t_ptr options,
const_char_ptr name,
Expand Down
26 changes: 26 additions & 0 deletions RocksDbSharp/RocksDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ public static RocksDb OpenReadOnly(OptionsHandle options, string path, bool erro
return new RocksDb(db, optionsReferences: null, cfOptionsRefs: null);
}

public static RocksDb OpenAsSecondary(OptionsHandle options, string path, string secondaryPath)
{
IntPtr db = Native.Instance.rocksdb_open_as_secondary(options.Handle, path, secondaryPath);
return new RocksDb(db, optionsReferences: null, cfOptionsRefs: null);
}

public static RocksDb OpenWithTtl(OptionsHandle options, string path, int ttlSeconds)
{
IntPtr db = Native.Instance.rocksdb_open_with_ttl(options.Handle, path, ttlSeconds);
Expand Down Expand Up @@ -87,6 +93,21 @@ public static RocksDb OpenReadOnly(DbOptions options, string path, ColumnFamilie
columnFamilies: cfHandleMap);
}

public static RocksDb OpenAsSecondary(DbOptions options, string path, string secondaryPath, ColumnFamilies columnFamilies)
{
string[] cfnames = columnFamilies.Names.ToArray();
IntPtr[] cfoptions = columnFamilies.OptionHandles.ToArray();
IntPtr[] cfhandles = new IntPtr[cfnames.Length];
IntPtr db = Native.Instance.rocksdb_open_as_secondary_column_families(options.Handle, path, secondaryPath, cfnames.Length, cfnames, cfoptions, cfhandles);
var cfHandleMap = new Dictionary<string, ColumnFamilyHandleInternal>();
foreach (var pair in cfnames.Zip(cfhandles.Select(cfh => new ColumnFamilyHandleInternal(cfh)), (name, cfh) => new { Name = name, Handle = cfh }))
cfHandleMap.Add(pair.Name, pair.Handle);
return new RocksDb(db,
optionsReferences: options.References,
cfOptionsRefs: columnFamilies.Select(cfd => cfd.Options.References).ToArray(),
columnFamilies: cfHandleMap);
}

/// <summary>
/// Usage:
/// <code><![CDATA[
Expand Down Expand Up @@ -306,5 +327,10 @@ public void CompactRange(string start, string limit, ColumnFamilyHandle cf = nul
encoding = Encoding.UTF8;
CompactRange(start == null ? null : encoding.GetBytes(start), limit == null ? null : encoding.GetBytes(limit), cf);
}

public void TryCatchUpWithPrimary()
{
Native.Instance.rocksdb_try_catch_up_with_primary(Handle);
}
}
}
4 changes: 2 additions & 2 deletions RocksDbSharp/RocksDbSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DefineConstants>TRACE</DefineConstants>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net40'">
<Reference Include="Microsoft.CSharp" />
</ItemGroup>

<!-- Move all packages here that complain about compatibility with net40 -->
<ItemGroup Condition="'$(TargetFramework)' != 'net40'">
<PackageReference Include="Microsoft.CSharp" Version="4.3.0" />
Expand Down
2 changes: 1 addition & 1 deletion Versions.targets.include
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<RocksDbVersion>6.2.2</RocksDbVersion>
<RocksDbVersion>6.6.3</RocksDbVersion>
<RocksDbSharpBuild>0</RocksDbSharpBuild>
<RocksDbNativeBuild>0</RocksDbNativeBuild>
</PropertyGroup>
Expand Down
41 changes: 41 additions & 0 deletions tests/RocksDbSharpTest/FunctionalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,47 @@ public void FunctionalTest()
Directory.Delete(dbname, true);
}

// Test OpenAsSecondary
{
var primeDb = "test-prime";
var secondaryDb = "test-secondary";

if (Directory.Exists(primeDb))
Directory.Delete(primeDb, true);

if (Directory.Exists(secondaryDb))
Directory.Delete(secondaryDb, true);

options = new RocksDbSharp.DbOptions()
.SetCreateIfMissing(true)
.SetCreateMissingColumnFamilies(true);

using (var db = RocksDb.Open(options, primeDb))
{
db.Put("one", "uno");
}

using (var db2 = RocksDb.OpenAsSecondary(options, primeDb, secondaryDb))
{
Assert.Equal("uno", db2.Get("one"));
using (var db = RocksDb.Open(options, primeDb))
{
db.Put("two", "dos");
}

Assert.Null(db2.Get("two"));

db2.TryCatchUpWithPrimary();

Assert.Equal("dos", db2.Get("two"));
}

if (Directory.Exists(primeDb))
Directory.Delete(primeDb, true);

if (Directory.Exists(secondaryDb))
Directory.Delete(secondaryDb, true);
}
}

class IntegerStringComparator : StringComparatorBase
Expand Down