From 8dddff0e2bd2d3231e586957947278763891817b Mon Sep 17 00:00:00 2001 From: Soar360 Date: Thu, 3 Dec 2020 13:34:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E7=89=88=20C#=20=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E7=94=9F=E6=88=90=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- maker/csharp/.gitignore | 341 ------------------ maker/csharp/DbMaker.sln | 25 -- maker/csharp/DbMaker/DbMaker.csproj | 8 - .../csharp/DbMaker/DbMakerConfigException.cs | 12 - maker/csharp/DbMaker/Extensions.cs | 40 -- .../DbMaker/Properties/launchSettings.json | 8 - maker/csharp/DbMaker/RandomAccessFile.cs | 64 ---- maker/csharp/README.md | 18 + maker/csharp/dbMaker.exe | Bin 0 -> 20480 bytes maker/csharp/src/.gitignore | 199 ++++++++++ .../IP2RegionDotNetDbMaker}/DataBlock.cs | 42 +-- .../IP2RegionDotNetDbMaker}/DbConfig.cs | 29 +- .../IP2RegionDotNetDbMaker}/DbMaker.cs | 239 ++++++------ .../DbMakerConfigException.cs | 17 + .../IP2RegionDotNetDbMaker}/DbSearcher.cs | 244 ++++++------- .../IP2RegionDotNetDbMaker}/HeaderBlock.cs | 27 +- .../IP2RegionDotNetDbMaker.csproj | 9 + .../IP2RegionDotNetDbMaker}/IndexBlock.cs | 19 +- .../csharp/src/IP2RegionDotNetDbMaker/Mock.cs | 244 +++++++++++++ .../IP2RegionDotNetDbMaker}/Util.cs | 41 +-- 20 files changed, 786 insertions(+), 840 deletions(-) delete mode 100644 maker/csharp/.gitignore delete mode 100644 maker/csharp/DbMaker.sln delete mode 100644 maker/csharp/DbMaker/DbMaker.csproj delete mode 100644 maker/csharp/DbMaker/DbMakerConfigException.cs delete mode 100644 maker/csharp/DbMaker/Extensions.cs delete mode 100644 maker/csharp/DbMaker/Properties/launchSettings.json delete mode 100644 maker/csharp/DbMaker/RandomAccessFile.cs create mode 100644 maker/csharp/README.md create mode 100644 maker/csharp/dbMaker.exe create mode 100644 maker/csharp/src/.gitignore rename maker/csharp/{DbMaker => src/IP2RegionDotNetDbMaker}/DataBlock.cs (72%) rename maker/csharp/{DbMaker => src/IP2RegionDotNetDbMaker}/DbConfig.cs (78%) rename maker/csharp/{DbMaker => src/IP2RegionDotNetDbMaker}/DbMaker.cs (62%) create mode 100644 maker/csharp/src/IP2RegionDotNetDbMaker/DbMakerConfigException.cs rename maker/csharp/{DbMaker => src/IP2RegionDotNetDbMaker}/DbSearcher.cs (68%) rename maker/csharp/{DbMaker => src/IP2RegionDotNetDbMaker}/HeaderBlock.cs (79%) create mode 100644 maker/csharp/src/IP2RegionDotNetDbMaker/IP2RegionDotNetDbMaker.csproj rename maker/csharp/{DbMaker => src/IP2RegionDotNetDbMaker}/IndexBlock.cs (89%) create mode 100644 maker/csharp/src/IP2RegionDotNetDbMaker/Mock.cs rename maker/csharp/{DbMaker => src/IP2RegionDotNetDbMaker}/Util.cs (78%) diff --git a/maker/csharp/.gitignore b/maker/csharp/.gitignore deleted file mode 100644 index 3e8a155..0000000 --- a/maker/csharp/.gitignore +++ /dev/null @@ -1,341 +0,0 @@ -## Ignore Visual Studio temporary files, build results, and -## files generated by popular Visual Studio add-ons. -## -## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore - -# User-specific files -*.rsuser -*.suo -*.user -*.userosscache -*.sln.docstates - -# User-specific files (MonoDevelop/Xamarin Studio) -*.userprefs - -# Build results -[Dd]ebug/ -[Dd]ebugPublic/ -[Rr]elease/ -[Rr]eleases/ -x64/ -x86/ -[Aa][Rr][Mm]/ -[Aa][Rr][Mm]64/ -bld/ -[Bb]in/ -[Oo]bj/ -[Ll]og/ - -# Visual Studio 2015/2017 cache/options directory -.vs/ -# Uncomment if you have tasks that create the project's static files in wwwroot -#wwwroot/ - -# Visual Studio 2017 auto generated files -Generated\ Files/ - -# MSTest test Results -[Tt]est[Rr]esult*/ -[Bb]uild[Ll]og.* - -# NUNIT -*.VisualState.xml -TestResult.xml - -# Build Results of an ATL Project -[Dd]ebugPS/ -[Rr]eleasePS/ -dlldata.c - -# Benchmark Results -BenchmarkDotNet.Artifacts/ - -# .NET Core -project.lock.json -project.fragment.lock.json -artifacts/ - -# StyleCop -StyleCopReport.xml - -# Files built by Visual Studio -*_i.c -*_p.c -*_h.h -*.ilk -*.meta -*.obj -*.iobj -*.pch -*.pdb -*.ipdb -*.pgc -*.pgd -*.rsp -*.sbr -*.tlb -*.tli -*.tlh -*.tmp -*.tmp_proj -*_wpftmp.csproj -*.log -*.vspscc -*.vssscc -.builds -*.pidb -*.svclog -*.scc - -# Chutzpah Test files -_Chutzpah* - -# Visual C++ cache files -ipch/ -*.aps -*.ncb -*.opendb -*.opensdf -*.sdf -*.cachefile -*.VC.db -*.VC.VC.opendb - -# Visual Studio profiler -*.psess -*.vsp -*.vspx -*.sap - -# Visual Studio Trace Files -*.e2e - -# TFS 2012 Local Workspace -$tf/ - -# Guidance Automation Toolkit -*.gpState - -# ReSharper is a .NET coding add-in -_ReSharper*/ -*.[Rr]e[Ss]harper -*.DotSettings.user - -# JustCode is a .NET coding add-in -.JustCode - -# TeamCity is a build add-in -_TeamCity* - -# DotCover is a Code Coverage Tool -*.dotCover - -# AxoCover is a Code Coverage Tool -.axoCover/* -!.axoCover/settings.json - -# Visual Studio code coverage results -*.coverage -*.coveragexml - -# NCrunch -_NCrunch_* -.*crunch*.local.xml -nCrunchTemp_* - -# MightyMoose -*.mm.* -AutoTest.Net/ - -# Web workbench (sass) -.sass-cache/ - -# Installshield output folder -[Ee]xpress/ - -# DocProject is a documentation generator add-in -DocProject/buildhelp/ -DocProject/Help/*.HxT -DocProject/Help/*.HxC -DocProject/Help/*.hhc -DocProject/Help/*.hhk -DocProject/Help/*.hhp -DocProject/Help/Html2 -DocProject/Help/html - -# Click-Once directory -publish/ - -# Publish Web Output -*.[Pp]ublish.xml -*.azurePubxml -# Note: Comment the next line if you want to checkin your web deploy settings, -# but database connection strings (with potential passwords) will be unencrypted -*.pubxml -*.publishproj - -# Microsoft Azure Web App publish settings. Comment the next line if you want to -# checkin your Azure Web App publish settings, but sensitive information contained -# in these scripts will be unencrypted -PublishScripts/ - -# NuGet Packages -*.nupkg -# The packages folder can be ignored because of Package Restore -**/[Pp]ackages/* -# except build/, which is used as an MSBuild target. -!**/[Pp]ackages/build/ -# Uncomment if necessary however generally it will be regenerated when needed -#!**/[Pp]ackages/repositories.config -# NuGet v3's project.json files produces more ignorable files -*.nuget.props -*.nuget.targets - -# Microsoft Azure Build Output -csx/ -*.build.csdef - -# Microsoft Azure Emulator -ecf/ -rcf/ - -# Windows Store app package directories and files -AppPackages/ -BundleArtifacts/ -Package.StoreAssociation.xml -_pkginfo.txt -*.appx - -# Visual Studio cache files -# files ending in .cache can be ignored -*.[Cc]ache -# but keep track of directories ending in .cache -!?*.[Cc]ache/ - -# Others -ClientBin/ -~$* -*~ -*.dbmdl -*.dbproj.schemaview -*.jfm -*.pfx -*.publishsettings -orleans.codegen.cs - -# Including strong name files can present a security risk -# (https://github.com/github/gitignore/pull/2483#issue-259490424) -#*.snk - -# Since there are multiple workflows, uncomment next line to ignore bower_components -# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) -#bower_components/ -# ASP.NET Core default setup: bower directory is configured as wwwroot/lib/ and bower restore is true -**/wwwroot/lib/ - -# RIA/Silverlight projects -Generated_Code/ - -# Backup & report files from converting an old project file -# to a newer Visual Studio version. Backup files are not needed, -# because we have git ;-) -_UpgradeReport_Files/ -Backup*/ -UpgradeLog*.XML -UpgradeLog*.htm -ServiceFabricBackup/ -*.rptproj.bak - -# SQL Server files -*.mdf -*.ldf -*.ndf - -# Business Intelligence projects -*.rdl.data -*.bim.layout -*.bim_*.settings -*.rptproj.rsuser - -# Microsoft Fakes -FakesAssemblies/ - -# GhostDoc plugin setting file -*.GhostDoc.xml - -# Node.js Tools for Visual Studio -.ntvs_analysis.dat -node_modules/ - -# Visual Studio 6 build log -*.plg - -# Visual Studio 6 workspace options file -*.opt - -# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) -*.vbw - -# Visual Studio LightSwitch build output -**/*.HTMLClient/GeneratedArtifacts -**/*.DesktopClient/GeneratedArtifacts -**/*.DesktopClient/ModelManifest.xml -**/*.Server/GeneratedArtifacts -**/*.Server/ModelManifest.xml -_Pvt_Extensions - -# Paket dependency manager -.paket/paket.exe -paket-files/ - -# FAKE - F# Make -.fake/ - -# JetBrains Rider -.idea/ -*.sln.iml - -# CodeRush personal settings -.cr/personal - -# Python Tools for Visual Studio (PTVS) -__pycache__/ -*.pyc - -# Cake - Uncomment if you are using it -# tools/** -# !tools/packages.config - -# Tabs Studio -*.tss - -# Telerik's JustMock configuration file -*.jmconfig - -# BizTalk build output -*.btp.cs -*.btm.cs -*.odx.cs -*.xsd.cs - -# OpenCover UI analysis results -OpenCover/ - -# Azure Stream Analytics local run output -ASALocalRun/ - -# MSBuild Binary and Structured Log -*.binlog - -# NVidia Nsight GPU debugger configuration file -*.nvuser - -# MFractors (Xamarin productivity tool) working folder -.mfractor/ - -# Local History for Visual Studio -.localhistory/ - -# BeatPulse healthcheck temp database -healthchecksdb diff --git a/maker/csharp/DbMaker.sln b/maker/csharp/DbMaker.sln deleted file mode 100644 index bd53da2..0000000 --- a/maker/csharp/DbMaker.sln +++ /dev/null @@ -1,25 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.29102.190 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DbMaker", "DbMaker\DbMaker.csproj", "{724424E3-EC3C-4819-A29C-AA7FD7F2357F}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {724424E3-EC3C-4819-A29C-AA7FD7F2357F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {724424E3-EC3C-4819-A29C-AA7FD7F2357F}.Debug|Any CPU.Build.0 = Debug|Any CPU - {724424E3-EC3C-4819-A29C-AA7FD7F2357F}.Release|Any CPU.ActiveCfg = Release|Any CPU - {724424E3-EC3C-4819-A29C-AA7FD7F2357F}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {135F4345-E533-41E0-ACA9-7F345351555D} - EndGlobalSection -EndGlobal diff --git a/maker/csharp/DbMaker/DbMaker.csproj b/maker/csharp/DbMaker/DbMaker.csproj deleted file mode 100644 index 2c0dacc..0000000 --- a/maker/csharp/DbMaker/DbMaker.csproj +++ /dev/null @@ -1,8 +0,0 @@ - - - - Exe - netcoreapp3.1 - - - diff --git a/maker/csharp/DbMaker/DbMakerConfigException.cs b/maker/csharp/DbMaker/DbMakerConfigException.cs deleted file mode 100644 index 7e1399e..0000000 --- a/maker/csharp/DbMaker/DbMakerConfigException.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; - -namespace DbMaker -{ - public class DbMakerConfigException : Exception - { - public DbMakerConfigException(String message) : base(message) - { - - } - } -} \ No newline at end of file diff --git a/maker/csharp/DbMaker/Extensions.cs b/maker/csharp/DbMaker/Extensions.cs deleted file mode 100644 index 092c1f4..0000000 --- a/maker/csharp/DbMaker/Extensions.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace DbMaker -{ - public static class Extensions - { - public static byte[] getBytes(this string str, string encode) - { - return Encoding.GetEncoding(encode).GetBytes(str); - } - - public static Boolean containsKey(this IDictionary dic, TKey key) - { - return dic.ContainsKey(key); - } - - public static TValue get(this IDictionary dic, TKey key) - { - return dic[key]; - } - - public static void put(this IDictionary dic, TKey key, TValue value) - { - //dic.Add(key,value); - dic[key] = value; - } - - public static void add(this IList list, TItem item) - { - list.Add(item); - } - - public static void printStackTrace(this Exception e) - { - Console.WriteLine(e); - } - } -} \ No newline at end of file diff --git a/maker/csharp/DbMaker/Properties/launchSettings.json b/maker/csharp/DbMaker/Properties/launchSettings.json deleted file mode 100644 index 7345383..0000000 --- a/maker/csharp/DbMaker/Properties/launchSettings.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "profiles": { - "DbMaker": { - "commandName": "Project", - "commandLineArgs": " -src ip.merge.txt -region global_region.csv" - } - } -} \ No newline at end of file diff --git a/maker/csharp/DbMaker/RandomAccessFile.cs b/maker/csharp/DbMaker/RandomAccessFile.cs deleted file mode 100644 index 585821e..0000000 --- a/maker/csharp/DbMaker/RandomAccessFile.cs +++ /dev/null @@ -1,64 +0,0 @@ -using System; -using System.IO; - -namespace DbMaker -{ - public class RandomAccessFile : IDisposable - { - private readonly Stream _stream; - - public RandomAccessFile(String file) : this(File.Open(file, FileMode.OpenOrCreate)) - { - - } - - public RandomAccessFile(Stream stream) - { - if (stream == null) - { - throw new ArgumentNullException(nameof(stream)); - } - _stream = stream; - } - - public void Dispose() - { - _stream?.Dispose(); - } - - public long length() - { - return _stream.Length; - } - - public void seek(long position) - { - _stream.Seek(position, SeekOrigin.Begin); - } - - public void readFully(byte[] dbBinStr, int offset, int count) - { - _stream.Read(dbBinStr, offset, count); - } - - public void close() - { - _stream.Dispose(); - } - - public void write(byte[] bytes) - { - _stream.Write(bytes); - } - - public void write(int i) - { - - } - - public long getFilePointer() - { - return _stream.Position; - } - } -} \ No newline at end of file diff --git a/maker/csharp/README.md b/maker/csharp/README.md new file mode 100644 index 0000000..309759b --- /dev/null +++ b/maker/csharp/README.md @@ -0,0 +1,18 @@ +# db maker C# implementation + +### 1, 如何编译 ? +``` +cd src +dotnet build +``` + + +### 2, 如何生成数据库文件 ? +``` +dbMaker -src ./data/ip.merge.txt -region ./data/global_region.csv +``` + +### 3, 迁移介绍 + +https://www.coderbusy.com/archives/855.html + diff --git a/maker/csharp/dbMaker.exe b/maker/csharp/dbMaker.exe new file mode 100644 index 0000000000000000000000000000000000000000..d0f895fc564e8481b511298900f1bbec82f9ed7a GIT binary patch literal 20480 zcmeHvYj7Obb>`{EO!o{1L-q_Xc#s5#03So*!KW08A|-+ZNJ1hAk^o6Sq67{GLvko! z8tNHPgh)D2EXU5u(n=*pYteS3sO*)Kvdclvk$8jyzF#3mn|0Ea(W`4_GXLeLNVPpG@PDx zC-Xh^_0g3+>x2D7`!$1ZEWY_lEbeS|9`8eE2;~n4R~Ko zpPq95Tp94weJIc-w)%Plk}FMgqQ@(F5Uq5ZM}-ZoWdv|1{_FAbQ!WTuS9%&h=5+wS znrkP~t{PCPbFiN;W~oz%p1XzUBP)qCz%4ayWtw+#-LGe$LKMu5i{N4E=0#{?>Y zGIQM~-7r%bG{B5?$qkz53(z6`Jd;N|DGa<$aR@_4;C(*QLKO4?H}1o{uiHn2Uem{y z!-`zD4>#zQJ~|zP4kpVf?S;b8uu}2;OW++BVH;k4;r&a4x>DsjXl7dNvO4VULC?BQ zvev;Coi;)S!p;fYX0+Xvx?)7bT`}^Y?@Xx`JlqR|edUM@206U2?aP@V3 z$v>;D2U&-9Fq<>z4ovh$eV#7P#H2Y{w0J~_#)^{ zy;P6TP{bt+aP%T~2sMbkaneJIlzC!kxrcij`lhaEbyin`JdDDOnOTI6B~0q*{p+9y z`>-E(xs22q_YU@l(e`i~kIbC{=;v7z7sA3dXuagH3K2Bvr36=PRGH_J4I#P>jsnav z#`-|VWZa40^lw;>cOHsllIY^MmXI03C4@^jxu_wzxN|2ti@mQfQ7_{h$9bf%FS1ZDO zCsn0K4extsE!_peI^D{^Ky>&~rGwX~1&TMsw*Cd%VrRC2C}wAF!i^Pdaay`UTmOig zQkk29T;2g}tJ4bXb!eA5OF_H0hPKJ4bs93rK;dMwcNcWEWwd=)TTaSpa+(=h=!URzR*ZeM%vrW= z&DE<{y@x8MdY?tra!)YZwnST|2`=`2#Ldm9Ddyhm74n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Q0I^V8W< z_jDoWxh3~xxjW}hZ_AdZd(LibPfuryg_HSGIau689+;)m6=nmIIN8BFgatbBiq)Pj zV!hkZJ~)RLPg4aP9YNzBynSk4uQ-&wSw4b0>ApjKHnmGX$S&n`vv{vF=fl8wI1z)M z`N@M`;Vh0GPvuMRkn84vYZ_OT57v#`ENi?!e>Ojrp5k&xd$u%CJnLfJYfsM>)bZJl z_LDe~%lk$Nll9lpCQyC-0^PT-uQ&k&+t&vpBwxZH0RAOhC)9V;>Z`ne2?qVE4gVYu F{2$i - */ +*/ public class DataBlock { /** - * city id - */ + * city id + */ private int city_id; /** - * region address - */ + * region address + */ private String region; /** - * region ptr in the db file - */ + * region ptr in the db file + */ private int dataPtr; /** - * construct method - * - * @param city_id - * @param region region string - * @param dataPtr data ptr - */ + * construct method + * + * @param city_id + * @param region region string + * @param dataPtr data ptr + */ public DataBlock(int city_id, String region, int dataPtr) { this.city_id = city_id; @@ -42,7 +41,6 @@ namespace DbMaker public DataBlock(int city_id, String region) : this(city_id, region, 0) { - //this(city_id, region, 0); } public int getCityId() @@ -78,12 +76,14 @@ namespace DbMaker return this; } - public override String ToString() + + public String toString() { StringBuilder sb = new StringBuilder(); - sb.Append(city_id).Append('|').Append(region).Append('|').Append(dataPtr); - return sb.ToString(); + sb.append(city_id).append('|').append(region).append('|').append(dataPtr); + return sb.toString(); } + } -} \ No newline at end of file +} diff --git a/maker/csharp/DbMaker/DbConfig.cs b/maker/csharp/src/IP2RegionDotNetDbMaker/DbConfig.cs similarity index 78% rename from maker/csharp/DbMaker/DbConfig.cs rename to maker/csharp/src/IP2RegionDotNetDbMaker/DbConfig.cs index 56f0c58..c3fcb2b 100644 --- a/maker/csharp/DbMaker/DbConfig.cs +++ b/maker/csharp/src/IP2RegionDotNetDbMaker/DbConfig.cs @@ -1,30 +1,31 @@ -namespace DbMaker +using System; +namespace IP2RegionDotNetDbMaker { /** * database configuration class * * @author chenxin - */ +*/ public class DbConfig { /** - * total header data block size - */ + * total header data block size + */ private int totalHeaderSize; /** - * max index data block size - * u should always choice the fastest read block size - */ + * max index data block size + * u should always choice the fastest read block size + */ private int indexBlockSize; /** - * construct method - * - * @param totalHeaderSize - * @throws DbMakerConfigException - */ + * construct method + * + * @param totalHeaderSize + * @ + */ public DbConfig(int totalHeaderSize) { if ((totalHeaderSize % 8) != 0) @@ -38,7 +39,6 @@ namespace DbMaker public DbConfig() : this(8 * 2048) { - //this(8 * 2048); } public int getTotalHeaderSize() @@ -63,5 +63,4 @@ namespace DbMaker return this; } } - -} \ No newline at end of file +} diff --git a/maker/csharp/DbMaker/DbMaker.cs b/maker/csharp/src/IP2RegionDotNetDbMaker/DbMaker.cs similarity index 62% rename from maker/csharp/DbMaker/DbMaker.cs rename to maker/csharp/src/IP2RegionDotNetDbMaker/DbMaker.cs index 5e3dce5..41648d1 100644 --- a/maker/csharp/DbMaker/DbMaker.cs +++ b/maker/csharp/src/IP2RegionDotNetDbMaker/DbMaker.cs @@ -1,14 +1,9 @@ -using System; -using System.Collections.Generic; -using System.Globalization; -using System.IO; -using System.Linq; -using System.Text; -using DbMaker; +using System; -namespace DbMaker +namespace IP2RegionDotNetDbMaker { + /** * fast ip db maker * @@ -50,24 +45,24 @@ namespace DbMaker /** * ip source file path */ - private FileStream ipSrcFile; + private File ipSrcFile; /** * buffer */ - private List indexPool; - private List headerPool; + private LinkedList indexPool; + private LinkedList headerPool; /** * global region Id mapping data */ - private FileStream globalRegionFile = null; - private Dictionary globalRegionMap = null; + private File globalRegionFile = null; + private HashMap globalRegionMap = null; /** * region and data ptr mapping data */ - private Dictionary regionPtrPool = null; + private HashMap regionPtrPool = null; /** * construct method @@ -84,18 +79,17 @@ namespace DbMaker String globalRegionFile) { this.dbConfig = config; - this.ipSrcFile = File.OpenRead(ipSrcFile); //new File(ipSrcFile); - this.globalRegionMap = new Dictionary(); //new HashMap(); - this.regionPtrPool = new Dictionary(); //new HashMap(); + this.ipSrcFile = new File(ipSrcFile); + this.globalRegionMap = new HashMap(); + this.regionPtrPool = new HashMap(); if (globalRegionFile != null) { - this.globalRegionFile = File.OpenRead(globalRegionFile); //new File(globalRegionFile); + this.globalRegionFile = new File(globalRegionFile); } - //if ( this.ipSrcFile.exists() == false ) { - if (!File.Exists(ipSrcFile)) + if (this.ipSrcFile.exists() == false) { - throw new IOException("Error: Invalid file path " + ipSrcFile); + throw new Exception("Error: Invalid file path " + ipSrcFile); } } @@ -103,7 +97,7 @@ namespace DbMaker * initialize the db file * * @param raf - * @throws IOException + * @ */ private void initDbFile(RandomAccessFile raf) { @@ -112,15 +106,15 @@ namespace DbMaker raf.write(new byte[8]); //super block raf.write(new byte[dbConfig.getTotalHeaderSize()]); //header block - headerPool = new List(); //new LinkedList(); - indexPool = new List(); //new LinkedList(); + headerPool = new LinkedList(); + indexPool = new LinkedList(); } /** * make the Db file * * @param dbFile target output file path - * @throws IOException + * @ */ public void make(String dbFile) { @@ -128,30 +122,24 @@ namespace DbMaker if (globalRegionFile != null) { Console.WriteLine("+-Try to load the global region data ..."); - StreamReader greader = new StreamReader((globalRegionFile)); + BufferedReader greader = new BufferedReader(new FileReader(globalRegionFile)); String gline = null; - while (!greader.EndOfStream) + while ((gline = greader.readLine()) != null) { - gline = greader.ReadLine();// != null - if (String.IsNullOrWhiteSpace(gline)) - { - continue; - } - - String[] p = gline.Split(","); + String[] p = gline.split(","); if (p.Length != 5) continue; //push the mapping globalRegionMap.put(p[2], Int32.Parse(p[0])); } - greader.Close(); + greader.close(); Console.WriteLine("|--[Ok]"); } //alloc the header size - StreamReader reader = new StreamReader(this.ipSrcFile); - RandomAccessFile raf = new RandomAccessFile(dbFile); + BufferedReader reader = new BufferedReader(new FileReader(this.ipSrcFile)); + RandomAccessFile raf = new RandomAccessFile(dbFile, "rw"); //init the db file initDbFile(raf); @@ -160,31 +148,25 @@ namespace DbMaker //analysis main loop Console.WriteLine("+-Try to write the data blocks ... "); String line = null; - while (!reader.EndOfStream) + while ((line = reader.readLine()) != null) { - //line = reader.ReadLine() - line = reader.ReadLine(); - if (String.IsNullOrWhiteSpace(line)) - { - continue; - } - line = line.Trim(); - if (line.Length == 0) continue; - if (line[0] == '#') continue; + line = line.trim(); + if (line.length() == 0) continue; + if (line.charAt(0) == '#') continue; //1. get the start ip int sIdx = 0, eIdx = 0; - if ((eIdx = line.IndexOf('|', sIdx + 1)) == -1) continue; - String startIp = line.Substring(sIdx, eIdx); + if ((eIdx = line.indexOf('|', sIdx + 1)) == -1) continue; + String startIp = line.substring(sIdx, eIdx); //2. get the end ip sIdx = eIdx + 1; - if ((eIdx = line.IndexOf('|', sIdx + 1)) == -1) continue; - String endIp = line.Substring(sIdx, eIdx - sIdx); + if ((eIdx = line.indexOf('|', sIdx + 1)) == -1) continue; + String endIp = line.substring(sIdx, eIdx); //3. get the region sIdx = eIdx + 1; - String region = line.Substring(sIdx); + String region = line.substring(sIdx); Console.WriteLine("+-Try to process item " + line); addDataBlock(raf, startIp, endIp, region); @@ -199,21 +181,22 @@ namespace DbMaker //record the start block IndexBlock indexBlock = null; HeaderBlock hb = null; - indexBlock = indexPool.First(); + indexBlock = indexPool.getFirst(); long indexStartIp = indexBlock.getStartIp(), indexStratPtr = raf.getFilePointer(), indexEndPtr; headerPool.add(new HeaderBlock(indexStartIp, (int)(indexStratPtr))); int blockLength = IndexBlock.getIndexBlockLength(); int counter = 0, shotCounter = (dbConfig.getIndexBlockSize() / blockLength) - 1; - //Iterator indexIt = indexPool.iterator(); - //while ( indexIt.hasNext() ) { - foreach (var block in indexPool) + //var indexIt = indexPool.iterator(); + //while (indexIt.hasNext()) + foreach (var indexIt in indexPool.iterator()) { + indexBlock = indexIt; if (++counter >= shotCounter) { hb = new HeaderBlock( - block.getStartIp(), + indexBlock.getStartIp(), (int)raf.getFilePointer() ); @@ -222,13 +205,13 @@ namespace DbMaker } //write the buffer - raf.write(block.getBytes()); + raf.write(indexBlock.getBytes()); } //record the end block if (counter > 0) { - indexBlock = indexPool.Last(); + indexBlock = indexPool.getLast(); hb = new HeaderBlock( indexBlock.getStartIp(), ((int)raf.getFilePointer()) - IndexBlock.getIndexBlockLength() @@ -251,12 +234,13 @@ namespace DbMaker //write the header blocks Console.WriteLine("+-Try to write the header blocks ... "); - //Iterator headerIt = headerPool.iterator(); - //while ( headerIt.hasNext() ) { - // HeaderBlock headerBlock = headerIt.next(); - // raf.write(headerBlock.getBytes()); + //var headerIt = headerPool.iterator(); + //while (headerIt.hasNext()) + //{ + // HeaderBlock headerBlock = headerIt.next(); + // raf.write(headerBlock.getBytes()); //} - foreach (var headerBlock in headerPool) + foreach (var headerBlock in headerPool.iterator()) { raf.write(headerBlock.getBytes()); } @@ -266,14 +250,13 @@ namespace DbMaker raf.seek(raf.length()); //Calendar cal = Calendar.getInstance(); //SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd"); - var date = DateTime.Now; - String copyright = "Created by lionsoul at " + date.ToString("yyyy/MM/dd"); - var timespan = new DateTimeOffset(date).ToUnixTimeSeconds(); - raf.write((int)(timespan)); //the unix timestamp - raf.write(copyright.getBytes("UTF-8")); + String copyright = "Created by lionsoul at " + DateTime.Now.ToString("yyyy/MM/dd"); //dateFormat.format(cal.getTime()); + var timestamp = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000; + raf.write((Int32)timestamp); //the unix timestamp + raf.write(copyright.getBytes()); Console.WriteLine("|--[Ok]"); - reader.Close(); + reader.close(); raf.close(); } @@ -289,44 +272,50 @@ namespace DbMaker RandomAccessFile raf, String startIp, String endIp, String region) { - - byte[] data = region.getBytes("UTF-8"); - int dataPtr = 0; - - /*byte[] city = new byte[4]; - int city_id = getCityId(region); - Util.writeIntLong(city, 0, city_id); - dataPtr = (int)raf.getFilePointer(); - raf.write(city); - raf.write(data);*/ - - //check region ptr pool first - if (regionPtrPool.containsKey(region)) + try { - DataBlock dataBlock = regionPtrPool.get(region); - dataPtr = dataBlock.getDataPtr(); - Console.WriteLine("dataPtr: " + dataPtr + ", region: " + region); - } - else - { - byte[] city = new byte[4]; + byte[] data = region.getBytes("UTF-8"); + int dataPtr = 0; + + /*byte[] city = new byte[4]; int city_id = getCityId(region); Util.writeIntLong(city, 0, city_id); dataPtr = (int)raf.getFilePointer(); raf.write(city); - raf.write(data); + raf.write(data);*/ - regionPtrPool.put(region, new DataBlock(city_id, region, dataPtr)); + //check region ptr pool first + if (regionPtrPool.containsKey(region)) + { + DataBlock dataBlock = regionPtrPool.get(region); + dataPtr = dataBlock.getDataPtr(); + Console.WriteLine("dataPtr: " + dataPtr + ", region: " + region); + } + else + { + byte[] city = new byte[4]; + int city_id = getCityId(region); + Util.writeIntLong(city, 0, city_id); + dataPtr = (int)raf.getFilePointer(); + raf.write(city); + raf.write(data); + + regionPtrPool.put(region, new DataBlock(city_id, region, dataPtr)); + } + + //add the data index blocks + IndexBlock ib = new IndexBlock( + Util.ip2long(startIp), + Util.ip2long(endIp), + dataPtr, + data.Length + 4 //4 bytes for the city id + ); + indexPool.add(ib); + } + catch (Exception e) + { + Console.WriteLine(e); } - - //add the data index blocks - IndexBlock ib = new IndexBlock( - Util.ip2long(startIp), - Util.ip2long(endIp), - dataPtr, - data.Length + 4 //4 bytes for the city id - ); - indexPool.add(ib); } /** @@ -337,16 +326,16 @@ namespace DbMaker */ public int getCityId(String region) { - String[] p = region.Split("|"); + String[] p = region.split("|"); if (p.Length != 5) return 0; String key = null; - int? intv = null; + Int32? intv = null; for (int i = 3; i >= 0; i--) { - if (p[i].Equals("0")) continue; + if (p[i].equals("0")) continue; if (i == 3 - && p[i].IndexOf("省直辖县级") > -1) + && p[i].indexOf("省直辖县级") > -1) { key = p[2] + p[3]; } @@ -355,12 +344,8 @@ namespace DbMaker key = p[i]; } - if (!globalRegionMap.ContainsKey(key)) - { - return 0; - } intv = globalRegionMap.get(key); - //if (intv == null) return 0; + if (intv == null) return 0; return intv.Value; } @@ -378,12 +363,12 @@ namespace DbMaker return this; } - public FileStream getIpSrcFile() + public File getIpSrcFile() { return ipSrcFile; } - public DbMaker setIpSrcFile(FileStream ipSrcFile) + public DbMaker setIpSrcFile(File ipSrcFile) { this.ipSrcFile = ipSrcFile; return this; @@ -399,15 +384,15 @@ namespace DbMaker String[] path = new String[] { null, null }; for (int i = 0; i < args.Length; i++) { - if (args[i].Equals("-src")) + if (args[i].equals("-src")) { path[0] = args[++i]; } - else if (args[i].Equals("-region")) + else if (args[i].equals("-region")) { path[1] = args[++i]; } - else if (args[i].Equals("-dst")) + else if (args[i].equals("-dst")) { dstDir = args[++i]; } @@ -417,42 +402,32 @@ namespace DbMaker { if (path[i] == null) { - Console.WriteLine("Usage: DbMaker " + Console.WriteLine("Usage: dbMaker " + "-src [source text file path] " + "-region [global region file path]"); - Console.WriteLine("eg: DbMaker " + Console.WriteLine("eg: dbMaker " + "-src ./data/ip.merge.txt -region ./data/origin/global_region.csv"); - //System.exit(0); Environment.Exit(0); + return; } } //check and stdlize the destination directory - if (!dstDir.EndsWith("/")) + if (!dstDir.endsWith("/")) { dstDir = dstDir + "/"; } try { - if (!Directory.Exists(dstDir)) - { - Directory.CreateDirectory(dstDir); - } DbConfig config = new DbConfig(); DbMaker dbMaker = new DbMaker(config, path[0], path[1]); dbMaker.make(dstDir + "ip2region.db"); } - catch (DbMakerConfigException e) + catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - catch (IOException e) - { - // TODO Auto-generated catch block - e.printStackTrace(); + Console.WriteLine(e); } } } -} \ No newline at end of file +} diff --git a/maker/csharp/src/IP2RegionDotNetDbMaker/DbMakerConfigException.cs b/maker/csharp/src/IP2RegionDotNetDbMaker/DbMakerConfigException.cs new file mode 100644 index 0000000..8d19506 --- /dev/null +++ b/maker/csharp/src/IP2RegionDotNetDbMaker/DbMakerConfigException.cs @@ -0,0 +1,17 @@ +using System; +namespace IP2RegionDotNetDbMaker +{ + + /** + * configuration exception + * + * @author chenxin +*/ + public class DbMakerConfigException : Exception + { + public DbMakerConfigException(string info) : base(info) + { + + } + } +} diff --git a/maker/csharp/DbMaker/DbSearcher.cs b/maker/csharp/src/IP2RegionDotNetDbMaker/DbSearcher.cs similarity index 68% rename from maker/csharp/DbMaker/DbSearcher.cs rename to maker/csharp/src/IP2RegionDotNetDbMaker/DbSearcher.cs index e8cb63b..47eabff 100644 --- a/maker/csharp/DbMaker/DbSearcher.cs +++ b/maker/csharp/src/IP2RegionDotNetDbMaker/DbSearcher.cs @@ -1,72 +1,71 @@ -using System; +using System; using System.Text; -namespace DbMaker +namespace IP2RegionDotNetDbMaker { + /** - * ip db searcher class (Not thread safe) - * - * @author chenxin + * ip db searcher class (Not thread safe) + * + * @author chenxin */ - public class DbSearcher : IDisposable + public class DbSearcher { - public const int BTREE_ALGORITHM = 1; - public const int BINARY_ALGORITHM = 2; - public const int MEMORY_ALGORITYM = 3; + public static int BTREE_ALGORITHM = 1; + public static int BINARY_ALGORITHM = 2; + public static int MEMORY_ALGORITYM = 3; /** - * db config - */ + * db config + */ private DbConfig dbConfig = null; /** - * db file access handler - */ + * db file access handler + */ private RandomAccessFile raf = null; /** - * header blocks buffer - */ + * header blocks buffer + */ private long[] HeaderSip = null; - private int[] HeaderPtr = null; private int headerLength; /** - * super blocks info - */ + * super blocks info + */ private long firstIndexPtr = 0; - private long lastIndexPtr = 0; private int totalIndexBlocks = 0; /** - * for memory mode - * the original db binary string - */ + * for memory mode + * the original db binary string + */ private byte[] dbBinStr = null; /** - * construct class - * - * @param bdConfig - * @param dbFile - * @throws FileNotFoundException - */ + * construct class + * + * @param bdConfig + * @param dbFile + * @ + */ public DbSearcher(DbConfig dbConfig, String dbFile) { this.dbConfig = dbConfig; - raf = new RandomAccessFile(dbFile); + raf = new RandomAccessFile(dbFile, "r"); } /** - * construct method with self-define std ip2region bianry string support - * Thanks to the issue from Wendal at https://gitee.com/lionsoul/ip2region/issues/IILFL - * - * @param dbConfig - * @param dbBinStr - */ + * construct method with self-define std ip2region bianry string support + * Thanks to the issue from Wendal at https://gitee.com/lionsoul/ip2region/issues/IILFL + * + * @param dbConfig + * @param dbBinStr + */ public DbSearcher(DbConfig dbConfig, byte[] dbBinStr) { this.dbConfig = dbConfig; @@ -74,28 +73,28 @@ namespace DbMaker firstIndexPtr = Util.getIntLong(dbBinStr, 0); lastIndexPtr = Util.getIntLong(dbBinStr, 4); - totalIndexBlocks = (int) ((lastIndexPtr - firstIndexPtr) / IndexBlock.getIndexBlockLength()) + 1; + totalIndexBlocks = (int)((lastIndexPtr - firstIndexPtr) / IndexBlock.getIndexBlockLength()) + 1; } /** - * get the region with a int ip address with memory binary search algorithm - * - * @param ip - * @throws IOException - */ + * get the region with a int ip address with memory binary search algorithm + * + * @param ip + * @ + */ public DataBlock memorySearch(long ip) { int blen = IndexBlock.getIndexBlockLength(); if (dbBinStr == null) { - dbBinStr = new byte[(int) raf.length()]; + dbBinStr = new byte[(int)raf.length()]; raf.seek(0L); raf.readFully(dbBinStr, 0, dbBinStr.Length); //initialize the global vars firstIndexPtr = Util.getIntLong(dbBinStr, 0); lastIndexPtr = Util.getIntLong(dbBinStr, 4); - totalIndexBlocks = (int) ((lastIndexPtr - firstIndexPtr) / blen) + 1; + totalIndexBlocks = (int)((lastIndexPtr - firstIndexPtr) / blen) + 1; } //search the index blocks to define the data @@ -104,7 +103,7 @@ namespace DbMaker while (l <= h) { int m = (l + h) >> 1; - int p = (int) (firstIndexPtr + m * blen); + int p = (int)(firstIndexPtr + m * blen); sip = Util.getIntLong(dbBinStr, p); if (ip < sip) @@ -130,26 +129,21 @@ namespace DbMaker if (dataptr == 0) return null; //get the data - int dataLen = (int) ((dataptr >> 24) & 0xFF); - int dataPtr = (int) ((dataptr & 0x00FFFFFF)); - int city_id = (int) Util.getIntLong(dbBinStr, dataPtr); + int dataLen = (int)((dataptr >> 24) & 0xFF); + int dataPtr = (int)((dataptr & 0x00FFFFFF)); + int city_id = (int)Util.getIntLong(dbBinStr, dataPtr); //String region = new String(dbBinStr, dataPtr + 4, dataLen - 4, "UTF-8"); - var region = ReadString(dbBinStr, dataPtr + 4, dataLen - 4); + var region = Encoding.UTF8.GetString(dbBinStr, dataPtr + 4, dataLen - 4); return new DataBlock(city_id, region, dataPtr); } - private string ReadString(byte[] bytes, int dataPtr, int dataLen) - { - return Encoding.UTF8.GetString(bytes, dataPtr, dataLen); - } - /** - * get the region throught the ip address with memory binary search algorithm - * - * @param ip - * @return DataBlock - * @throws IOException - */ + * get the region throught the ip address with memory binary search algorithm + * + * @param ip + * @return DataBlock + * @ + */ public DataBlock memorySearch(String ip) { return memorySearch(Util.ip2long(ip)); @@ -157,11 +151,11 @@ namespace DbMaker /** - * get by index ptr - * - * @param indexPtr - * @throws IOException - */ + * get by index ptr + * + * @param indexPtr + * @ + */ public DataBlock getByIndexPtr(long ptr) { raf.seek(ptr); @@ -171,48 +165,48 @@ namespace DbMaker //long endIp = Util.getIntLong(buffer, 4); long extra = Util.getIntLong(buffer, 8); - int dataLen = (int) ((extra >> 24) & 0xFF); - int dataPtr = (int) ((extra & 0x00FFFFFF)); + int dataLen = (int)((extra >> 24) & 0xFF); + int dataPtr = (int)((extra & 0x00FFFFFF)); raf.seek(dataPtr); byte[] data = new byte[dataLen]; raf.readFully(data, 0, data.Length); - int city_id = (int) Util.getIntLong(data, 0); + int city_id = (int)Util.getIntLong(data, 0); //String region = new String(data, 4, data.Length - 4, "UTF-8"); - var region = ReadString(data, 4, data.Length - 4); + var region = Encoding.UTF8.GetString(data, 4, data.Length - 4); return new DataBlock(city_id, region, dataPtr); } /** - * get the region with a int ip address with b-tree algorithm - * - * @param ip - * @throws IOException - */ + * get the region with a int ip address with b-tree algorithm + * + * @param ip + * @ + */ public DataBlock btreeSearch(long ip) { //check and load the header if (HeaderSip == null) { - raf.seek(8L); //pass the super block + raf.seek(8L); //pass the super block byte[] b = new byte[dbConfig.getTotalHeaderSize()]; // byte[] b = new byte[4096]; raf.readFully(b, 0, b.Length); //fill the header - int len = b.Length >> 3, idx = 0; //b.lenght / 8 + int len = b.Length >> 3, idx = 0; //b.lenght / 8 HeaderSip = new long[len]; - HeaderPtr = new int [len]; - long startIp, xDataPtr; + HeaderPtr = new int[len]; + long startIp, dataPtr2; for (int i = 0; i < b.Length; i += 8) { startIp = Util.getIntLong(b, i); - xDataPtr = Util.getIntLong(b, i + 4); - if (xDataPtr == 0) break; + dataPtr2 = Util.getIntLong(b, i + 4); + if (dataPtr2 == 0) break; HeaderSip[idx] = startIp; - HeaderPtr[idx] = (int) xDataPtr; + HeaderPtr[idx] = (int)dataPtr2; idx++; } @@ -266,7 +260,6 @@ namespace DbMaker eptr = HeaderPtr[m]; break; } - h = m - 1; } else @@ -283,7 +276,6 @@ namespace DbMaker eptr = HeaderPtr[m + 1]; break; } - l = m + 1; } } @@ -293,12 +285,11 @@ namespace DbMaker //2. search the index blocks to define the data int blockLen = eptr - sptr, blen = IndexBlock.getIndexBlockLength(); - byte[] iBuffer = new byte[blockLen + blen]; //include the right border block + byte[] iBuffer = new byte[blockLen + blen]; //include the right border block raf.seek(sptr); raf.readFully(iBuffer, 0, iBuffer.Length); - l = 0; - h = blockLen / blen; + l = 0; h = blockLen / blen; long sip, eip, dataptr = 0; while (l <= h) { @@ -328,37 +319,37 @@ namespace DbMaker if (dataptr == 0) return null; //3. get the data - int dataLen = (int) ((dataptr >> 24) & 0xFF); - int dataPtr = (int) ((dataptr & 0x00FFFFFF)); + int dataLen = (int)((dataptr >> 24) & 0xFF); + int dataPtr = (int)((dataptr & 0x00FFFFFF)); raf.seek(dataPtr); byte[] data = new byte[dataLen]; raf.readFully(data, 0, data.Length); - int city_id = (int) Util.getIntLong(data, 0); + int city_id = (int)Util.getIntLong(data, 0); //String region = new String(data, 4, data.Length - 4, "UTF-8"); - var region = ReadString(data, 4, data.Length - 4); + var region = Encoding.UTF8.GetString(data, 4, data.Length); return new DataBlock(city_id, region, dataPtr); } /** - * get the region throught the ip address with b-tree search algorithm - * - * @param ip - * @return DataBlock - * @throws IOException - */ + * get the region throught the ip address with b-tree search algorithm + * + * @param ip + * @return DataBlock + * @ + */ public DataBlock btreeSearch(String ip) { return btreeSearch(Util.ip2long(ip)); } /** - * get the region with a int ip address with binary search algorithm - * - * @param ip - * @throws IOException - */ + * get the region with a int ip address with binary search algorithm + * + * @param ip + * @ + */ public DataBlock binarySearch(long ip) { int blen = IndexBlock.getIndexBlockLength(); @@ -370,7 +361,7 @@ namespace DbMaker //initialize the global vars firstIndexPtr = Util.getIntLong(superBytes, 0); lastIndexPtr = Util.getIntLong(superBytes, 4); - totalIndexBlocks = (int) ((lastIndexPtr - firstIndexPtr) / blen) + 1; + totalIndexBlocks = (int)((lastIndexPtr - firstIndexPtr) / blen) + 1; } //search the index blocks to define the data @@ -380,7 +371,7 @@ namespace DbMaker while (l <= h) { int m = (l + h) >> 1; - raf.seek(firstIndexPtr + m * blen); //set the file pointer + raf.seek(firstIndexPtr + m * blen); //set the file pointer raf.readFully(buffer, 0, buffer.Length); sip = Util.getIntLong(buffer, 0); if (ip < sip) @@ -406,58 +397,53 @@ namespace DbMaker if (dataptr == 0) return null; //get the data - int dataLen = (int) ((dataptr >> 24) & 0xFF); - int dataPtr = (int) ((dataptr & 0x00FFFFFF)); + int dataLen = (int)((dataptr >> 24) & 0xFF); + int dataPtr = (int)((dataptr & 0x00FFFFFF)); raf.seek(dataPtr); byte[] data = new byte[dataLen]; raf.readFully(data, 0, data.Length); - int city_id = (int) Util.getIntLong(data, 0); - //String region = new String(data, 4, data.Length - 4, "UTF-8"); - var region = ReadString(data, 4, data.Length - 4); + int city_id = (int)Util.getIntLong(data, 0); + //String region = new String(data, 4, data.length - 4, "UTF-8"); + var region = Encoding.UTF8.GetString(data, 4, data.Length - 4); return new DataBlock(city_id, region, dataPtr); } /** - * get the region throught the ip address with binary search algorithm - * - * @param ip - * @return DataBlock - * @throws IOException - */ + * get the region throught the ip address with binary search algorithm + * + * @param ip + * @return DataBlock + * @ + */ public DataBlock binarySearch(String ip) { return binarySearch(Util.ip2long(ip)); } /** - * get the db config - * - * @return DbConfig - */ + * get the db config + * + * @return DbConfig + */ public DbConfig getDbConfig() { return dbConfig; } /** - * close the db - * - * @throws IOException - */ - private void close() + * close the db + * + * @ + */ + public void close() { - HeaderSip = null; //let gc do its work + HeaderSip = null; //let gc do its work HeaderPtr = null; dbBinStr = null; raf.close(); } - public void Dispose() - { - close(); - } } - -} \ No newline at end of file +} diff --git a/maker/csharp/DbMaker/HeaderBlock.cs b/maker/csharp/src/IP2RegionDotNetDbMaker/HeaderBlock.cs similarity index 79% rename from maker/csharp/DbMaker/HeaderBlock.cs rename to maker/csharp/src/IP2RegionDotNetDbMaker/HeaderBlock.cs index ac120fc..432adfb 100644 --- a/maker/csharp/DbMaker/HeaderBlock.cs +++ b/maker/csharp/src/IP2RegionDotNetDbMaker/HeaderBlock.cs @@ -1,21 +1,22 @@ -namespace DbMaker +using System; +namespace IP2RegionDotNetDbMaker { /** - * header block class - * - * @author chenxin + * header block class + * + * @author chenxin */ public class HeaderBlock { /** - * index block start ip address - */ + * index block start ip address + */ private long indexStartIp; /** - * ip address - */ + * ip address + */ private int indexPtr; public HeaderBlock(long indexStartIp, int indexPtr) @@ -47,10 +48,10 @@ namespace DbMaker } /** - * get the bytes for db storage - * - * @return byte[] - */ + * get the bytes for db storage + * + * @return byte[] + */ public byte[] getBytes() { /* @@ -67,4 +68,4 @@ namespace DbMaker return b; } } -} \ No newline at end of file +} diff --git a/maker/csharp/src/IP2RegionDotNetDbMaker/IP2RegionDotNetDbMaker.csproj b/maker/csharp/src/IP2RegionDotNetDbMaker/IP2RegionDotNetDbMaker.csproj new file mode 100644 index 0000000..bfede7e --- /dev/null +++ b/maker/csharp/src/IP2RegionDotNetDbMaker/IP2RegionDotNetDbMaker.csproj @@ -0,0 +1,9 @@ + + + + Exe + net452;net5.0 + dbMaker + + + diff --git a/maker/csharp/DbMaker/IndexBlock.cs b/maker/csharp/src/IP2RegionDotNetDbMaker/IndexBlock.cs similarity index 89% rename from maker/csharp/DbMaker/IndexBlock.cs rename to maker/csharp/src/IP2RegionDotNetDbMaker/IndexBlock.cs index 1d2c843..f3c2911 100644 --- a/maker/csharp/DbMaker/IndexBlock.cs +++ b/maker/csharp/src/IP2RegionDotNetDbMaker/IndexBlock.cs @@ -1,11 +1,12 @@ -namespace DbMaker +using System; +namespace IP2RegionDotNetDbMaker { /** * item index class * * @author chenxin - */ +*/ public class IndexBlock { private static int LENGTH = 12; @@ -88,10 +89,10 @@ namespace DbMaker } /** - * get the bytes for storage - * - * @return byte[] - */ + * get the bytes for storage + * + * @return byte[] + */ public byte[] getBytes() { /* @@ -102,8 +103,8 @@ namespace DbMaker */ byte[] b = new byte[12]; - Util.writeIntLong(b, 0, startIp); //start ip - Util.writeIntLong(b, 4, endIp); //end ip + Util.writeIntLong(b, 0, startIp); //start ip + Util.writeIntLong(b, 4, endIp); //end ip //write the data ptr and the length long mix = dataPtr | ((dataLen << 24) & 0xFF000000L); @@ -112,4 +113,4 @@ namespace DbMaker return b; } } -} \ No newline at end of file +} diff --git a/maker/csharp/src/IP2RegionDotNetDbMaker/Mock.cs b/maker/csharp/src/IP2RegionDotNetDbMaker/Mock.cs new file mode 100644 index 0000000..369bec8 --- /dev/null +++ b/maker/csharp/src/IP2RegionDotNetDbMaker/Mock.cs @@ -0,0 +1,244 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IP2RegionDotNetDbMaker +{ + public static class Extensions + { + public static Int32 length(this string str) + { + if (String.IsNullOrWhiteSpace(str)) + { + return 0; + } + return str.Length; + } + public static string trim(this string str) + { + return str.Trim(); + } + public static char charAt(this string str, Int32 i) + { + return str[i]; + } + public static int indexOf(this string str, string value) + { + return str.IndexOf(value); + } + public static int indexOf(this string str, char value, Int32 start) + { + return str.IndexOf(value, start); + } + public static string substring(this string str, Int32 startIndex) + { + return str.Substring(startIndex); + } + public static string substring(this string str, Int32 startIndex, Int32 endIndex) + { + return str.Substring(startIndex, endIndex - startIndex); + } + public static bool equals(this string str1, string str2) + { + return String.Equals(str1, str2, StringComparison.InvariantCultureIgnoreCase); + } + public static string[] split(this string str, string separator) + { + return str.Split(new[] { separator }, StringSplitOptions.None); + } + public static byte[] getBytes(this string str) + { + return Encoding.UTF8.GetBytes(str); + } + public static byte[] getBytes(this string str, string encoding) + { + return Encoding.GetEncoding(encoding).GetBytes(str); + } + public static bool endsWith(this string str, string value) + { + return str.EndsWith(value); + } + } + public class StringBuilder + { + private readonly System.Text.StringBuilder _builder = new System.Text.StringBuilder(); + + internal StringBuilder append(object value) + { + _builder.Append(value); + return this; + } + + internal string toString() + { + return _builder.ToString(); + } + } + public class File + { + public File(string ipSrcFile) + { + _fileInfo = new FileInfo(ipSrcFile); + } + private FileInfo _fileInfo; + public FileInfo FileInfo => _fileInfo; + internal bool exists() + { + return _fileInfo.Exists; + } + } + public class LinkedList : List + { + internal T getFirst() + { + return this[0]; + } + + internal void add(T item) + { + this.Add(item); + } + + internal T getLast() + { + return this[this.Count - 1]; + } + + internal IEnumerable iterator() + { + return this; + } + } + public class HashMap : Dictionary + { + internal void put(K k, V v) + { + this[k] = v; + } + + internal bool containsKey(K key) + { + return this.ContainsKey(key); + } + + internal V get(K k) + { + if (containsKey(k)) + { + return this[k]; + } + return default; + } + + } + public class FileReader + { + private File globalRegionFile; + private Queue _lines = new Queue(); + public FileReader(File file) + { + this.globalRegionFile = file; + using (var fs = file.FileInfo.OpenRead()) + { + using (var sr = new StreamReader(fs)) + { + while (!sr.EndOfStream) + { + var line = sr.ReadLine(); + _lines.Enqueue(line); + } + } + } + } + + internal string readLine() + { + if (_lines.Count > 0) + { + return _lines.Dequeue(); + } + return null; + } + + internal void close() + { + } + } + public class BufferedReader + { + private FileReader fileReader; + + public BufferedReader(FileReader fileReader) + { + this.fileReader = fileReader; + } + + internal string readLine() + { + return fileReader.readLine(); + } + + internal void close() + { + fileReader.close(); + } + } + public class RandomAccessFile + { + private string dbFile; + private Stream stream; + + internal void seek(long v) + { + stream.Seek(v, SeekOrigin.Begin); + } + + internal void write(byte[] vs) + { + stream.Write(vs, 0, vs.Length); + } + + internal void readFully(byte[] dbBinStr, int v, int length) + { + stream.Read(dbBinStr, v, length); + } + + private string v; + + public RandomAccessFile(string dbFile, string v) + { + this.dbFile = dbFile; + this.v = v; + this.stream = new FileStream(dbFile, FileMode.OpenOrCreate, FileAccess.ReadWrite); + } + public long length() + { + return this.stream.Length; + } + + internal void close() + { + if (stream == null) + { + return; + } + this.stream.Flush(); + this.stream.Close(); + this.stream = null; + } + + internal long getFilePointer() + { + return stream.Position; + } + + internal void write(int v) + { + var bytes = BitConverter.GetBytes(v); + stream.Write(bytes, 0, bytes.Length); + } + } +} diff --git a/maker/csharp/DbMaker/Util.cs b/maker/csharp/src/IP2RegionDotNetDbMaker/Util.cs similarity index 78% rename from maker/csharp/DbMaker/Util.cs rename to maker/csharp/src/IP2RegionDotNetDbMaker/Util.cs index a92f8af..0ed13ba 100644 --- a/maker/csharp/DbMaker/Util.cs +++ b/maker/csharp/src/IP2RegionDotNetDbMaker/Util.cs @@ -1,15 +1,14 @@ -using System; -using System.Text; +using System; -namespace DbMaker + +namespace IP2RegionDotNetDbMaker { - /** * util class * * @author chenxin - */ +*/ public class Util { /** @@ -24,8 +23,7 @@ namespace DbMaker { for (int i = 0; i < bytes; i++) { - //b[offset++] = (byte)((v >>> (8 * i)) & 0xFF); - b[offset++] = (byte) ((v >> (8 * i)) & 0xFF); + b[offset++] = (byte)((v >> (8 * i)) & 0xFF); } } @@ -38,10 +36,10 @@ namespace DbMaker */ public static void writeIntLong(byte[] b, int offset, long v) { - b[offset++] = (byte) ((v >> 0) & 0xFF); - b[offset++] = (byte) ((v >> 8) & 0xFF); - b[offset++] = (byte) ((v >> 16) & 0xFF); - b[offset] = (byte) ((v >> 24) & 0xFF); + b[offset++] = (byte)((v >> 0) & 0xFF); + b[offset++] = (byte)((v >> 8) & 0xFF); + b[offset++] = (byte)((v >> 16) & 0xFF); + b[offset] = (byte)((v >> 24) & 0xFF); } /** @@ -119,12 +117,12 @@ namespace DbMaker StringBuilder sb = new StringBuilder(); sb - .Append((ip >> 24) & 0xFF).Append('.') - .Append((ip >> 16) & 0xFF).Append('.') - .Append((ip >> 8) & 0xFF).Append('.') - .Append((ip >> 0) & 0xFF); + .append((ip >> 24) & 0xFF).append('.') + .append((ip >> 16) & 0xFF).append('.') + .append((ip >> 8) & 0xFF).append('.') + .append((ip >> 0) & 0xFF); - return sb.ToString(); + return sb.toString(); } /** @@ -133,21 +131,18 @@ namespace DbMaker * @param ip * @return boolean */ - public static Boolean isIpAddress(String ip) + public static bool isIpAddress(String ip) { - String[] p = ip.Split("\\."); + String[] p = ip.split("."); if (p.Length != 4) return false; - - //for ( String pp : p ) { - //for ( String pp : p ) { foreach (var pp in p) { if (pp.Length > 3) return false; - int val = Int32.Parse(pp); //Integer.valueOf(pp); + var val = Int32.Parse(pp); if (val > 255) return false; } return true; } } -} \ No newline at end of file +}