rename to Java style.
This commit is contained in:
parent
37c6349f10
commit
9843232542
|
|
@ -1,47 +1,89 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
namespace DbMaker
|
namespace DbMaker
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// data block class
|
/**
|
||||||
/// </summary>
|
* data block class
|
||||||
|
*
|
||||||
|
* @author chenxin<chenxin619315@gmail.com>
|
||||||
|
*/
|
||||||
public class DataBlock
|
public class DataBlock
|
||||||
{
|
{
|
||||||
/// <summary>
|
/**
|
||||||
/// construct method
|
* city id
|
||||||
/// </summary>
|
*/
|
||||||
/// <param name="cityId"></param>
|
private int city_id;
|
||||||
/// <param name="region"></param>
|
|
||||||
/// <param name="dataPtr"></param>
|
/**
|
||||||
public DataBlock(int cityId, string region, int dataPtr)
|
* region address
|
||||||
|
*/
|
||||||
|
private String region;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* region ptr in the db file
|
||||||
|
*/
|
||||||
|
private int dataPtr;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* construct method
|
||||||
|
*
|
||||||
|
* @param city_id
|
||||||
|
* @param region region string
|
||||||
|
* @param dataPtr data ptr
|
||||||
|
*/
|
||||||
|
public DataBlock(int city_id, String region, int dataPtr)
|
||||||
{
|
{
|
||||||
CityId = cityId;
|
this.city_id = city_id;
|
||||||
Region = region;
|
this.region = region;
|
||||||
DataPtr = dataPtr;
|
this.dataPtr = dataPtr;
|
||||||
}
|
}
|
||||||
|
|
||||||
public DataBlock(int cityId, string region) : this(cityId, region, 0)
|
public DataBlock(int city_id, String region) : this(city_id, region, 0)
|
||||||
{
|
{
|
||||||
|
//this(city_id, region, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
public int getCityId()
|
||||||
/// city id
|
|
||||||
/// </summary>
|
|
||||||
public int CityId { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// region address
|
|
||||||
/// </summary>
|
|
||||||
public string Region { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// region ptr in the db file
|
|
||||||
/// </summary>
|
|
||||||
public int DataPtr { get; set; }
|
|
||||||
|
|
||||||
public override string ToString()
|
|
||||||
{
|
{
|
||||||
return String.Join("|", this.CityId, this.Region, this.DataPtr);
|
return city_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DataBlock setCityId(int city_id)
|
||||||
|
{
|
||||||
|
this.city_id = city_id;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRegion()
|
||||||
|
{
|
||||||
|
return region;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DataBlock setRegion(String region)
|
||||||
|
{
|
||||||
|
this.region = region;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getDataPtr()
|
||||||
|
{
|
||||||
|
return dataPtr;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DataBlock setDataPtr(int dataPtr)
|
||||||
|
{
|
||||||
|
this.dataPtr = dataPtr;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override String ToString()
|
||||||
|
{
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
|
||||||
|
sb.Append(city_id).Append('|').Append(region).Append('|').Append(dataPtr);
|
||||||
|
return sb.ToString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,49 +1,67 @@
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace DbMaker
|
namespace DbMaker
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// database configuration class
|
/**
|
||||||
/// </summary>
|
* database configuration class
|
||||||
|
*
|
||||||
|
* @author chenxin<chenxin619315@gmail.com>
|
||||||
|
*/
|
||||||
public class DbConfig
|
public class DbConfig
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* total header data block size
|
||||||
|
*/
|
||||||
|
private int totalHeaderSize;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* max index data block size
|
||||||
|
* u should always choice the fastest read block size
|
||||||
|
*/
|
||||||
|
private int indexBlockSize;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* construct method
|
||||||
|
*
|
||||||
|
* @param totalHeaderSize
|
||||||
|
* @throws DbMakerConfigException
|
||||||
|
*/
|
||||||
public DbConfig(int totalHeaderSize)
|
public DbConfig(int totalHeaderSize)
|
||||||
{
|
{
|
||||||
if (totalHeaderSize % 8 != 0)
|
if ((totalHeaderSize % 8) != 0)
|
||||||
{
|
{
|
||||||
throw new DbMakerConfigException("totalHeaderSize must be times of 8");
|
throw new DbMakerConfigException("totalHeaderSize must be times of 8");
|
||||||
}
|
}
|
||||||
|
|
||||||
TotalHeaderSize = totalHeaderSize;
|
this.totalHeaderSize = totalHeaderSize;
|
||||||
IndexBlockSize = 8192; //4*2048
|
this.indexBlockSize = 8192; //4 * 2048
|
||||||
}
|
}
|
||||||
|
|
||||||
public DbConfig() : this(8 * 2048)
|
public DbConfig() : this(8 * 2048)
|
||||||
{
|
{
|
||||||
|
//this(8 * 2048);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
public int getTotalHeaderSize()
|
||||||
/// total header data block size
|
|
||||||
/// </summary>
|
|
||||||
public int TotalHeaderSize { get; set; }
|
|
||||||
/// <summary>
|
|
||||||
/// max index data block size
|
|
||||||
/// u should always choice the fastest read block size
|
|
||||||
/// </summary>
|
|
||||||
public int IndexBlockSize { get; set; }
|
|
||||||
|
|
||||||
public DbConfig SetTotalHeaderSize(int totalHeaderSize)
|
|
||||||
{
|
{
|
||||||
this.TotalHeaderSize = totalHeaderSize;
|
return totalHeaderSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DbConfig setTotalHeaderSize(int totalHeaderSize)
|
||||||
|
{
|
||||||
|
this.totalHeaderSize = totalHeaderSize;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
public DbConfig SetIndexBlockSize(int dataBlockSize)
|
|
||||||
|
public int getIndexBlockSize()
|
||||||
{
|
{
|
||||||
this.IndexBlockSize = dataBlockSize;
|
return indexBlockSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DbConfig setIndexBlockSize(int dataBlockSize)
|
||||||
|
{
|
||||||
|
this.indexBlockSize = dataBlockSize;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -1,10 +1,8 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace DbMaker
|
namespace DbMaker
|
||||||
{
|
{
|
||||||
class DbMakerConfigException : Exception
|
public class DbMakerConfigException : Exception
|
||||||
{
|
{
|
||||||
public DbMakerConfigException(String message) : base(message)
|
public DbMakerConfigException(String message) : base(message)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,14 @@
|
||||||
using System;
|
using System;
|
||||||
using System.IO;
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace DbMaker
|
namespace DbMaker
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// ip db searcher class (Not thread safe)
|
/**
|
||||||
/// </summary>
|
* ip db searcher class (Not thread safe)
|
||||||
|
*
|
||||||
|
* @author chenxin<chenxin619315@gmail.com>
|
||||||
|
*/
|
||||||
public class DbSearcher : IDisposable
|
public class DbSearcher : IDisposable
|
||||||
{
|
{
|
||||||
public const int BTREE_ALGORITHM = 1;
|
public const int BTREE_ALGORITHM = 1;
|
||||||
|
|
@ -14,32 +16,36 @@ namespace DbMaker
|
||||||
public const int MEMORY_ALGORITYM = 3;
|
public const int MEMORY_ALGORITYM = 3;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* for memory mode
|
* db config
|
||||||
* the original db binary string
|
|
||||||
*/
|
*/
|
||||||
private byte[] dbBinStr;
|
private DbConfig dbConfig = null;
|
||||||
|
|
||||||
/**
|
|
||||||
* super blocks info
|
|
||||||
*/
|
|
||||||
private long firstIndexPtr;
|
|
||||||
|
|
||||||
private int headerLength;
|
|
||||||
private int[] HeaderPtr;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* header blocks buffer
|
|
||||||
*/
|
|
||||||
private long[] HeaderSip;
|
|
||||||
|
|
||||||
private long lastIndexPtr;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* db file access handler
|
* db file access handler
|
||||||
*/
|
*/
|
||||||
private readonly Stream raf;
|
private RandomAccessFile raf = null;
|
||||||
|
|
||||||
private int totalIndexBlocks;
|
/**
|
||||||
|
* header blocks buffer
|
||||||
|
*/
|
||||||
|
private long[] HeaderSip = null;
|
||||||
|
|
||||||
|
private int[] HeaderPtr = null;
|
||||||
|
private int headerLength;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* super blocks info
|
||||||
|
*/
|
||||||
|
private long firstIndexPtr = 0;
|
||||||
|
|
||||||
|
private long lastIndexPtr = 0;
|
||||||
|
private int totalIndexBlocks = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* for memory mode
|
||||||
|
* the original db binary string
|
||||||
|
*/
|
||||||
|
private byte[] dbBinStr = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* construct class
|
* construct class
|
||||||
|
|
@ -48,10 +54,10 @@ namespace DbMaker
|
||||||
* @param dbFile
|
* @param dbFile
|
||||||
* @throws FileNotFoundException
|
* @throws FileNotFoundException
|
||||||
*/
|
*/
|
||||||
public DbSearcher(DbConfig dbConfig, string dbFile)
|
public DbSearcher(DbConfig dbConfig, String dbFile)
|
||||||
{
|
{
|
||||||
this.dbConfig = dbConfig;
|
this.dbConfig = dbConfig;
|
||||||
raf = File.OpenRead(dbFile); //new RandomAccessFile(dbFile, "r");
|
raf = new RandomAccessFile(dbFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -68,17 +74,7 @@ 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;
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* db config
|
|
||||||
*/
|
|
||||||
public DbConfig dbConfig { get; set; }
|
|
||||||
|
|
||||||
public void Dispose()
|
|
||||||
{
|
|
||||||
Close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -87,15 +83,15 @@ namespace DbMaker
|
||||||
* @param ip
|
* @param ip
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public DataBlock MemorySearch(long ip)
|
public DataBlock memorySearch(long ip)
|
||||||
{
|
{
|
||||||
var 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, SeekOrigin.Begin);
|
raf.seek(0L);
|
||||||
//raf.readFully(dbBinStr, 0, dbBinStr.length);
|
raf.readFully(dbBinStr, 0, dbBinStr.Length);
|
||||||
raf.Read(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);
|
||||||
|
|
@ -107,8 +103,8 @@ namespace DbMaker
|
||||||
long sip, eip, dataptr = 0;
|
long sip, eip, dataptr = 0;
|
||||||
while (l <= h)
|
while (l <= h)
|
||||||
{
|
{
|
||||||
var m = l + h >> 1;
|
int m = (l + h) >> 1;
|
||||||
var 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)
|
||||||
|
|
@ -131,20 +127,22 @@ namespace DbMaker
|
||||||
}
|
}
|
||||||
|
|
||||||
//not matched
|
//not matched
|
||||||
if (dataptr == 0)
|
if (dataptr == 0) return null;
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
//get the data
|
//get the data
|
||||||
var dataLen = (int) (dataptr >> 24 & 0xFF);
|
int dataLen = (int) ((dataptr >> 24) & 0xFF);
|
||||||
var dataPtr = (int) (dataptr & 0x00FFFFFF);
|
int dataPtr = (int) ((dataptr & 0x00FFFFFF));
|
||||||
var 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 = Encoding.UTF8.GetString(dbBinStr, dataPtr + 4, dataLen - 4);
|
var region = ReadString(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
|
||||||
*
|
*
|
||||||
|
|
@ -152,9 +150,9 @@ namespace DbMaker
|
||||||
* @return DataBlock
|
* @return DataBlock
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public DataBlock MemorySearch(string ip)
|
public DataBlock memorySearch(String ip)
|
||||||
{
|
{
|
||||||
return MemorySearch(Util.ip2long(ip));
|
return memorySearch(Util.ip2long(ip));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -164,27 +162,25 @@ namespace DbMaker
|
||||||
* @param indexPtr
|
* @param indexPtr
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public DataBlock GetByIndexPtr(long ptr)
|
public DataBlock getByIndexPtr(long ptr)
|
||||||
{
|
{
|
||||||
raf.Seek(ptr, SeekOrigin.Begin);
|
raf.seek(ptr);
|
||||||
var
|
byte[] buffer = new byte[12];
|
||||||
buffer = new byte[12];
|
raf.readFully(buffer, 0, buffer.Length);
|
||||||
//raf.readFully(buffer, 0, buffer.length);
|
|
||||||
raf.Read(buffer, 0, buffer.Length);
|
|
||||||
//long startIp = Util.getIntLong(buffer, 0);
|
//long startIp = Util.getIntLong(buffer, 0);
|
||||||
//long endIp = Util.getIntLong(buffer, 4);
|
//long endIp = Util.getIntLong(buffer, 4);
|
||||||
var extra = Util.getIntLong(buffer, 8);
|
long extra = Util.getIntLong(buffer, 8);
|
||||||
|
|
||||||
var dataLen = (int) (extra >> 24 & 0xFF);
|
int dataLen = (int) ((extra >> 24) & 0xFF);
|
||||||
var dataPtr = (int) (extra & 0x00FFFFFF);
|
int dataPtr = (int) ((extra & 0x00FFFFFF));
|
||||||
|
|
||||||
raf.Seek(dataPtr, SeekOrigin.Begin);
|
raf.seek(dataPtr);
|
||||||
var data = new byte[dataLen];
|
byte[] data = new byte[dataLen];
|
||||||
//raf.readFully(data, 0, data.length);
|
raf.readFully(data, 0, data.Length);
|
||||||
raf.Read(data, 0, data.Length);
|
|
||||||
var 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 = Encoding.UTF8.GetString(data, 4, data.Length - 4);
|
var region = ReadString(data, 4, data.Length - 4);
|
||||||
return new DataBlock(city_id, region, dataPtr);
|
return new DataBlock(city_id, region, dataPtr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -194,32 +190,26 @@ namespace DbMaker
|
||||||
* @param ip
|
* @param ip
|
||||||
* @throws IOException
|
* @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
|
||||||
raf.Seek(8, SeekOrigin.Begin);
|
byte[] b = new byte[dbConfig.getTotalHeaderSize()];
|
||||||
var b = new byte[dbConfig.TotalHeaderSize];
|
|
||||||
// byte[] b = new byte[4096];
|
// byte[] b = new byte[4096];
|
||||||
//raf.readFully(b, 0, b.length);
|
raf.readFully(b, 0, b.Length);
|
||||||
raf.Read(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;
|
long startIp, xDataPtr;
|
||||||
long xDataPtr = 0;
|
for (int i = 0; i < b.Length; i += 8)
|
||||||
for (var i = 0; i < b.Length; i += 8)
|
|
||||||
{
|
{
|
||||||
startIp = Util.getIntLong(b, i);
|
startIp = Util.getIntLong(b, i);
|
||||||
xDataPtr = Util.getIntLong(b, i + 4);
|
xDataPtr = Util.getIntLong(b, i + 4);
|
||||||
if (xDataPtr == 0)
|
if (xDataPtr == 0) break;
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
HeaderSip[idx] = startIp;
|
HeaderSip[idx] = startIp;
|
||||||
HeaderPtr[idx] = (int) xDataPtr;
|
HeaderPtr[idx] = (int) xDataPtr;
|
||||||
|
|
@ -232,18 +222,17 @@ namespace DbMaker
|
||||||
//1. define the index block with the binary search
|
//1. define the index block with the binary search
|
||||||
if (ip == HeaderSip[0])
|
if (ip == HeaderSip[0])
|
||||||
{
|
{
|
||||||
return GetByIndexPtr(HeaderPtr[0]);
|
return getByIndexPtr(HeaderPtr[0]);
|
||||||
}
|
}
|
||||||
|
else if (ip == HeaderSip[headerLength - 1])
|
||||||
if (ip == HeaderSip[headerLength - 1])
|
|
||||||
{
|
{
|
||||||
return GetByIndexPtr(HeaderPtr[headerLength - 1]);
|
return getByIndexPtr(HeaderPtr[headerLength - 1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
int l = 0, h = headerLength, sptr = 0, eptr = 0;
|
int l = 0, h = headerLength, sptr = 0, eptr = 0;
|
||||||
while (l <= h)
|
while (l <= h)
|
||||||
{
|
{
|
||||||
var m = l + h >> 1;
|
int m = (l + h) >> 1;
|
||||||
|
|
||||||
//perfetc matched, just return it
|
//perfetc matched, just return it
|
||||||
if (ip == HeaderSip[m])
|
if (ip == HeaderSip[m])
|
||||||
|
|
@ -271,8 +260,7 @@ namespace DbMaker
|
||||||
eptr = HeaderPtr[m + 1];
|
eptr = HeaderPtr[m + 1];
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
else if (ip > HeaderSip[m - 1])
|
||||||
if (ip > HeaderSip[m - 1])
|
|
||||||
{
|
{
|
||||||
sptr = HeaderPtr[m - 1];
|
sptr = HeaderPtr[m - 1];
|
||||||
eptr = HeaderPtr[m];
|
eptr = HeaderPtr[m];
|
||||||
|
|
@ -289,8 +277,7 @@ namespace DbMaker
|
||||||
eptr = HeaderPtr[m];
|
eptr = HeaderPtr[m];
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
else if (ip <= HeaderSip[m + 1])
|
||||||
if (ip <= HeaderSip[m + 1])
|
|
||||||
{
|
{
|
||||||
sptr = HeaderPtr[m];
|
sptr = HeaderPtr[m];
|
||||||
eptr = HeaderPtr[m + 1];
|
eptr = HeaderPtr[m + 1];
|
||||||
|
|
@ -302,27 +289,21 @@ namespace DbMaker
|
||||||
}
|
}
|
||||||
|
|
||||||
//match nothing just stop it
|
//match nothing just stop it
|
||||||
if (sptr == 0)
|
if (sptr == 0) return null;
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
//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();
|
||||||
var
|
byte[] iBuffer = new byte[blockLen + blen]; //include the right border block
|
||||||
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);
|
|
||||||
raf.Seek(sptr, SeekOrigin.Begin);
|
|
||||||
raf.Read(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)
|
||||||
{
|
{
|
||||||
var m = l + h >> 1;
|
int m = (l + h) >> 1;
|
||||||
var p = m * blen;
|
int p = m * blen;
|
||||||
sip = Util.getIntLong(iBuffer, p);
|
sip = Util.getIntLong(iBuffer, p);
|
||||||
if (ip < sip)
|
if (ip < sip)
|
||||||
{
|
{
|
||||||
|
|
@ -344,23 +325,19 @@ namespace DbMaker
|
||||||
}
|
}
|
||||||
|
|
||||||
//not matched
|
//not matched
|
||||||
if (dataptr == 0)
|
if (dataptr == 0) return null;
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
//3. get the data
|
//3. get the data
|
||||||
var dataLen = (int) (dataptr >> 24 & 0xFF);
|
int dataLen = (int) ((dataptr >> 24) & 0xFF);
|
||||||
var dataPtr = (int) (dataptr & 0x00FFFFFF);
|
int dataPtr = (int) ((dataptr & 0x00FFFFFF));
|
||||||
|
|
||||||
//raf.seek(dataPtr);
|
raf.seek(dataPtr);
|
||||||
var data = new byte[dataLen];
|
byte[] data = new byte[dataLen];
|
||||||
// raf.readFully(data, 0, data.length);
|
raf.readFully(data, 0, data.Length);
|
||||||
raf.Seek(dataPtr, SeekOrigin.Begin);
|
|
||||||
raf.Read(data, 0, data.Length);
|
int city_id = (int) Util.getIntLong(data, 0);
|
||||||
var 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -371,9 +348,9 @@ namespace DbMaker
|
||||||
* @return DataBlock
|
* @return DataBlock
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public DataBlock BTreeSearch(string ip)
|
public DataBlock btreeSearch(String ip)
|
||||||
{
|
{
|
||||||
return BTreeSearch(Util.ip2long(ip));
|
return btreeSearch(Util.ip2long(ip));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -382,16 +359,14 @@ namespace DbMaker
|
||||||
* @param ip
|
* @param ip
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public DataBlock BinarySearch(long ip)
|
public DataBlock binarySearch(long ip)
|
||||||
{
|
{
|
||||||
var blen = IndexBlock.GetIndexBlockLength();
|
int blen = IndexBlock.getIndexBlockLength();
|
||||||
if (totalIndexBlocks == 0)
|
if (totalIndexBlocks == 0)
|
||||||
{
|
{
|
||||||
//raf.seek(0L);
|
raf.seek(0L);
|
||||||
var superBytes = new byte[8];
|
byte[] superBytes = new byte[8];
|
||||||
//raf.readFully(superBytes, 0, superBytes.length);
|
raf.readFully(superBytes, 0, superBytes.Length);
|
||||||
raf.Seek(0, SeekOrigin.Begin);
|
|
||||||
raf.Read(superBytes, 0, superBytes.Length);
|
|
||||||
//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);
|
||||||
|
|
@ -400,16 +375,13 @@ namespace DbMaker
|
||||||
|
|
||||||
//search the index blocks to define the data
|
//search the index blocks to define the data
|
||||||
int l = 0, h = totalIndexBlocks;
|
int l = 0, h = totalIndexBlocks;
|
||||||
var
|
byte[] buffer = new byte[blen];
|
||||||
buffer = new byte[blen];
|
|
||||||
long sip, eip, dataptr = 0;
|
long sip, eip, dataptr = 0;
|
||||||
while (l <= h)
|
while (l <= h)
|
||||||
{
|
{
|
||||||
var 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);
|
||||||
raf.Seek(firstIndexPtr + m * blen, SeekOrigin.Begin);
|
|
||||||
raf.Read(buffer, 0, buffer.Length);
|
|
||||||
sip = Util.getIntLong(buffer, 0);
|
sip = Util.getIntLong(buffer, 0);
|
||||||
if (ip < sip)
|
if (ip < sip)
|
||||||
{
|
{
|
||||||
|
|
@ -431,24 +403,19 @@ namespace DbMaker
|
||||||
}
|
}
|
||||||
|
|
||||||
//not matched
|
//not matched
|
||||||
if (dataptr == 0)
|
if (dataptr == 0) return null;
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
//get the data
|
//get the data
|
||||||
var dataLen = (int) (dataptr >> 24 & 0xFF);
|
int dataLen = (int) ((dataptr >> 24) & 0xFF);
|
||||||
var dataPtr = (int) (dataptr & 0x00FFFFFF);
|
int dataPtr = (int) ((dataptr & 0x00FFFFFF));
|
||||||
|
|
||||||
//raf.seek(dataPtr);
|
raf.seek(dataPtr);
|
||||||
var data = new byte[dataLen];
|
byte[] data = new byte[dataLen];
|
||||||
//raf.readFully(data, 0, data.length);
|
raf.readFully(data, 0, data.Length);
|
||||||
raf.Seek(dataPtr, SeekOrigin.Begin);
|
|
||||||
raf.Read(data, 0, data.Length);
|
|
||||||
|
|
||||||
var 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 = Encoding.UTF8.GetString(data, 4, data.Length - 4);
|
var region = ReadString(data, 4, data.Length - 4);
|
||||||
return new DataBlock(city_id, region, dataPtr);
|
return new DataBlock(city_id, region, dataPtr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -459,23 +426,38 @@ namespace DbMaker
|
||||||
* @return DataBlock
|
* @return DataBlock
|
||||||
* @throws IOException
|
* @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
|
||||||
|
*
|
||||||
|
* @return DbConfig
|
||||||
|
*/
|
||||||
|
public DbConfig getDbConfig()
|
||||||
|
{
|
||||||
|
return dbConfig;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* close the db
|
* close the db
|
||||||
*
|
*
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
private void Close()
|
private 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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -1,43 +1,57 @@
|
||||||
namespace DbMaker
|
namespace DbMaker
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// header block class
|
/**
|
||||||
/// </summary>
|
* header block class
|
||||||
|
*
|
||||||
|
* @author chenxin<chenxin619315@gmail.com>
|
||||||
|
*/
|
||||||
public class HeaderBlock
|
public class HeaderBlock
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* index block start ip address
|
||||||
|
*/
|
||||||
|
private long indexStartIp;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ip address
|
||||||
|
*/
|
||||||
|
private int indexPtr;
|
||||||
|
|
||||||
public HeaderBlock(long indexStartIp, int indexPtr)
|
public HeaderBlock(long indexStartIp, int indexPtr)
|
||||||
{
|
{
|
||||||
IndexStartIp = indexStartIp;
|
this.indexStartIp = indexStartIp;
|
||||||
IndexPtr = indexPtr;
|
this.indexPtr = indexPtr;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
public long getIndexStartIp()
|
||||||
/// index block start ip address
|
|
||||||
/// </summary>
|
|
||||||
public long IndexStartIp { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// ip address
|
|
||||||
/// </summary>
|
|
||||||
public int IndexPtr { get; set; }
|
|
||||||
|
|
||||||
public HeaderBlock SetIndexStartIp(long indexStartIp)
|
|
||||||
{
|
{
|
||||||
IndexStartIp = indexStartIp;
|
return indexStartIp;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HeaderBlock setIndexStartIp(long indexStartIp)
|
||||||
|
{
|
||||||
|
this.indexStartIp = indexStartIp;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HeaderBlock SetIndexPtr(int indexPtr)
|
public int getIndexPtr()
|
||||||
{
|
{
|
||||||
IndexPtr = indexPtr;
|
return indexPtr;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HeaderBlock setIndexPtr(int indexPtr)
|
||||||
|
{
|
||||||
|
this.indexPtr = indexPtr;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/**
|
||||||
/// get the bytes for db storage
|
* get the bytes for db storage
|
||||||
/// </summary>
|
*
|
||||||
/// <returns></returns>
|
* @return byte[]
|
||||||
public byte[] GetBytes()
|
*/
|
||||||
|
public byte[] getBytes()
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
* +------------+-----------+
|
* +------------+-----------+
|
||||||
|
|
@ -45,11 +59,10 @@
|
||||||
* +------------+-----------+
|
* +------------+-----------+
|
||||||
* start ip index ptr
|
* start ip index ptr
|
||||||
*/
|
*/
|
||||||
|
byte[] b = new byte[8];
|
||||||
|
|
||||||
var b = new byte[8];
|
Util.writeIntLong(b, 0, indexStartIp);
|
||||||
|
Util.writeIntLong(b, 4, indexPtr);
|
||||||
Util.writeIntLong(b, 0, IndexStartIp);
|
|
||||||
Util.writeIntLong(b, 4, IndexPtr);
|
|
||||||
|
|
||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,65 +1,88 @@
|
||||||
namespace DbMaker
|
namespace DbMaker
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// item index class
|
/**
|
||||||
/// </summary>
|
* item index class
|
||||||
|
*
|
||||||
|
* @author chenxin<chenxin619315@gmail.com>
|
||||||
|
*/
|
||||||
public class IndexBlock
|
public class IndexBlock
|
||||||
{
|
{
|
||||||
private static int LENGTH = 12;
|
private static int LENGTH = 12;
|
||||||
|
|
||||||
public IndexBlock(long startIp, long endIp, int dataPtr, int dataLen)
|
|
||||||
{
|
|
||||||
StartIp = startIp;
|
|
||||||
EndIp = endIp;
|
|
||||||
DataPtr = dataPtr;
|
|
||||||
DataLen = dataLen;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* start ip address
|
* start ip address
|
||||||
*/
|
*/
|
||||||
public long StartIp { get; set; }
|
private long startIp;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* end ip address
|
* end ip address
|
||||||
*/
|
*/
|
||||||
public long EndIp { get; set; }
|
private long endIp;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* data ptr and data length
|
* data ptr and data length
|
||||||
*/
|
*/
|
||||||
public int DataPtr { get; set; }
|
private int dataPtr;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* data length
|
* data length
|
||||||
*/
|
*/
|
||||||
public int DataLen { get; set; }
|
private int dataLen;
|
||||||
|
|
||||||
public IndexBlock SetStartIp(long startIp)
|
public IndexBlock(long startIp, long endIp, int dataPtr, int dataLen)
|
||||||
{
|
{
|
||||||
StartIp = startIp;
|
this.startIp = startIp;
|
||||||
|
this.endIp = endIp;
|
||||||
|
this.dataPtr = dataPtr;
|
||||||
|
this.dataLen = dataLen;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getStartIp()
|
||||||
|
{
|
||||||
|
return startIp;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IndexBlock setStartIp(long startIp)
|
||||||
|
{
|
||||||
|
this.startIp = startIp;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IndexBlock SetEndIp(long endIp)
|
public long getEndIp()
|
||||||
{
|
{
|
||||||
EndIp = endIp;
|
return endIp;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IndexBlock setEndIp(long endIp)
|
||||||
|
{
|
||||||
|
this.endIp = endIp;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IndexBlock SetDataPtr(int dataPtr)
|
public int getDataPtr()
|
||||||
{
|
{
|
||||||
DataPtr = dataPtr;
|
return dataPtr;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IndexBlock setDataPtr(int dataPtr)
|
||||||
|
{
|
||||||
|
this.dataPtr = dataPtr;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IndexBlock SetDataLen(int dataLen)
|
public int getDataLen()
|
||||||
{
|
{
|
||||||
DataLen = dataLen;
|
return dataLen;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IndexBlock setDataLen(int dataLen)
|
||||||
|
{
|
||||||
|
this.dataLen = dataLen;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int GetIndexBlockLength()
|
public static int getIndexBlockLength()
|
||||||
{
|
{
|
||||||
return LENGTH;
|
return LENGTH;
|
||||||
}
|
}
|
||||||
|
|
@ -69,7 +92,7 @@
|
||||||
*
|
*
|
||||||
* @return byte[]
|
* @return byte[]
|
||||||
*/
|
*/
|
||||||
public byte[] GetBytes()
|
public byte[] getBytes()
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
* +------------+-----------+-----------+
|
* +------------+-----------+-----------+
|
||||||
|
|
@ -77,13 +100,13 @@
|
||||||
* +------------+-----------+-----------+
|
* +------------+-----------+-----------+
|
||||||
* start ip end ip data ptr + len
|
* start ip end ip data ptr + len
|
||||||
*/
|
*/
|
||||||
var 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
|
||||||
var mix = DataPtr | DataLen << 24 & 0xFF000000L;
|
long mix = dataPtr | ((dataLen << 24) & 0xFF000000L);
|
||||||
Util.writeIntLong(b, 8, mix);
|
Util.writeIntLong(b, 8, mix);
|
||||||
|
|
||||||
return b;
|
return b;
|
||||||
|
|
|
||||||
|
|
@ -6,13 +6,7 @@ namespace DbMaker
|
||||||
{
|
{
|
||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
var fn = @"G:\src\ip2region\data\ip2region.db";
|
Console.WriteLine("Hello World!");
|
||||||
using (var searcher = new DbSearcher(new DbConfig(), fn))
|
|
||||||
{
|
|
||||||
Console.WriteLine(searcher.BinarySearch("202.102.227.68"));
|
|
||||||
Console.WriteLine(searcher.BTreeSearch("202.102.227.68"));
|
|
||||||
Console.ReadLine();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1,49 @@
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
namespace DbMaker
|
||||||
|
{
|
||||||
|
public class RandomAccessFile : IDisposable
|
||||||
|
{
|
||||||
|
private readonly Stream _stream;
|
||||||
|
|
||||||
|
public RandomAccessFile(String file) : this(File.OpenRead(file))
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,14 +1,16 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Net;
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace DbMaker
|
namespace DbMaker
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// util class
|
|
||||||
/// </summary>
|
/**
|
||||||
public static class Util
|
* util class
|
||||||
|
*
|
||||||
|
* @author chenxin<chenxin619315@gmail.com>
|
||||||
|
*/
|
||||||
|
public class Util
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* write specfield bytes to a byte array start from offset
|
* write specfield bytes to a byte array start from offset
|
||||||
|
|
@ -22,7 +24,8 @@ 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -35,10 +38,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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -95,14 +98,16 @@ namespace DbMaker
|
||||||
*/
|
*/
|
||||||
public static long ip2long(String ip)
|
public static long ip2long(String ip)
|
||||||
{
|
{
|
||||||
ip = ip.Trim();
|
throw new NotImplementedException();
|
||||||
String[] ips = ip.Split('.');
|
//String[] p = ip.Split("\\.");
|
||||||
long ip1 = Int64.Parse(ips[0]);
|
//if ( p.Length != 4 ) return 0;
|
||||||
long ip2 = Int64.Parse(ips[1]);
|
|
||||||
long ip3 = Int64.Parse(ips[2]);
|
//int p1 = ((Convert.ToInt32(p[0]) << 24) & 0xFF000000);
|
||||||
long ip4 = Int64.Parse(ips[3]);
|
//int p2 = ((Integer.valueOf(p[1]) << 16) & 0x00FF0000);
|
||||||
long ip2long = 1L * ip1 * 256 * 256 * 256 + ip2 * 256 * 256 + ip3 * 256 + ip4;
|
//int p3 = ((Integer.valueOf(p[2]) << 8) & 0x0000FF00);
|
||||||
return ip2long;
|
//int p4 = ((Integer.valueOf(p[3]) << 0) & 0x000000FF);
|
||||||
|
|
||||||
|
//return ((p1 | p2 | p3 | p4) & 0xFFFFFFFFL);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -135,12 +140,15 @@ namespace DbMaker
|
||||||
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 = Convert.ToInt32(pp); //Integer.valueOf(pp);
|
int val = Int32.Parse(pp); //Integer.valueOf(pp);
|
||||||
if (val > 255) return false;
|
if (val > 255) return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue