1.CodeStandardized 2.Support Netfx4.5 3.TestStandardized

This commit is contained in:
RocherKong 2018-07-30 00:31:49 +08:00
parent 4120c4e9c8
commit 11f527b207
23 changed files with 1136 additions and 850 deletions

View File

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

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,8 +1,9 @@
//*******************************
// Create By Rocher Kong
// Created By Rocher Kong
// Github https://github.com/RocherKong
// Date 2018.02.09
//*******************************
using IP2Region.Models;
using System;
using System.IO;
@ -19,29 +20,29 @@ namespace IP2Region
private DbConfig _dbConfig = null;
/**
* db file access handler
*/
/// <summary>
/// db file access handler
/// </summary>
private FileStream _raf = null;
/**
* header blocks buffer
*/
/// <summary>
/// header blocks buffer
/// </summary>
private long[] _headerSip = null;
private int[] _headerPtr = null;
private int _headerLength;
/**
* super blocks info
*/
/// <summary>
/// super blocks info
/// </summary>
private long _firstIndexPtr = 0;
private long _lastIndexPtr = 0;
private int _totalIndexBlocks = 0;
/**
* for memory mode
* the original db binary string
*/
/// <summary>
/// for memory mode
/// the original db binary string
/// </summary>
private byte[] _dbBinStr = null;
/// <summary>
@ -50,22 +51,16 @@ namespace IP2Region
private DataBlock GetByIndexPtr(long ptr)
{
_raf.Seek(ptr, SeekOrigin.Begin);
byte[]
buffer = new byte[12];
byte[] buffer = new byte[12];
_raf.Read(buffer, 0, buffer.Length);
long extra = Utils.getIntLong(buffer, 8);
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);
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);
}
@ -94,8 +89,8 @@ namespace IP2Region
_raf.Read(_dbBinStr, 0, _dbBinStr.Length);
//initialize the global vars
_firstIndexPtr = Utils.getIntLong(_dbBinStr, 0);
_lastIndexPtr = Utils.getIntLong(_dbBinStr, 4);
_firstIndexPtr = Utils.GetIntLong(_dbBinStr, 0);
_lastIndexPtr = Utils.GetIntLong(_dbBinStr, 4);
_totalIndexBlocks = (int)((_lastIndexPtr - _firstIndexPtr) / blen) + 1;
}
@ -108,7 +103,7 @@ namespace IP2Region
int m = (l + h) >> 1;
int p = (int)(_firstIndexPtr + m * blen);
sip = Utils.getIntLong(_dbBinStr, p);
sip = Utils.GetIntLong(_dbBinStr, p);
if (ip < sip)
{
@ -116,14 +111,14 @@ namespace IP2Region
}
else
{
sip = Utils.getIntLong(_dbBinStr, p + 4);
sip = Utils.GetIntLong(_dbBinStr, p + 4);
if (ip > sip)
{
l = m + 1;
}
else
{
sip = Utils.getIntLong(_dbBinStr, p + 8);
sip = Utils.GetIntLong(_dbBinStr, p + 8);
break;
}
}
@ -135,7 +130,7 @@ namespace IP2Region
//get the data
int dataLen = (int)((sip >> 24) & 0xFF);
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);
return new DataBlock(city_id, region, dataPtr);
@ -146,7 +141,7 @@ namespace IP2Region
/// </summary>
public DataBlock MemorySearch(string ip)
{
return MemorySearch(Utils.ip2long(ip));
return MemorySearch(Utils.Ip2long(ip));
}
/// <summary>
@ -158,10 +153,8 @@ namespace IP2Region
if (_headerSip == null)
{
_raf.Seek(8L, SeekOrigin.Begin); //pass the super block
//byte[] b = new byte[dbConfig.getTotalHeaderSize()];
byte[] b = new byte[4096];
_raf.Read(b, 0, b.Length);
//fill the header
int len = b.Length >> 3, idx = 0; //b.lenght / 8
_headerSip = new long[len];
@ -169,15 +162,14 @@ namespace IP2Region
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);
dataPtrTemp = Utils.GetIntLong(b, i + 4);
if (dataPtrTemp == 0) break;
_headerSip[idx] = startIp;
_headerPtr[idx] = (int)dataPtrTemp;
idx++;
}
_headerLength = idx;
}
@ -190,14 +182,11 @@ namespace IP2Region
{
return GetByIndexPtr(_headerPtr[_headerLength - 1]);
}
int l = 0, h = _headerLength, sptr = 0, eptr = 0;
int m = 0;
while (l <= h)
{
m = (l + h) >> 1;
//perfectly matched, just return it
if (ip == _headerSip[m])
{
@ -246,58 +235,49 @@ namespace IP2Region
l = m + 1;
}
}
//match nothing just stop it
if (sptr == 0) return null;
//2. search the index blocks to define the data
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);
l = 0; h = blockLen / blen;
long sip = 0;
int p = 0;
while (l <= h)
{
m = (l + h) >> 1;
p = m * blen;
sip = Utils.getIntLong(iBuffer, p);
sip = Utils.GetIntLong(iBuffer, p);
if (ip < sip)
{
h = m - 1;
}
else
{
sip = Utils.getIntLong(iBuffer, p + 4);
sip = Utils.GetIntLong(iBuffer, p + 4);
if (ip > sip)
{
l = m + 1;
}
else
{
sip = Utils.getIntLong(iBuffer, p + 8);
sip = Utils.GetIntLong(iBuffer, p + 8);
break;
}
}
}
//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);
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);
}
@ -306,7 +286,7 @@ namespace IP2Region
/// </summary>
public DataBlock BtreeSearch(string ip)
{
return BtreeSearch(Utils.ip2long(ip));
return BtreeSearch(Utils.Ip2long(ip));
}
/// <summary>
@ -321,8 +301,8 @@ namespace IP2Region
byte[] superBytes = new byte[8];
_raf.Read(superBytes, 0, superBytes.Length);
//initialize the global vars
_firstIndexPtr = Utils.getIntLong(superBytes, 0);
_lastIndexPtr = Utils.getIntLong(superBytes, 4);
_firstIndexPtr = Utils.GetIntLong(superBytes, 0);
_lastIndexPtr = Utils.GetIntLong(superBytes, 4);
_totalIndexBlocks = (int)((_lastIndexPtr - _firstIndexPtr) / blen) + 1;
}
@ -330,46 +310,40 @@ namespace IP2Region
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);
sip = Utils.GetIntLong(buffer, 0);
if (ip < sip)
{
h = m - 1;
}
else
{
sip = Utils.getIntLong(buffer, 4);
sip = Utils.GetIntLong(buffer, 4);
if (ip > sip)
{
l = m + 1;
}
else
{
sip = Utils.getIntLong(buffer, 8);
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);
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);
}
@ -378,43 +352,40 @@ namespace IP2Region
/// </summary>
public DataBlock BinarySearch(String ip)
{
return BinarySearch(Utils.ip2long(ip));
return BinarySearch(Utils.Ip2long(ip));
}
#endregion
#region Async Methods
/// <summary>
/// Get the region throught the ip address with memory binary search algorithm.
/// </summary>
public async Task<DataBlock> AsyncMemorySearch(string ip)
public async Task<DataBlock> MemorySearchAsync(string ip)
{
return await Task.FromResult(MemorySearch(ip));
}
/// <summary>
/// Get the region throught the ip address with b-tree search algorithm.
/// </summary>
public async Task<DataBlock> AsyncBtreeSearch(string ip)
public async Task<DataBlock> BtreeSearchAsync(string ip)
{
return await Task.FromResult(BtreeSearch(ip));
}
/// <summary>
/// Get the region throught the ip address with binary search algorithm.
/// </summary>
public async Task<DataBlock> AsyncBinarySearch(string ip)
public async Task<DataBlock> BinarySearchAsync(string ip)
{
return await Task.FromResult(BinarySearch(ip));
}
#endregion
/// <summary>
/// Close the db.
/// </summary>
public void Close()
{
_headerSip = null; //let gc do its work
_headerSip = null;
_headerPtr = null;
_dbBinStr = null;
_raf.Close();

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net46</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net46;net45;net47</TargetFrameworks>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Rocher</Authors>
<Company>Rocher</Company>
@ -10,7 +10,7 @@
<Copyright>Rocher</Copyright>
<PackageTags>ip2region c# IP</PackageTags>
<RepositoryUrl>https://gitee.com/rocherkong/IP2Region</RepositoryUrl>
<Version>1.1.0</Version>
<Version>1.2.0</Version>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|netstandard2.0|AnyCPU'">
@ -18,7 +18,7 @@
</PropertyGroup>
<ItemGroup>
<None Update="DBFile\ip2region.db">
<None Update="DB\ip2region.db">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

View File

@ -1,13 +1,8 @@
//*******************************
// Create By Rocher Kong
// Created By Rocher Kong
// Github https://github.com/RocherKong
// Date 2018.02.09
//
// Modified By Dongwei
// Date 2018.07.18
// GitHub https://github.com/Maledong
//*******************************
namespace IP2Region.Models
{
public class DataBlock

View File

@ -1,13 +1,8 @@
//*******************************
// Create By Rocher Kong
// Created By Rocher Kong
// Github https://github.com/RocherKong
// Date 2018.02.09
//
// Modified By Dongwei
// Date 2018.07.18
// GitHub https://github.com/Maledong
//*******************************
using System;
namespace IP2Region.Models

View File

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

View File

@ -1,11 +1,7 @@
//*******************************
// Create By Rocher Kong
// Created By Rocher Kong
// Github https://github.com/RocherKong
// Date 2018.02.09
//
// Modified By Dongwei
// Date 2018.07.18
// GitHub https://github.com/Maledong
//*******************************
namespace IP2Region
@ -56,12 +52,12 @@ namespace IP2Region
*/
byte[] b = new byte[12];
Utils.writeIntLong(b, 0, StartIP); //start ip
Utils.writeIntLong(b, 4, EndIp); //end ip
Utils.WriteIntLong(b, 0, StartIP); //start ip
Utils.WriteIntLong(b, 4, EndIp); //end ip
//write the data ptr and the length
long mix = DataPtr | ((DataLen << 24) & 0xFF000000L);
Utils.writeIntLong(b, 8, mix);
Utils.WriteIntLong(b, 8, mix);
return b;
}

View File

@ -1,12 +1,9 @@
//*******************************
// Create By Rocher Kong
// Created By Rocher Kong
// Github https://github.com/RocherKong
// Date 2018.02.09
//
// Modified By Dongwei
// Date 2018.07.18
// GitHub https://github.com/Maledong
//*******************************
using System;
namespace IP2Region
@ -14,7 +11,6 @@ namespace IP2Region
public class IPInValidException : Exception
{
const string ERROR_MSG = "IP Illigel. Please input a valid IP.";
public IPInValidException() : base(ERROR_MSG) { }
}
internal static class Utils
@ -33,7 +29,7 @@ namespace IP2Region
/// <summary>
/// Write a int to a byte array.
/// </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 >> 8) & 0xFF);
@ -44,7 +40,7 @@ namespace IP2Region
/// <summary>
/// Get a int from a byte array start from the specifiled offset.
/// </summary>
public static long getIntLong(byte[] b, int offset)
public static long GetIntLong(byte[] b, int offset)
{
return (
((b[offset++] & 0x000000FFL)) |
@ -57,7 +53,7 @@ namespace IP2Region
/// <summary>
/// Get a int from a byte array start from the specifield offset.
/// </summary>
public static int getInt3(byte[] b, int offset)
public static int GetInt3(byte[] b, int offset)
{
return (
(b[offset++] & 0x000000FF) |
@ -66,7 +62,7 @@ namespace IP2Region
);
}
public static int getInt2(byte[] b, int offset)
public static int GetInt2(byte[] b, int offset)
{
return (
(b[offset++] & 0x000000FF) |
@ -74,17 +70,16 @@ namespace IP2Region
);
}
public static int getInt1(byte[] b, int offset)
public static int GetInt1(byte[] b, int offset)
{
return (
(b[offset] & 0x000000FF)
);
}
/// <summary>
/// String ip to long ip.
/// </summary>
public static long ip2long(string ip)
public static long Ip2long(string ip)
{
string[] p = ip.Split('.');
if (p.Length != 4) throw new IPInValidException();
@ -97,7 +92,6 @@ namespace IP2Region
throw new IPInValidException();
}
}
var bip1 = long.TryParse(p[0], out long ip1);
var bip2 = long.TryParse(p[1], out long ip2);
var bip3 = long.TryParse(p[2], out long ip3);
@ -109,8 +103,6 @@ namespace IP2Region
{
throw new IPInValidException();
}
long p1 = ((ip1 << 24) & 0xFF000000);
long p2 = ((ip2 << 16) & 0x00FF0000);
long p3 = ((ip3 << 8) & 0x0000FF00);
@ -121,7 +113,7 @@ namespace IP2Region
/// <summary>
/// Int to ip string.
/// </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}";
}

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菜单栏上“测试”=>“运行”=>“所有测试”,检测是否通过全部测试即可(你可以根据需要自行增加特定的单元测试)。