This commit is contained in:
Lion 2021-03-08 22:27:16 +08:00
commit 70b12eaae2
20 changed files with 786 additions and 840 deletions

View File

@ -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

View File

@ -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

View File

@ -1,8 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
</Project>

View File

@ -1,12 +0,0 @@
using System;
namespace DbMaker
{
public class DbMakerConfigException : Exception
{
public DbMakerConfigException(String message) : base(message)
{
}
}
}

View File

@ -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<TKey, TValue>(this IDictionary<TKey, TValue> dic, TKey key)
{
return dic.ContainsKey(key);
}
public static TValue get<TKey, TValue>(this IDictionary<TKey, TValue> dic, TKey key)
{
return dic[key];
}
public static void put<TKey, TValue>(this IDictionary<TKey, TValue> dic, TKey key, TValue value)
{
//dic.Add(key,value);
dic[key] = value;
}
public static void add<TItem>(this IList<TItem> list, TItem item)
{
list.Add(item);
}
public static void printStackTrace(this Exception e)
{
Console.WriteLine(e);
}
}
}

View File

@ -1,8 +0,0 @@
{
"profiles": {
"DbMaker": {
"commandName": "Project",
"commandLineArgs": " -src ip.merge.txt -region global_region.csv"
}
}
}

View File

@ -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;
}
}
}

18
maker/csharp/README.md Normal file
View File

@ -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

BIN
maker/csharp/dbMaker.exe Normal file

Binary file not shown.

199
maker/csharp/src/.gitignore vendored Normal file
View File

@ -0,0 +1,199 @@
#################
## Visual Studio
#################
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
# User-specific files
*.suo
*.user
*.sln.docstates
# Build results
.vs/
[Dd]ebug/
[Rr]elease/
x64/
[Bb]in/
[Oo]bj/
# Build scripts folder
[Aa]pp[Bb]uild/
[Oo]ut/
# Visual Studio Code files
.vscode/
# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*
*_i.c
*_p.c
*.ilk
*.meta
*.obj
*.pch
*.pdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*.log
*.vspscc
*.vssscc
.builds
*.pidb
*.log
*.scc
# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opensdf
*.sdf
*.cachefile
# Visual Studio profiler
*.psess
*.vsp
*.vspx
# Guidance Automation Toolkit
*.gpState
# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper
# TeamCity is a build add-in
_TeamCity*
# DotCover is a Code Coverage Tool
*.dotCover
# NCrunch
*.ncrunch*
.*crunch*.local.xml
# 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
*.Publish.xml
*.pubxml
#Launch settings
**/Properties/launchSettings.json
# NuGet Packages Directory
## TODO: If you have NuGet Package Restore enabled, uncomment the next line
#packages/
# Windows Azure Build Output
csx
*.build.csdef
# Windows Store app package directory
AppPackages/
# Others
sql/
*.Cache
ClientBin/
[Ss]tyle[Cc]op.*
~$*
*~
*.dbmdl
*.[Pp]ublish.xml
*.pfx
*.publishsettings
# 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
# SQL Server files
App_Data/*.mdf
App_Data/*.ldf
#############
## Windows detritus
#############
# Windows image file caches
Thumbs.db
ehthumbs.db
# Folder config file
Desktop.ini
# Recycle Bin used on file shares
$RECYCLE.BIN/
# Mac crap
.DS_Store
#######################
## nopCommerce specific
###########
glob:*.user
*.patch
*.hg
src/Presentation/Nop.Web/.config/*
src/Presentation/Nop.Web/App_Data/browscap.crawlersonly.xml
src/Presentation/Nop.Web/App_Data/installedPlugins.json
src/Presentation/Nop.Web/App_Data/plugins.json
src/Presentation/Nop.Web/App_Data/dataSettings.json
src/Presentation/Nop.Web/App_Data/appsettings.json
src/Presentation/Nop.Web/App_Data/TempUploads/*
!src/Presentation/Nop.Web/App_Data/TempUploads/Index.htm
src/Presentation/Nop.Web/App_Data/DataProtectionKeys/*
!src/Presentation/Nop.Web/App_Data/DataProtectionKeys/Index.htm
src/Presentation/Nop.Web/node_modules/*
src/Presentation/Nop.Web/Plugins/*
!src/Presentation/Nop.Web/Plugins/bin
src/Presentation/Nop.Web/Plugins/bin/*
!src/Presentation/Nop.Web/Plugins/bin/placeholder.txt
src/Presentation/Nop.Web/wwwroot/bundles/*
!src/Presentation/Nop.Web/wwwroot/bundles/Index.htm
src/Presentation/Nop.Web/wwwroot/db_backups/*.bak
!src/Presentation/Nop.Web/wwwroot/db_backups/Index.htm
src/Presentation/Nop.Web/wwwroot/images/thumbs/*
!src/Presentation/Nop.Web/wwwroot/images/thumbs/placeholder.txt
src/Presentation/Nop.Web/wwwroot/images/uploaded/*
!src/Presentation/Nop.Web/wwwroot/images/uploaded/placeholder.txt
src/Presentation/Nop.Web/wwwroot/files/exportimport/*
!src/Presentation/Nop.Web/wwwroot/files/exportimport/Index.htm
src/Presentation/Nop.Web/wwwroot/lib/cldr-data/main*
!src/Presentation/Nop.Web/wwwroot/lib/cldr-data/main/en/*
!src/Presentation/Nop.Web/wwwroot/lib/cldr-data/main/main.zip
src/packages/*

View File

@ -1,38 +1,37 @@
using System; using System;
using System.Text;
namespace DbMaker namespace IP2RegionDotNetDbMaker
{ {
/** /**
* data block class * data block class
* *
* @author chenxin<chenxin619315@gmail.com> * @author chenxin<chenxin619315@gmail.com>
*/ */
public class DataBlock public class DataBlock
{ {
/** /**
* city id * city id
*/ */
private int city_id; private int city_id;
/** /**
* region address * region address
*/ */
private String region; private String region;
/** /**
* region ptr in the db file * region ptr in the db file
*/ */
private int dataPtr; private int dataPtr;
/** /**
* construct method * construct method
* *
* @param city_id * @param city_id
* @param region region string * @param region region string
* @param dataPtr data ptr * @param dataPtr data ptr
*/ */
public DataBlock(int city_id, String region, int dataPtr) public DataBlock(int city_id, String region, int dataPtr)
{ {
this.city_id = city_id; this.city_id = city_id;
@ -42,7 +41,6 @@ namespace DbMaker
public DataBlock(int city_id, String region) : this(city_id, region, 0) public DataBlock(int city_id, String region) : this(city_id, region, 0)
{ {
//this(city_id, region, 0);
} }
public int getCityId() public int getCityId()
@ -78,12 +76,14 @@ namespace DbMaker
return this; return this;
} }
public override String ToString()
public String toString()
{ {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.Append(city_id).Append('|').Append(region).Append('|').Append(dataPtr); sb.append(city_id).append('|').append(region).append('|').append(dataPtr);
return sb.ToString(); return sb.toString();
} }
} }
} }

View File

@ -1,30 +1,31 @@
namespace DbMaker using System;
namespace IP2RegionDotNetDbMaker
{ {
/** /**
* database configuration class * database configuration class
* *
* @author chenxin<chenxin619315@gmail.com> * @author chenxin<chenxin619315@gmail.com>
*/ */
public class DbConfig public class DbConfig
{ {
/** /**
* total header data block size * total header data block size
*/ */
private int totalHeaderSize; private int totalHeaderSize;
/** /**
* max index data block size * max index data block size
* u should always choice the fastest read block size * u should always choice the fastest read block size
*/ */
private int indexBlockSize; private int indexBlockSize;
/** /**
* construct method * construct method
* *
* @param totalHeaderSize * @param totalHeaderSize
* @throws DbMakerConfigException * @
*/ */
public DbConfig(int totalHeaderSize) public DbConfig(int totalHeaderSize)
{ {
if ((totalHeaderSize % 8) != 0) if ((totalHeaderSize % 8) != 0)
@ -38,7 +39,6 @@ namespace DbMaker
public DbConfig() : this(8 * 2048) public DbConfig() : this(8 * 2048)
{ {
//this(8 * 2048);
} }
public int getTotalHeaderSize() public int getTotalHeaderSize()
@ -63,5 +63,4 @@ namespace DbMaker
return this; return this;
} }
} }
}
}

View File

@ -1,14 +1,9 @@
using System; using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using DbMaker;
namespace DbMaker namespace IP2RegionDotNetDbMaker
{ {
/** /**
* fast ip db maker * fast ip db maker
* *
@ -50,24 +45,24 @@ namespace DbMaker
/** /**
* ip source file path * ip source file path
*/ */
private FileStream ipSrcFile; private File ipSrcFile;
/** /**
* buffer * buffer
*/ */
private List<IndexBlock> indexPool; private LinkedList<IndexBlock> indexPool;
private List<HeaderBlock> headerPool; private LinkedList<HeaderBlock> headerPool;
/** /**
* global region Id mapping data * global region Id mapping data
*/ */
private FileStream globalRegionFile = null; private File globalRegionFile = null;
private Dictionary<String, int> globalRegionMap = null; private HashMap<String, int?> globalRegionMap = null;
/** /**
* region and data ptr mapping data * region and data ptr mapping data
*/ */
private Dictionary<String, DataBlock> regionPtrPool = null; private HashMap<String, DataBlock> regionPtrPool = null;
/** /**
* construct method * construct method
@ -84,18 +79,17 @@ namespace DbMaker
String globalRegionFile) String globalRegionFile)
{ {
this.dbConfig = config; this.dbConfig = config;
this.ipSrcFile = File.OpenRead(ipSrcFile); //new File(ipSrcFile); this.ipSrcFile = new File(ipSrcFile);
this.globalRegionMap = new Dictionary<string, int>(); //new HashMap<String, Integer>(); this.globalRegionMap = new HashMap<String, int?>();
this.regionPtrPool = new Dictionary<string, DataBlock>(); //new HashMap<String, DataBlock>(); this.regionPtrPool = new HashMap<String, DataBlock>();
if (globalRegionFile != null) if (globalRegionFile != null)
{ {
this.globalRegionFile = File.OpenRead(globalRegionFile); //new File(globalRegionFile); this.globalRegionFile = new File(globalRegionFile);
} }
//if ( this.ipSrcFile.exists() == false ) { if (this.ipSrcFile.exists() == false)
if (!File.Exists(ipSrcFile))
{ {
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 * initialize the db file
* *
* @param raf * @param raf
* @throws IOException * @
*/ */
private void initDbFile(RandomAccessFile raf) private void initDbFile(RandomAccessFile raf)
{ {
@ -112,15 +106,15 @@ namespace DbMaker
raf.write(new byte[8]); //super block raf.write(new byte[8]); //super block
raf.write(new byte[dbConfig.getTotalHeaderSize()]); //header block raf.write(new byte[dbConfig.getTotalHeaderSize()]); //header block
headerPool = new List<HeaderBlock>(); //new LinkedList<HeaderBlock>(); headerPool = new LinkedList<HeaderBlock>();
indexPool = new List<IndexBlock>(); //new LinkedList<IndexBlock>(); indexPool = new LinkedList<IndexBlock>();
} }
/** /**
* make the Db file * make the Db file
* *
* @param dbFile target output file path * @param dbFile target output file path
* @throws IOException * @
*/ */
public void make(String dbFile) public void make(String dbFile)
{ {
@ -128,30 +122,24 @@ namespace DbMaker
if (globalRegionFile != null) if (globalRegionFile != null)
{ {
Console.WriteLine("+-Try to load the global region data ..."); Console.WriteLine("+-Try to load the global region data ...");
StreamReader greader = new StreamReader((globalRegionFile)); BufferedReader greader = new BufferedReader(new FileReader(globalRegionFile));
String gline = null; String gline = null;
while (!greader.EndOfStream) while ((gline = greader.readLine()) != null)
{ {
gline = greader.ReadLine();// != null String[] p = gline.split(",");
if (String.IsNullOrWhiteSpace(gline))
{
continue;
}
String[] p = gline.Split(",");
if (p.Length != 5) continue; if (p.Length != 5) continue;
//push the mapping //push the mapping
globalRegionMap.put(p[2], Int32.Parse(p[0])); globalRegionMap.put(p[2], Int32.Parse(p[0]));
} }
greader.Close(); greader.close();
Console.WriteLine("|--[Ok]"); Console.WriteLine("|--[Ok]");
} }
//alloc the header size //alloc the header size
StreamReader reader = new StreamReader(this.ipSrcFile); BufferedReader reader = new BufferedReader(new FileReader(this.ipSrcFile));
RandomAccessFile raf = new RandomAccessFile(dbFile); RandomAccessFile raf = new RandomAccessFile(dbFile, "rw");
//init the db file //init the db file
initDbFile(raf); initDbFile(raf);
@ -160,31 +148,25 @@ namespace DbMaker
//analysis main loop //analysis main loop
Console.WriteLine("+-Try to write the data blocks ... "); Console.WriteLine("+-Try to write the data blocks ... ");
String line = null; String line = null;
while (!reader.EndOfStream) while ((line = reader.readLine()) != null)
{ {
//line = reader.ReadLine() line = line.trim();
line = reader.ReadLine(); if (line.length() == 0) continue;
if (String.IsNullOrWhiteSpace(line)) if (line.charAt(0) == '#') continue;
{
continue;
}
line = line.Trim();
if (line.Length == 0) continue;
if (line[0] == '#') continue;
//1. get the start ip //1. get the start ip
int sIdx = 0, eIdx = 0; int sIdx = 0, eIdx = 0;
if ((eIdx = line.IndexOf('|', sIdx + 1)) == -1) continue; if ((eIdx = line.indexOf('|', sIdx + 1)) == -1) continue;
String startIp = line.Substring(sIdx, eIdx); String startIp = line.substring(sIdx, eIdx);
//2. get the end ip //2. get the end ip
sIdx = eIdx + 1; sIdx = eIdx + 1;
if ((eIdx = line.IndexOf('|', sIdx + 1)) == -1) continue; if ((eIdx = line.indexOf('|', sIdx + 1)) == -1) continue;
String endIp = line.Substring(sIdx, eIdx - sIdx); String endIp = line.substring(sIdx, eIdx);
//3. get the region //3. get the region
sIdx = eIdx + 1; sIdx = eIdx + 1;
String region = line.Substring(sIdx); String region = line.substring(sIdx);
Console.WriteLine("+-Try to process item " + line); Console.WriteLine("+-Try to process item " + line);
addDataBlock(raf, startIp, endIp, region); addDataBlock(raf, startIp, endIp, region);
@ -199,21 +181,22 @@ namespace DbMaker
//record the start block //record the start block
IndexBlock indexBlock = null; IndexBlock indexBlock = null;
HeaderBlock hb = null; HeaderBlock hb = null;
indexBlock = indexPool.First(); indexBlock = indexPool.getFirst();
long indexStartIp = indexBlock.getStartIp(), long indexStartIp = indexBlock.getStartIp(),
indexStratPtr = raf.getFilePointer(), indexEndPtr; indexStratPtr = raf.getFilePointer(), indexEndPtr;
headerPool.add(new HeaderBlock(indexStartIp, (int)(indexStratPtr))); headerPool.add(new HeaderBlock(indexStartIp, (int)(indexStratPtr)));
int blockLength = IndexBlock.getIndexBlockLength(); int blockLength = IndexBlock.getIndexBlockLength();
int counter = 0, shotCounter = (dbConfig.getIndexBlockSize() / blockLength) - 1; int counter = 0, shotCounter = (dbConfig.getIndexBlockSize() / blockLength) - 1;
//Iterator<IndexBlock> indexIt = indexPool.iterator(); //var indexIt = indexPool.iterator();
//while ( indexIt.hasNext() ) { //while (indexIt.hasNext())
foreach (var block in indexPool) foreach (var indexIt in indexPool.iterator())
{ {
indexBlock = indexIt;
if (++counter >= shotCounter) if (++counter >= shotCounter)
{ {
hb = new HeaderBlock( hb = new HeaderBlock(
block.getStartIp(), indexBlock.getStartIp(),
(int)raf.getFilePointer() (int)raf.getFilePointer()
); );
@ -222,13 +205,13 @@ namespace DbMaker
} }
//write the buffer //write the buffer
raf.write(block.getBytes()); raf.write(indexBlock.getBytes());
} }
//record the end block //record the end block
if (counter > 0) if (counter > 0)
{ {
indexBlock = indexPool.Last(); indexBlock = indexPool.getLast();
hb = new HeaderBlock( hb = new HeaderBlock(
indexBlock.getStartIp(), indexBlock.getStartIp(),
((int)raf.getFilePointer()) - IndexBlock.getIndexBlockLength() ((int)raf.getFilePointer()) - IndexBlock.getIndexBlockLength()
@ -251,12 +234,13 @@ namespace DbMaker
//write the header blocks //write the header blocks
Console.WriteLine("+-Try to write the header blocks ... "); Console.WriteLine("+-Try to write the header blocks ... ");
//Iterator<HeaderBlock> headerIt = headerPool.iterator(); //var headerIt = headerPool.iterator();
//while ( headerIt.hasNext() ) { //while (headerIt.hasNext())
// HeaderBlock headerBlock = headerIt.next(); //{
// raf.write(headerBlock.getBytes()); // HeaderBlock headerBlock = headerIt.next();
// raf.write(headerBlock.getBytes());
//} //}
foreach (var headerBlock in headerPool) foreach (var headerBlock in headerPool.iterator())
{ {
raf.write(headerBlock.getBytes()); raf.write(headerBlock.getBytes());
} }
@ -266,14 +250,13 @@ namespace DbMaker
raf.seek(raf.length()); raf.seek(raf.length());
//Calendar cal = Calendar.getInstance(); //Calendar cal = Calendar.getInstance();
//SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd"); //SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
var date = DateTime.Now; String copyright = "Created by lionsoul at " + DateTime.Now.ToString("yyyy/MM/dd"); //dateFormat.format(cal.getTime());
String copyright = "Created by lionsoul at " + date.ToString("yyyy/MM/dd"); var timestamp = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
var timespan = new DateTimeOffset(date).ToUnixTimeSeconds(); raf.write((Int32)timestamp); //the unix timestamp
raf.write((int)(timespan)); //the unix timestamp raf.write(copyright.getBytes());
raf.write(copyright.getBytes("UTF-8"));
Console.WriteLine("|--[Ok]"); Console.WriteLine("|--[Ok]");
reader.Close(); reader.close();
raf.close(); raf.close();
} }
@ -289,44 +272,50 @@ namespace DbMaker
RandomAccessFile raf, RandomAccessFile raf,
String startIp, String endIp, String region) String startIp, String endIp, String region)
{ {
try
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))
{ {
DataBlock dataBlock = regionPtrPool.get(region); byte[] data = region.getBytes("UTF-8");
dataPtr = dataBlock.getDataPtr(); int dataPtr = 0;
Console.WriteLine("dataPtr: " + dataPtr + ", region: " + region);
} /*byte[] city = new byte[4];
else
{
byte[] city = new byte[4];
int city_id = getCityId(region); int city_id = getCityId(region);
Util.writeIntLong(city, 0, city_id); Util.writeIntLong(city, 0, city_id);
dataPtr = (int)raf.getFilePointer(); dataPtr = (int)raf.getFilePointer();
raf.write(city); 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) public int getCityId(String region)
{ {
String[] p = region.Split("|"); String[] p = region.split("|");
if (p.Length != 5) return 0; if (p.Length != 5) return 0;
String key = null; String key = null;
int? intv = null; Int32? intv = null;
for (int i = 3; i >= 0; i--) for (int i = 3; i >= 0; i--)
{ {
if (p[i].Equals("0")) continue; if (p[i].equals("0")) continue;
if (i == 3 if (i == 3
&& p[i].IndexOf("省直辖县级") > -1) && p[i].indexOf("省直辖县级") > -1)
{ {
key = p[2] + p[3]; key = p[2] + p[3];
} }
@ -355,12 +344,8 @@ namespace DbMaker
key = p[i]; key = p[i];
} }
if (!globalRegionMap.ContainsKey(key))
{
return 0;
}
intv = globalRegionMap.get(key); intv = globalRegionMap.get(key);
//if (intv == null) return 0; if (intv == null) return 0;
return intv.Value; return intv.Value;
} }
@ -378,12 +363,12 @@ namespace DbMaker
return this; return this;
} }
public FileStream getIpSrcFile() public File getIpSrcFile()
{ {
return ipSrcFile; return ipSrcFile;
} }
public DbMaker setIpSrcFile(FileStream ipSrcFile) public DbMaker setIpSrcFile(File ipSrcFile)
{ {
this.ipSrcFile = ipSrcFile; this.ipSrcFile = ipSrcFile;
return this; return this;
@ -399,15 +384,15 @@ namespace DbMaker
String[] path = new String[] { null, null }; String[] path = new String[] { null, null };
for (int i = 0; i < args.Length; i++) for (int i = 0; i < args.Length; i++)
{ {
if (args[i].Equals("-src")) if (args[i].equals("-src"))
{ {
path[0] = args[++i]; path[0] = args[++i];
} }
else if (args[i].Equals("-region")) else if (args[i].equals("-region"))
{ {
path[1] = args[++i]; path[1] = args[++i];
} }
else if (args[i].Equals("-dst")) else if (args[i].equals("-dst"))
{ {
dstDir = args[++i]; dstDir = args[++i];
} }
@ -417,42 +402,32 @@ namespace DbMaker
{ {
if (path[i] == null) if (path[i] == null)
{ {
Console.WriteLine("Usage: DbMaker " Console.WriteLine("Usage: dbMaker "
+ "-src [source text file path] " + "-src [source text file path] "
+ "-region [global region 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"); + "-src ./data/ip.merge.txt -region ./data/origin/global_region.csv");
//System.exit(0);
Environment.Exit(0); Environment.Exit(0);
return;
} }
} }
//check and stdlize the destination directory //check and stdlize the destination directory
if (!dstDir.EndsWith("/")) if (!dstDir.endsWith("/"))
{ {
dstDir = dstDir + "/"; dstDir = dstDir + "/";
} }
try try
{ {
if (!Directory.Exists(dstDir))
{
Directory.CreateDirectory(dstDir);
}
DbConfig config = new DbConfig(); DbConfig config = new DbConfig();
DbMaker dbMaker = new DbMaker(config, path[0], path[1]); DbMaker dbMaker = new DbMaker(config, path[0], path[1]);
dbMaker.make(dstDir + "ip2region.db"); dbMaker.make(dstDir + "ip2region.db");
} }
catch (DbMakerConfigException e) catch (Exception e)
{ {
// TODO Auto-generated catch block Console.WriteLine(e);
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} }
} }
} }
} }

View File

@ -0,0 +1,17 @@
using System;
namespace IP2RegionDotNetDbMaker
{
/**
* configuration exception
*
* @author chenxin<chenxin619315@gmail.com>
*/
public class DbMakerConfigException : Exception
{
public DbMakerConfigException(string info) : base(info)
{
}
}
}

View File

@ -1,72 +1,71 @@
using System; using System;
using System.Text; using System.Text;
namespace DbMaker namespace IP2RegionDotNetDbMaker
{ {
/** /**
* ip db searcher class (Not thread safe) * ip db searcher class (Not thread safe)
* *
* @author chenxin<chenxin619315@gmail.com> * @author chenxin<chenxin619315@gmail.com>
*/ */
public class DbSearcher : IDisposable public class DbSearcher
{ {
public const int BTREE_ALGORITHM = 1; public static int BTREE_ALGORITHM = 1;
public const int BINARY_ALGORITHM = 2; public static int BINARY_ALGORITHM = 2;
public const int MEMORY_ALGORITYM = 3; public static int MEMORY_ALGORITYM = 3;
/** /**
* db config * db config
*/ */
private DbConfig dbConfig = null; private DbConfig dbConfig = null;
/** /**
* db file access handler * db file access handler
*/ */
private RandomAccessFile raf = null; private RandomAccessFile raf = null;
/** /**
* header blocks buffer * header blocks buffer
*/ */
private long[] HeaderSip = null; private long[] HeaderSip = null;
private int[] HeaderPtr = null; private int[] HeaderPtr = null;
private int headerLength; private int headerLength;
/** /**
* super blocks info * super blocks info
*/ */
private long firstIndexPtr = 0; private long firstIndexPtr = 0;
private long lastIndexPtr = 0; private long lastIndexPtr = 0;
private int totalIndexBlocks = 0; private int totalIndexBlocks = 0;
/** /**
* for memory mode * for memory mode
* the original db binary string * the original db binary string
*/ */
private byte[] dbBinStr = null; private byte[] dbBinStr = null;
/** /**
* construct class * construct class
* *
* @param bdConfig * @param bdConfig
* @param dbFile * @param dbFile
* @throws FileNotFoundException * @
*/ */
public DbSearcher(DbConfig dbConfig, String dbFile) public DbSearcher(DbConfig dbConfig, String dbFile)
{ {
this.dbConfig = dbConfig; this.dbConfig = dbConfig;
raf = new RandomAccessFile(dbFile); raf = new RandomAccessFile(dbFile, "r");
} }
/** /**
* construct method with self-define std ip2region bianry string support * construct method with self-define std ip2region bianry string support
* Thanks to the issue from Wendal at https://gitee.com/lionsoul/ip2region/issues/IILFL * Thanks to the issue from Wendal at https://gitee.com/lionsoul/ip2region/issues/IILFL
* *
* @param dbConfig * @param dbConfig
* @param dbBinStr * @param dbBinStr
*/ */
public DbSearcher(DbConfig dbConfig, byte[] dbBinStr) public DbSearcher(DbConfig dbConfig, byte[] dbBinStr)
{ {
this.dbConfig = dbConfig; this.dbConfig = dbConfig;
@ -74,28 +73,28 @@ namespace DbMaker
firstIndexPtr = Util.getIntLong(dbBinStr, 0); firstIndexPtr = Util.getIntLong(dbBinStr, 0);
lastIndexPtr = Util.getIntLong(dbBinStr, 4); 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 * get the region with a int ip address with memory binary search algorithm
* *
* @param ip * @param ip
* @throws IOException * @
*/ */
public DataBlock memorySearch(long ip) public DataBlock memorySearch(long ip)
{ {
int blen = IndexBlock.getIndexBlockLength(); int blen = IndexBlock.getIndexBlockLength();
if (dbBinStr == null) if (dbBinStr == null)
{ {
dbBinStr = new byte[(int) raf.length()]; dbBinStr = new byte[(int)raf.length()];
raf.seek(0L); raf.seek(0L);
raf.readFully(dbBinStr, 0, dbBinStr.Length); raf.readFully(dbBinStr, 0, dbBinStr.Length);
//initialize the global vars //initialize the global vars
firstIndexPtr = Util.getIntLong(dbBinStr, 0); firstIndexPtr = Util.getIntLong(dbBinStr, 0);
lastIndexPtr = Util.getIntLong(dbBinStr, 4); 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 //search the index blocks to define the data
@ -104,7 +103,7 @@ namespace DbMaker
while (l <= h) while (l <= h)
{ {
int m = (l + h) >> 1; int m = (l + h) >> 1;
int p = (int) (firstIndexPtr + m * blen); int p = (int)(firstIndexPtr + m * blen);
sip = Util.getIntLong(dbBinStr, p); sip = Util.getIntLong(dbBinStr, p);
if (ip < sip) if (ip < sip)
@ -130,26 +129,21 @@ namespace DbMaker
if (dataptr == 0) return null; if (dataptr == 0) return null;
//get the data //get the data
int dataLen = (int) ((dataptr >> 24) & 0xFF); int dataLen = (int)((dataptr >> 24) & 0xFF);
int dataPtr = (int) ((dataptr & 0x00FFFFFF)); int dataPtr = (int)((dataptr & 0x00FFFFFF));
int city_id = (int) Util.getIntLong(dbBinStr, dataPtr); int city_id = (int)Util.getIntLong(dbBinStr, dataPtr);
//String region = new String(dbBinStr, dataPtr + 4, dataLen - 4, "UTF-8"); //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); 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 * get the region throught the ip address with memory binary search algorithm
* *
* @param ip * @param ip
* @return DataBlock * @return DataBlock
* @throws IOException * @
*/ */
public DataBlock memorySearch(String ip) public DataBlock memorySearch(String ip)
{ {
return memorySearch(Util.ip2long(ip)); return memorySearch(Util.ip2long(ip));
@ -157,11 +151,11 @@ namespace DbMaker
/** /**
* get by index ptr * get by index ptr
* *
* @param indexPtr * @param indexPtr
* @throws IOException * @
*/ */
public DataBlock getByIndexPtr(long ptr) public DataBlock getByIndexPtr(long ptr)
{ {
raf.seek(ptr); raf.seek(ptr);
@ -171,48 +165,48 @@ namespace DbMaker
//long endIp = Util.getIntLong(buffer, 4); //long endIp = Util.getIntLong(buffer, 4);
long extra = Util.getIntLong(buffer, 8); long extra = Util.getIntLong(buffer, 8);
int dataLen = (int) ((extra >> 24) & 0xFF); int dataLen = (int)((extra >> 24) & 0xFF);
int dataPtr = (int) ((extra & 0x00FFFFFF)); int dataPtr = (int)((extra & 0x00FFFFFF));
raf.seek(dataPtr); raf.seek(dataPtr);
byte[] data = new byte[dataLen]; byte[] data = new byte[dataLen];
raf.readFully(data, 0, data.Length); 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"); //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); return new DataBlock(city_id, region, dataPtr);
} }
/** /**
* get the region with a int ip address with b-tree algorithm * get the region with a int ip address with b-tree algorithm
* *
* @param ip * @param ip
* @throws IOException * @
*/ */
public DataBlock btreeSearch(long ip) public DataBlock btreeSearch(long ip)
{ {
//check and load the header //check and load the header
if (HeaderSip == null) 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[dbConfig.getTotalHeaderSize()];
// byte[] b = new byte[4096]; // byte[] b = new byte[4096];
raf.readFully(b, 0, b.Length); raf.readFully(b, 0, b.Length);
//fill the header //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]; HeaderSip = new long[len];
HeaderPtr = new int [len]; HeaderPtr = new int[len];
long startIp, xDataPtr; long startIp, dataPtr2;
for (int i = 0; i < b.Length; i += 8) for (int i = 0; i < b.Length; i += 8)
{ {
startIp = Util.getIntLong(b, i); startIp = Util.getIntLong(b, i);
xDataPtr = Util.getIntLong(b, i + 4); dataPtr2 = Util.getIntLong(b, i + 4);
if (xDataPtr == 0) break; if (dataPtr2 == 0) break;
HeaderSip[idx] = startIp; HeaderSip[idx] = startIp;
HeaderPtr[idx] = (int) xDataPtr; HeaderPtr[idx] = (int)dataPtr2;
idx++; idx++;
} }
@ -266,7 +260,6 @@ namespace DbMaker
eptr = HeaderPtr[m]; eptr = HeaderPtr[m];
break; break;
} }
h = m - 1; h = m - 1;
} }
else else
@ -283,7 +276,6 @@ namespace DbMaker
eptr = HeaderPtr[m + 1]; eptr = HeaderPtr[m + 1];
break; break;
} }
l = m + 1; l = m + 1;
} }
} }
@ -293,12 +285,11 @@ namespace DbMaker
//2. search the index blocks to define the data //2. search the index blocks to define the data
int blockLen = eptr - sptr, blen = IndexBlock.getIndexBlockLength(); 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.seek(sptr);
raf.readFully(iBuffer, 0, iBuffer.Length); raf.readFully(iBuffer, 0, iBuffer.Length);
l = 0; l = 0; h = blockLen / blen;
h = blockLen / blen;
long sip, eip, dataptr = 0; long sip, eip, dataptr = 0;
while (l <= h) while (l <= h)
{ {
@ -328,37 +319,37 @@ namespace DbMaker
if (dataptr == 0) return null; if (dataptr == 0) return null;
//3. get the data //3. get the data
int dataLen = (int) ((dataptr >> 24) & 0xFF); int dataLen = (int)((dataptr >> 24) & 0xFF);
int dataPtr = (int) ((dataptr & 0x00FFFFFF)); int dataPtr = (int)((dataptr & 0x00FFFFFF));
raf.seek(dataPtr); raf.seek(dataPtr);
byte[] data = new byte[dataLen]; byte[] data = new byte[dataLen];
raf.readFully(data, 0, data.Length); 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"); //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); return new DataBlock(city_id, region, dataPtr);
} }
/** /**
* get the region throught the ip address with b-tree search algorithm * get the region throught the ip address with b-tree search algorithm
* *
* @param ip * @param ip
* @return DataBlock * @return DataBlock
* @throws IOException * @
*/ */
public DataBlock btreeSearch(String ip) public DataBlock btreeSearch(String ip)
{ {
return btreeSearch(Util.ip2long(ip)); return btreeSearch(Util.ip2long(ip));
} }
/** /**
* get the region with a int ip address with binary search algorithm * get the region with a int ip address with binary search algorithm
* *
* @param ip * @param ip
* @throws IOException * @
*/ */
public DataBlock binarySearch(long ip) public DataBlock binarySearch(long ip)
{ {
int blen = IndexBlock.getIndexBlockLength(); int blen = IndexBlock.getIndexBlockLength();
@ -370,7 +361,7 @@ namespace DbMaker
//initialize the global vars //initialize the global vars
firstIndexPtr = Util.getIntLong(superBytes, 0); firstIndexPtr = Util.getIntLong(superBytes, 0);
lastIndexPtr = Util.getIntLong(superBytes, 4); 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 //search the index blocks to define the data
@ -380,7 +371,7 @@ namespace DbMaker
while (l <= h) while (l <= h)
{ {
int m = (l + h) >> 1; 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); raf.readFully(buffer, 0, buffer.Length);
sip = Util.getIntLong(buffer, 0); sip = Util.getIntLong(buffer, 0);
if (ip < sip) if (ip < sip)
@ -406,58 +397,53 @@ namespace DbMaker
if (dataptr == 0) return null; if (dataptr == 0) return null;
//get the data //get the data
int dataLen = (int) ((dataptr >> 24) & 0xFF); int dataLen = (int)((dataptr >> 24) & 0xFF);
int dataPtr = (int) ((dataptr & 0x00FFFFFF)); int dataPtr = (int)((dataptr & 0x00FFFFFF));
raf.seek(dataPtr); raf.seek(dataPtr);
byte[] data = new byte[dataLen]; byte[] data = new byte[dataLen];
raf.readFully(data, 0, data.Length); 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"); //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); return new DataBlock(city_id, region, dataPtr);
} }
/** /**
* get the region throught the ip address with binary search algorithm * get the region throught the ip address with binary search algorithm
* *
* @param ip * @param ip
* @return DataBlock * @return DataBlock
* @throws IOException * @
*/ */
public DataBlock binarySearch(String ip) public DataBlock binarySearch(String ip)
{ {
return binarySearch(Util.ip2long(ip)); return binarySearch(Util.ip2long(ip));
} }
/** /**
* get the db config * get the db config
* *
* @return DbConfig * @return DbConfig
*/ */
public DbConfig getDbConfig() public DbConfig getDbConfig()
{ {
return dbConfig; return dbConfig;
} }
/** /**
* close the db * close the db
* *
* @throws IOException * @
*/ */
private void close() public void close()
{ {
HeaderSip = null; //let gc do its work HeaderSip = null; //let gc do its work
HeaderPtr = null; HeaderPtr = null;
dbBinStr = null; dbBinStr = null;
raf.close(); raf.close();
} }
public void Dispose()
{
close();
}
} }
}
}

View File

@ -1,21 +1,22 @@
namespace DbMaker using System;
namespace IP2RegionDotNetDbMaker
{ {
/** /**
* header block class * header block class
* *
* @author chenxin<chenxin619315@gmail.com> * @author chenxin<chenxin619315@gmail.com>
*/ */
public class HeaderBlock public class HeaderBlock
{ {
/** /**
* index block start ip address * index block start ip address
*/ */
private long indexStartIp; private long indexStartIp;
/** /**
* ip address * ip address
*/ */
private int indexPtr; private int indexPtr;
public HeaderBlock(long indexStartIp, int indexPtr) public HeaderBlock(long indexStartIp, int indexPtr)
@ -47,10 +48,10 @@ namespace DbMaker
} }
/** /**
* get the bytes for db storage * get the bytes for db storage
* *
* @return byte[] * @return byte[]
*/ */
public byte[] getBytes() public byte[] getBytes()
{ {
/* /*
@ -67,4 +68,4 @@ namespace DbMaker
return b; return b;
} }
} }
} }

View File

@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net452;net5.0</TargetFrameworks>
<AssemblyName>dbMaker</AssemblyName>
</PropertyGroup>
</Project>

View File

@ -1,11 +1,12 @@
namespace DbMaker using System;
namespace IP2RegionDotNetDbMaker
{ {
/** /**
* item index class * item index class
* *
* @author chenxin<chenxin619315@gmail.com> * @author chenxin<chenxin619315@gmail.com>
*/ */
public class IndexBlock public class IndexBlock
{ {
private static int LENGTH = 12; private static int LENGTH = 12;
@ -88,10 +89,10 @@ namespace DbMaker
} }
/** /**
* get the bytes for storage * get the bytes for storage
* *
* @return byte[] * @return byte[]
*/ */
public byte[] getBytes() public byte[] getBytes()
{ {
/* /*
@ -102,8 +103,8 @@ namespace DbMaker
*/ */
byte[] b = new byte[12]; byte[] b = new byte[12];
Util.writeIntLong(b, 0, startIp); //start ip Util.writeIntLong(b, 0, startIp); //start ip
Util.writeIntLong(b, 4, endIp); //end ip Util.writeIntLong(b, 4, endIp); //end ip
//write the data ptr and the length //write the data ptr and the length
long mix = dataPtr | ((dataLen << 24) & 0xFF000000L); long mix = dataPtr | ((dataLen << 24) & 0xFF000000L);
@ -112,4 +113,4 @@ namespace DbMaker
return b; return b;
} }
} }
} }

View File

@ -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<T> : List<T>
{
internal T getFirst()
{
return this[0];
}
internal void add(T item)
{
this.Add(item);
}
internal T getLast()
{
return this[this.Count - 1];
}
internal IEnumerable<T> iterator()
{
return this;
}
}
public class HashMap<K, V> : Dictionary<K, V>
{
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<String> _lines = new Queue<string>();
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);
}
}
}

View File

@ -1,15 +1,14 @@
using System; using System;
using System.Text;
namespace DbMaker
namespace IP2RegionDotNetDbMaker
{ {
/** /**
* util class * util class
* *
* @author chenxin<chenxin619315@gmail.com> * @author chenxin<chenxin619315@gmail.com>
*/ */
public class Util public class Util
{ {
/** /**
@ -24,8 +23,7 @@ namespace DbMaker
{ {
for (int i = 0; i < bytes; i++) for (int i = 0; i < bytes; i++)
{ {
//b[offset++] = (byte)((v >>> (8 * i)) & 0xFF); b[offset++] = (byte)((v >> (8 * i)) & 0xFF);
b[offset++] = (byte) ((v >> (8 * i)) & 0xFF);
} }
} }
@ -38,10 +36,10 @@ namespace DbMaker
*/ */
public static void writeIntLong(byte[] b, int offset, long v) public static void writeIntLong(byte[] b, int offset, long v)
{ {
b[offset++] = (byte) ((v >> 0) & 0xFF); b[offset++] = (byte)((v >> 0) & 0xFF);
b[offset++] = (byte) ((v >> 8) & 0xFF); b[offset++] = (byte)((v >> 8) & 0xFF);
b[offset++] = (byte) ((v >> 16) & 0xFF); b[offset++] = (byte)((v >> 16) & 0xFF);
b[offset] = (byte) ((v >> 24) & 0xFF); b[offset] = (byte)((v >> 24) & 0xFF);
} }
/** /**
@ -119,12 +117,12 @@ namespace DbMaker
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb sb
.Append((ip >> 24) & 0xFF).Append('.') .append((ip >> 24) & 0xFF).append('.')
.Append((ip >> 16) & 0xFF).Append('.') .append((ip >> 16) & 0xFF).append('.')
.Append((ip >> 8) & 0xFF).Append('.') .append((ip >> 8) & 0xFF).append('.')
.Append((ip >> 0) & 0xFF); .append((ip >> 0) & 0xFF);
return sb.ToString(); return sb.toString();
} }
/** /**
@ -133,21 +131,18 @@ namespace DbMaker
* @param ip * @param ip
* @return boolean * @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; if (p.Length != 4) return false;
//for ( String pp : p ) {
//for ( String pp : p ) {
foreach (var pp in p) foreach (var pp in p)
{ {
if (pp.Length > 3) return false; if (pp.Length > 3) return false;
int val = Int32.Parse(pp); //Integer.valueOf(pp); var val = Int32.Parse(pp);
if (val > 255) return false; if (val > 255) return false;
} }
return true; return true;
} }
} }
} }