Merge pull request #63 from RocherKong/master

Code Refactoring
This commit is contained in:
lionsoul 2018-07-30 10:26:21 +08:00 committed by GitHub
commit d239a43fa5
23 changed files with 1136 additions and 850 deletions

View File

@ -18,7 +18,7 @@ namespace BenchmarkTest.BenchTests
[Benchmark] [Benchmark]
public async Task TestAsyncSpeedForMemorySearch() public async Task TestAsyncSpeedForMemorySearch()
{ {
await _dbSearcher.AsyncMemorySearch(validIp); await _dbSearcher.MemorySearchAsync(validIp);
} }
[Benchmark] [Benchmark]
@ -29,7 +29,7 @@ namespace BenchmarkTest.BenchTests
[Benchmark] [Benchmark]
public async Task TestAsyncBinarySearch() public async Task TestAsyncBinarySearch()
{ {
await _dbSearcher.AsyncBinarySearch(validIp); await _dbSearcher.BinarySearchAsync(validIp);
} }
[Benchmark] [Benchmark]
@ -40,7 +40,7 @@ namespace BenchmarkTest.BenchTests
[Benchmark] [Benchmark]
public async Task TestAsyncSpeedForBTreeSearch() public async Task TestAsyncSpeedForBTreeSearch()
{ {
await _dbSearcher.AsyncBtreeSearch(validIp); await _dbSearcher.BtreeSearchAsync(validIp);
} }
} }
} }

View File

@ -1,154 +1,154 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup> <PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{053EA51E-2246-4FCB-A106-9B53E70FDC3F}</ProjectGuid> <ProjectGuid>{053EA51E-2246-4FCB-A106-9B53E70FDC3F}</ProjectGuid>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<RootNamespace>BenchmarkTest</RootNamespace> <RootNamespace>BenchmarkTest</RootNamespace>
<AssemblyName>BenchmarkTest</AssemblyName> <AssemblyName>BenchmarkTest</AssemblyName>
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion> <TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment> <FileAlignment>512</FileAlignment>
<TargetFrameworkProfile /> <TargetFrameworkProfile />
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget> <PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols> <DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType> <DebugType>full</DebugType>
<Optimize>false</Optimize> <Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath> <OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants> <DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget> <PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType> <DebugType>pdbonly</DebugType>
<Optimize>true</Optimize> <Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath> <OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants> <DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="BenchmarkDotNet, Version=0.10.14.0, Culture=neutral, PublicKeyToken=aa0ca2f9092cefc4, processorArchitecture=MSIL"> <Reference Include="BenchmarkDotNet, Version=0.10.14.0, Culture=neutral, PublicKeyToken=aa0ca2f9092cefc4, processorArchitecture=MSIL">
<HintPath>..\packages\BenchmarkDotNet.0.10.14\lib\net46\BenchmarkDotNet.dll</HintPath> <HintPath>..\packages\BenchmarkDotNet.0.10.14\lib\net46\BenchmarkDotNet.dll</HintPath>
</Reference> </Reference>
<Reference Include="BenchmarkDotNet.Core, Version=0.10.14.0, Culture=neutral, PublicKeyToken=aa0ca2f9092cefc4, processorArchitecture=MSIL"> <Reference Include="BenchmarkDotNet.Core, Version=0.10.14.0, Culture=neutral, PublicKeyToken=aa0ca2f9092cefc4, processorArchitecture=MSIL">
<HintPath>..\packages\BenchmarkDotNet.Core.0.10.14\lib\net46\BenchmarkDotNet.Core.dll</HintPath> <HintPath>..\packages\BenchmarkDotNet.Core.0.10.14\lib\net46\BenchmarkDotNet.Core.dll</HintPath>
</Reference> </Reference>
<Reference Include="BenchmarkDotNet.Toolchains.Roslyn, Version=0.10.14.0, Culture=neutral, PublicKeyToken=aa0ca2f9092cefc4, processorArchitecture=MSIL"> <Reference Include="BenchmarkDotNet.Toolchains.Roslyn, Version=0.10.14.0, Culture=neutral, PublicKeyToken=aa0ca2f9092cefc4, processorArchitecture=MSIL">
<HintPath>..\packages\BenchmarkDotNet.Toolchains.Roslyn.0.10.14\lib\net46\BenchmarkDotNet.Toolchains.Roslyn.dll</HintPath> <HintPath>..\packages\BenchmarkDotNet.Toolchains.Roslyn.0.10.14\lib\net46\BenchmarkDotNet.Toolchains.Roslyn.dll</HintPath>
</Reference> </Reference>
<Reference Include="Microsoft.CodeAnalysis, Version=2.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> <Reference Include="Microsoft.CodeAnalysis, Version=2.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.CodeAnalysis.Common.2.6.1\lib\netstandard1.3\Microsoft.CodeAnalysis.dll</HintPath> <HintPath>..\packages\Microsoft.CodeAnalysis.Common.2.6.1\lib\netstandard1.3\Microsoft.CodeAnalysis.dll</HintPath>
</Reference> </Reference>
<Reference Include="Microsoft.CodeAnalysis.CSharp, Version=2.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> <Reference Include="Microsoft.CodeAnalysis.CSharp, Version=2.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.CodeAnalysis.CSharp.2.6.1\lib\netstandard1.3\Microsoft.CodeAnalysis.CSharp.dll</HintPath> <HintPath>..\packages\Microsoft.CodeAnalysis.CSharp.2.6.1\lib\netstandard1.3\Microsoft.CodeAnalysis.CSharp.dll</HintPath>
</Reference> </Reference>
<Reference Include="Microsoft.DotNet.InternalAbstractions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL"> <Reference Include="Microsoft.DotNet.InternalAbstractions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.DotNet.InternalAbstractions.1.0.0\lib\net451\Microsoft.DotNet.InternalAbstractions.dll</HintPath> <HintPath>..\packages\Microsoft.DotNet.InternalAbstractions.1.0.0\lib\net451\Microsoft.DotNet.InternalAbstractions.dll</HintPath>
</Reference> </Reference>
<Reference Include="Microsoft.DotNet.PlatformAbstractions, Version=1.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL"> <Reference Include="Microsoft.DotNet.PlatformAbstractions, Version=1.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.DotNet.PlatformAbstractions.1.1.1\lib\net451\Microsoft.DotNet.PlatformAbstractions.dll</HintPath> <HintPath>..\packages\Microsoft.DotNet.PlatformAbstractions.1.1.1\lib\net451\Microsoft.DotNet.PlatformAbstractions.dll</HintPath>
</Reference> </Reference>
<Reference Include="Microsoft.Win32.Registry, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> <Reference Include="Microsoft.Win32.Registry, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Win32.Registry.4.3.0\lib\net46\Microsoft.Win32.Registry.dll</HintPath> <HintPath>..\packages\Microsoft.Win32.Registry.4.3.0\lib\net46\Microsoft.Win32.Registry.dll</HintPath>
</Reference> </Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.AppContext, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> <Reference Include="System.AppContext, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.AppContext.4.3.0\lib\net46\System.AppContext.dll</HintPath> <HintPath>..\packages\System.AppContext.4.3.0\lib\net46\System.AppContext.dll</HintPath>
</Reference> </Reference>
<Reference Include="System.Collections.Immutable, Version=1.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> <Reference Include="System.Collections.Immutable, Version=1.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Collections.Immutable.1.3.1\lib\portable-net45+win8+wp8+wpa81\System.Collections.Immutable.dll</HintPath> <HintPath>..\packages\System.Collections.Immutable.1.3.1\lib\portable-net45+win8+wp8+wpa81\System.Collections.Immutable.dll</HintPath>
</Reference> </Reference>
<Reference Include="System.ComponentModel.Composition" /> <Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Console, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> <Reference Include="System.Console, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Console.4.3.0\lib\net46\System.Console.dll</HintPath> <HintPath>..\packages\System.Console.4.3.0\lib\net46\System.Console.dll</HintPath>
</Reference> </Reference>
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="Microsoft.CSharp" /> <Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" /> <Reference Include="System.Data" />
<Reference Include="System.Diagnostics.FileVersionInfo, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> <Reference Include="System.Diagnostics.FileVersionInfo, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Diagnostics.FileVersionInfo.4.3.0\lib\net46\System.Diagnostics.FileVersionInfo.dll</HintPath> <HintPath>..\packages\System.Diagnostics.FileVersionInfo.4.3.0\lib\net46\System.Diagnostics.FileVersionInfo.dll</HintPath>
</Reference> </Reference>
<Reference Include="System.Diagnostics.StackTrace, Version=4.0.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> <Reference Include="System.Diagnostics.StackTrace, Version=4.0.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Diagnostics.StackTrace.4.3.0\lib\net46\System.Diagnostics.StackTrace.dll</HintPath> <HintPath>..\packages\System.Diagnostics.StackTrace.4.3.0\lib\net46\System.Diagnostics.StackTrace.dll</HintPath>
</Reference> </Reference>
<Reference Include="System.IO.Compression, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL"> <Reference Include="System.IO.Compression, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\System.IO.Compression.4.3.0\lib\net46\System.IO.Compression.dll</HintPath> <HintPath>..\packages\System.IO.Compression.4.3.0\lib\net46\System.IO.Compression.dll</HintPath>
</Reference> </Reference>
<Reference Include="System.IO.FileSystem, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> <Reference Include="System.IO.FileSystem, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.IO.FileSystem.4.3.0\lib\net46\System.IO.FileSystem.dll</HintPath> <HintPath>..\packages\System.IO.FileSystem.4.3.0\lib\net46\System.IO.FileSystem.dll</HintPath>
</Reference> </Reference>
<Reference Include="System.IO.FileSystem.Primitives, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> <Reference Include="System.IO.FileSystem.Primitives, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.IO.FileSystem.Primitives.4.3.0\lib\net46\System.IO.FileSystem.Primitives.dll</HintPath> <HintPath>..\packages\System.IO.FileSystem.Primitives.4.3.0\lib\net46\System.IO.FileSystem.Primitives.dll</HintPath>
</Reference> </Reference>
<Reference Include="System.Management" /> <Reference Include="System.Management" />
<Reference Include="System.Net.Http" /> <Reference Include="System.Net.Http" />
<Reference Include="System.Numerics" /> <Reference Include="System.Numerics" />
<Reference Include="System.Reflection.Metadata, Version=1.4.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> <Reference Include="System.Reflection.Metadata, Version=1.4.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Reflection.Metadata.1.4.2\lib\portable-net45+win8\System.Reflection.Metadata.dll</HintPath> <HintPath>..\packages\System.Reflection.Metadata.1.4.2\lib\portable-net45+win8\System.Reflection.Metadata.dll</HintPath>
</Reference> </Reference>
<Reference Include="System.Security.Cryptography.Algorithms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> <Reference Include="System.Security.Cryptography.Algorithms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Security.Cryptography.Algorithms.4.3.0\lib\net46\System.Security.Cryptography.Algorithms.dll</HintPath> <HintPath>..\packages\System.Security.Cryptography.Algorithms.4.3.0\lib\net46\System.Security.Cryptography.Algorithms.dll</HintPath>
</Reference> </Reference>
<Reference Include="System.Security.Cryptography.Encoding, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> <Reference Include="System.Security.Cryptography.Encoding, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Security.Cryptography.Encoding.4.3.0\lib\net46\System.Security.Cryptography.Encoding.dll</HintPath> <HintPath>..\packages\System.Security.Cryptography.Encoding.4.3.0\lib\net46\System.Security.Cryptography.Encoding.dll</HintPath>
</Reference> </Reference>
<Reference Include="System.Security.Cryptography.Primitives, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> <Reference Include="System.Security.Cryptography.Primitives, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Security.Cryptography.Primitives.4.3.0\lib\net46\System.Security.Cryptography.Primitives.dll</HintPath> <HintPath>..\packages\System.Security.Cryptography.Primitives.4.3.0\lib\net46\System.Security.Cryptography.Primitives.dll</HintPath>
</Reference> </Reference>
<Reference Include="System.Security.Cryptography.X509Certificates, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> <Reference Include="System.Security.Cryptography.X509Certificates, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Security.Cryptography.X509Certificates.4.3.0\lib\net46\System.Security.Cryptography.X509Certificates.dll</HintPath> <HintPath>..\packages\System.Security.Cryptography.X509Certificates.4.3.0\lib\net46\System.Security.Cryptography.X509Certificates.dll</HintPath>
</Reference> </Reference>
<Reference Include="System.Text.Encoding.CodePages, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> <Reference Include="System.Text.Encoding.CodePages, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Text.Encoding.CodePages.4.3.0\lib\net46\System.Text.Encoding.CodePages.dll</HintPath> <HintPath>..\packages\System.Text.Encoding.CodePages.4.3.0\lib\net46\System.Text.Encoding.CodePages.dll</HintPath>
</Reference> </Reference>
<Reference Include="System.Threading.Tasks.Extensions, Version=4.1.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL"> <Reference Include="System.Threading.Tasks.Extensions, Version=4.1.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Threading.Tasks.Extensions.4.3.0\lib\portable-net45+win8+wp8+wpa81\System.Threading.Tasks.Extensions.dll</HintPath> <HintPath>..\packages\System.Threading.Tasks.Extensions.4.3.0\lib\portable-net45+win8+wp8+wpa81\System.Threading.Tasks.Extensions.dll</HintPath>
</Reference> </Reference>
<Reference Include="System.Threading.Thread, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> <Reference Include="System.Threading.Thread, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Threading.Thread.4.3.0\lib\net46\System.Threading.Thread.dll</HintPath> <HintPath>..\packages\System.Threading.Thread.4.3.0\lib\net46\System.Threading.Thread.dll</HintPath>
</Reference> </Reference>
<Reference Include="System.ValueTuple, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL"> <Reference Include="System.ValueTuple, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.ValueTuple.4.3.0\lib\netstandard1.0\System.ValueTuple.dll</HintPath> <HintPath>..\packages\System.ValueTuple.4.3.0\lib\netstandard1.0\System.ValueTuple.dll</HintPath>
</Reference> </Reference>
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" /> <Reference Include="System.Xml.Linq" />
<Reference Include="System.Xml.ReaderWriter, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> <Reference Include="System.Xml.ReaderWriter, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Xml.ReaderWriter.4.3.0\lib\net46\System.Xml.ReaderWriter.dll</HintPath> <HintPath>..\packages\System.Xml.ReaderWriter.4.3.0\lib\net46\System.Xml.ReaderWriter.dll</HintPath>
</Reference> </Reference>
<Reference Include="System.Xml.XmlDocument, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> <Reference Include="System.Xml.XmlDocument, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Xml.XmlDocument.4.3.0\lib\net46\System.Xml.XmlDocument.dll</HintPath> <HintPath>..\packages\System.Xml.XmlDocument.4.3.0\lib\net46\System.Xml.XmlDocument.dll</HintPath>
</Reference> </Reference>
<Reference Include="System.Xml.XPath, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> <Reference Include="System.Xml.XPath, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Xml.XPath.4.3.0\lib\net46\System.Xml.XPath.dll</HintPath> <HintPath>..\packages\System.Xml.XPath.4.3.0\lib\net46\System.Xml.XPath.dll</HintPath>
</Reference> </Reference>
<Reference Include="System.Xml.XPath.XDocument, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> <Reference Include="System.Xml.XPath.XDocument, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Xml.XPath.XDocument.4.3.0\lib\net46\System.Xml.XPath.XDocument.dll</HintPath> <HintPath>..\packages\System.Xml.XPath.XDocument.4.3.0\lib\net46\System.Xml.XPath.XDocument.dll</HintPath>
</Reference> </Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="BenchTests\BenchTestBase.cs" /> <Compile Include="BenchTests\BenchTestBase.cs" />
<Compile Include="BenchTests\NormalBenchmarkTests.cs" /> <Compile Include="BenchTests\NormalBenchmarkTests.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="App.config" /> <None Include="App.config" />
<None Include="packages.config" /> <None Include="packages.config" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Analyzer Include="..\packages\Microsoft.CodeAnalysis.Analyzers.1.1.0\analyzers\dotnet\cs\Microsoft.CodeAnalysis.Analyzers.dll" /> <Analyzer Include="..\packages\Microsoft.CodeAnalysis.Analyzers.1.1.0\analyzers\dotnet\cs\Microsoft.CodeAnalysis.Analyzers.dll" />
<Analyzer Include="..\packages\Microsoft.CodeAnalysis.Analyzers.1.1.0\analyzers\dotnet\cs\Microsoft.CodeAnalysis.CSharp.Analyzers.dll" /> <Analyzer Include="..\packages\Microsoft.CodeAnalysis.Analyzers.1.1.0\analyzers\dotnet\cs\Microsoft.CodeAnalysis.CSharp.Analyzers.dll" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\IP2Region\IP2Region.csproj"> <ProjectReference Include="..\IP2Region\IP2Region.csproj">
<Project>{4c6032ee-43a5-4d5c-88f8-c411069e49e0}</Project> <Project>{4c6032ee-43a5-4d5c-88f8-c411069e49e0}</Project>
<Name>IP2Region</Name> <Name>IP2Region</Name>
</ProjectReference> </ProjectReference>
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project> </Project>

View File

@ -0,0 +1,59 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Order;
using IP2Region.Models;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace IP2Region.Test.Benchmark
{
[RankColumn]
public class DbSearch_Test : TestBase
{
private string RandomIP = "";
[Benchmark]
public DataBlock MemorySearch()
{
RandomIP = GetRandomIP();
//Console.WriteLine(RandomIP);
return _search.MemorySearch(RandomIP);
}
[Benchmark]
public async Task<DataBlock> MemorySearch_Async()
{
RandomIP = GetRandomIP();
return await _search.MemorySearchAsync(RandomIP);
}
[Benchmark]
public DataBlock BinarySearch()
{
RandomIP = GetRandomIP();
return _search.BinarySearch(RandomIP);
}
[Benchmark]
public async Task<DataBlock> BinarySearch_Async()
{
RandomIP = GetRandomIP();
return await _search.BinarySearchAsync(RandomIP);
}
[Benchmark]
public DataBlock BtreeSearch()
{
RandomIP = GetRandomIP();
return _search.BtreeSearch(RandomIP);
}
[Benchmark]
public async Task<DataBlock> BtreeSearch_Async()
{
RandomIP = GetRandomIP();
return await _search.BtreeSearchAsync(RandomIP);
}
}
}

View File

@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.11.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\IP2Region\IP2Region.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,15 @@
using BenchmarkDotNet.Running;
using System;
namespace IP2Region.Test.Benchmark
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Welcome To IP2Regin!");
var summary = BenchmarkRunner.Run<DbSearch_Test>();
Console.ReadLine();
}
}
}

View File

@ -0,0 +1,51 @@
using BenchmarkDotNet.Attributes;
using System;
using System.Collections.Generic;
using System.Text;
namespace IP2Region.Test.Benchmark
{
public class TestBase
{
protected DbSearcher _search;
private readonly string _dBFilePath = "";
public TestBase()
{
}
public TestBase(String DBFilePath)
{
this._dBFilePath = DBFilePath;
}
[GlobalSetup]
public void Init()
{
if (String.IsNullOrEmpty(_dBFilePath))
{
_search = new DbSearcher(AppContext.BaseDirectory + @"\DB\ip2region.db");
}
else
{
_search = new DbSearcher(_dBFilePath);
}
}
[GlobalCleanup]
public void Dispose()
{
_search?.Dispose();
}
public String GetRandomIP()
{
return new Random(Guid.NewGuid().GetHashCode()).Next(0, 255).ToString() + "."
+ new Random(Guid.NewGuid().GetHashCode()).Next(0, 255).ToString() + "."
+ new Random(Guid.NewGuid().GetHashCode()).Next(0, 255).ToString() + "."
+ new Random(Guid.NewGuid().GetHashCode()).Next(0, 255).ToString();
}
}
}

Binary file not shown.

View File

@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\IP2Region\IP2Region.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="DB\ip2region.db">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

@ -0,0 +1,36 @@
using System;
using System.Threading.Tasks;
using Xunit;
namespace IP2Region.Test.xUnit
{
public class UnitTest1
{
private readonly DbSearcher _search;
public UnitTest1()
{
_search = new DbSearcher(Environment.CurrentDirectory + @"\DB\ip2region.db");
}
[Fact]
public void Search_Test()
{
Assert.NotNull(_search.MemorySearch("183.192.62.65").Region);
Assert.NotNull(_search.MemorySearchAsync("183.192.62.65").Result.Region);
Assert.NotNull(_search.BinarySearch("183.192.62.65").Region);
Assert.NotNull(_search.BinarySearchAsync("183.192.62.65").Result.Region);
Assert.NotNull(_search.BtreeSearch("183.192.62.65").Region);
Assert.NotNull(_search.BtreeSearchAsync("183.192.62.65").Result.Region);
}
[Fact]
public async Task SearchAsync_Test()
{
var result = await _search.MemorySearchAsync("183.192.62.65");
Assert.NotNull(result.Region);
}
}
}

Binary file not shown.

View File

@ -1,429 +1,400 @@
//******************************* //*******************************
// Create By Rocher Kong // Created By Rocher Kong
// Github https://github.com/RocherKong // Github https://github.com/RocherKong
// Date 2018.02.09 // Date 2018.02.09
//******************************* //*******************************
using IP2Region.Models; using IP2Region.Models;
using System; using System;
using System.IO; using System.IO;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace IP2Region namespace IP2Region
{ {
public class DbSearcher : IDisposable public class DbSearcher : IDisposable
{ {
const int BTREE_ALGORITHM = 1; const int BTREE_ALGORITHM = 1;
const int BINARY_ALGORITHM = 2; const int BINARY_ALGORITHM = 2;
const int MEMORY_ALGORITYM = 3; const int MEMORY_ALGORITYM = 3;
private DbConfig _dbConfig = null; private DbConfig _dbConfig = null;
/** /// <summary>
* db file access handler /// db file access handler
*/ /// </summary>
private FileStream _raf = null; private FileStream _raf = null;
/** /// <summary>
* header blocks buffer /// header blocks buffer
*/ /// </summary>
private long[] _headerSip = null; private long[] _headerSip = null;
private int[] _headerPtr = null; private int[] _headerPtr = null;
private int _headerLength; private int _headerLength;
/** /// <summary>
* super blocks info /// super blocks info
*/ /// </summary>
private long _firstIndexPtr = 0; private long _firstIndexPtr = 0;
private long _lastIndexPtr = 0; private long _lastIndexPtr = 0;
private int _totalIndexBlocks = 0; private int _totalIndexBlocks = 0;
/** /// <summary>
* for memory mode /// for memory mode
* the original db binary string /// the original db binary string
*/ /// </summary>
private byte[] _dbBinStr = null; private byte[] _dbBinStr = null;
/// <summary> /// <summary>
/// Get by index ptr. /// Get by index ptr.
/// </summary> /// </summary>
private DataBlock GetByIndexPtr(long ptr) private DataBlock GetByIndexPtr(long ptr)
{ {
_raf.Seek(ptr, SeekOrigin.Begin); _raf.Seek(ptr, SeekOrigin.Begin);
byte[] byte[] buffer = new byte[12];
buffer = new byte[12];
_raf.Read(buffer, 0, buffer.Length); _raf.Read(buffer, 0, buffer.Length);
long extra = Utils.GetIntLong(buffer, 8);
int dataLen = (int)((extra >> 24) & 0xFF);
int dataPtr = (int)((extra & 0x00FFFFFF));
_raf.Seek(dataPtr, SeekOrigin.Begin);
byte[] data = new byte[dataLen];
_raf.Read(data, 0, data.Length);
int city_id = (int)Utils.GetIntLong(data, 0);
string region = Encoding.UTF8.GetString(data, 4, data.Length - 4);
return new DataBlock(city_id, region, dataPtr);
}
long extra = Utils.getIntLong(buffer, 8); public DbSearcher(DbConfig dbConfig, string dbFile)
{
int dataLen = (int)((extra >> 24) & 0xFF);
int dataPtr = (int)((extra & 0x00FFFFFF));
_raf.Seek(dataPtr, SeekOrigin.Begin);
byte[] data = new byte[dataLen];
_raf.Read(data, 0, data.Length);
int city_id = (int)Utils.getIntLong(data, 0);
string region = Encoding.UTF8.GetString(data, 4, data.Length - 4);
return new DataBlock(city_id, region, dataPtr);
}
public DbSearcher(DbConfig dbConfig, string dbFile)
{
if (_dbConfig == null) if (_dbConfig == null)
{ {
_dbConfig = dbConfig; _dbConfig = dbConfig;
} }
_raf = new FileStream(dbFile, FileMode.Open, FileAccess.Read, FileShare.Read); _raf = new FileStream(dbFile, FileMode.Open, FileAccess.Read, FileShare.Read);
} }
public DbSearcher(string dbFile) : this(null, dbFile) { } public DbSearcher(string dbFile) : this(null, dbFile) { }
#region Sync Methods #region Sync Methods
/// <summary> /// <summary>
/// Get the region with a int ip address with memory binary search algorithm. /// Get the region with a int ip address with memory binary search algorithm.
/// </summary> /// </summary>
private DataBlock MemorySearch(long ip) private DataBlock MemorySearch(long ip)
{ {
int blen = IndexBlock.LENGTH; int blen = IndexBlock.LENGTH;
if (_dbBinStr == null) if (_dbBinStr == null)
{ {
_dbBinStr = new byte[(int)_raf.Length]; _dbBinStr = new byte[(int)_raf.Length];
_raf.Seek(0L, SeekOrigin.Begin); _raf.Seek(0L, SeekOrigin.Begin);
_raf.Read(_dbBinStr, 0, _dbBinStr.Length); _raf.Read(_dbBinStr, 0, _dbBinStr.Length);
//initialize the global vars //initialize the global vars
_firstIndexPtr = Utils.getIntLong(_dbBinStr, 0); _firstIndexPtr = Utils.GetIntLong(_dbBinStr, 0);
_lastIndexPtr = Utils.getIntLong(_dbBinStr, 4); _lastIndexPtr = Utils.GetIntLong(_dbBinStr, 4);
_totalIndexBlocks = (int)((_lastIndexPtr - _firstIndexPtr) / blen) + 1; _totalIndexBlocks = (int)((_lastIndexPtr - _firstIndexPtr) / blen) + 1;
} }
//search the index blocks to define the data //search the index blocks to define the data
int l = 0, h = _totalIndexBlocks; int l = 0, h = _totalIndexBlocks;
long sip = 0; long sip = 0;
while (l <= h) while (l <= h)
{ {
int m = (l + h) >> 1; int m = (l + h) >> 1;
int p = (int)(_firstIndexPtr + m * blen); int p = (int)(_firstIndexPtr + m * blen);
sip = Utils.getIntLong(_dbBinStr, p); sip = Utils.GetIntLong(_dbBinStr, p);
if (ip < sip) if (ip < sip)
{ {
h = m - 1; h = m - 1;
} }
else else
{ {
sip = Utils.getIntLong(_dbBinStr, p + 4); sip = Utils.GetIntLong(_dbBinStr, p + 4);
if (ip > sip) if (ip > sip)
{ {
l = m + 1; l = m + 1;
} }
else else
{ {
sip = Utils.getIntLong(_dbBinStr, p + 8); sip = Utils.GetIntLong(_dbBinStr, p + 8);
break; break;
} }
} }
} }
//not matched //not matched
if (sip == 0) return null; if (sip == 0) return null;
//get the data //get the data
int dataLen = (int)((sip >> 24) & 0xFF); int dataLen = (int)((sip >> 24) & 0xFF);
int dataPtr = (int)((sip & 0x00FFFFFF)); int dataPtr = (int)((sip & 0x00FFFFFF));
int city_id = (int)Utils.getIntLong(_dbBinStr, dataPtr); int city_id = (int)Utils.GetIntLong(_dbBinStr, dataPtr);
string region = Encoding.UTF8.GetString(_dbBinStr, dataPtr + 4, dataLen - 4);//new String(dbBinStr, dataPtr + 4, dataLen - 4, Encoding.UTF8); string region = Encoding.UTF8.GetString(_dbBinStr, dataPtr + 4, dataLen - 4);//new String(dbBinStr, dataPtr + 4, dataLen - 4, Encoding.UTF8);
return new DataBlock(city_id, region, dataPtr); return new DataBlock(city_id, region, dataPtr);
} }
/// <summary> /// <summary>
/// Get the region throught the ip address with memory binary search algorithm. /// Get the region throught the ip address with memory binary search algorithm.
/// </summary> /// </summary>
public DataBlock MemorySearch(string ip) public DataBlock MemorySearch(string ip)
{ {
return MemorySearch(Utils.ip2long(ip)); return MemorySearch(Utils.Ip2long(ip));
} }
/// <summary> /// <summary>
/// Get the region with a int ip address with b-tree algorithm. /// Get the region with a int ip address with b-tree algorithm.
/// </summary> /// </summary>
private DataBlock BtreeSearch(long ip) private DataBlock BtreeSearch(long ip)
{ {
//check and load the header //check and load the header
if (_headerSip == null) if (_headerSip == null)
{ {
_raf.Seek(8L, SeekOrigin.Begin); //pass the super block _raf.Seek(8L, SeekOrigin.Begin); //pass the super block
//byte[] b = new byte[dbConfig.getTotalHeaderSize()]; byte[] b = new byte[4096];
byte[] b = new byte[4096]; _raf.Read(b, 0, b.Length);
_raf.Read(b, 0, b.Length); //fill the header
int len = b.Length >> 3, idx = 0; //b.lenght / 8
//fill the header _headerSip = new long[len];
int len = b.Length >> 3, idx = 0; //b.lenght / 8 _headerPtr = new int[len];
_headerSip = new long[len]; long startIp, dataPtrTemp;
_headerPtr = new int[len]; for (int i = 0; i < b.Length; i += 8)
long startIp, dataPtrTemp; {
for (int i = 0; i < b.Length; i += 8) startIp = Utils.GetIntLong(b, i);
{ dataPtrTemp = Utils.GetIntLong(b, i + 4);
startIp = Utils.getIntLong(b, i); if (dataPtrTemp == 0) break;
dataPtrTemp = Utils.getIntLong(b, i + 4);
if (dataPtrTemp == 0) break; _headerSip[idx] = startIp;
_headerPtr[idx] = (int)dataPtrTemp;
_headerSip[idx] = startIp; idx++;
_headerPtr[idx] = (int)dataPtrTemp; }
idx++; _headerLength = idx;
} }
_headerLength = idx; //1. define the index block with the binary search
} if (ip == _headerSip[0])
{
//1. define the index block with the binary search return GetByIndexPtr(_headerPtr[0]);
if (ip == _headerSip[0]) }
{ else if (ip == _headerPtr[_headerLength - 1])
return GetByIndexPtr(_headerPtr[0]); {
} return GetByIndexPtr(_headerPtr[_headerLength - 1]);
else if (ip == _headerPtr[_headerLength - 1]) }
{ int l = 0, h = _headerLength, sptr = 0, eptr = 0;
return GetByIndexPtr(_headerPtr[_headerLength - 1]); int m = 0;
} while (l <= h)
{
int l = 0, h = _headerLength, sptr = 0, eptr = 0; m = (l + h) >> 1;
int m = 0; //perfectly matched, just return it
if (ip == _headerSip[m])
while (l <= h) {
{ if (m > 0)
m = (l + h) >> 1; {
sptr = _headerPtr[m - 1];
//perfectly matched, just return it eptr = _headerPtr[m];
if (ip == _headerSip[m]) }
{ else
if (m > 0) {
{ sptr = _headerPtr[m];
sptr = _headerPtr[m - 1]; eptr = _headerPtr[m + 1];
eptr = _headerPtr[m]; }
} }
else //less then the middle value
{ else if (ip < _headerSip[m])
sptr = _headerPtr[m]; {
eptr = _headerPtr[m + 1]; if (m == 0)
} {
} sptr = _headerPtr[m];
//less then the middle value eptr = _headerPtr[m + 1];
else if (ip < _headerSip[m]) break;
{ }
if (m == 0) else if (ip > _headerSip[m - 1])
{ {
sptr = _headerPtr[m]; sptr = _headerPtr[m - 1];
eptr = _headerPtr[m + 1]; eptr = _headerPtr[m];
break; break;
} }
else if (ip > _headerSip[m - 1]) h = m - 1;
{ }
sptr = _headerPtr[m - 1]; else
eptr = _headerPtr[m]; {
break; if (m == _headerLength - 1)
} {
h = m - 1; sptr = _headerPtr[m - 1];
} eptr = _headerPtr[m];
else break;
{ }
if (m == _headerLength - 1) else if (ip <= _headerSip[m + 1])
{ {
sptr = _headerPtr[m - 1]; sptr = _headerPtr[m];
eptr = _headerPtr[m]; eptr = _headerPtr[m + 1];
break; break;
} }
else if (ip <= _headerSip[m + 1]) l = m + 1;
{ }
sptr = _headerPtr[m]; }
eptr = _headerPtr[m + 1]; //match nothing just stop it
break; if (sptr == 0) return null;
} //2. search the index blocks to define the data
l = m + 1; int blockLen = eptr - sptr, blen = IndexBlock.LENGTH;
} byte[] iBuffer = new byte[blockLen + blen]; //include the right border block
} _raf.Seek(sptr, SeekOrigin.Begin);
_raf.Read(iBuffer, 0, iBuffer.Length);
//match nothing just stop it l = 0; h = blockLen / blen;
if (sptr == 0) return null; long sip = 0;
int p = 0;
//2. search the index blocks to define the data while (l <= h)
int blockLen = eptr - sptr, blen = IndexBlock.LENGTH; {
byte[] iBuffer = new byte[blockLen + blen]; //include the right border block m = (l + h) >> 1;
_raf.Seek(sptr, SeekOrigin.Begin); p = m * blen;
_raf.Read(iBuffer, 0, iBuffer.Length); sip = Utils.GetIntLong(iBuffer, p);
if (ip < sip)
l = 0; h = blockLen / blen; {
long sip = 0; h = m - 1;
int p = 0; }
else
while (l <= h) {
{ sip = Utils.GetIntLong(iBuffer, p + 4);
m = (l + h) >> 1; if (ip > sip)
p = m * blen; {
sip = Utils.getIntLong(iBuffer, p); l = m + 1;
if (ip < sip) }
{ else
h = m - 1; {
} sip = Utils.GetIntLong(iBuffer, p + 8);
else break;
{ }
sip = Utils.getIntLong(iBuffer, p + 4); }
if (ip > sip) }
{ //not matched
l = m + 1; if (sip == 0) return null;
} //3. get the data
else int dataLen = (int)((sip >> 24) & 0xFF);
{ int dataPtr = (int)((sip & 0x00FFFFFF));
sip = Utils.getIntLong(iBuffer, p + 8); _raf.Seek(dataPtr, SeekOrigin.Begin);
break; byte[] data = new byte[dataLen];
} _raf.Read(data, 0, data.Length);
} int city_id = (int)Utils.GetIntLong(data, 0);
} String region = Encoding.UTF8.GetString(data, 4, data.Length - 4);// new String(data, 4, data.Length - 4, "UTF-8");
return new DataBlock(city_id, region, dataPtr);
//not matched }
if (sip == 0) return null;
//3. get the data
int dataLen = (int)((sip >> 24) & 0xFF);
int dataPtr = (int)((sip & 0x00FFFFFF));
_raf.Seek(dataPtr, SeekOrigin.Begin);
byte[] data = new byte[dataLen];
_raf.Read(data, 0, data.Length);
int city_id = (int)Utils.getIntLong(data, 0);
String region = Encoding.UTF8.GetString(data, 4, data.Length - 4);// new String(data, 4, data.Length - 4, "UTF-8");
return new DataBlock(city_id, region, dataPtr);
}
/// <summary> /// <summary>
/// Get the region throught the ip address with b-tree search algorithm. /// Get the region throught the ip address with b-tree search algorithm.
/// </summary> /// </summary>
public DataBlock BtreeSearch(string ip) public DataBlock BtreeSearch(string ip)
{ {
return BtreeSearch(Utils.ip2long(ip)); return BtreeSearch(Utils.Ip2long(ip));
} }
/// <summary> /// <summary>
/// Get the region with a int ip address with binary search algorithm. /// Get the region with a int ip address with binary search algorithm.
/// </summary> /// </summary>
private DataBlock BinarySearch(long ip) private DataBlock BinarySearch(long ip)
{ {
int blen = IndexBlock.LENGTH; int blen = IndexBlock.LENGTH;
if (_totalIndexBlocks == 0) if (_totalIndexBlocks == 0)
{ {
_raf.Seek(0L, SeekOrigin.Begin); _raf.Seek(0L, SeekOrigin.Begin);
byte[] superBytes = new byte[8]; byte[] superBytes = new byte[8];
_raf.Read(superBytes, 0, superBytes.Length); _raf.Read(superBytes, 0, superBytes.Length);
//initialize the global vars //initialize the global vars
_firstIndexPtr = Utils.getIntLong(superBytes, 0); _firstIndexPtr = Utils.GetIntLong(superBytes, 0);
_lastIndexPtr = Utils.getIntLong(superBytes, 4); _lastIndexPtr = Utils.GetIntLong(superBytes, 4);
_totalIndexBlocks = (int)((_lastIndexPtr - _firstIndexPtr) / blen) + 1; _totalIndexBlocks = (int)((_lastIndexPtr - _firstIndexPtr) / blen) + 1;
} }
//search the index blocks to define the data
int l = 0, h = _totalIndexBlocks;
byte[] buffer = new byte[blen];
long sip = 0;
while (l <= h)
{
int m = (l + h) >> 1;
_raf.Seek(_firstIndexPtr + m * blen, SeekOrigin.Begin); //set the file pointer
_raf.Read(buffer, 0, buffer.Length);
sip = Utils.getIntLong(buffer, 0);
if (ip < sip)
{
h = m - 1;
}
else
{
sip = Utils.getIntLong(buffer, 4);
if (ip > sip)
{
l = m + 1;
}
else
{
sip = Utils.getIntLong(buffer, 8);
break;
}
}
}
//not matched
if (sip == 0) return null;
//get the data
int dataLen = (int)((sip >> 24) & 0xFF);
int dataPtr = (int)((sip & 0x00FFFFFF));
_raf.Seek(dataPtr, SeekOrigin.Begin);
byte[] data = new byte[dataLen];
_raf.Read(data, 0, data.Length);
int city_id = (int)Utils.getIntLong(data, 0); //search the index blocks to define the data
String region = Encoding.UTF8.GetString(data, 4, data.Length - 4);//new String(data, 4, data.Length - 4, "UTF-8"); int l = 0, h = _totalIndexBlocks;
byte[] buffer = new byte[blen];
return new DataBlock(city_id, region, dataPtr); long sip = 0;
while (l <= h)
{
int m = (l + h) >> 1;
_raf.Seek(_firstIndexPtr + m * blen, SeekOrigin.Begin); //set the file pointer
_raf.Read(buffer, 0, buffer.Length);
sip = Utils.GetIntLong(buffer, 0);
if (ip < sip)
{
h = m - 1;
}
else
{
sip = Utils.GetIntLong(buffer, 4);
if (ip > sip)
{
l = m + 1;
}
else
{
sip = Utils.GetIntLong(buffer, 8);
break;
}
}
}
//not matched
if (sip == 0) return null;
//get the data
int dataLen = (int)((sip >> 24) & 0xFF);
int dataPtr = (int)((sip & 0x00FFFFFF));
_raf.Seek(dataPtr, SeekOrigin.Begin);
byte[] data = new byte[dataLen];
_raf.Read(data, 0, data.Length);
int city_id = (int)Utils.GetIntLong(data, 0);
String region = Encoding.UTF8.GetString(data, 4, data.Length - 4);//new String(data, 4, data.Length - 4, "UTF-8");
return new DataBlock(city_id, region, dataPtr);
} }
/// <summary> /// <summary>
/// Get the region throught the ip address with binary search algorithm. /// Get the region throught the ip address with binary search algorithm.
/// </summary> /// </summary>
public DataBlock BinarySearch(String ip) public DataBlock BinarySearch(String ip)
{ {
return BinarySearch(Utils.ip2long(ip)); return BinarySearch(Utils.Ip2long(ip));
} }
#endregion #endregion
#region Async Methods #region Async Methods
/// <summary> /// <summary>
/// Get the region throught the ip address with memory binary search algorithm. /// Get the region throught the ip address with memory binary search algorithm.
/// </summary> /// </summary>
public async Task<DataBlock> AsyncMemorySearch(string ip) public async Task<DataBlock> MemorySearchAsync(string ip)
{ {
return await Task.FromResult(MemorySearch(ip)); return await Task.FromResult(MemorySearch(ip));
} }
/// <summary> /// <summary>
/// Get the region throught the ip address with b-tree search algorithm. /// Get the region throught the ip address with b-tree search algorithm.
/// </summary> /// </summary>
public async Task<DataBlock> AsyncBtreeSearch(string ip) public async Task<DataBlock> BtreeSearchAsync(string ip)
{ {
return await Task.FromResult(BtreeSearch(ip)); return await Task.FromResult(BtreeSearch(ip));
} }
/// <summary> /// <summary>
/// Get the region throught the ip address with binary search algorithm. /// Get the region throught the ip address with binary search algorithm.
/// </summary> /// </summary>
public async Task<DataBlock> AsyncBinarySearch(string ip) public async Task<DataBlock> BinarySearchAsync(string ip)
{ {
return await Task.FromResult(BinarySearch(ip)); return await Task.FromResult(BinarySearch(ip));
} }
#endregion #endregion
/// <summary> /// <summary>
/// Close the db. /// Close the db.
/// </summary> /// </summary>
public void Close() public void Close()
{ {
_headerSip = null; //let gc do its work _headerSip = null;
_headerPtr = null; _headerPtr = null;
_dbBinStr = null; _dbBinStr = null;
_raf.Close(); _raf.Close();
} }
public void Dispose() public void Dispose()
{ {
Close(); Close();
} }
} }
} }

View File

@ -1,26 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFrameworks>netstandard2.0;net46</TargetFrameworks> <TargetFrameworks>netstandard2.0;net46;net45;net47</TargetFrameworks>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Rocher</Authors> <Authors>Rocher</Authors>
<Company>Rocher</Company> <Company>Rocher</Company>
<Description>准确率99.9%的ip地址定位库0.0x毫秒级查询, 支持Binary,B树,内存三种查询算法妈妈再也不用担心我的ip地址定位</Description> <Description>准确率99.9%的ip地址定位库0.0x毫秒级查询, 支持Binary,B树,内存三种查询算法妈妈再也不用担心我的ip地址定位</Description>
<PackageReleaseNotes>ip2region - 最自由的ip地址查询库ip到地区的映射库提供Binary,B树和纯内存三种查询算法妈妈再也不用担心我的ip地址定位。</PackageReleaseNotes> <PackageReleaseNotes>ip2region - 最自由的ip地址查询库ip到地区的映射库提供Binary,B树和纯内存三种查询算法妈妈再也不用担心我的ip地址定位。</PackageReleaseNotes>
<Copyright>Rocher</Copyright> <Copyright>Rocher</Copyright>
<PackageTags>ip2region c# IP</PackageTags> <PackageTags>ip2region c# IP</PackageTags>
<RepositoryUrl>https://gitee.com/rocherkong/IP2Region</RepositoryUrl> <RepositoryUrl>https://gitee.com/rocherkong/IP2Region</RepositoryUrl>
<Version>1.1.0</Version> <Version>1.2.0</Version>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|netstandard2.0|AnyCPU'"> <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|netstandard2.0|AnyCPU'">
<LangVersion>7</LangVersion> <LangVersion>7</LangVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<None Update="DBFile\ip2region.db"> <None Update="DB\ip2region.db">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None> </None>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,55 +1,50 @@
//******************************* //*******************************
// Create By Rocher Kong // Created By Rocher Kong
// Github https://github.com/RocherKong // Github https://github.com/RocherKong
// Date 2018.02.09 // Date 2018.02.09
// //*******************************
// Modified By Dongwei namespace IP2Region.Models
// Date 2018.07.18 {
// GitHub https://github.com/Maledong public class DataBlock
//*******************************
namespace IP2Region.Models
{
public class DataBlock
{ {
#region Private Properties #region Private Properties
public int CityID public int CityID
{ {
get; get;
private set; private set;
} }
public string Region public string Region
{ {
get; get;
private set; private set;
} }
public int DataPtr public int DataPtr
{ {
get; get;
private set; private set;
} }
#endregion #endregion
#region Constructor #region Constructor
public DataBlock(int city_id, string region, int dataPtr = 0) public DataBlock(int city_id, string region, int dataPtr = 0)
{ {
CityID = city_id; CityID = city_id;
Region = region; Region = region;
DataPtr = dataPtr; DataPtr = dataPtr;
}
public DataBlock(int city_id, string region):this(city_id,region,0)
{
} }
#endregion
public DataBlock(int city_id, string region):this(city_id,region,0)
public override string ToString() {
{ }
return $"{CityID}|{Region}|{DataPtr}"; #endregion
}
public override string ToString()
} {
return $"{CityID}|{Region}|{DataPtr}";
} }
}
}

View File

@ -1,54 +1,49 @@
//******************************* //*******************************
// Create By Rocher Kong // Created By Rocher Kong
// Github https://github.com/RocherKong // Github https://github.com/RocherKong
// Date 2018.02.09 // Date 2018.02.09
// //*******************************
// Modified By Dongwei
// Date 2018.07.18
// GitHub https://github.com/Maledong
//*******************************
using System; using System;
namespace IP2Region.Models namespace IP2Region.Models
{ {
public class DbMakerConfigException : Exception public class DbMakerConfigException : Exception
{ {
public string ErrMsg { get; private set; } public string ErrMsg { get; private set; }
public DbMakerConfigException(string errMsg) public DbMakerConfigException(string errMsg)
{ {
ErrMsg = errMsg; ErrMsg = errMsg;
} }
} }
public class DbConfig public class DbConfig
{ {
public int TotalHeaderSize public int TotalHeaderSize
{ {
get; get;
private set; private set;
} }
public int indexBlockSize public int indexBlockSize
{ {
get; get;
private set; private set;
} }
public DbConfig(int totalHeaderSize) public DbConfig(int totalHeaderSize)
{ {
if ((totalHeaderSize % 8) != 0) if ((totalHeaderSize % 8) != 0)
{ {
throw new DbMakerConfigException("totalHeaderSize must be times of 8"); throw new DbMakerConfigException("totalHeaderSize must be times of 8");
} }
TotalHeaderSize = totalHeaderSize; TotalHeaderSize = totalHeaderSize;
//4 * 2048 //4 * 2048
indexBlockSize = 8192; indexBlockSize = 8192;
} }
public DbConfig():this(8 * 2048) public DbConfig():this(8 * 2048)
{ {
} }
} }
} }

View File

@ -1,56 +1,49 @@
//******************************* //*******************************
// Create By Rocher Kong // Created By Rocher Kong
// Github https://github.com/RocherKong // Github https://github.com/RocherKong
// Date 2018.02.09 // Date 2018.02.09
// //*******************************
// Modified By Dongwei
// Date 2018.07.18 namespace IP2Region.Models
// GitHub https://github.com/Maledong {
//******************************* internal class HeaderBlock
{
namespace IP2Region.Models
{
internal class HeaderBlock
{
public long IndexStartIp public long IndexStartIp
{ {
get; get;
private set; private set;
} }
public int IndexPtr public int IndexPtr
{ {
get; get;
private set; private set;
} }
public HeaderBlock(long indexStartIp, int indexPtr) public HeaderBlock(long indexStartIp, int indexPtr)
{ {
IndexStartIp = indexStartIp; IndexStartIp = indexStartIp;
IndexPtr = indexPtr; IndexPtr = indexPtr;
} }
/// <summary> /// <summary>
/// Get the bytes for total storage /// Get the bytes for total storage
/// </summary> /// </summary>
/// <returns> /// <returns>
/// Bytes gotten. /// Bytes gotten.
/// </returns> /// </returns>
public byte[] GetBytes() public byte[] GetBytes()
{ {
/* /*
* +------------+-----------+ * +------------+-----------+
* | 4bytes | 4bytes | * | 4bytes | 4bytes |
* +------------+-----------+ * +------------+-----------+
* start ip index ptr * start ip index ptr
*/ */
byte[] b = new byte[8]; byte[] b = new byte[8];
Utils.WriteIntLong(b, 0, IndexStartIp);
Utils.writeIntLong(b, 0, IndexStartIp); Utils.WriteIntLong(b, 4, IndexPtr);
Utils.writeIntLong(b, 4, IndexPtr); return b;
}
return b; }
}
}
} }

View File

@ -1,69 +1,65 @@
//******************************* //*******************************
// Create By Rocher Kong // Created By Rocher Kong
// Github https://github.com/RocherKong // Github https://github.com/RocherKong
// Date 2018.02.09 // Date 2018.02.09
// //*******************************
// Modified By Dongwei
// Date 2018.07.18 namespace IP2Region
// GitHub https://github.com/Maledong {
//******************************* internal class IndexBlock
{
namespace IP2Region public const int LENGTH = 12;
{
internal class IndexBlock
{
public const int LENGTH = 12;
public long StartIP public long StartIP
{ {
get; get;
private set; private set;
} }
public long EndIp public long EndIp
{ {
get; get;
private set; private set;
} }
public uint DataPtr public uint DataPtr
{ {
get; get;
private set; private set;
} }
public int DataLen public int DataLen
{ {
get; get;
private set; private set;
} }
public IndexBlock(long startIp, long endIp, uint dataPtr, int dataLen) public IndexBlock(long startIp, long endIp, uint dataPtr, int dataLen)
{ {
StartIP = startIp; StartIP = startIp;
EndIp = endIp; EndIp = endIp;
DataPtr = dataPtr; DataPtr = dataPtr;
DataLen = dataLen; DataLen = dataLen;
} }
public byte[] GetBytes() public byte[] GetBytes()
{ {
/* /*
* +------------+-----------+-----------+ * +------------+-----------+-----------+
* | 4bytes | 4bytes | 4bytes | * | 4bytes | 4bytes | 4bytes |
* +------------+-----------+-----------+ * +------------+-----------+-----------+
* start ip end ip data ptr + len * start ip end ip data ptr + len
*/ */
byte[] b = new byte[12]; byte[] b = new byte[12];
Utils.writeIntLong(b, 0, StartIP); //start ip Utils.WriteIntLong(b, 0, StartIP); //start ip
Utils.writeIntLong(b, 4, EndIp); //end ip Utils.WriteIntLong(b, 4, EndIp); //end ip
//write the data ptr and the length //write the data ptr and the length
long mix = DataPtr | ((DataLen << 24) & 0xFF000000L); long mix = DataPtr | ((DataLen << 24) & 0xFF000000L);
Utils.writeIntLong(b, 8, mix); Utils.WriteIntLong(b, 8, mix);
return b; return b;
} }
} }
} }

View File

@ -1,130 +1,122 @@
//******************************* //*******************************
// Create By Rocher Kong // Created By Rocher Kong
// Github https://github.com/RocherKong // Github https://github.com/RocherKong
// Date 2018.02.09 // Date 2018.02.09
// //*******************************
// Modified By Dongwei
// Date 2018.07.18 using System;
// GitHub https://github.com/Maledong
//******************************* namespace IP2Region
using System; {
namespace IP2Region
{
public class IPInValidException : Exception public class IPInValidException : Exception
{ {
const string ERROR_MSG = "IP Illigel. Please input a valid IP."; const string ERROR_MSG = "IP Illigel. Please input a valid IP.";
public IPInValidException() : base(ERROR_MSG) { } public IPInValidException() : base(ERROR_MSG) { }
} }
internal static class Utils internal static class Utils
{ {
/// <summary> /// <summary>
/// Write specfield bytes to a byte array start from offset. /// Write specfield bytes to a byte array start from offset.
/// </summary> /// </summary>
public static void Write(byte[] b, int offset, ulong v, int bytes) public static void Write(byte[] b, int offset, ulong v, int bytes)
{ {
for (int i = 0; i < bytes; i++) for (int i = 0; i < bytes; i++)
{ {
b[offset++] = (byte)((v >> (8 * i)) & 0xFF); b[offset++] = (byte)((v >> (8 * i)) & 0xFF);
} }
} }
/// <summary> /// <summary>
/// Write a int to a byte array. /// Write a int to a byte array.
/// </summary> /// </summary>
public static void writeIntLong(byte[] b, int offset, long v) public static void WriteIntLong(byte[] b, int offset, long v)
{ {
b[offset++] = (byte)((v >> 0) & 0xFF); b[offset++] = (byte)((v >> 0) & 0xFF);
b[offset++] = (byte)((v >> 8) & 0xFF); b[offset++] = (byte)((v >> 8) & 0xFF);
b[offset++] = (byte)((v >> 16) & 0xFF); b[offset++] = (byte)((v >> 16) & 0xFF);
b[offset] = (byte)((v >> 24) & 0xFF); b[offset] = (byte)((v >> 24) & 0xFF);
} }
/// <summary> /// <summary>
/// Get a int from a byte array start from the specifiled offset. /// Get a int from a byte array start from the specifiled offset.
/// </summary> /// </summary>
public static long getIntLong(byte[] b, int offset) public static long GetIntLong(byte[] b, int offset)
{ {
return ( return (
((b[offset++] & 0x000000FFL)) | ((b[offset++] & 0x000000FFL)) |
((b[offset++] << 8) & 0x0000FF00L) | ((b[offset++] << 8) & 0x0000FF00L) |
((b[offset++] << 16) & 0x00FF0000L) | ((b[offset++] << 16) & 0x00FF0000L) |
((b[offset] << 24) & 0xFF000000L) ((b[offset] << 24) & 0xFF000000L)
); );
} }
/// <summary> /// <summary>
/// Get a int from a byte array start from the specifield offset. /// Get a int from a byte array start from the specifield offset.
/// </summary> /// </summary>
public static int getInt3(byte[] b, int offset) public static int GetInt3(byte[] b, int offset)
{ {
return ( return (
(b[offset++] & 0x000000FF) | (b[offset++] & 0x000000FF) |
(b[offset++] & 0x0000FF00) | (b[offset++] & 0x0000FF00) |
(b[offset] & 0x00FF0000) (b[offset] & 0x00FF0000)
); );
} }
public static int getInt2(byte[] b, int offset) public static int GetInt2(byte[] b, int offset)
{ {
return ( return (
(b[offset++] & 0x000000FF) | (b[offset++] & 0x000000FF) |
(b[offset] & 0x0000FF00) (b[offset] & 0x0000FF00)
); );
} }
public static int getInt1(byte[] b, int offset) public static int GetInt1(byte[] b, int offset)
{ {
return ( return (
(b[offset] & 0x000000FF) (b[offset] & 0x000000FF)
); );
} }
/// <summary> /// <summary>
/// String ip to long ip. /// String ip to long ip.
/// </summary> /// </summary>
public static long ip2long(string ip) public static long Ip2long(string ip)
{ {
string[] p = ip.Split('.'); string[] p = ip.Split('.');
if (p.Length != 4) throw new IPInValidException(); if (p.Length != 4) throw new IPInValidException();
foreach (string pp in p) foreach (string pp in p)
{ {
if (pp.Length > 3) throw new IPInValidException(); if (pp.Length > 3) throw new IPInValidException();
if (!int.TryParse(pp, out int value) || value > 255) if (!int.TryParse(pp, out int value) || value > 255)
{ {
throw new IPInValidException(); throw new IPInValidException();
} }
} }
var bip1 = long.TryParse(p[0], out long ip1);
var bip1 = long.TryParse(p[0], out long ip1); var bip2 = long.TryParse(p[1], out long ip2);
var bip2 = long.TryParse(p[1], out long ip2); var bip3 = long.TryParse(p[2], out long ip3);
var bip3 = long.TryParse(p[2], out long ip3); var bip4 = long.TryParse(p[3], out long ip4);
var bip4 = long.TryParse(p[3], out long ip4);
if (!bip1 || !bip2 || !bip3 || !bip4
if (!bip1 || !bip2 || !bip3 || !bip4 || ip4 > 255 || ip1 > 255 || ip2 > 255 || ip3 > 255
|| ip4 > 255 || ip1 > 255 || ip2 > 255 || ip3 > 255 || ip4 < 0 || ip1 < 0 || ip2 < 0 || ip3 < 0)
|| ip4 < 0 || ip1 < 0 || ip2 < 0 || ip3 < 0) {
{ throw new IPInValidException();
throw new IPInValidException();
} }
long p1 = ((ip1 << 24) & 0xFF000000);
long p2 = ((ip2 << 16) & 0x00FF0000);
long p1 = ((ip1 << 24) & 0xFF000000); long p3 = ((ip3 << 8) & 0x0000FF00);
long p2 = ((ip2 << 16) & 0x00FF0000); long p4 = ((ip4 << 0) & 0x000000FF);
long p3 = ((ip3 << 8) & 0x0000FF00); return ((p1 | p2 | p3 | p4) & 0xFFFFFFFFL);
long p4 = ((ip4 << 0) & 0x000000FF);
return ((p1 | p2 | p3 | p4) & 0xFFFFFFFFL);
} }
/// <summary> /// <summary>
/// Int to ip string. /// Int to ip string.
/// </summary> /// </summary>
public static string long2ip(long ip) public static string Long2ip(long ip)
{ {
return $"{(ip >> 24) & 0xFF}.{(ip >> 16) & 0xFF}.{(ip >> 8) & 0xFF}.{ip & 0xFF}"; return $"{(ip >> 24) & 0xFF}.{(ip >> 16) & 0xFF}.{(ip >> 8) & 0xFF}.{ip & 0xFF}";
} }
} }
} }

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>

Binary file not shown.

View File

@ -0,0 +1,60 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{2D73D7EB-AE36-4039-A839-DA40209052A4}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>IP2Region_NetFx_Test</RootNamespace>
<AssemblyName>IP2Region_NetFx_Test</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="DB\ip2region.db">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\IP2Region\IP2Region.csproj">
<Project>{a27d09f4-4da8-4925-90ec-43e5f5288630}</Project>
<Name>IP2Region</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -0,0 +1,19 @@
using IP2Region;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IP2Region_NetFx_Test
{
class Program
{
static void Main(string[] args)
{
DbSearcher _search = new DbSearcher(Environment.CurrentDirectory + @"\DB\ip2region.db");
Console.WriteLine(_search.MemorySearch("183.192.62.65").Region);
Console.Read();
}
}
}

View File

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// 有关程序集的一般信息由以下
// 控制。更改这些特性值可修改
// 与程序集关联的信息。
[assembly: AssemblyTitle("IP2Region_NetFx_Test")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("IP2Region_NetFx_Test")]
[assembly: AssemblyCopyright("Copyright © 2018")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// 将 ComVisible 设置为 false 会使此程序集中的类型
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
//请将此类型的 ComVisible 特性设置为 true。
[assembly: ComVisible(false)]
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
[assembly: Guid("2d73d7eb-ae36-4039-a839-da40209052a4")]
// 程序集的版本信息由下列四个值组成:
//
// 主版本
// 次版本
// 生成号
// 修订号
//
// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
// 方法是按如下所示使用“*”: :
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@ -1,18 +1,38 @@
# C# 客户端 # IP2Region C# Client
## 实现情况: ## How To Use
### 1.Install from Nuget. Support .Net Framework 4.5+ And netstandard 2.0(.net core)
```powershell
Install-Package IP2Region
```
### 2.Init DbSearcher with Newest DBFile Downloaded into your project.(https://github.com/lionsoul2014/ip2region/blob/master/data/ip2region.db)
```c#
DbSearcher _search=new DbSearcher(Environment.CurrentDirectory + @"\DB\ip2region.db");
```
### 3.Invoke Search Method.(MemorySearch,BtreeSearch,BinarySearch)
```c#
_search.MemorySearch("183.192.62.65").Region;
_search.MemorySearchAsync("183.192.62.65").Result.Region;
_search.BinarySearch("183.192.62.65").Region;
_search.BinarySearchAsync("183.192.62.65").Result.Region;
_search.BtreeSearch("183.192.62.65").Region;
_search.BtreeSearchAsync("183.192.62.65").Result.Region;
```
现已实现同步和异步查询,具体使用方法可以参考 UnitTests 中的使用方法。 ## Test Result(From /IP2Region.Test.Benchmark)
Method | Mean | Error | StdDev | Rank |
------------------- |---------: |----------: |----------: |-----: |
MemorySearch | 10.66 us | 0.1424 us | 0.1332 us | 1 |
MemorySearch_Async | 10.90 us | 0.2060 us | 0.2023 us | 2 |
BinarySearch | 52.22 us | 0.6403 us | 0.5347 us | 5 |
BinarySearch_Async | 53.03 us | 1.0271 us | 0.9608 us | 6 |
BtreeSearch | 19.05 us | 0.2464 us | 0.2305 us | 3 |
BtreeSearch_Async | 19.40 us | 0.3820 us | 0.6690 us | 4 |
## 如何贡献? ## Contribute History
| Name | Github | Responsibility | Date | Remark |
| ------ | ------ | ------ | ------ | ------ |
| RocherKong | https://github.com/RocherKong | Creator | 20180209|
| Dongwei | https://github.com/Maledong | Contributor | 20180708 | 1.Async 2.NetFxBenchmark 3.Rename of some Methods
| RocherKong | https://github.com/RocherKong | Creator | 20180209| 1.CodeStandardized 2.Support Netfx4.5 3.TestStandardized
你可以任意修改代码,但必须确保通过全部的单元测试。
在此之前,请保证你已经安装了 Net4.6 和 Net Core 2.0 版本。
操作步骤:
1使用 VS2017推荐可以到 https://visualstudio.microsoft.com/zh-hans/downloads/ 下载。),或者其它相关版本打开 IP2Region 项目。
2使用 VS 打开IP2Region 项目。
3保存 sln 文件到 c# 文件夹下(该文件应该自动被忽略)。
4右键点击“解决方案IP2Region”添加 UnitTests 和 BenchmarkTest 项目。
5请右键点击整个解决方案选择“重新生成解决方案”。
6菜单栏上“测试”=>“运行”=>“所有测试”,检测是否通过全部测试即可(你可以根据需要自行增加特定的单元测试)。