Skip to content

Commit aa30d1b

Browse files
committed
convert projects to .net sdk format and modify design of rocksdbnative to deploy an assembly to act as a beacon
This fixes the problem with using RocksDbSharp in a unit test environment as well as any environment that uses shadow copying or runs from a different codebase than usual. The native libraries have been removed from the RocksDbSharp csproj and moved to a new RocksDbNative csproj. The RocksDbNative csproj now also builds an assembly with a single class and a single method for getting the codebase. If you previously had a project reference to the RocksDbSharp project, native binaries were automatically copied for you. This is no longer true and not desired. Now the project reference works like the package reference and you should add a project reference to RocksDbNative to get the native binaries.
1 parent a410e7c commit aa30d1b

28 files changed

+148
-487
lines changed

RocksDbNative/NativePackage.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
3+
namespace RocksDbSharp
4+
{
5+
public class NativePackage
6+
{
7+
public static string GetCodeBase()
8+
#if NETSTANDARD1_6
9+
=> AppContext.BaseDirectory;
10+
#else
11+
=> AppDomain.CurrentDomain.BaseDirectory;
12+
#endif
13+
}
14+
}

RocksDbNative/RocksDbNative.csproj

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<Title>RocksDbNative</Title>
5+
<TargetFrameworks>netstandard1.6;net40;net45</TargetFrameworks>
6+
<Version>5.4.6.7</Version>
7+
<AssemblyVersion>$(Version)</AssemblyVersion>
8+
<FileVersion>$(Version)</FileVersion>
9+
<Description>Native RocksDb Binaries (Install RocksDbSharp package for managed wraper)</Description>
10+
<Authors>Warren Falk</Authors>
11+
<Company>Warren Falk</Company>
12+
<Product>Warren Falk</Product>
13+
<PackageLicenseUrl>https://raw.githubusercontent.com/warrenfalk/rocksdb-sharp/master/LICENSE</PackageLicenseUrl>
14+
<PackageProjectUrl>https://github.com/warrenfalk/rocksdb-sharp</PackageProjectUrl>
15+
<PackageIconUrl>http://rocksdb.org/static/logo.svg</PackageIconUrl>
16+
<RepositoryUrl>https://github.com/warrenfalk/rocksdb-sharp.git</RepositoryUrl>
17+
<PackageTags>rocksdb leveldb embedded database</PackageTags>
18+
<Copyright>Copyright 2016</Copyright>
19+
<PackageReleaseNotes>Native RocksDb Binaries (Install RocksDbSharp package for managed wraper)</PackageReleaseNotes>
20+
<GeneratePackageOnBuild>False</GeneratePackageOnBuild>
21+
</PropertyGroup>
22+
23+
<ItemGroup>
24+
<None Include="native\**">
25+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
26+
<Pack>true</Pack>
27+
<PackagePath>build\native\</PackagePath>
28+
</None>
29+
<None Remove="pack\**" />
30+
<None Include="pack\RocksDbNative.targets">
31+
<Pack>true</Pack>
32+
<PackagePath>build\</PackagePath>
33+
</None>
34+
</ItemGroup>
35+
36+
<Target Name="BeforeBuild">
37+
<Error Text="The RocksDbSharp native binaries could not be found. Run the download script (download-native.cmd or download-native.sh in the RocksDbSharp project folder)" Condition="!Exists('native\amd64\rocksdb.dll')" />
38+
</Target>
39+
</Project>

nuget/RocksDbNative.targets renamed to RocksDbNative/pack/RocksDbNative.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
1+
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
22
<ItemGroup>
33
<!--
44
Note: this is the way we want to do it, but xbuild is broken and they don't expect to fix it

RocksDbSharp.sln

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 15
4-
VisualStudioVersion = 15.0.26403.3
4+
VisualStudioVersion = 15.0.26430.12
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleExampleLowLevel", "examples\SimpleExampleLowLevel\SimpleExampleLowLevel.csproj", "{904017BB-5660-418B-B132-46EA6DF54788}"
77
EndProject
@@ -15,6 +15,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RocksDbSharpTest", "tests\R
1515
EndProject
1616
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RocksDbSharp", "RocksDbSharp\RocksDbSharp.csproj", "{53056B3C-FAC0-41ED-9C5B-8BA41DF85F61}"
1717
EndProject
18+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RocksDbNative", "RocksDbNative\RocksDbNative.csproj", "{5DE4B734-CD28-435A-A552-257FDCCDE50A}"
19+
EndProject
1820
Global
1921
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2022
Debug|Any CPU = Debug|Any CPU
@@ -45,6 +47,10 @@ Global
4547
{53056B3C-FAC0-41ED-9C5B-8BA41DF85F61}.Debug|Any CPU.Build.0 = Debug|Any CPU
4648
{53056B3C-FAC0-41ED-9C5B-8BA41DF85F61}.Release|Any CPU.ActiveCfg = Release|Any CPU
4749
{53056B3C-FAC0-41ED-9C5B-8BA41DF85F61}.Release|Any CPU.Build.0 = Release|Any CPU
50+
{5DE4B734-CD28-435A-A552-257FDCCDE50A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
51+
{5DE4B734-CD28-435A-A552-257FDCCDE50A}.Debug|Any CPU.Build.0 = Debug|Any CPU
52+
{5DE4B734-CD28-435A-A552-257FDCCDE50A}.Release|Any CPU.ActiveCfg = Release|Any CPU
53+
{5DE4B734-CD28-435A-A552-257FDCCDE50A}.Release|Any CPU.Build.0 = Release|Any CPU
4854
EndGlobalSection
4955
GlobalSection(SolutionProperties) = preSolution
5056
HideSolutionNode = FALSE

RocksDbSharp/AutoNativeImport.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,24 @@ public static T Import<T>(INativeLibImporter importer, string libName, string ve
360360
"",
361361
};
362362

363+
// If the RocksDbNative package is referenced, then dynamically load it here so that it can tell us where the native libraries are
364+
string nativeCodeBase = null;
365+
try
366+
{
367+
var nativeLibName = new AssemblyName("RocksDbNative");
368+
var native = Assembly.Load(nativeLibName);
369+
var nativePkgClass = native.GetTypes().First(t => t.FullName == "RocksDbSharp.NativePackage");
370+
var getCodeBaseMethod = nativePkgClass.GetMethod("GetCodeBase");
371+
var getCodeBase = getCodeBaseMethod.CreateDelegate<Func<string>>();
372+
nativeCodeBase = getCodeBase();
373+
}
374+
catch (Exception)
375+
{
376+
}
377+
363378
var basePaths = new string[] {
379+
nativeCodeBase,
380+
Path.GetDirectoryName(UriToPath(Transitional.CurrentFramework.GetBaseDirectory())),
364381
Path.GetDirectoryName(UriToPath(Assembly.GetEntryAssembly()?.CodeBase)),
365382
Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location),
366383
Path.GetDirectoryName(UriToPath(typeof(PosixImporter).GetTypeInfo().Assembly.CodeBase)),
@@ -401,7 +418,7 @@ public static T Import<T>(INativeLibImporter importer, string libName, string ve
401418
return t;
402419
}
403420

404-
throw new NativeLoadException("Unable to locate rocksdb native library, either install it, or use RocksDbNative nuget package\nSearched:" + string.Join("\n", search.Select(s => $"{s.Path}: ({s.Error.GetType().Name}) {s.Error.Message}")), null);
421+
throw new NativeLoadException("Unable to locate rocksdb native library, either install it, or use RocksDbNative nuget package\nSearched:\n" + string.Join("\n", search.Select(s => $"{s.Path}: ({s.Error.GetType().Name}) {s.Error.Message}")), null);
405422
}
406423

407424
private static string UriToPath(string uriString)

RocksDbSharp/RocksDbSharp.csproj

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4+
<Title>RocksDbSharp</Title>
45
<TargetFrameworks>netstandard1.6;net40;net45</TargetFrameworks>
56
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
6-
<Version>5.4.6.0</Version>
7+
<Version>5.4.6.7</Version>
8+
<AssemblyVersion>$(Version)</AssemblyVersion>
9+
<FileVersion>$(Version)</FileVersion>
710
<Description>.Net Bindings for RocksDb. See the Project Site for more information. (Note: Also install RocksDbNative package to have native binaries included with build)</Description>
8-
<AssemblyVersion>5.4.6.0</AssemblyVersion>
9-
<FileVersion>5.4.6.0</FileVersion>
1011
<Authors>Warren Falk</Authors>
1112
<Company>Warren Falk</Company>
1213
<Product>Warren Falk</Product>
@@ -31,6 +32,10 @@
3132
<DefineConstants>TRACE</DefineConstants>
3233
</PropertyGroup>
3334

35+
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard1.6'">
36+
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="1.1.2" />
37+
</ItemGroup>
38+
3439
<ItemGroup Condition="'$(TargetFramework)' == 'net40'">
3540
<Reference Include="Microsoft.CSharp" />
3641
</ItemGroup>
@@ -43,29 +48,4 @@
4348
<PackageReference Include="System.Runtime.InteropServices" Version="4.3.0" />
4449
</ItemGroup>
4550

46-
<ItemGroup>
47-
<None Include="..\native\amd64\rocksdb.dll">
48-
<Link>native\amd64\rocksdb.dll</Link>
49-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
50-
</None>
51-
<None Include="..\native\amd64\librocksdb.so">
52-
<Link>native\amd64\librocksdb.so</Link>
53-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
54-
</None>
55-
<None Include="..\native\amd64\librocksdb.dylib">
56-
<Link>native\amd64\librocksdb.dylib</Link>
57-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
58-
</None>
59-
<None Include="..\native\i386\librocksdb.so">
60-
<Link>native\i386\librocksdb.so</Link>
61-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
62-
</None>
63-
<None Include="..\native\i386\librocksdb.dylib">
64-
<Link>native\i386\librocksdb.dylib</Link>
65-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
66-
</None>
67-
</ItemGroup>
68-
<Target Name="BeforeBuild">
69-
<Error Text="The RocksDbSharp native binaries could not be found. Run the download script (download-native.cmd or download-native.sh in the RocksDbSharp project folder)" Condition="!Exists('..\native\amd64\rocksdb.dll')" />
70-
</Target>
7151
</Project>

RocksDbSharp/Transitional.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ public static IntPtr GetFunctionPointerForDelegate<T>(T func)
8282
#else
8383
=> Marshal.GetFunctionPointerForDelegate((Delegate)(object)func);
8484
#endif
85+
86+
public static string GetBaseDirectory()
87+
#if NETSTANDARD1_6
88+
=> AppContext.BaseDirectory;
89+
#else
90+
=> AppDomain.CurrentDomain.BaseDirectory;
91+
#endif
92+
8593
}
8694

8795
internal static class TransitionalExtensions
@@ -90,6 +98,14 @@ internal static class TransitionalExtensions
9098
public static long GetLongLength<T>(this T[] array, int dimension) => array.GetLength(dimension);
9199
#endif
92100

101+
public static T CreateDelegate<T>(this MethodInfo methodInfo)
102+
#if NET40
103+
=> (T)(object)Delegate.CreateDelegate(typeof(T), methodInfo);
104+
#else
105+
=> (T)(object)methodInfo.CreateDelegate(typeof(T));
106+
#endif
107+
108+
93109
#if NET40
94110
public static TypeInfo CreateTypeInfo(this TypeBuilder builder)
95111
{

download-native.cmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
@powershell wget https://github.com/warrenfalk/rocksdb-sharp/releases/download/v5.4.6.2/native-6e05979.zip -OutFile native.zip ; Add-Type -AssemblyName System.IO.Compression.FileSystem ; If (Test-Path native) { remove-item -Recurse native } ; [System.IO.Compression.ZipFile]::ExtractToDirectory(\"native.zip\", \".\native\") ; remove-item native.zip
1+
@powershell wget https://github.com/warrenfalk/rocksdb-sharp/releases/download/v5.4.6.7/native-6e05979.zip -OutFile native.zip ; Add-Type -AssemblyName System.IO.Compression.FileSystem ; If (Test-Path RocksDbNative\native) { remove-item -Recurse RocksDbNative\native } ; [System.IO.Compression.ZipFile]::ExtractToDirectory(\"native.zip\", \".\RocksDbNative\native\") ; remove-item native.zip

download-native.sh

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#!/bin/sh
2-
[ -f native.zip ] || curl -L https://github.com/warrenfalk/rocksdb-sharp/releases/download/v5.4.6.2/native-6e05979.zip --output native.zip
3-
[ -f native.zip ] && { mkdir -p native; cd native && unzip ../native.zip; }
1+
#!/bin/bash
2+
[ -f native.zip ] || curl -L https://github.com/warrenfalk/rocksdb-sharp/releases/download/v5.4.6.7/native-6e05979.zip --output native.zip
3+
[ -f native.zip ] && { mkdir -p RocksDbNative/native; cd RocksDbNative/native && unzip ../../native.zip; }
44
[ -f native.zip -a -f native/amd64/librocksdb.so ] && rm native.zip
5-

examples/ColumnFamilyExample/App.config

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 4 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,11 @@
1-
<?xml version="1.0" encoding="utf-8"?>
2-
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3-
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
1+
<Project Sdk="Microsoft.NET.Sdk">
42
<PropertyGroup>
5-
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6-
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7-
<ProjectGuid>{AF539614-596B-4271-B1BC-A96F58B22329}</ProjectGuid>
3+
<TargetFramework>net45</TargetFramework>
84
<OutputType>Exe</OutputType>
9-
<AppDesignerFolder>Properties</AppDesignerFolder>
10-
<RootNamespace>ColumnFamilyExample</RootNamespace>
11-
<AssemblyName>ColumnFamilyExample</AssemblyName>
12-
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
13-
<FileAlignment>512</FileAlignment>
14-
</PropertyGroup>
15-
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
165
<PlatformTarget>x64</PlatformTarget>
17-
<DebugSymbols>true</DebugSymbols>
18-
<DebugType>full</DebugType>
19-
<Optimize>false</Optimize>
20-
<OutputPath>bin\Debug\</OutputPath>
21-
<DefineConstants>DEBUG;TRACE</DefineConstants>
22-
<ErrorReport>prompt</ErrorReport>
23-
<WarningLevel>4</WarningLevel>
24-
</PropertyGroup>
25-
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26-
<PlatformTarget>AnyCPU</PlatformTarget>
27-
<DebugType>pdbonly</DebugType>
28-
<Optimize>true</Optimize>
29-
<OutputPath>bin\Release\</OutputPath>
30-
<DefineConstants>TRACE</DefineConstants>
31-
<ErrorReport>prompt</ErrorReport>
32-
<WarningLevel>4</WarningLevel>
336
</PropertyGroup>
347
<ItemGroup>
35-
<Reference Include="System" />
36-
<Reference Include="System.Core" />
37-
<Reference Include="System.Xml.Linq" />
38-
<Reference Include="System.Data.DataSetExtensions" />
39-
<Reference Include="Microsoft.CSharp" />
40-
<Reference Include="System.Data" />
41-
<Reference Include="System.Net.Http" />
42-
<Reference Include="System.Xml" />
43-
</ItemGroup>
44-
<ItemGroup>
45-
<Compile Include="Program.cs" />
46-
<Compile Include="Properties\AssemblyInfo.cs" />
47-
</ItemGroup>
48-
<ItemGroup>
49-
<None Include="App.config" />
50-
</ItemGroup>
51-
<ItemGroup>
52-
<ProjectReference Include="..\..\RocksDbSharp\RocksDbSharp.csproj">
53-
<Project>{53056b3c-fac0-41ed-9c5b-8ba41df85f61}</Project>
54-
<Name>RocksDbSharp</Name>
55-
</ProjectReference>
8+
<ProjectReference Include="..\..\RocksDbNative\RocksDbNative.csproj" />
9+
<ProjectReference Include="..\..\RocksDbSharp\RocksDbSharp.csproj" />
5610
</ItemGroup>
57-
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
58-
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
59-
Other similar extension points exist, see Microsoft.Common.targets.
60-
<Target Name="BeforeBuild">
61-
</Target>
62-
<Target Name="AfterBuild">
63-
</Target>
64-
-->
6511
</Project>

examples/ColumnFamilyExample/Properties/AssemblyInfo.cs

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,12 @@
1-
<?xml version="1.0" encoding="utf-8"?>
2-
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
1+
<Project Sdk="Microsoft.NET.Sdk">
32
<PropertyGroup>
4-
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5-
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6-
<ProjectGuid>{8731668E-3F6B-4B6D-BC95-552804EDDB4D}</ProjectGuid>
3+
<TargetFramework>net45</TargetFramework>
74
<OutputType>Exe</OutputType>
8-
<RootNamespace>PrefixExample</RootNamespace>
9-
<AssemblyName>PrefixExample</AssemblyName>
10-
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
11-
</PropertyGroup>
12-
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
13-
<DebugSymbols>true</DebugSymbols>
14-
<DebugType>full</DebugType>
15-
<Optimize>false</Optimize>
16-
<OutputPath>bin\Debug</OutputPath>
17-
<DefineConstants>DEBUG;</DefineConstants>
18-
<ErrorReport>prompt</ErrorReport>
19-
<WarningLevel>4</WarningLevel>
20-
<Externalconsole>true</Externalconsole>
21-
<PlatformTarget>x64</PlatformTarget>
22-
</PropertyGroup>
23-
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
24-
<DebugType>full</DebugType>
25-
<Optimize>true</Optimize>
26-
<OutputPath>bin\Release</OutputPath>
27-
<ErrorReport>prompt</ErrorReport>
28-
<WarningLevel>4</WarningLevel>
29-
<Externalconsole>true</Externalconsole>
305
<PlatformTarget>x64</PlatformTarget>
6+
<AssemblyVersion>1.0.0</AssemblyVersion>
317
</PropertyGroup>
328
<ItemGroup>
33-
<Reference Include="System" />
34-
</ItemGroup>
35-
<ItemGroup>
36-
<Compile Include="Program.cs" />
37-
<Compile Include="Properties\AssemblyInfo.cs" />
38-
</ItemGroup>
39-
<ItemGroup>
40-
<ProjectReference Include="..\..\RocksDbSharp\RocksDbSharp.csproj">
41-
<Project>{53056b3c-fac0-41ed-9c5b-8ba41df85f61}</Project>
42-
<Name>RocksDbSharp</Name>
43-
</ProjectReference>
9+
<ProjectReference Include="..\..\RocksDbNative\RocksDbNative.csproj" />
10+
<ProjectReference Include="..\..\RocksDbSharp\RocksDbSharp.csproj" />
4411
</ItemGroup>
45-
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
4612
</Project>

0 commit comments

Comments
 (0)