commit
9c69fc237d
|
|
@ -1,34 +0,0 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<configuration>
|
|
||||||
<startup>
|
|
||||||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
|
|
||||||
</startup>
|
|
||||||
<runtime>
|
|
||||||
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
|
|
||||||
<dependentAssembly>
|
|
||||||
<assemblyIdentity name="System.Collections.Immutable" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
|
|
||||||
<bindingRedirect oldVersion="0.0.0.0-1.2.1.0" newVersion="1.2.1.0" />
|
|
||||||
</dependentAssembly>
|
|
||||||
<dependentAssembly>
|
|
||||||
<assemblyIdentity name="System.IO.Compression" publicKeyToken="b77a5c561934e089" culture="neutral" />
|
|
||||||
<bindingRedirect oldVersion="0.0.0.0-4.1.2.0" newVersion="4.1.2.0" />
|
|
||||||
</dependentAssembly>
|
|
||||||
<dependentAssembly>
|
|
||||||
<assemblyIdentity name="System.IO.FileSystem" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
|
|
||||||
<bindingRedirect oldVersion="0.0.0.0-4.0.2.0" newVersion="4.0.2.0" />
|
|
||||||
</dependentAssembly>
|
|
||||||
<dependentAssembly>
|
|
||||||
<assemblyIdentity name="System.IO.FileSystem.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
|
|
||||||
<bindingRedirect oldVersion="0.0.0.0-4.0.2.0" newVersion="4.0.2.0" />
|
|
||||||
</dependentAssembly>
|
|
||||||
<dependentAssembly>
|
|
||||||
<assemblyIdentity name="System.Security.Cryptography.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
|
|
||||||
<bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.1.0" />
|
|
||||||
</dependentAssembly>
|
|
||||||
<dependentAssembly>
|
|
||||||
<assemblyIdentity name="System.Xml.XPath.XDocument" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
|
|
||||||
<bindingRedirect oldVersion="0.0.0.0-4.0.2.0" newVersion="4.0.2.0" />
|
|
||||||
</dependentAssembly>
|
|
||||||
</assemblyBinding>
|
|
||||||
</runtime>
|
|
||||||
</configuration>
|
|
||||||
|
|
@ -1,21 +0,0 @@
|
||||||
using BenchmarkDotNet.Attributes;
|
|
||||||
using IP2Region;
|
|
||||||
|
|
||||||
namespace BenchmarkTest.BenchTests
|
|
||||||
{
|
|
||||||
public class BenchTestBase
|
|
||||||
{
|
|
||||||
protected DbSearcher _dbSearcher = null;
|
|
||||||
|
|
||||||
[GlobalSetup]
|
|
||||||
public void Init()
|
|
||||||
{
|
|
||||||
_dbSearcher = new DbSearcher("../../../../../data/ip2region.db");
|
|
||||||
}
|
|
||||||
[GlobalCleanup]
|
|
||||||
public void Clearup()
|
|
||||||
{
|
|
||||||
_dbSearcher.Dispose();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,46 +0,0 @@
|
||||||
using BenchmarkDotNet.Attributes;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace BenchmarkTest.BenchTests
|
|
||||||
{
|
|
||||||
public class NormalBenchmarkTests : BenchTestBase
|
|
||||||
{
|
|
||||||
[Params("0.0.0.0", "210.109.255.230", "192.168.0.1", "255.255.255.255", "183.196.233.159",
|
|
||||||
"77.49.66.88", "210.248.255.231", "10.10.10.10", "197.84.60.202", "35.193.251.120",
|
|
||||||
"20.108.91.101", "120.196.148.137", "249.255.250.200", "112.65.1.130")]
|
|
||||||
public string validIp = null;
|
|
||||||
|
|
||||||
[Benchmark]
|
|
||||||
public void TestSyncSpeedForMemorySearch()
|
|
||||||
{
|
|
||||||
_dbSearcher.MemorySearch(validIp);
|
|
||||||
}
|
|
||||||
[Benchmark]
|
|
||||||
public async Task TestAsyncSpeedForMemorySearch()
|
|
||||||
{
|
|
||||||
await _dbSearcher.MemorySearchAsync(validIp);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Benchmark]
|
|
||||||
public void TestSyncSpeedForBinarySearch()
|
|
||||||
{
|
|
||||||
_dbSearcher.BinarySearch(validIp);
|
|
||||||
}
|
|
||||||
[Benchmark]
|
|
||||||
public async Task TestAsyncBinarySearch()
|
|
||||||
{
|
|
||||||
await _dbSearcher.BinarySearchAsync(validIp);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Benchmark]
|
|
||||||
public void TestSyncSpeedForBTreeSearch()
|
|
||||||
{
|
|
||||||
_dbSearcher.BtreeSearch(validIp);
|
|
||||||
}
|
|
||||||
[Benchmark]
|
|
||||||
public async Task TestAsyncSpeedForBTreeSearch()
|
|
||||||
{
|
|
||||||
await _dbSearcher.BtreeSearchAsync(validIp);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,154 +0,0 @@
|
||||||
<?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>{053EA51E-2246-4FCB-A106-9B53E70FDC3F}</ProjectGuid>
|
|
||||||
<OutputType>Exe</OutputType>
|
|
||||||
<RootNamespace>BenchmarkTest</RootNamespace>
|
|
||||||
<AssemblyName>BenchmarkTest</AssemblyName>
|
|
||||||
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
|
|
||||||
<FileAlignment>512</FileAlignment>
|
|
||||||
<TargetFrameworkProfile />
|
|
||||||
</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="BenchmarkDotNet, Version=0.10.14.0, Culture=neutral, PublicKeyToken=aa0ca2f9092cefc4, processorArchitecture=MSIL">
|
|
||||||
<HintPath>..\packages\BenchmarkDotNet.0.10.14\lib\net46\BenchmarkDotNet.dll</HintPath>
|
|
||||||
</Reference>
|
|
||||||
<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>
|
|
||||||
</Reference>
|
|
||||||
<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>
|
|
||||||
</Reference>
|
|
||||||
<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>
|
|
||||||
</Reference>
|
|
||||||
<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>
|
|
||||||
</Reference>
|
|
||||||
<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>
|
|
||||||
</Reference>
|
|
||||||
<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>
|
|
||||||
</Reference>
|
|
||||||
<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>
|
|
||||||
</Reference>
|
|
||||||
<Reference Include="System" />
|
|
||||||
<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>
|
|
||||||
</Reference>
|
|
||||||
<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>
|
|
||||||
</Reference>
|
|
||||||
<Reference Include="System.ComponentModel.Composition" />
|
|
||||||
<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>
|
|
||||||
</Reference>
|
|
||||||
<Reference Include="System.Core" />
|
|
||||||
<Reference Include="Microsoft.CSharp" />
|
|
||||||
<Reference Include="System.Data" />
|
|
||||||
<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>
|
|
||||||
</Reference>
|
|
||||||
<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>
|
|
||||||
</Reference>
|
|
||||||
<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>
|
|
||||||
</Reference>
|
|
||||||
<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>
|
|
||||||
</Reference>
|
|
||||||
<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>
|
|
||||||
</Reference>
|
|
||||||
<Reference Include="System.Management" />
|
|
||||||
<Reference Include="System.Net.Http" />
|
|
||||||
<Reference Include="System.Numerics" />
|
|
||||||
<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>
|
|
||||||
</Reference>
|
|
||||||
<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>
|
|
||||||
</Reference>
|
|
||||||
<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>
|
|
||||||
</Reference>
|
|
||||||
<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>
|
|
||||||
</Reference>
|
|
||||||
<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>
|
|
||||||
</Reference>
|
|
||||||
<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>
|
|
||||||
</Reference>
|
|
||||||
<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>
|
|
||||||
</Reference>
|
|
||||||
<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>
|
|
||||||
</Reference>
|
|
||||||
<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>
|
|
||||||
</Reference>
|
|
||||||
<Reference Include="System.Xml" />
|
|
||||||
<Reference Include="System.Xml.Linq" />
|
|
||||||
<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>
|
|
||||||
</Reference>
|
|
||||||
<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>
|
|
||||||
</Reference>
|
|
||||||
<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>
|
|
||||||
</Reference>
|
|
||||||
<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>
|
|
||||||
</Reference>
|
|
||||||
</ItemGroup>
|
|
||||||
<ItemGroup>
|
|
||||||
<Compile Include="BenchTests\BenchTestBase.cs" />
|
|
||||||
<Compile Include="BenchTests\NormalBenchmarkTests.cs" />
|
|
||||||
<Compile Include="Program.cs" />
|
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
|
||||||
</ItemGroup>
|
|
||||||
<ItemGroup>
|
|
||||||
<None Include="App.config" />
|
|
||||||
<None Include="packages.config" />
|
|
||||||
</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.CSharp.Analyzers.dll" />
|
|
||||||
</ItemGroup>
|
|
||||||
<ItemGroup>
|
|
||||||
<ProjectReference Include="..\IP2Region\IP2Region.csproj">
|
|
||||||
<Project>{4c6032ee-43a5-4d5c-88f8-c411069e49e0}</Project>
|
|
||||||
<Name>IP2Region</Name>
|
|
||||||
</ProjectReference>
|
|
||||||
</ItemGroup>
|
|
||||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
|
||||||
</Project>
|
|
||||||
|
|
@ -1,13 +0,0 @@
|
||||||
using BenchmarkDotNet.Running;
|
|
||||||
using BenchmarkTest.BenchTests;
|
|
||||||
|
|
||||||
namespace BenchmarkTest
|
|
||||||
{
|
|
||||||
public class Program
|
|
||||||
{
|
|
||||||
public static void Main(string[] args)
|
|
||||||
{
|
|
||||||
BenchmarkRunner.Run<NormalBenchmarkTests>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,35 +0,0 @@
|
||||||
using System.Reflection;
|
|
||||||
using System.Runtime.InteropServices;
|
|
||||||
|
|
||||||
// 有关程序集的一般信息由以下
|
|
||||||
// 控制。更改这些特性值可修改
|
|
||||||
// 与程序集关联的信息。
|
|
||||||
[assembly: AssemblyTitle("BenchmarkTest")]
|
|
||||||
[assembly: AssemblyDescription("")]
|
|
||||||
[assembly: AssemblyConfiguration("")]
|
|
||||||
[assembly: AssemblyCompany("")]
|
|
||||||
[assembly: AssemblyProduct("BenchmarkTest")]
|
|
||||||
[assembly: AssemblyCopyright("Copyright © 2018")]
|
|
||||||
[assembly: AssemblyTrademark("")]
|
|
||||||
[assembly: AssemblyCulture("")]
|
|
||||||
|
|
||||||
// 将 ComVisible 设置为 false 会使此程序集中的类型
|
|
||||||
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
|
|
||||||
//请将此类型的 ComVisible 特性设置为 true。
|
|
||||||
[assembly: ComVisible(false)]
|
|
||||||
|
|
||||||
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
|
|
||||||
[assembly: Guid("053ea51e-2246-4fcb-a106-9b53e70fdc3f")]
|
|
||||||
|
|
||||||
// 程序集的版本信息由下列四个值组成:
|
|
||||||
//
|
|
||||||
// 主版本
|
|
||||||
// 次版本
|
|
||||||
// 生成号
|
|
||||||
// 修订号
|
|
||||||
//
|
|
||||||
// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
|
|
||||||
// 方法是按如下所示使用“*”: :
|
|
||||||
// [assembly: AssemblyVersion("1.0.*")]
|
|
||||||
[assembly: AssemblyVersion("1.0.0.0")]
|
|
||||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
|
||||||
|
|
@ -1,54 +0,0 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<packages>
|
|
||||||
<package id="BenchmarkDotNet" version="0.10.14" targetFramework="net46" />
|
|
||||||
<package id="BenchmarkDotNet.Core" version="0.10.14" targetFramework="net46" />
|
|
||||||
<package id="BenchmarkDotNet.Toolchains.Roslyn" version="0.10.14" targetFramework="net46" />
|
|
||||||
<package id="Microsoft.CodeAnalysis.Analyzers" version="1.1.0" targetFramework="net46" />
|
|
||||||
<package id="Microsoft.CodeAnalysis.Common" version="2.6.1" targetFramework="net46" />
|
|
||||||
<package id="Microsoft.CodeAnalysis.CSharp" version="2.6.1" targetFramework="net46" />
|
|
||||||
<package id="Microsoft.DotNet.InternalAbstractions" version="1.0.0" targetFramework="net46" />
|
|
||||||
<package id="Microsoft.DotNet.PlatformAbstractions" version="1.1.1" targetFramework="net46" />
|
|
||||||
<package id="Microsoft.Win32.Registry" version="4.3.0" targetFramework="net46" />
|
|
||||||
<package id="System.AppContext" version="4.3.0" targetFramework="net46" />
|
|
||||||
<package id="System.Collections" version="4.3.0" targetFramework="net46" />
|
|
||||||
<package id="System.Collections.Concurrent" version="4.3.0" targetFramework="net46" />
|
|
||||||
<package id="System.Collections.Immutable" version="1.3.1" targetFramework="net46" />
|
|
||||||
<package id="System.Console" version="4.3.0" targetFramework="net46" />
|
|
||||||
<package id="System.Diagnostics.Debug" version="4.3.0" targetFramework="net46" />
|
|
||||||
<package id="System.Diagnostics.FileVersionInfo" version="4.3.0" targetFramework="net46" />
|
|
||||||
<package id="System.Diagnostics.StackTrace" version="4.3.0" targetFramework="net46" />
|
|
||||||
<package id="System.Diagnostics.Tools" version="4.3.0" targetFramework="net46" />
|
|
||||||
<package id="System.Dynamic.Runtime" version="4.3.0" targetFramework="net46" />
|
|
||||||
<package id="System.Globalization" version="4.3.0" targetFramework="net46" />
|
|
||||||
<package id="System.IO.Compression" version="4.3.0" targetFramework="net46" />
|
|
||||||
<package id="System.IO.FileSystem" version="4.3.0" targetFramework="net46" />
|
|
||||||
<package id="System.IO.FileSystem.Primitives" version="4.3.0" targetFramework="net46" />
|
|
||||||
<package id="System.Linq" version="4.3.0" targetFramework="net46" />
|
|
||||||
<package id="System.Linq.Expressions" version="4.3.0" targetFramework="net46" />
|
|
||||||
<package id="System.Reflection" version="4.3.0" targetFramework="net46" />
|
|
||||||
<package id="System.Reflection.Metadata" version="1.4.2" targetFramework="net46" />
|
|
||||||
<package id="System.Resources.ResourceManager" version="4.3.0" targetFramework="net46" />
|
|
||||||
<package id="System.Runtime" version="4.3.0" targetFramework="net46" />
|
|
||||||
<package id="System.Runtime.Extensions" version="4.3.0" targetFramework="net46" />
|
|
||||||
<package id="System.Runtime.InteropServices" version="4.3.0" targetFramework="net46" />
|
|
||||||
<package id="System.Runtime.Numerics" version="4.3.0" targetFramework="net46" />
|
|
||||||
<package id="System.Security.Cryptography.Algorithms" version="4.3.0" targetFramework="net46" />
|
|
||||||
<package id="System.Security.Cryptography.Encoding" version="4.3.0" targetFramework="net46" />
|
|
||||||
<package id="System.Security.Cryptography.Primitives" version="4.3.0" targetFramework="net46" />
|
|
||||||
<package id="System.Security.Cryptography.X509Certificates" version="4.3.0" targetFramework="net46" />
|
|
||||||
<package id="System.Text.Encoding" version="4.3.0" targetFramework="net46" />
|
|
||||||
<package id="System.Text.Encoding.CodePages" version="4.3.0" targetFramework="net46" />
|
|
||||||
<package id="System.Text.Encoding.Extensions" version="4.3.0" targetFramework="net46" />
|
|
||||||
<package id="System.Threading" version="4.3.0" targetFramework="net46" />
|
|
||||||
<package id="System.Threading.Tasks" version="4.3.0" targetFramework="net46" />
|
|
||||||
<package id="System.Threading.Tasks.Extensions" version="4.3.0" targetFramework="net46" />
|
|
||||||
<package id="System.Threading.Tasks.Parallel" version="4.3.0" targetFramework="net46" />
|
|
||||||
<package id="System.Threading.Thread" version="4.3.0" targetFramework="net46" />
|
|
||||||
<package id="System.ValueTuple" version="4.3.0" targetFramework="net46" />
|
|
||||||
<package id="System.Xml.ReaderWriter" version="4.3.0" targetFramework="net46" />
|
|
||||||
<package id="System.Xml.XDocument" version="4.3.0" targetFramework="net46" />
|
|
||||||
<package id="System.Xml.XmlDocument" version="4.3.0" targetFramework="net46" />
|
|
||||||
<package id="System.Xml.XmlSerializer" version="4.3.0" targetFramework="net46" />
|
|
||||||
<package id="System.Xml.XPath" version="4.3.0" targetFramework="net46" />
|
|
||||||
<package id="System.Xml.XPath.XDocument" version="4.3.0" targetFramework="net46" />
|
|
||||||
</packages>
|
|
||||||
|
|
@ -1,9 +1,5 @@
|
||||||
using BenchmarkDotNet.Attributes;
|
using BenchmarkDotNet.Attributes;
|
||||||
using BenchmarkDotNet.Order;
|
|
||||||
using IP2Region.Models;
|
using IP2Region.Models;
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace IP2Region.Test.Benchmark
|
namespace IP2Region.Test.Benchmark
|
||||||
|
|
@ -17,7 +13,6 @@ namespace IP2Region.Test.Benchmark
|
||||||
public DataBlock MemorySearch()
|
public DataBlock MemorySearch()
|
||||||
{
|
{
|
||||||
RandomIP = GetRandomIP();
|
RandomIP = GetRandomIP();
|
||||||
//Console.WriteLine(RandomIP);
|
|
||||||
return _search.MemorySearch(RandomIP);
|
return _search.MemorySearch(RandomIP);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ namespace IP2Region.Test.Benchmark
|
||||||
{
|
{
|
||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Welcome To IP2Regin!");
|
Console.WriteLine("Now starting benchmark test, please wait......");
|
||||||
var summary = BenchmarkRunner.Run<DbSearch_Test>();
|
var summary = BenchmarkRunner.Run<DbSearch_Test>();
|
||||||
Console.ReadLine();
|
Console.ReadLine();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,5 @@
|
||||||
using BenchmarkDotNet.Attributes;
|
using BenchmarkDotNet.Attributes;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace IP2Region.Test.Benchmark
|
namespace IP2Region.Test.Benchmark
|
||||||
{
|
{
|
||||||
|
|
@ -17,7 +15,7 @@ namespace IP2Region.Test.Benchmark
|
||||||
|
|
||||||
public TestBase(String DBFilePath)
|
public TestBase(String DBFilePath)
|
||||||
{
|
{
|
||||||
this._dBFilePath = DBFilePath;
|
_dBFilePath = DBFilePath;
|
||||||
}
|
}
|
||||||
|
|
||||||
[GlobalSetup]
|
[GlobalSetup]
|
||||||
|
|
@ -37,7 +35,7 @@ namespace IP2Region.Test.Benchmark
|
||||||
[GlobalCleanup]
|
[GlobalCleanup]
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
_search?.Dispose();
|
_search.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String GetRandomIP()
|
public String GetRandomIP()
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,51 @@
|
||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace IP2Region.Test.xUnit
|
||||||
|
{
|
||||||
|
public class SearchTest : IDisposable
|
||||||
|
{
|
||||||
|
private readonly DbSearcher _search;
|
||||||
|
|
||||||
|
public SearchTest()
|
||||||
|
{
|
||||||
|
_search = new DbSearcher(Environment.CurrentDirectory + @"\DB\ip2region.db");
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Search_Test()
|
||||||
|
{
|
||||||
|
string memResult = _search.MemorySearch("183.192.62.65").Region;
|
||||||
|
string binarySearchResult = _search.BinarySearch("183.192.62.65").Region;
|
||||||
|
string binaryTreeSearchResult = _search.BtreeSearch("183.192.62.65").Region;
|
||||||
|
|
||||||
|
Assert.NotNull(memResult);
|
||||||
|
Assert.NotNull(binarySearchResult);
|
||||||
|
Assert.NotNull(binaryTreeSearchResult);
|
||||||
|
|
||||||
|
Assert.Equal(memResult, binarySearchResult);
|
||||||
|
Assert.Equal(binaryTreeSearchResult, memResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task SearchAsync_Test()
|
||||||
|
{
|
||||||
|
// We don't need the synchronizeContext, so just set to false
|
||||||
|
var memResult = await _search.MemorySearchAsync("183.192.62.65").ConfigureAwait(false);
|
||||||
|
var binarySearchResult = await _search.BinarySearchAsync("183.192.62.65").ConfigureAwait(false);
|
||||||
|
var bTreeSearchResult = await _search.BtreeSearchAsync("183.192.62.65").ConfigureAwait(false);
|
||||||
|
|
||||||
|
Assert.NotNull(memResult.Region);
|
||||||
|
Assert.NotNull(binarySearchResult.Region);
|
||||||
|
Assert.NotNull(bTreeSearchResult.Region);
|
||||||
|
|
||||||
|
Assert.Equal(memResult.Region, binarySearchResult.Region);
|
||||||
|
Assert.Equal(bTreeSearchResult.Region, memResult.Region);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
_search.Dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,36 +0,0 @@
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -360,23 +360,23 @@ namespace IP2Region
|
||||||
/// <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> MemorySearchAsync(string ip)
|
public Task<DataBlock> MemorySearchAsync(string ip)
|
||||||
{
|
{
|
||||||
return await Task.FromResult(MemorySearch(ip));
|
return 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> BtreeSearchAsync(string ip)
|
public Task<DataBlock> BtreeSearchAsync(string ip)
|
||||||
{
|
{
|
||||||
return await Task.FromResult(BtreeSearch(ip));
|
return 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> BinarySearchAsync(string ip)
|
public Task<DataBlock> BinarySearchAsync(string ip)
|
||||||
{
|
{
|
||||||
return await Task.FromResult(BinarySearch(ip));
|
return Task.FromResult(BinarySearch(ip));
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
@ -397,4 +397,4 @@ namespace IP2Region
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,5 @@
|
||||||
using IP2Region;
|
using IP2Region;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace IP2Region_NetFx_Test
|
namespace IP2Region_NetFx_Test
|
||||||
{
|
{
|
||||||
|
|
@ -11,9 +7,11 @@ namespace IP2Region_NetFx_Test
|
||||||
{
|
{
|
||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
DbSearcher _search = new DbSearcher(Environment.CurrentDirectory + @"\DB\ip2region.db");
|
using (var _search = new DbSearcher(Environment.CurrentDirectory + @"\DB\ip2region.db"))
|
||||||
Console.WriteLine(_search.MemorySearch("183.192.62.65").Region);
|
{
|
||||||
Console.Read();
|
Console.WriteLine(_search.MemorySearch("183.192.62.65").Region);
|
||||||
|
Console.Read();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
// 有关程序集的一般信息由以下
|
// 有关程序集的一般信息由以下
|
||||||
|
|
|
||||||
|
|
@ -1,22 +1,30 @@
|
||||||
# IP2Region C# Client
|
# IP2Region C# Client
|
||||||
|
|
||||||
## How To Use
|
## How To Use
|
||||||
### 1.Install from Nuget. Support .Net Framework 4.5+ And netstandard 2.0(.net core)
|
### 1.Install from Nuget. Support .Net Framework (>=4.5) And netstandard 2.0(.net core)
|
||||||
```powershell
|
```powershell
|
||||||
Install-Package IP2Region
|
Install-Package IP2Region
|
||||||
```
|
```
|
||||||
### 2.Init DbSearcher with Newest DBFile Downloaded into your project.(https://github.com/lionsoul2014/ip2region/blob/master/data/ip2region.db)
|
### 2.Init DbSearcher with [the newest DBFile](https://github.com/lionsoul2014/ip2region/blob/master/data/ip2region.db) Downloaded into your project, and invoke your search methods as below:
|
||||||
```c#
|
```csharp
|
||||||
DbSearcher _search=new DbSearcher(Environment.CurrentDirectory + @"\DB\ip2region.db");
|
using (var _search = new DbSearcher(Environment.CurrentDirectory + @"\DB\ip2region.db"))
|
||||||
```
|
{
|
||||||
### 3.Invoke Search Method.(MemorySearch,BtreeSearch,BinarySearch)
|
|
||||||
```c#
|
|
||||||
_search.MemorySearch("183.192.62.65").Region;
|
_search.MemorySearch("183.192.62.65").Region;
|
||||||
_search.MemorySearchAsync("183.192.62.65").Result.Region;
|
|
||||||
_search.BinarySearch("183.192.62.65").Region;
|
_search.BinarySearch("183.192.62.65").Region;
|
||||||
_search.BinarySearchAsync("183.192.62.65").Result.Region;
|
|
||||||
_search.BtreeSearch("183.192.62.65").Region;
|
_search.BtreeSearch("183.192.62.65").Region;
|
||||||
_search.BtreeSearchAsync("183.192.62.65").Result.Region;
|
}
|
||||||
|
```
|
||||||
|
For async methods (Methods with the suffix "Async"), just put them with `async` and `await`.
|
||||||
|
If you don't need the `SynchronizedContext`, please use `ConfigureAwait(false)`.
|
||||||
|
For more about this, please read https://msdn.microsoft.com/en-us/magazine/jj991977.aspx.
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
public async Task SearchAsync_Test()
|
||||||
|
{
|
||||||
|
// We don't need the synchronizeContext, so just set to false.
|
||||||
|
// So as for BinarySearchAsync and BtreeSearchAsync
|
||||||
|
var memResult = await _search.MemorySearchAsync("183.192.62.65").ConfigureAwait(false);
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Test Result(From /IP2Region.Test.Benchmark)
|
## Test Result(From /IP2Region.Test.Benchmark)
|
||||||
|
|
@ -28,11 +36,3 @@ BinarySearch | 52.22 us | 0.6403 us | 0.5347 us | 5 |
|
||||||
BinarySearch_Async | 53.03 us | 1.0271 us | 0.9608 us | 6 |
|
BinarySearch_Async | 53.03 us | 1.0271 us | 0.9608 us | 6 |
|
||||||
BtreeSearch | 19.05 us | 0.2464 us | 0.2305 us | 3 |
|
BtreeSearch | 19.05 us | 0.2464 us | 0.2305 us | 3 |
|
||||||
BtreeSearch_Async | 19.40 us | 0.3820 us | 0.6690 us | 4 |
|
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
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,50 +0,0 @@
|
||||||
using IP2Region;
|
|
||||||
using IP2Region.Models;
|
|
||||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
||||||
|
|
||||||
namespace UnitTests
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// This test class is mainly used to test your exception types.
|
|
||||||
/// It should throw exceptions of differnt kinds of types on certain occations.
|
|
||||||
/// </summary>
|
|
||||||
[TestClass]
|
|
||||||
public class ExceptionTest
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Path error should throw "DbMakerConfigException"
|
|
||||||
/// </summary>
|
|
||||||
[TestMethod]
|
|
||||||
[ExpectedException(typeof(DbMakerConfigException))]
|
|
||||||
public void TestDbMakerConfigException()
|
|
||||||
{
|
|
||||||
using (var _dbSearcher = new DbSearcher(new DbConfig(255), "../../../../../data/ip2region.db")) { }
|
|
||||||
}
|
|
||||||
/// <summary>
|
|
||||||
/// Invalid IP should throw "IPInValidException"
|
|
||||||
/// </summary>
|
|
||||||
[TestMethod]
|
|
||||||
public void TestInvalidIP()
|
|
||||||
{
|
|
||||||
using (var _dbSearcher = new DbSearcher("../../../../../data/ip2region.db"))
|
|
||||||
{
|
|
||||||
var invalidIps = new string[] { "256.255.1.1", "-1.0.0.0", "192.168.4", "x.y.z" };
|
|
||||||
var counter = 0;
|
|
||||||
|
|
||||||
foreach (var item in invalidIps)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
_dbSearcher.MemorySearch(item);
|
|
||||||
}
|
|
||||||
catch (IPInValidException)
|
|
||||||
{
|
|
||||||
counter++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Assert.AreEqual(counter, 4);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,148 +0,0 @@
|
||||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
||||||
using IP2Region;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace UnitTests
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// This class is used to test with normal IPs (Chinese, Foreign and Special).
|
|
||||||
/// Use async functions for testing.
|
|
||||||
/// </summary>
|
|
||||||
[TestClass]
|
|
||||||
public class NormalAsyncTest
|
|
||||||
{
|
|
||||||
private static DbSearcher _dbSearcher = null;
|
|
||||||
|
|
||||||
[ClassInitialize]
|
|
||||||
public static void Init(TestContext context)
|
|
||||||
{
|
|
||||||
_dbSearcher = new DbSearcher("../../../../../data/ip2region.db");
|
|
||||||
}
|
|
||||||
|
|
||||||
[ClassCleanup]
|
|
||||||
public static void ClearUp()
|
|
||||||
{
|
|
||||||
_dbSearcher.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
#region Normal Valid Chinese IP Test Cases
|
|
||||||
[TestMethod]
|
|
||||||
[TestCategory("Async Chinese Ip Test")]
|
|
||||||
public async Task AsyncBinarySearchTestForChinese()
|
|
||||||
{
|
|
||||||
const int cityId = 2163;
|
|
||||||
const string region = "中国|0|广东省|深圳市|鹏博士";
|
|
||||||
|
|
||||||
var result = await _dbSearcher.AsyncBinarySearch("101.105.35.57");
|
|
||||||
Assert.AreEqual(result.CityID, cityId);
|
|
||||||
Assert.AreEqual(result.Region, region);
|
|
||||||
}
|
|
||||||
[TestMethod]
|
|
||||||
[TestCategory("Async Chinese Ip Test")]
|
|
||||||
public async Task AsyncBtreeSearchTestForChinese()
|
|
||||||
{
|
|
||||||
const int cityId = 2163;
|
|
||||||
const string region = "中国|0|广东省|深圳市|鹏博士";
|
|
||||||
|
|
||||||
var result = await _dbSearcher.AsyncBtreeSearch("101.105.35.57");
|
|
||||||
Assert.AreEqual(result.CityID, cityId);
|
|
||||||
Assert.AreEqual(result.Region, region);
|
|
||||||
}
|
|
||||||
[TestMethod]
|
|
||||||
[TestCategory("Async Chinese Ip Test")]
|
|
||||||
public async Task AsyncMemorySearchTestForChinese()
|
|
||||||
{
|
|
||||||
const int cityId = 2163;
|
|
||||||
const string region = "中国|0|广东省|深圳市|鹏博士";
|
|
||||||
|
|
||||||
var result = await _dbSearcher.AsyncMemorySearch("101.105.35.57");
|
|
||||||
Assert.AreEqual(result.CityID, cityId);
|
|
||||||
Assert.AreEqual(result.Region, region);
|
|
||||||
}
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Normal Valid Foreign IP Test Cases
|
|
||||||
[TestMethod]
|
|
||||||
[TestCategory("Async Foreign Ip Test")]
|
|
||||||
public async Task AsyncBinarySearchTestForForeign()
|
|
||||||
{
|
|
||||||
const int cityId = 71;
|
|
||||||
const string region = "荷兰|0|0|0|0";
|
|
||||||
|
|
||||||
var result = await _dbSearcher.AsyncBinarySearch("86.84.21.60");
|
|
||||||
Assert.AreEqual(result.CityID, cityId);
|
|
||||||
Assert.AreEqual(result.Region, region);
|
|
||||||
}
|
|
||||||
[TestMethod]
|
|
||||||
[TestCategory("Async Foreign Ip Test")]
|
|
||||||
public async Task AsyncBtreeSearchTestForForeign()
|
|
||||||
{
|
|
||||||
const int cityId = 71;
|
|
||||||
const string region = "荷兰|0|0|0|0";
|
|
||||||
|
|
||||||
var result = await _dbSearcher.AsyncBtreeSearch("86.84.21.60");
|
|
||||||
Assert.AreEqual(result.CityID, cityId);
|
|
||||||
Assert.AreEqual(result.Region, region);
|
|
||||||
}
|
|
||||||
[TestMethod]
|
|
||||||
[TestCategory("Async Foreign Ip Test")]
|
|
||||||
public async Task AsyncMemorySearchTestForForeign()
|
|
||||||
{
|
|
||||||
const int cityId = 71;
|
|
||||||
const string region = "荷兰|0|0|0|0";
|
|
||||||
|
|
||||||
var result = await _dbSearcher.AsyncMemorySearch("86.84.21.60");
|
|
||||||
Assert.AreEqual(result.CityID, cityId);
|
|
||||||
Assert.AreEqual(result.Region, region);
|
|
||||||
}
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Normal Valid Special Ip Test Cases
|
|
||||||
[TestMethod]
|
|
||||||
[TestCategory("Async Special Ip Test")]
|
|
||||||
public async Task AsyncBinarySearchTestForSpecial()
|
|
||||||
{
|
|
||||||
const int cityId = 0;
|
|
||||||
const string region = "0|0|0|内网IP|内网IP";
|
|
||||||
string[] specialIps = new string[] { "255.255.255.255", "0.0.0.0" };
|
|
||||||
|
|
||||||
foreach (var item in specialIps)
|
|
||||||
{
|
|
||||||
var result = await _dbSearcher.AsyncBinarySearch(item);
|
|
||||||
Assert.AreEqual(result.CityID, cityId);
|
|
||||||
Assert.AreEqual(result.Region, region);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
[TestMethod]
|
|
||||||
[TestCategory("Async Special Ip Test")]
|
|
||||||
public async Task AsyncBtreeSearchTestForSpecial()
|
|
||||||
{
|
|
||||||
const int cityId = 0;
|
|
||||||
const string region = "0|0|0|内网IP|内网IP";
|
|
||||||
string[] specialIps = new string[] { "255.255.255.255", "0.0.0.0" };
|
|
||||||
|
|
||||||
foreach (var item in specialIps)
|
|
||||||
{
|
|
||||||
var result = await _dbSearcher.AsyncBtreeSearch(item);
|
|
||||||
Assert.AreEqual(result.CityID, cityId);
|
|
||||||
Assert.AreEqual(result.Region, region);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
[TestMethod]
|
|
||||||
[TestCategory("Async Special Ip Test")]
|
|
||||||
public async Task AsyncMemorySearchTestForSpecial()
|
|
||||||
{
|
|
||||||
const int cityId = 0;
|
|
||||||
const string region = "0|0|0|内网IP|内网IP";
|
|
||||||
string[] specialIps = new string[] { "255.255.255.255", "0.0.0.0" };
|
|
||||||
|
|
||||||
foreach (var item in specialIps)
|
|
||||||
{
|
|
||||||
var result = await _dbSearcher.AsyncMemorySearch(item);
|
|
||||||
Assert.AreEqual(result.CityID, cityId);
|
|
||||||
Assert.AreEqual(result.Region, region);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endregion
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,146 +0,0 @@
|
||||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
||||||
using IP2Region;
|
|
||||||
namespace UnitTests
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// This class is used to test with normal IPs (Chinese, Foreign and Special).
|
|
||||||
/// Use sync functions for testing.
|
|
||||||
/// </summary>
|
|
||||||
[TestClass]
|
|
||||||
public class NormalSyncTest
|
|
||||||
{
|
|
||||||
private static DbSearcher _dbSearcher = null;
|
|
||||||
|
|
||||||
[ClassInitialize]
|
|
||||||
public static void Init(TestContext context)
|
|
||||||
{
|
|
||||||
_dbSearcher = new DbSearcher("../../../../../data/ip2region.db");
|
|
||||||
}
|
|
||||||
|
|
||||||
[ClassCleanup]
|
|
||||||
public static void ClearUp()
|
|
||||||
{
|
|
||||||
_dbSearcher.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
#region Normal Valid Chinese IP Test Cases
|
|
||||||
[TestMethod]
|
|
||||||
[TestCategory("Chinese Ip Test")]
|
|
||||||
public void BinarySearchTestForChinese()
|
|
||||||
{
|
|
||||||
const int cityId = 2163;
|
|
||||||
const string region = "中国|0|广东省|深圳市|鹏博士";
|
|
||||||
|
|
||||||
var result = _dbSearcher.BinarySearch("101.105.35.57");
|
|
||||||
Assert.AreEqual(result.CityID, cityId);
|
|
||||||
Assert.AreEqual(result.Region, region);
|
|
||||||
}
|
|
||||||
[TestMethod]
|
|
||||||
[TestCategory("Chinese Ip Test")]
|
|
||||||
public void BtreeSearchTestForChinese()
|
|
||||||
{
|
|
||||||
const int cityId = 2163;
|
|
||||||
const string region = "中国|0|广东省|深圳市|鹏博士";
|
|
||||||
|
|
||||||
var result = _dbSearcher.BtreeSearch("101.105.35.57");
|
|
||||||
Assert.AreEqual(result.CityID, cityId);
|
|
||||||
Assert.AreEqual(result.Region, region);
|
|
||||||
}
|
|
||||||
[TestMethod]
|
|
||||||
[TestCategory("Chinese Ip Test")]
|
|
||||||
public void MemorySearchTestForChinese()
|
|
||||||
{
|
|
||||||
const int cityId = 2163;
|
|
||||||
const string region = "中国|0|广东省|深圳市|鹏博士";
|
|
||||||
|
|
||||||
var result = _dbSearcher.MemorySearch("101.105.35.57");
|
|
||||||
Assert.AreEqual(result.CityID, cityId);
|
|
||||||
Assert.AreEqual(result.Region, region);
|
|
||||||
}
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Normal Valid Foreign IP Test Cases
|
|
||||||
[TestMethod]
|
|
||||||
[TestCategory("Foreign Ip Test")]
|
|
||||||
public void BinarySearchTestForForeign()
|
|
||||||
{
|
|
||||||
const int cityId = 71;
|
|
||||||
const string region = "荷兰|0|0|0|0";
|
|
||||||
|
|
||||||
var result = _dbSearcher.BinarySearch("86.84.21.60");
|
|
||||||
Assert.AreEqual(result.CityID, cityId);
|
|
||||||
Assert.AreEqual(result.Region, region);
|
|
||||||
}
|
|
||||||
[TestMethod]
|
|
||||||
[TestCategory("Foreign Ip Test")]
|
|
||||||
public void BtreeSearchTestForForeign()
|
|
||||||
{
|
|
||||||
const int cityId = 71;
|
|
||||||
const string region = "荷兰|0|0|0|0";
|
|
||||||
|
|
||||||
var result = _dbSearcher.BtreeSearch("86.84.21.60");
|
|
||||||
Assert.AreEqual(result.CityID, cityId);
|
|
||||||
Assert.AreEqual(result.Region, region);
|
|
||||||
}
|
|
||||||
[TestMethod]
|
|
||||||
[TestCategory("Foreign Ip Test")]
|
|
||||||
public void MemorySearchTestForForeign()
|
|
||||||
{
|
|
||||||
const int cityId = 71;
|
|
||||||
const string region = "荷兰|0|0|0|0";
|
|
||||||
|
|
||||||
var result = _dbSearcher.MemorySearch("86.84.21.60");
|
|
||||||
Assert.AreEqual(result.CityID, cityId);
|
|
||||||
Assert.AreEqual(result.Region, region);
|
|
||||||
}
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Normal Valid Special Ip Test Cases
|
|
||||||
[TestMethod]
|
|
||||||
[TestCategory("Special Ip Test")]
|
|
||||||
public void BinarySearchTestForSpecial()
|
|
||||||
{
|
|
||||||
const int cityId = 0;
|
|
||||||
const string region = "0|0|0|内网IP|内网IP";
|
|
||||||
string[] specialIps = new string[] { "255.255.255.255", "0.0.0.0" };
|
|
||||||
|
|
||||||
foreach (var item in specialIps)
|
|
||||||
{
|
|
||||||
var result = _dbSearcher.BinarySearch(item);
|
|
||||||
Assert.AreEqual(result.CityID, cityId);
|
|
||||||
Assert.AreEqual(result.Region, region);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
[TestMethod]
|
|
||||||
[TestCategory("Special Ip Test")]
|
|
||||||
public void BtreeSearchTestForSpecial()
|
|
||||||
{
|
|
||||||
const int cityId = 0;
|
|
||||||
const string region = "0|0|0|内网IP|内网IP";
|
|
||||||
string[] specialIps = new string[] { "255.255.255.255", "0.0.0.0" };
|
|
||||||
|
|
||||||
foreach (var item in specialIps)
|
|
||||||
{
|
|
||||||
var result = _dbSearcher.BtreeSearch(item);
|
|
||||||
Assert.AreEqual(result.CityID, cityId);
|
|
||||||
Assert.AreEqual(result.Region, region);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
[TestMethod]
|
|
||||||
[TestCategory("Special Ip Test")]
|
|
||||||
public void MemorySearchTestForSpecial()
|
|
||||||
{
|
|
||||||
const int cityId = 0;
|
|
||||||
const string region = "0|0|0|内网IP|内网IP";
|
|
||||||
string[] specialIps = new string[] { "255.255.255.255", "0.0.0.0" };
|
|
||||||
|
|
||||||
foreach (var item in specialIps)
|
|
||||||
{
|
|
||||||
var result = _dbSearcher.MemorySearch(item);
|
|
||||||
Assert.AreEqual(result.CityID, cityId);
|
|
||||||
Assert.AreEqual(result.Region, region);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endregion
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,19 +0,0 @@
|
||||||
using System.Reflection;
|
|
||||||
using System.Runtime.InteropServices;
|
|
||||||
|
|
||||||
[assembly: AssemblyTitle("UnitTests")]
|
|
||||||
[assembly: AssemblyDescription("")]
|
|
||||||
[assembly: AssemblyConfiguration("")]
|
|
||||||
[assembly: AssemblyCompany("")]
|
|
||||||
[assembly: AssemblyProduct("UnitTests")]
|
|
||||||
[assembly: AssemblyCopyright("Copyright © 2018")]
|
|
||||||
[assembly: AssemblyTrademark("")]
|
|
||||||
[assembly: AssemblyCulture("")]
|
|
||||||
|
|
||||||
[assembly: ComVisible(false)]
|
|
||||||
|
|
||||||
[assembly: Guid("38abc99f-cc14-47e3-9eeb-06c67d5ac33f")]
|
|
||||||
|
|
||||||
// [assembly: AssemblyVersion("1.0.*")]
|
|
||||||
[assembly: AssemblyVersion("1.0.0.0")]
|
|
||||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
|
||||||
|
|
@ -1,76 +0,0 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
|
||||||
<Import Project="..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.props" Condition="Exists('..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.props')" />
|
|
||||||
<PropertyGroup>
|
|
||||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
|
||||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
|
||||||
<ProjectGuid>{38ABC99F-CC14-47E3-9EEB-06C67D5AC33F}</ProjectGuid>
|
|
||||||
<OutputType>Library</OutputType>
|
|
||||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
|
||||||
<RootNamespace>UnitTests</RootNamespace>
|
|
||||||
<AssemblyName>UnitTests</AssemblyName>
|
|
||||||
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
|
|
||||||
<FileAlignment>512</FileAlignment>
|
|
||||||
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
|
|
||||||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">15.0</VisualStudioVersion>
|
|
||||||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
|
|
||||||
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
|
|
||||||
<IsCodedUITest>False</IsCodedUITest>
|
|
||||||
<TestProjectType>UnitTest</TestProjectType>
|
|
||||||
<NuGetPackageImportStamp>
|
|
||||||
</NuGetPackageImportStamp>
|
|
||||||
<TargetFrameworkProfile />
|
|
||||||
</PropertyGroup>
|
|
||||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
|
||||||
<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' ">
|
|
||||||
<DebugType>pdbonly</DebugType>
|
|
||||||
<Optimize>true</Optimize>
|
|
||||||
<OutputPath>bin\Release\</OutputPath>
|
|
||||||
<DefineConstants>TRACE</DefineConstants>
|
|
||||||
<ErrorReport>prompt</ErrorReport>
|
|
||||||
<WarningLevel>4</WarningLevel>
|
|
||||||
</PropertyGroup>
|
|
||||||
<ItemGroup>
|
|
||||||
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
|
||||||
<HintPath>..\packages\MSTest.TestFramework.1.2.1\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll</HintPath>
|
|
||||||
</Reference>
|
|
||||||
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
|
||||||
<HintPath>..\packages\MSTest.TestFramework.1.2.1\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll</HintPath>
|
|
||||||
</Reference>
|
|
||||||
<Reference Include="System" />
|
|
||||||
<Reference Include="System.Core" />
|
|
||||||
</ItemGroup>
|
|
||||||
<ItemGroup>
|
|
||||||
<Compile Include="NormalAsyncTest.cs" />
|
|
||||||
<Compile Include="NormalSyncTest.cs" />
|
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
|
||||||
<Compile Include="ExceptionTest.cs" />
|
|
||||||
</ItemGroup>
|
|
||||||
<ItemGroup>
|
|
||||||
<None Include="packages.config" />
|
|
||||||
</ItemGroup>
|
|
||||||
<ItemGroup>
|
|
||||||
<ProjectReference Include="..\IP2Region\IP2Region.csproj">
|
|
||||||
<Project>{4c6032ee-43a5-4d5c-88f8-c411069e49e0}</Project>
|
|
||||||
<Name>IP2Region</Name>
|
|
||||||
</ProjectReference>
|
|
||||||
</ItemGroup>
|
|
||||||
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
|
|
||||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
|
||||||
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
|
|
||||||
<PropertyGroup>
|
|
||||||
<ErrorText>这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。</ErrorText>
|
|
||||||
</PropertyGroup>
|
|
||||||
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.props'))" />
|
|
||||||
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.targets'))" />
|
|
||||||
</Target>
|
|
||||||
<Import Project="..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.targets" Condition="Exists('..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.targets')" />
|
|
||||||
</Project>
|
|
||||||
|
|
@ -1,5 +0,0 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<packages>
|
|
||||||
<package id="MSTest.TestAdapter" version="1.2.1" targetFramework="net45" />
|
|
||||||
<package id="MSTest.TestFramework" version="1.2.1" targetFramework="net45" />
|
|
||||||
</packages>
|
|
||||||
|
|
@ -1,3 +1,56 @@
|
||||||
# nodejs 客户端
|
# nodejs 客户端
|
||||||
|
|
||||||
现已实现同步查询,异步查询未实现,用法参考 ip2region.spec.js
|
## 实现情况:
|
||||||
|
|
||||||
|
现已实现同步和异步查询,具体使用方法可以参考 `nodejs\tests\constructorTest.spec.js` 和`nodejs\tests\createTest.spec.js`。
|
||||||
|
|
||||||
|
## 如何贡献?
|
||||||
|
|
||||||
|
你可以任意修改代码,但必须确保通过全部的单元测试。要保证通过全部的单元测试,请在 Nodejs 控制台下切换到 nodejs 目录:
|
||||||
|
|
||||||
|
1)在此之前,请先运行 `npm i` 确保你已经安装了各类初始化第三方工具。
|
||||||
|
2)然后运行 `npm run coverage` 确保你的代码可以通过全部测试(必要时可以添加测试),同时保证代码覆盖率都是绿色(80%以上)。
|
||||||
|
|
||||||
|
```bash
|
||||||
|
D:\Projects\ip2region\binding\nodejs>npm run coverage
|
||||||
|
|
||||||
|
> ip2region@0.0.1 coverage D:\Projects\ip2region\binding\nodejs
|
||||||
|
> npm run test && jest --coverage
|
||||||
|
|
||||||
|
|
||||||
|
> ip2region@0.0.1 test D:\Projects\ip2region\binding\nodejs
|
||||||
|
> jest
|
||||||
|
|
||||||
|
PASS tests\constructorTest.spec.js
|
||||||
|
PASS tests\createTest.spec.js
|
||||||
|
PASS tests\exceptionTest.spec.js
|
||||||
|
|
||||||
|
Snapshot Summary
|
||||||
|
› 168 snapshots written in 2 test suites.
|
||||||
|
|
||||||
|
Test Suites: 3 passed, 3 total
|
||||||
|
Tests: 14 passed, 14 total
|
||||||
|
Snapshots: 168 added, 168 total
|
||||||
|
Time: 1.645s
|
||||||
|
Ran all test suites.
|
||||||
|
PASS tests\constructorTest.spec.js
|
||||||
|
PASS tests\createTest.spec.js
|
||||||
|
PASS tests\exceptionTest.spec.js
|
||||||
|
----------------------|----------|----------|----------|----------|-------------------|
|
||||||
|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
|
||||||
|
----------------------|----------|----------|----------|----------|-------------------|
|
||||||
|
All files | 92.34 | 80.77 | 96 | 93.83 | |
|
||||||
|
nodejs | 91.95 | 80.26 | 95.65 | 93.51 | |
|
||||||
|
ip2region.js | 91.95 | 80.26 | 95.65 | 93.51 |... 09,410,460,484 |
|
||||||
|
nodejs/tests/utils | 100 | 100 | 100 | 100 | |
|
||||||
|
asyncFor.js | 100 | 100 | 100 | 100 | |
|
||||||
|
fetchMainVersion.js | 100 | 100 | 100 | 100 | |
|
||||||
|
testData.js | 100 | 100 | 100 | 100 | |
|
||||||
|
----------------------|----------|----------|----------|----------|-------------------|
|
||||||
|
|
||||||
|
Test Suites: 3 passed, 3 total
|
||||||
|
Tests: 14 passed, 14 total
|
||||||
|
Snapshots: 168 passed, 168 total
|
||||||
|
Time: 1.792s
|
||||||
|
Ran all test suites.
|
||||||
|
```
|
||||||
|
|
@ -1,29 +0,0 @@
|
||||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
|
||||||
|
|
||||||
exports[`ip2region binarySearch 1`] = `
|
|
||||||
Object {
|
|
||||||
"city": 2163,
|
|
||||||
"region": "中国|0|广东省|深圳市|阿里云",
|
|
||||||
}
|
|
||||||
`;
|
|
||||||
|
|
||||||
exports[`ip2region binarySearch 2`] = `
|
|
||||||
Object {
|
|
||||||
"city": 0,
|
|
||||||
"region": "0|0|0|内网IP|内网IP",
|
|
||||||
}
|
|
||||||
`;
|
|
||||||
|
|
||||||
exports[`ip2region should query 1`] = `
|
|
||||||
Object {
|
|
||||||
"city": 2163,
|
|
||||||
"region": "中国|0|广东省|深圳市|阿里云",
|
|
||||||
}
|
|
||||||
`;
|
|
||||||
|
|
||||||
exports[`ip2region should query 2`] = `
|
|
||||||
Object {
|
|
||||||
"city": 0,
|
|
||||||
"region": "0|0|0|内网IP|内网IP",
|
|
||||||
}
|
|
||||||
`;
|
|
||||||
|
|
@ -5,20 +5,18 @@
|
||||||
*
|
*
|
||||||
* @author dongyado<dongyado@gmail.com>
|
* @author dongyado<dongyado@gmail.com>
|
||||||
* @author leeching<leeching.fx@gmail.com>
|
* @author leeching<leeching.fx@gmail.com>
|
||||||
|
* @author dongwei<maledong_github@outlook.com>
|
||||||
*/
|
*/
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
|
||||||
const IP_BASE = [16777216, 65536, 256, 1];
|
//#region Private Functions
|
||||||
const INDEX_BLOCK_LENGTH = 12;
|
|
||||||
const TOTAL_HEADER_LENGTH = 8192;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert ip to long (xxx.xxx.xxx.xxx to a integer)
|
* Convert ip to long (xxx.xxx.xxx.xxx to a integer)
|
||||||
*
|
*
|
||||||
* @param {string} ip
|
* @param {string} ip
|
||||||
* @return {number} long value
|
* @return {number} long value
|
||||||
*/
|
*/
|
||||||
function ip2long(ip) {
|
function _ip2long(ip) {
|
||||||
const arr = ip.split('.');
|
const arr = ip.split('.');
|
||||||
if (arr.length !== 4) {
|
if (arr.length !== 4) {
|
||||||
throw new Error('invalid ip');
|
throw new Error('invalid ip');
|
||||||
|
|
@ -39,7 +37,7 @@ function ip2long(ip) {
|
||||||
* @param {number} offset
|
* @param {number} offset
|
||||||
* @return {number} long value
|
* @return {number} long value
|
||||||
*/
|
*/
|
||||||
function getLong(buffer, offset) {
|
function _getLong(buffer, offset) {
|
||||||
const val =
|
const val =
|
||||||
(buffer[offset] & 0x000000ff) |
|
(buffer[offset] & 0x000000ff) |
|
||||||
((buffer[offset + 1] << 8) & 0x0000ff00) |
|
((buffer[offset + 1] << 8) & 0x0000ff00) |
|
||||||
|
|
@ -48,82 +46,156 @@ function getLong(buffer, offset) {
|
||||||
return val < 0 ? val >>> 0 : val;
|
return val < 0 ? val >>> 0 : val;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
//#endregion
|
||||||
* @typedef {Object} SearchResult
|
|
||||||
* @property {number} city
|
|
||||||
* @property {string} region
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
//#region Private Variables
|
||||||
|
|
||||||
|
// We don't wanna expose a private global settings to
|
||||||
|
// the public for safety reason.
|
||||||
|
const _globalInstances = new Map();
|
||||||
|
|
||||||
|
const IP_BASE = [16777216, 65536, 256, 1];
|
||||||
|
const INDEX_BLOCK_LENGTH = 12;
|
||||||
|
const TOTAL_HEADER_LENGTH = 8192;
|
||||||
|
|
||||||
|
// Private Message Symbols for functions
|
||||||
|
const PrepareHeader = Symbol('#PrepareHeader');
|
||||||
|
const CalTotalBlocks = Symbol('#CalsTotalBlocks');
|
||||||
|
const ReadDataSync = Symbol('#ReadDataSync');
|
||||||
|
const ReadData = Symbol('#ReadData');
|
||||||
|
|
||||||
|
//#endregion
|
||||||
class IP2Region {
|
class IP2Region {
|
||||||
static create(dbPath) {
|
|
||||||
const oldInstance = IP2Region._instances.get(dbPath);
|
//#region Private Functions
|
||||||
if (oldInstance) {
|
|
||||||
return oldInstance;
|
[CalTotalBlocks]() {
|
||||||
} else {
|
const superBlock = new Buffer(8);
|
||||||
const instance = new IP2Region({ dbPath });
|
fs.readSync(this.dbFd, superBlock, 0, 8, 0);
|
||||||
IP2Region._instances.set(dbPath, instance);
|
this.firstIndexPtr = _getLong(superBlock, 0);
|
||||||
return instance;
|
this.lastIndexPtr = _getLong(superBlock, 4);
|
||||||
|
this.totalBlocks =
|
||||||
|
(this.lastIndexPtr - this.firstIndexPtr) / INDEX_BLOCK_LENGTH + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
[PrepareHeader]() {
|
||||||
|
fs.readSync(
|
||||||
|
this.dbFd,
|
||||||
|
this.headerIndexBuffer,
|
||||||
|
0,
|
||||||
|
TOTAL_HEADER_LENGTH,
|
||||||
|
8
|
||||||
|
);
|
||||||
|
|
||||||
|
for (let i = 0; i < TOTAL_HEADER_LENGTH; i += 8) {
|
||||||
|
const startIp = _getLong(this.headerIndexBuffer, i);
|
||||||
|
const dataPtr = _getLong(this.headerIndexBuffer, i + 4);
|
||||||
|
if (dataPtr == 0) break;
|
||||||
|
|
||||||
|
this.headerSip.push(startIp);
|
||||||
|
this.headerPtr.push(dataPtr);
|
||||||
|
this.headerLen++; // header index size count
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[ReadData](dataPos, callBack) {
|
||||||
|
if (dataPos == 0) return callBack(null,null);
|
||||||
|
const dataLen = (dataPos >> 24) & 0xff;
|
||||||
|
dataPos = dataPos & 0x00ffffff;
|
||||||
|
const dataBuffer = new Buffer(dataLen);
|
||||||
|
|
||||||
|
fs.read(this.dbFd, dataBuffer, 0, dataLen, dataPos, (err, result) => {
|
||||||
|
if (err) {
|
||||||
|
callBack(err, null);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
const city = _getLong(dataBuffer, 0);
|
||||||
|
const region = dataBuffer.toString('utf8', 4, dataLen);
|
||||||
|
callBack(null, { city, region });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
[ReadDataSync](dataPos) {
|
||||||
|
if (dataPos == 0) return null;
|
||||||
|
const dataLen = (dataPos >> 24) & 0xff;
|
||||||
|
dataPos = dataPos & 0x00ffffff;
|
||||||
|
const dataBuffer = new Buffer(dataLen);
|
||||||
|
|
||||||
|
fs.readSync(this.dbFd, dataBuffer, 0, dataLen, dataPos);
|
||||||
|
|
||||||
|
const city = _getLong(dataBuffer, 0);
|
||||||
|
const region = dataBuffer.toString('utf8', 4, dataLen);
|
||||||
|
|
||||||
|
return { city, region };
|
||||||
|
}
|
||||||
|
//#endregion
|
||||||
|
|
||||||
|
//#region Static Functions
|
||||||
|
|
||||||
|
// Single Instance
|
||||||
|
static create(dbPath) {
|
||||||
|
let existInstance = _globalInstances.get(dbPath);
|
||||||
|
if (existInstance == null) {
|
||||||
|
existInstance = new IP2Region({ dbPath: dbPath });
|
||||||
|
}
|
||||||
|
return existInstance;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* For backward compatibility
|
* For backward compatibility
|
||||||
*/
|
*/
|
||||||
static destroy() {
|
static destroy() {
|
||||||
IP2Region._instances.forEach(([key, instance]) => {
|
_globalInstances.forEach(([key, instance]) => {
|
||||||
instance.destroy();
|
instance.destroy();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//#endregion
|
||||||
|
|
||||||
constructor(options = {}) {
|
constructor(options = {}) {
|
||||||
|
|
||||||
const { dbPath } = options;
|
const { dbPath } = options;
|
||||||
if (!dbPath || !fs.existsSync(dbPath)) {
|
|
||||||
throw new Error(`[ip2region] db file not exists : ${dbPath}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
this.dbFd = fs.openSync(dbPath, 'r');
|
||||||
this.dbFd = fs.openSync(dbPath, 'r');
|
|
||||||
} catch (e) {
|
|
||||||
throw new Error(
|
|
||||||
`[ip2region] Can not open ip2region.db file , path: ${dbPath}`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
IP2Region._instances.set((this.dbPath = dbPath), this);
|
this.dbPath = dbPath;
|
||||||
|
|
||||||
|
_globalInstances.set(this.dbPath, this);
|
||||||
|
|
||||||
this.totalBlocks = this.firstIndexPtr = this.lastIndexPtr = 0;
|
this.totalBlocks = this.firstIndexPtr = this.lastIndexPtr = 0;
|
||||||
this.calcTotalBlocks();
|
this[CalTotalBlocks]();
|
||||||
|
|
||||||
this.headerIndexBuffer = new Buffer(TOTAL_HEADER_LENGTH);
|
this.headerIndexBuffer = new Buffer(TOTAL_HEADER_LENGTH);
|
||||||
this.headerSip = [];
|
this.headerSip = [];
|
||||||
this.headerPtr = [];
|
this.headerPtr = [];
|
||||||
this.headerLen = 0;
|
this.headerLen = 0;
|
||||||
this.prepareHeader();
|
this[PrepareHeader]();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//#region Public Functions
|
||||||
/**
|
/**
|
||||||
* @public
|
* Destroy the current file by closing it.
|
||||||
*/
|
*/
|
||||||
destroy() {
|
destroy() {
|
||||||
fs.closeSync(ip2rObj.dbFd);
|
fs.closeSync(this.dbFd);
|
||||||
IP2Region._instances.delete(this.dbPath);
|
_globalInstances.delete(this.dbPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @public
|
* Sync of binarySearch.
|
||||||
* @param {string} ip
|
* @param {string} ip The IP address to search for.
|
||||||
* @return {SearchResult}
|
* @return {SearchResult} A result something like `{ city: 2163, region: '中国|0|广东省|深圳市|阿里云' }`
|
||||||
*/
|
*/
|
||||||
binarySearchSync(ip) {
|
binarySearchSync(ip) {
|
||||||
ip = ip2long(ip);
|
|
||||||
|
ip = _ip2long(ip);
|
||||||
|
|
||||||
let low = 0;
|
let low = 0;
|
||||||
let mid = 0;
|
let mid = 0;
|
||||||
let high = this.totalBlocks;
|
let high = this.totalBlocks;
|
||||||
let dataPos = 0;
|
|
||||||
let pos = 0;
|
let pos = 0;
|
||||||
let sip = 0;
|
let sip = 0;
|
||||||
let eip = 0;
|
|
||||||
const indexBuffer = new Buffer(12);
|
const indexBuffer = new Buffer(12);
|
||||||
|
|
||||||
// binary search
|
// binary search
|
||||||
|
|
@ -131,30 +203,89 @@ class IP2Region {
|
||||||
mid = (low + high) >> 1;
|
mid = (low + high) >> 1;
|
||||||
pos = this.firstIndexPtr + mid * INDEX_BLOCK_LENGTH;
|
pos = this.firstIndexPtr + mid * INDEX_BLOCK_LENGTH;
|
||||||
fs.readSync(this.dbFd, indexBuffer, 0, INDEX_BLOCK_LENGTH, pos);
|
fs.readSync(this.dbFd, indexBuffer, 0, INDEX_BLOCK_LENGTH, pos);
|
||||||
sip = getLong(indexBuffer, 0);
|
sip = _getLong(indexBuffer, 0);
|
||||||
|
|
||||||
if (ip < sip) {
|
if (ip < sip) {
|
||||||
high = mid - 1;
|
high = mid - 1;
|
||||||
} else {
|
} else {
|
||||||
eip = getLong(indexBuffer, 4);
|
sip = _getLong(indexBuffer, 4);
|
||||||
if (ip > eip) {
|
if (ip > sip) {
|
||||||
low = mid + 1;
|
low = mid + 1;
|
||||||
} else {
|
} else {
|
||||||
dataPos = getLong(indexBuffer, 8);
|
sip = _getLong(indexBuffer, 8);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return this.readData(dataPos);
|
return this[ReadDataSync](sip);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @public
|
* Async of binarySearch.
|
||||||
* @param {string} ip
|
* @param {string} ip The IP address to search for.
|
||||||
* @return {SearchResult}
|
* @param {Function} callBack The callBack function with two parameters, if successful,
|
||||||
|
* err is null and result is `{ city: 2163, region: '中国|0|广东省|深圳市|阿里云' }`
|
||||||
|
*/
|
||||||
|
binarySearch(ip, callBack) {
|
||||||
|
|
||||||
|
ip = _ip2long(ip);
|
||||||
|
|
||||||
|
let low = 0;
|
||||||
|
let mid = 0;
|
||||||
|
let high = this.totalBlocks;
|
||||||
|
let pos = 0;
|
||||||
|
let sip = 0;
|
||||||
|
const indexBuffer = new Buffer(12);
|
||||||
|
const _self = this;
|
||||||
|
|
||||||
|
// Because `while` is a sync method, we have to convert this to a recursive loop
|
||||||
|
// and in each loop we should continue calling `setImmediate` until we found the IP.
|
||||||
|
function _innerAsyncWhile() {
|
||||||
|
if (low <= high) {
|
||||||
|
mid = (low + high) >> 1;
|
||||||
|
pos = _self.firstIndexPtr + mid * INDEX_BLOCK_LENGTH;
|
||||||
|
|
||||||
|
// Now async read the file
|
||||||
|
fs.read(_self.dbFd, indexBuffer, 0, INDEX_BLOCK_LENGTH, pos, (err) => {
|
||||||
|
|
||||||
|
if (err) {
|
||||||
|
return callBack(err, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
sip = _getLong(indexBuffer, 0);
|
||||||
|
|
||||||
|
if (ip < sip) {
|
||||||
|
high = mid - 1;
|
||||||
|
setImmediate(_innerAsyncWhile);
|
||||||
|
} else {
|
||||||
|
sip = _getLong(indexBuffer, 4);
|
||||||
|
if (ip > sip) {
|
||||||
|
low = mid + 1;
|
||||||
|
setImmediate(_innerAsyncWhile);
|
||||||
|
} else {
|
||||||
|
sip = _getLong(indexBuffer, 8);
|
||||||
|
_self[ReadData](sip, (err, result) => {
|
||||||
|
callBack(err, result);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call this immediately
|
||||||
|
_innerAsyncWhile();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sync of btreeSearch.
|
||||||
|
* @param {string} ip The IP address to search for.
|
||||||
|
* @return {Function} A result something like `{ city: 2163, region: '中国|0|广东省|深圳市|阿里云' }`
|
||||||
*/
|
*/
|
||||||
btreeSearchSync(ip) {
|
btreeSearchSync(ip) {
|
||||||
ip = ip2long(ip);
|
|
||||||
|
ip = _ip2long(ip);
|
||||||
|
|
||||||
// first search (in header index)
|
// first search (in header index)
|
||||||
let low = 0;
|
let low = 0;
|
||||||
|
|
@ -221,84 +352,143 @@ class IP2Region {
|
||||||
|
|
||||||
let p = 0;
|
let p = 0;
|
||||||
let sip = 0;
|
let sip = 0;
|
||||||
let eip = 0;
|
|
||||||
let dataPtr = 0;
|
|
||||||
|
|
||||||
while (low <= high) {
|
while (low <= high) {
|
||||||
mid = (low + high) >> 1;
|
mid = (low + high) >> 1;
|
||||||
p = mid * INDEX_BLOCK_LENGTH;
|
p = mid * INDEX_BLOCK_LENGTH;
|
||||||
sip = getLong(blockBuffer, p);
|
sip = _getLong(blockBuffer, p);
|
||||||
|
|
||||||
if (ip < sip) {
|
if (ip < sip) {
|
||||||
high = mid - 1;
|
high = mid - 1;
|
||||||
} else {
|
} else {
|
||||||
eip = getLong(blockBuffer, p + 4);
|
sip = _getLong(blockBuffer, p + 4);
|
||||||
if (ip > eip) {
|
if (ip > sip) {
|
||||||
low = mid + 1;
|
low = mid + 1;
|
||||||
} else {
|
} else {
|
||||||
dataPtr = getLong(blockBuffer, p + 8);
|
sip = _getLong(blockBuffer, p + 8);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return this.readData(dataPtr);
|
return this[ReadDataSync](sip);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @private
|
* Async of btreeSearch.
|
||||||
*/
|
* @param {string} ip The IP address to search for.
|
||||||
calcTotalBlocks() {
|
* @param {Function} callBack The callBack function with two parameters, if successful,
|
||||||
const superBlock = new Buffer(8);
|
* err is null and result is `{ city: 2163, region: '中国|0|广东省|深圳市|阿里云' }`
|
||||||
fs.readSync(this.dbFd, superBlock, 0, 8, 0);
|
*/
|
||||||
this.firstIndexPtr = getLong(superBlock, 0);
|
btreeSearch(ip, callBack) {
|
||||||
this.lastIndexPtr = getLong(superBlock, 4);
|
ip = _ip2long(ip);
|
||||||
this.totalBlocks =
|
|
||||||
(this.lastIndexPtr - this.firstIndexPtr) / INDEX_BLOCK_LENGTH + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
// first search (in header index)
|
||||||
* @private
|
let low = 0;
|
||||||
*/
|
let mid = 0;
|
||||||
prepareHeader() {
|
let high = this.headerLen;
|
||||||
fs.readSync(
|
let sptr = 0;
|
||||||
this.dbFd,
|
let eptr = 0;
|
||||||
this.headerIndexBuffer,
|
|
||||||
0,
|
|
||||||
TOTAL_HEADER_LENGTH,
|
|
||||||
8
|
|
||||||
);
|
|
||||||
|
|
||||||
for (let i = 0; i < TOTAL_HEADER_LENGTH; i += 8) {
|
while (low <= high) {
|
||||||
const startIp = getLong(this.headerIndexBuffer, i);
|
mid = (low + high) >> 1;
|
||||||
const dataPtr = getLong(this.headerIndexBuffer, i + 4);
|
|
||||||
if (dataPtr == 0) break;
|
|
||||||
|
|
||||||
this.headerSip.push(startIp);
|
if (ip == this.headerSip[mid]) {
|
||||||
this.headerPtr.push(dataPtr);
|
if (mid > 0) {
|
||||||
this.headerLen++; // header index size count
|
sptr = this.headerPtr[mid - 1];
|
||||||
|
eptr = this.headerPtr[mid];
|
||||||
|
} else {
|
||||||
|
sptr = this.headerPtr[mid];
|
||||||
|
eptr = this.headerPtr[mid + 1];
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ip < this.headerSip[mid]) {
|
||||||
|
if (mid == 0) {
|
||||||
|
sptr = this.headerPtr[mid];
|
||||||
|
eptr = this.headerPtr[mid + 1];
|
||||||
|
break;
|
||||||
|
} else if (ip > this.headerSip[mid - 1]) {
|
||||||
|
sptr = this.headerPtr[mid - 1];
|
||||||
|
eptr = this.headerPtr[mid];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
high = mid - 1;
|
||||||
|
} else {
|
||||||
|
if (mid == this.headerLen - 1) {
|
||||||
|
sptr = this.headerPtr[mid - 1];
|
||||||
|
eptr = this.headerPtr[mid];
|
||||||
|
break;
|
||||||
|
} else if (ip <= this.headerSip[mid + 1]) {
|
||||||
|
sptr = this.headerPtr[mid];
|
||||||
|
eptr = this.headerPtr[mid + 1];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
low = mid + 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// match nothing
|
||||||
|
if (sptr == 0) return callBack(null, null);
|
||||||
|
|
||||||
|
let p = 0;
|
||||||
|
let sip = 0;
|
||||||
|
|
||||||
|
// second search (in index)
|
||||||
|
const blockLen = eptr - sptr;
|
||||||
|
const blockBuffer = new Buffer(blockLen + INDEX_BLOCK_LENGTH);
|
||||||
|
low = 0;
|
||||||
|
high = blockLen / INDEX_BLOCK_LENGTH;
|
||||||
|
|
||||||
|
const _self = this;
|
||||||
|
|
||||||
|
function _innerAsyncWhile() {
|
||||||
|
|
||||||
|
if (low <= high) {
|
||||||
|
|
||||||
|
mid = (low + high) >> 1;
|
||||||
|
p = mid * INDEX_BLOCK_LENGTH;
|
||||||
|
|
||||||
|
// Use this to call the method itself as
|
||||||
|
// an asynchronize step
|
||||||
|
fs.read(_self.dbFd, blockBuffer,
|
||||||
|
0,
|
||||||
|
blockLen + INDEX_BLOCK_LENGTH,
|
||||||
|
sptr, (err) => {
|
||||||
|
|
||||||
|
if (err) {
|
||||||
|
return callBack(err, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
sip = _getLong(blockBuffer, p);
|
||||||
|
|
||||||
|
if (ip < sip) {
|
||||||
|
high = mid - 1;
|
||||||
|
setImmediate(_innerAsyncWhile);
|
||||||
|
} else {
|
||||||
|
sip = _getLong(blockBuffer, p + 4);
|
||||||
|
if (ip > sip) {
|
||||||
|
low = mid + 1;
|
||||||
|
setImmediate(_innerAsyncWhile);
|
||||||
|
} else {
|
||||||
|
sip = _getLong(blockBuffer, p + 8);
|
||||||
|
_self[ReadData](sip, (err, result) => {
|
||||||
|
callBack(err, result);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// If we found nothing, return null
|
||||||
|
return callBack(null, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_innerAsyncWhile();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
//#endregion
|
||||||
* @private
|
|
||||||
* @param {number} dataPos
|
|
||||||
* @return {SearchResult}
|
|
||||||
*/
|
|
||||||
readData(dataPos) {
|
|
||||||
if (dataPos == 0) return null;
|
|
||||||
const dataLen = (dataPos >> 24) & 0xff;
|
|
||||||
dataPos = dataPos & 0x00ffffff;
|
|
||||||
const dataBuffer = new Buffer(dataLen);
|
|
||||||
|
|
||||||
fs.readSync(this.dbFd, dataBuffer, 0, dataLen, dataPos);
|
|
||||||
|
|
||||||
const city = getLong(dataBuffer, 0);
|
|
||||||
const region = dataBuffer.toString('utf8', 4, dataLen);
|
|
||||||
|
|
||||||
return { city, region };
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
IP2Region._instances = new Map();
|
|
||||||
|
|
||||||
module.exports = IP2Region;
|
module.exports = IP2Region;
|
||||||
|
|
|
||||||
|
|
@ -1,24 +0,0 @@
|
||||||
const path = require('path');
|
|
||||||
const IP2Region = require('./ip2region');
|
|
||||||
|
|
||||||
describe('ip2region', () => {
|
|
||||||
let instance;
|
|
||||||
|
|
||||||
beforeAll(() => {
|
|
||||||
instance = IP2Region.create(path.join(__dirname, '../../data/ip2region.db'));
|
|
||||||
});
|
|
||||||
|
|
||||||
afterAll(() => {
|
|
||||||
instance.destroy();
|
|
||||||
});
|
|
||||||
|
|
||||||
test('should query', () => {
|
|
||||||
expect(instance.btreeSearchSync('120.24.78.68')).toMatchSnapshot();
|
|
||||||
expect(instance.btreeSearchSync('10.10.10.10')).toMatchSnapshot();
|
|
||||||
});
|
|
||||||
|
|
||||||
test('binarySearch', () => {
|
|
||||||
expect(instance.binarySearchSync('120.24.78.68')).toMatchSnapshot();
|
|
||||||
expect(instance.binarySearchSync('10.10.10.10')).toMatchSnapshot();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
@ -4,7 +4,8 @@
|
||||||
"description": "ip database ",
|
"description": "ip database ",
|
||||||
"main": "ip2region.js",
|
"main": "ip2region.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "jest"
|
"test": "jest",
|
||||||
|
"coverage":"npm run test && jest --coverage"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,108 @@
|
||||||
|
// This test is used for tesing of a static function `create` of IP2Region
|
||||||
|
const IP2Region = require('../ip2region');
|
||||||
|
const testIps = require('./utils/testData');
|
||||||
|
const asyncFor = require('./utils/asyncFor');
|
||||||
|
|
||||||
|
describe('Constructor Test', () => {
|
||||||
|
let instance;
|
||||||
|
|
||||||
|
beforeAll(() => {
|
||||||
|
instance = new IP2Region({ dbPath: '../../data/ip2region.db' });
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(() => {
|
||||||
|
IP2Region.destroy();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('btreeSearchSync query', () => {
|
||||||
|
for (const ip of testIps) {
|
||||||
|
expect(instance.btreeSearchSync(ip)).toMatchSnapshot();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test('binarySearchSync query', () => {
|
||||||
|
for (const ip of testIps) {
|
||||||
|
expect(instance.binarySearchSync(ip)).toMatchSnapshot();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
//#region callBack
|
||||||
|
test('binarySearch query', (done) => {
|
||||||
|
asyncFor(testIps,
|
||||||
|
(value, continueCallBack) => {
|
||||||
|
instance.binarySearch(value, (err, result) => {
|
||||||
|
expect(err).toBe(null);
|
||||||
|
expect(result).toMatchSnapshot();
|
||||||
|
continueCallBack();
|
||||||
|
});
|
||||||
|
},
|
||||||
|
() => { done() });
|
||||||
|
});
|
||||||
|
|
||||||
|
test('btreeSearch query', (done) => {
|
||||||
|
asyncFor(testIps,
|
||||||
|
(value, continueCallBack) => {
|
||||||
|
instance.btreeSearch(value, (err, result) => {
|
||||||
|
expect(err).toBe(null);
|
||||||
|
expect(result).toMatchSnapshot();
|
||||||
|
continueCallBack();
|
||||||
|
});
|
||||||
|
},
|
||||||
|
() => { done() });
|
||||||
|
});
|
||||||
|
|
||||||
|
//#endregion
|
||||||
|
|
||||||
|
//#region Async Promisify test
|
||||||
|
const node_ver = require('./utils/fetchMainVersion');
|
||||||
|
|
||||||
|
// If we have Nodejs >= 8, we now support `async` and `await`
|
||||||
|
if (node_ver >= 8) {
|
||||||
|
|
||||||
|
const asyncBinarySearch = async (ip) => {
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
instance.binarySearch(ip, (err, result) => {
|
||||||
|
if (err) {
|
||||||
|
reject(err);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
resolve(result);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
const asyncBtreeSearch = async (ip) => {
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
instance.btreeSearch(ip, (err, result) => {
|
||||||
|
if (err) {
|
||||||
|
reject(err);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
resolve(result);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
test('async binarySearch query', async () => {
|
||||||
|
for (let i = 0; i < testIps.length; ++i) {
|
||||||
|
const result = await asyncBinarySearch(testIps[i]);
|
||||||
|
expect(result).toMatchSnapshot();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test('async btreeSearch query', async () => {
|
||||||
|
for (let i = 0; i < testIps.length; ++i) {
|
||||||
|
const result = await asyncBtreeSearch(testIps[i]);
|
||||||
|
expect(result).toMatchSnapshot();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
//#endregion
|
||||||
|
});
|
||||||
|
|
@ -0,0 +1,108 @@
|
||||||
|
// This test is used for tesing of a static function `create` of IP2Region
|
||||||
|
const IP2Region = require('../ip2region');
|
||||||
|
const testIps = require('./utils/testData');
|
||||||
|
const asyncFor = require('./utils/asyncFor');
|
||||||
|
|
||||||
|
describe('Create Test', () => {
|
||||||
|
let instance;
|
||||||
|
|
||||||
|
beforeAll(() => {
|
||||||
|
instance = IP2Region.create('../../data/ip2region.db');
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(() => {
|
||||||
|
IP2Region.destroy();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('btreeSearchSync query', () => {
|
||||||
|
for (const ip of testIps) {
|
||||||
|
expect(instance.btreeSearchSync(ip)).toMatchSnapshot();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test('binarySearchSync query', () => {
|
||||||
|
for (const ip of testIps) {
|
||||||
|
expect(instance.binarySearchSync(ip)).toMatchSnapshot();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
//#region callBack
|
||||||
|
test('binarySearch query', (done) => {
|
||||||
|
asyncFor(testIps,
|
||||||
|
(value, continueCallBack) => {
|
||||||
|
instance.binarySearch(value, (err, result) => {
|
||||||
|
expect(err).toBe(null);
|
||||||
|
expect(result).toMatchSnapshot();
|
||||||
|
continueCallBack();
|
||||||
|
});
|
||||||
|
},
|
||||||
|
() => { done() });
|
||||||
|
});
|
||||||
|
|
||||||
|
test('btreeSearch query', (done) => {
|
||||||
|
asyncFor(testIps,
|
||||||
|
(value, continueCallBack) => {
|
||||||
|
instance.btreeSearch(value, (err, result) => {
|
||||||
|
expect(err).toBe(null);
|
||||||
|
expect(result).toMatchSnapshot();
|
||||||
|
continueCallBack();
|
||||||
|
});
|
||||||
|
},
|
||||||
|
() => { done() });
|
||||||
|
});
|
||||||
|
|
||||||
|
//#endregion
|
||||||
|
|
||||||
|
//#region Async Promisify test
|
||||||
|
const node_ver = require('./utils/fetchMainVersion');
|
||||||
|
|
||||||
|
// If we have Nodejs >= 8, we now support `async` and `await`
|
||||||
|
if (node_ver >= 8) {
|
||||||
|
|
||||||
|
const asyncBinarySearch = async (ip) => {
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
instance.binarySearch(ip, (err, result) => {
|
||||||
|
if (err) {
|
||||||
|
reject(err);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
resolve(result);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
const asyncBtreeSearch = async (ip) => {
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
instance.btreeSearch(ip, (err, result) => {
|
||||||
|
if (err) {
|
||||||
|
reject(err);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
resolve(result);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
test('async binarySearch query', async () => {
|
||||||
|
for (let i = 0; i < testIps.length; ++i) {
|
||||||
|
const result = await asyncBinarySearch(testIps[i]);
|
||||||
|
expect(result).toMatchSnapshot();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test('async btreeSearch query', async () => {
|
||||||
|
for (let i = 0; i < testIps.length; ++i) {
|
||||||
|
const result = await asyncBtreeSearch(testIps[i]);
|
||||||
|
expect(result).toMatchSnapshot();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
//#endregion
|
||||||
|
});
|
||||||
|
|
@ -0,0 +1,28 @@
|
||||||
|
// This test is used for tesing of exceptions
|
||||||
|
|
||||||
|
const IP2Region = require('../ip2region');
|
||||||
|
|
||||||
|
describe('Constructor Test', () => {
|
||||||
|
let instance;
|
||||||
|
|
||||||
|
beforeAll(() => {
|
||||||
|
instance = new IP2Region({ dbPath: '../../data/ip2region.db' })
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(() => {
|
||||||
|
instance.destroy();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('IP invalid test', () => {
|
||||||
|
const invalidIps = ['255.234.233', '255.255.-1.255', null, undefined, '', 'x.255.y.200'];
|
||||||
|
for (const ip of invalidIps) {
|
||||||
|
expect(() => instance.btreeSearchSync(ip)).toThrow();
|
||||||
|
expect(() => instance.binarySearchSync(ip)).toThrow();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test('File Not Found test', () => {
|
||||||
|
expect(() => new IP2Region({ dbPath: 'A Bad File or Path Here' })).toThrow();
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
@ -0,0 +1,23 @@
|
||||||
|
/**
|
||||||
|
* Async For
|
||||||
|
* @param {Array} groupArray
|
||||||
|
* @param {Function} exeCallBack
|
||||||
|
* @param {Function} finalCallBack
|
||||||
|
*/
|
||||||
|
function asyncFor(groupArray, exeCallBack, finalCallBack) {
|
||||||
|
|
||||||
|
let i = 0;
|
||||||
|
|
||||||
|
function _innerAsyncLoop() {
|
||||||
|
if (i < groupArray.length) {
|
||||||
|
exeCallBack(groupArray[i++], _innerAsyncLoop);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
finalCallBack();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_innerAsyncLoop();
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = asyncFor;
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
let node_ver = process.version
|
||||||
|
// Because nodejs's version is something like `v8.11.3`. So we should ignore `v` first
|
||||||
|
node_ver = node_ver.substr(1);
|
||||||
|
// Splitted by `.`
|
||||||
|
node_ver = node_ver.split('.');
|
||||||
|
// Take the main version number
|
||||||
|
node_ver = parseInt(node_ver[0]);
|
||||||
|
|
||||||
|
module.exports = node_ver;
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
module.exports = [
|
||||||
|
'0.0.0.0',
|
||||||
|
'10.10.10.10',
|
||||||
|
'210.109.255.230',
|
||||||
|
'192.168.0.1',
|
||||||
|
'255.255.255.255',
|
||||||
|
'77.49.66.88',
|
||||||
|
'210.248.255.231',
|
||||||
|
'35.193.251.120',
|
||||||
|
'197.84.60.202',
|
||||||
|
'183.196.233.159',
|
||||||
|
'20.108.91.101',
|
||||||
|
'120.196.148.137',
|
||||||
|
'249.255.250.200',
|
||||||
|
'112.65.1.130'
|
||||||
|
]
|
||||||
Loading…
Reference in New Issue