mirror of
https://github.com/netfun2000/ip2region.git
synced 2026-02-27 09:44:31 +08:00
conflicts fixed
This commit is contained in:
@@ -3,54 +3,6 @@ package org.lionsoul.ip2region;
|
||||
/**
|
||||
* data block class
|
||||
*
|
||||
<<<<<<< HEAD
|
||||
* @author chenxin<chenxin619315@gmail.com>
|
||||
*/
|
||||
public class DataBlock
|
||||
{
|
||||
/**
|
||||
* city id
|
||||
*/
|
||||
private int city_id;
|
||||
|
||||
/**
|
||||
* region address
|
||||
*/
|
||||
private String region;
|
||||
|
||||
public DataBlock( int city_id, String region )
|
||||
{
|
||||
this.city_id = city_id;
|
||||
this.region = region;
|
||||
}
|
||||
|
||||
public int getCityId() {
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
sb.append(city_id).append('|').append(region);
|
||||
return sb.toString();
|
||||
}
|
||||
=======
|
||||
* @author chenxin<chenxin619315@gmail.com>
|
||||
*/
|
||||
public class DataBlock
|
||||
@@ -97,5 +49,4 @@ public class DataBlock
|
||||
sb.append(city_id).append('|').append(region);
|
||||
return sb.toString();
|
||||
}
|
||||
>>>>>>> 7e51a4909fdb01014f948c2dbc5cfb5fbed9ce56
|
||||
}
|
||||
|
||||
@@ -7,57 +7,6 @@ package org.lionsoul.ip2region;
|
||||
*/
|
||||
public class DbConfig
|
||||
{
|
||||
<<<<<<< HEAD
|
||||
/**
|
||||
* 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
|
||||
* @param dataBlockSize
|
||||
* @throws DbMakerConfigException
|
||||
*/
|
||||
public DbConfig( int totalHeaderSize ) throws DbMakerConfigException {
|
||||
if ( (totalHeaderSize % 8) != 0 )
|
||||
{
|
||||
throw new DbMakerConfigException("totalHeaderSize must be times of 8");
|
||||
}
|
||||
|
||||
this.totalHeaderSize = totalHeaderSize;
|
||||
this.indexBlockSize = 4096; //4 * 1024
|
||||
}
|
||||
|
||||
public DbConfig() throws DbMakerConfigException {
|
||||
this(8192);
|
||||
}
|
||||
|
||||
public int getTotalHeaderSize() {
|
||||
return totalHeaderSize;
|
||||
}
|
||||
|
||||
public DbConfig setTotalHeaderSize(int totalHeaderSize) {
|
||||
this.totalHeaderSize = totalHeaderSize;
|
||||
return this;
|
||||
}
|
||||
|
||||
public int getIndexBlockSize() {
|
||||
return indexBlockSize;
|
||||
}
|
||||
|
||||
public DbConfig setIndexBlockSize(int dataBlockSize) {
|
||||
this.indexBlockSize = dataBlockSize;
|
||||
return this;
|
||||
}
|
||||
=======
|
||||
/**
|
||||
* total header data block size
|
||||
*/
|
||||
@@ -107,5 +56,4 @@ public class DbConfig
|
||||
this.indexBlockSize = dataBlockSize;
|
||||
return this;
|
||||
}
|
||||
>>>>>>> 7e51a4909fdb01014f948c2dbc5cfb5fbed9ce56
|
||||
}
|
||||
|
||||
@@ -7,21 +7,6 @@ package org.lionsoul.ip2region;
|
||||
*/
|
||||
public class DbMakerConfigException extends Exception
|
||||
{
|
||||
<<<<<<< HEAD
|
||||
private static final long serialVersionUID = 4495714680349884838L;
|
||||
|
||||
public DbMakerConfigException( String info ) {
|
||||
super(info);
|
||||
}
|
||||
|
||||
public DbMakerConfigException( Throwable res ) {
|
||||
super(res);
|
||||
}
|
||||
|
||||
public DbMakerConfigException( String info, Throwable res ) {
|
||||
super(info, res);
|
||||
}
|
||||
=======
|
||||
private static final long serialVersionUID = 4495714680349884838L;
|
||||
|
||||
public DbMakerConfigException( String info ) {
|
||||
@@ -35,5 +20,4 @@ public class DbMakerConfigException extends Exception
|
||||
public DbMakerConfigException( String info, Throwable res ) {
|
||||
super(info, res);
|
||||
}
|
||||
>>>>>>> 7e51a4909fdb01014f948c2dbc5cfb5fbed9ce56
|
||||
}
|
||||
|
||||
@@ -11,310 +11,6 @@ import java.io.RandomAccessFile;
|
||||
*/
|
||||
public class DbSearcher
|
||||
{
|
||||
<<<<<<< HEAD
|
||||
public static final int BTREE_ALGORITHM = 1;
|
||||
public static final int BIN_ALGORITHM = 2;
|
||||
|
||||
/**
|
||||
* db config
|
||||
*/
|
||||
private DbConfig dbConfig = null;
|
||||
|
||||
/**
|
||||
* db file access handler
|
||||
*/
|
||||
private RandomAccessFile raf = null;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* construct class
|
||||
*
|
||||
* @param bdConfig
|
||||
* @param dbFile
|
||||
* @throws FileNotFoundException
|
||||
*/
|
||||
public DbSearcher( DbConfig dbConfig, String dbFile ) throws FileNotFoundException
|
||||
{
|
||||
this.dbConfig = dbConfig;
|
||||
raf = new RandomAccessFile(dbFile, "r");
|
||||
}
|
||||
|
||||
/**
|
||||
* get by index ptr
|
||||
*
|
||||
* @param indexPtr
|
||||
* @throws IOException
|
||||
*/
|
||||
public DataBlock getByIndexPtr( long ptr ) throws IOException
|
||||
{
|
||||
raf.seek(ptr);
|
||||
byte[] buffer = new byte[12];
|
||||
raf.readFully(buffer, 0, buffer.length);
|
||||
//long startIp = Util.getIntLong(buffer, 0);
|
||||
//long endIp = Util.getIntLong(buffer, 4);
|
||||
long extra = Util.getIntLong(buffer, 8);
|
||||
|
||||
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);
|
||||
String region = new String(data, 4, data.length - 4, "UTF-8");
|
||||
|
||||
return new DataBlock(city_id, region);
|
||||
}
|
||||
|
||||
/**
|
||||
* get the region with a int ip address with b-tree algorithm
|
||||
*
|
||||
* @param ip
|
||||
* @throws IOException
|
||||
*/
|
||||
public DataBlock btreeSearch( long ip ) throws IOException
|
||||
{
|
||||
//check and load the header
|
||||
if ( HeaderSip == null )
|
||||
{
|
||||
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
|
||||
HeaderSip = new long[len];
|
||||
HeaderPtr = new int [len];
|
||||
long startIp, dataPtr;
|
||||
for ( int i = 0; i < b.length; i += 8 ) {
|
||||
startIp = Util.getIntLong(b, i);
|
||||
dataPtr = Util.getIntLong(b, i + 4);
|
||||
if ( dataPtr == 0 ) break;
|
||||
|
||||
HeaderSip[idx] = startIp;
|
||||
HeaderPtr[idx] = (int)dataPtr;
|
||||
idx++;
|
||||
}
|
||||
|
||||
headerLength = idx;
|
||||
}
|
||||
|
||||
//1. define the index block with the binary search
|
||||
if ( ip == HeaderSip[0] ) {
|
||||
return getByIndexPtr(HeaderPtr[0]);
|
||||
} else if ( ip == HeaderSip[headerLength-1] ) {
|
||||
return getByIndexPtr(HeaderPtr[headerLength-1]);
|
||||
}
|
||||
|
||||
int l = 0, h = headerLength, sptr = 0, eptr = 0;
|
||||
while ( l <= h )
|
||||
{
|
||||
int m = (l + h) >> 1;
|
||||
|
||||
//perfetc matched, just return it
|
||||
if ( ip == HeaderSip[m] ) {
|
||||
if ( m > 0 ) {
|
||||
sptr = HeaderPtr[m-1];
|
||||
eptr = HeaderPtr[m ];
|
||||
} else {
|
||||
sptr = HeaderPtr[m ];
|
||||
eptr = HeaderPtr[m+1];
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
//less then the middle value
|
||||
if ( ip < HeaderSip[m] ) {
|
||||
if ( m == 0 ) {
|
||||
sptr = HeaderPtr[m ];
|
||||
eptr = HeaderPtr[m+1];
|
||||
break;
|
||||
} else if ( ip > HeaderSip[m-1] ) {
|
||||
sptr = HeaderPtr[m-1];
|
||||
eptr = HeaderPtr[m ];
|
||||
break;
|
||||
}
|
||||
h = m - 1;
|
||||
} else {
|
||||
if ( m == headerLength - 1 ) {
|
||||
sptr = HeaderPtr[m-1];
|
||||
eptr = HeaderPtr[m ];
|
||||
break;
|
||||
} else if ( ip <= HeaderSip[m+1] ) {
|
||||
sptr = HeaderPtr[m ];
|
||||
eptr = HeaderPtr[m+1];
|
||||
break;
|
||||
}
|
||||
l = m + 1;
|
||||
}
|
||||
}
|
||||
|
||||
//match nothing just stop it
|
||||
if ( sptr == 0 ) return null;
|
||||
|
||||
//2. search the index blocks to define the data
|
||||
int blockLen = eptr - sptr, blen = IndexBlock.getIndexBlockLength();
|
||||
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;
|
||||
long sip, eip, dataptr = 0;
|
||||
while ( l <= h ) {
|
||||
int m = (l + h) >> 1;
|
||||
int p = m * blen;
|
||||
sip = Util.getIntLong(iBuffer, p);
|
||||
if ( ip < sip ) {
|
||||
h = m - 1;
|
||||
} else {
|
||||
eip = Util.getIntLong(iBuffer, p + 4);
|
||||
if ( ip > eip ) {
|
||||
l = m + 1;
|
||||
} else {
|
||||
dataptr = Util.getIntLong(iBuffer, p + 8);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//not matched
|
||||
if ( dataptr == 0 ) return null;
|
||||
|
||||
//3. get the data
|
||||
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");
|
||||
|
||||
return new DataBlock(city_id, region);
|
||||
}
|
||||
|
||||
/**
|
||||
* get the region throught the ip address with b-tree search algorithm
|
||||
*
|
||||
* @param ip
|
||||
* @return DataBlock
|
||||
* @throws IOException
|
||||
*/
|
||||
public DataBlock btreeSearch( String ip ) throws IOException
|
||||
{
|
||||
return btreeSearch(Util.ip2long(ip));
|
||||
}
|
||||
|
||||
/**
|
||||
* get the region with a int ip address with binary search algorithm
|
||||
*
|
||||
* @param ip
|
||||
* @throws IOException
|
||||
*/
|
||||
public DataBlock binarySearch( long ip ) throws IOException
|
||||
{
|
||||
int blen = IndexBlock.getIndexBlockLength();
|
||||
if ( totalIndexBlocks == 0 )
|
||||
{
|
||||
raf.seek(0L);
|
||||
byte[] superBytes = new byte[8];
|
||||
raf.readFully(superBytes, 0, superBytes.length);
|
||||
//initialize the global vars
|
||||
firstIndexPtr = Util.getIntLong(superBytes, 0);
|
||||
lastIndexPtr = Util.getIntLong(superBytes, 4);
|
||||
totalIndexBlocks = (int)((lastIndexPtr - firstIndexPtr)/blen) + 1;
|
||||
}
|
||||
|
||||
//search the index blocks to define the data
|
||||
int l = 0, h = totalIndexBlocks;
|
||||
byte[] buffer = new byte[blen];
|
||||
long sip, eip, dataptr = 0;
|
||||
while ( l <= h ) {
|
||||
int m = (l + h) >> 1;
|
||||
raf.seek(firstIndexPtr + m * blen); //set the file pointer
|
||||
raf.readFully(buffer, 0, buffer.length);
|
||||
sip = Util.getIntLong(buffer, 0);
|
||||
if ( ip < sip ) {
|
||||
h = m - 1;
|
||||
} else {
|
||||
eip = Util.getIntLong(buffer, 4);
|
||||
if ( ip > eip ) {
|
||||
l = m + 1;
|
||||
} else {
|
||||
dataptr = Util.getIntLong(buffer, 8);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//not matched
|
||||
if ( dataptr == 0 ) return null;
|
||||
|
||||
//get the data
|
||||
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");
|
||||
|
||||
return new DataBlock(city_id, region);
|
||||
}
|
||||
|
||||
/**
|
||||
* get the region throught the ip address with binary search algorithm
|
||||
*
|
||||
* @param ip
|
||||
* @return DataBlock
|
||||
* @throws IOException
|
||||
*/
|
||||
public DataBlock binarySearch( String ip ) throws IOException
|
||||
{
|
||||
return binarySearch(Util.ip2long(ip));
|
||||
}
|
||||
|
||||
/**
|
||||
* get the db config
|
||||
*
|
||||
* @return DbConfig
|
||||
*/
|
||||
public DbConfig getDbConfig()
|
||||
{
|
||||
return dbConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* close the db
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
public void close() throws IOException
|
||||
{
|
||||
HeaderSip = null; //let gc do its work
|
||||
HeaderPtr = null;
|
||||
raf.close();
|
||||
}
|
||||
=======
|
||||
public static final int BTREE_ALGORITHM = 1;
|
||||
public static final int BIN_ALGORITHM = 2;
|
||||
|
||||
@@ -617,5 +313,4 @@ public class DbSearcher
|
||||
HeaderPtr = null;
|
||||
raf.close();
|
||||
}
|
||||
>>>>>>> 7e51a4909fdb01014f948c2dbc5cfb5fbed9ce56
|
||||
}
|
||||
|
||||
@@ -7,62 +7,6 @@ package org.lionsoul.ip2region;
|
||||
*/
|
||||
public class HeaderBlock
|
||||
{
|
||||
<<<<<<< HEAD
|
||||
/**
|
||||
* index block start ip address
|
||||
*/
|
||||
private long indexStartIp;
|
||||
|
||||
/**
|
||||
* ip address
|
||||
*/
|
||||
private int indexPtr;
|
||||
|
||||
public HeaderBlock( long indexStartIp, int indexPtr )
|
||||
{
|
||||
this.indexStartIp = indexStartIp;
|
||||
this.indexPtr = indexPtr;
|
||||
}
|
||||
|
||||
public long getIndexStartIp() {
|
||||
return indexStartIp;
|
||||
}
|
||||
|
||||
public HeaderBlock setIndexStartIp(long indexStartIp) {
|
||||
this.indexStartIp = indexStartIp;
|
||||
return this;
|
||||
}
|
||||
|
||||
public int getIndexPtr() {
|
||||
return indexPtr;
|
||||
}
|
||||
|
||||
public HeaderBlock setIndexPtr(int indexPtr) {
|
||||
this.indexPtr = indexPtr;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* get the bytes for db storage
|
||||
*
|
||||
* @return byte[]
|
||||
*/
|
||||
public byte[] getBytes()
|
||||
{
|
||||
/*
|
||||
* +------------+-----------+
|
||||
* | 4bytes | 4bytes |
|
||||
* +------------+-----------+
|
||||
* start ip index ptr
|
||||
*/
|
||||
byte[] b = new byte[8];
|
||||
|
||||
Util.writeIntLong(b, 0, indexStartIp);
|
||||
Util.writeIntLong(b, 4, indexPtr);
|
||||
|
||||
return b;
|
||||
}
|
||||
=======
|
||||
/**
|
||||
* index block start ip address
|
||||
*/
|
||||
@@ -117,5 +61,4 @@ public class HeaderBlock
|
||||
|
||||
return b;
|
||||
}
|
||||
>>>>>>> 7e51a4909fdb01014f948c2dbc5cfb5fbed9ce56
|
||||
}
|
||||
|
||||
@@ -7,102 +7,6 @@ package org.lionsoul.ip2region;
|
||||
*/
|
||||
public class IndexBlock
|
||||
{
|
||||
<<<<<<< HEAD
|
||||
private static int LENGTH = 12;
|
||||
|
||||
/**
|
||||
* start ip address
|
||||
*/
|
||||
private long startIp;
|
||||
|
||||
/**
|
||||
* end ip address
|
||||
*/
|
||||
private long endIp;
|
||||
|
||||
/**
|
||||
* data ptr and data length
|
||||
*/
|
||||
private int dataPtr;
|
||||
|
||||
/**
|
||||
* data length
|
||||
*/
|
||||
private int dataLen;
|
||||
|
||||
public IndexBlock(long startIp, long endIp, int dataPtr, int dataLen)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
public long getEndIp() {
|
||||
return endIp;
|
||||
}
|
||||
|
||||
public IndexBlock setEndIp(long endIp) {
|
||||
this.endIp = endIp;
|
||||
return this;
|
||||
}
|
||||
|
||||
public int getDataPtr() {
|
||||
return dataPtr;
|
||||
}
|
||||
|
||||
public IndexBlock setDataPtr(int dataPtr) {
|
||||
this.dataPtr = dataPtr;
|
||||
return this;
|
||||
}
|
||||
|
||||
public int getDataLen() {
|
||||
return dataLen;
|
||||
}
|
||||
|
||||
public IndexBlock setDataLen(int dataLen) {
|
||||
this.dataLen = dataLen;
|
||||
return this;
|
||||
}
|
||||
|
||||
public static int getIndexBlockLength() {
|
||||
return LENGTH;
|
||||
}
|
||||
|
||||
/**
|
||||
* get the bytes for storage
|
||||
*
|
||||
* @return byte[]
|
||||
*/
|
||||
public byte[] getBytes()
|
||||
{
|
||||
/*
|
||||
* +------------+-----------+-----------+
|
||||
* | 4bytes | 4bytes | 4bytes |
|
||||
* +------------+-----------+-----------+
|
||||
* start ip end ip data ptr + len
|
||||
*/
|
||||
byte[] b = new byte[12];
|
||||
|
||||
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);
|
||||
Util.writeIntLong(b, 8, mix);
|
||||
|
||||
return b;
|
||||
}
|
||||
=======
|
||||
private static int LENGTH = 12;
|
||||
|
||||
/**
|
||||
@@ -197,5 +101,4 @@ public class IndexBlock
|
||||
|
||||
return b;
|
||||
}
|
||||
>>>>>>> 7e51a4909fdb01014f948c2dbc5cfb5fbed9ce56
|
||||
}
|
||||
|
||||
@@ -7,143 +7,6 @@ package org.lionsoul.ip2region;
|
||||
*/
|
||||
public class Util
|
||||
{
|
||||
<<<<<<< HEAD
|
||||
/**
|
||||
* write specfield bytes to a byte array start from offset
|
||||
*
|
||||
* @param b
|
||||
* @param offset
|
||||
* @param v
|
||||
* @param bytes
|
||||
*/
|
||||
public static void write( byte[] b, int offset, long v, int bytes)
|
||||
{
|
||||
for ( int i = 0; i < bytes; i++ )
|
||||
{
|
||||
b[offset++] = (byte)((v >>> (8 * i)) & 0xFF);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* write a int to a byte array
|
||||
*
|
||||
* @param b
|
||||
* @param offet
|
||||
* @param v
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* get a int from a byte array start from the specifiled offset
|
||||
*
|
||||
* @param b
|
||||
* @param offset
|
||||
*/
|
||||
public static long getIntLong( byte[] b, int offset )
|
||||
{
|
||||
return (
|
||||
((b[offset++] & 0x000000FFL)) |
|
||||
((b[offset++] << 8) & 0x0000FF00L) |
|
||||
((b[offset++] << 16) & 0x00FF0000L) |
|
||||
((b[offset ] << 24) & 0xFF000000L)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* get a int from a byte array start from the specifield offset
|
||||
*
|
||||
* @param b
|
||||
* @param offset
|
||||
*/
|
||||
public static int getInt3( byte[] b, int offset )
|
||||
{
|
||||
return (
|
||||
(b[offset++] & 0x000000FF) |
|
||||
(b[offset++] & 0x0000FF00) |
|
||||
(b[offset ] & 0x00FF0000)
|
||||
);
|
||||
}
|
||||
|
||||
public static int getInt2( byte[] b, int offset )
|
||||
{
|
||||
return (
|
||||
(b[offset++] & 0x000000FF) |
|
||||
(b[offset ] & 0x0000FF00)
|
||||
);
|
||||
}
|
||||
|
||||
public static int getInt1( byte[] b, int offset )
|
||||
{
|
||||
return (
|
||||
(b[offset] & 0x000000FF)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* string ip to long ip
|
||||
*
|
||||
* @param ip
|
||||
* @return long
|
||||
*/
|
||||
public static long ip2long( String ip )
|
||||
{
|
||||
String[] p = ip.split("\\.");
|
||||
if ( p.length != 4 ) return 0;
|
||||
|
||||
int p1 = ((Integer.valueOf(p[0]) << 24) & 0xFF000000);
|
||||
int p2 = ((Integer.valueOf(p[1]) << 16) & 0x00FF0000);
|
||||
int p3 = ((Integer.valueOf(p[2]) << 8) & 0x0000FF00);
|
||||
int p4 = ((Integer.valueOf(p[3]) << 0) & 0x000000FF);
|
||||
|
||||
return ((p1 | p2 | p3 | p4) & 0xFFFFFFFFL);
|
||||
}
|
||||
|
||||
/**
|
||||
* int to ip string
|
||||
*
|
||||
* @param ip
|
||||
* @return string
|
||||
*/
|
||||
public static String long2ip( long ip )
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
sb
|
||||
.append((ip >> 24) & 0xFF).append('.')
|
||||
.append((ip >> 16) & 0xFF).append('.')
|
||||
.append((ip >> 8) & 0xFF).append('.')
|
||||
.append((ip >> 0) & 0xFF);
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* check the validate of the specifeld ip address
|
||||
*
|
||||
* @param ip
|
||||
* @return boolean
|
||||
*/
|
||||
public static boolean isIpAddress( String ip )
|
||||
{
|
||||
String[] p = ip.split("\\.");
|
||||
if ( p.length != 4 ) return false;
|
||||
|
||||
for ( String pp : p )
|
||||
{
|
||||
if ( pp.length() > 3 ) return false;
|
||||
int val = Integer.valueOf(pp);
|
||||
if ( val > 255 ) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
=======
|
||||
/**
|
||||
* write specfield bytes to a byte array start from offset
|
||||
*
|
||||
@@ -279,5 +142,4 @@ public class Util
|
||||
|
||||
return true;
|
||||
}
|
||||
>>>>>>> 7e51a4909fdb01014f948c2dbc5cfb5fbed9ce56
|
||||
}
|
||||
|
||||
@@ -18,69 +18,6 @@ import org.lionsoul.ip2region.Util;
|
||||
*/
|
||||
public class TestSearcher
|
||||
{
|
||||
<<<<<<< HEAD
|
||||
public static void main(String[] argv)
|
||||
{
|
||||
if ( argv.length == 0 ) {
|
||||
System.out.println("| Usage: java -jar ip2region-{version}.jar [ip2region db file]");
|
||||
return;
|
||||
}
|
||||
|
||||
int algorithm = DbSearcher.BTREE_ALGORITHM;
|
||||
File file = new File(argv[0]);
|
||||
if ( file.exists() == false ) {
|
||||
System.out.println("Error: Invalid ip2region.db file");
|
||||
return;
|
||||
}
|
||||
|
||||
if ( argv.length > 1 ) {
|
||||
if ( argv[1].equalsIgnoreCase("binary")) algorithm = DbSearcher.BIN_ALGORITHM;
|
||||
}
|
||||
|
||||
try {
|
||||
System.out.println("initializing "+((algorithm==2)?"Binary":"B-tree")+" ... ");
|
||||
DbConfig config = new DbConfig();
|
||||
DbSearcher seacher = new DbSearcher(config, argv[0]);
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
|
||||
|
||||
System.out.println("+----------------------------------+");
|
||||
System.out.println("| ip2region test shell |");
|
||||
System.out.println("| Author: chenxin619315@gmail.com |");
|
||||
System.out.println("| Type 'quit' to exit program |");
|
||||
System.out.println("+----------------------------------+");
|
||||
|
||||
double sTime = 0, cTime = 0;
|
||||
String line = null;
|
||||
DataBlock dataBlock = null;
|
||||
while ( true )
|
||||
{
|
||||
System.out.print("ip2region>> ");
|
||||
line = reader.readLine().trim();
|
||||
if ( line.length() < 2 ) continue;
|
||||
if ( line.equalsIgnoreCase("quit") ) break;
|
||||
if ( Util.isIpAddress(line) == false ) {
|
||||
System.out.println("Error: Invalid ip address");
|
||||
continue;
|
||||
}
|
||||
|
||||
sTime = System.nanoTime();
|
||||
dataBlock = algorithm==2 ? seacher.binarySearch(line) : seacher.btreeSearch(line);
|
||||
cTime = (System.nanoTime() - sTime) / 1000000;
|
||||
System.out.printf("%s in %.5f millseconds\n", dataBlock, cTime);
|
||||
}
|
||||
|
||||
reader.close();
|
||||
seacher.close();
|
||||
System.out.println("+--Bye");
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (DbMakerConfigException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
=======
|
||||
public static void main(String[] argv)
|
||||
{
|
||||
if ( argv.length == 0 ) {
|
||||
@@ -142,5 +79,4 @@ public class TestSearcher
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
>>>>>>> 7e51a4909fdb01014f948c2dbc5cfb5fbed9ce56
|
||||
}
|
||||
|
||||
@@ -19,81 +19,6 @@ import org.lionsoul.ip2region.DbSearcher;
|
||||
|
||||
public class TestUnit {
|
||||
|
||||
<<<<<<< HEAD
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
DbSearcher _searcher = new DbSearcher(new DbConfig(), "./data/ip2region.db");
|
||||
BufferedReader bfr = new BufferedReader(new FileReader("./data/ip.merge.txt"));
|
||||
BufferedWriter bwr = new BufferedWriter(new FileWriter("./data/error_log.txt", true));
|
||||
int errCount = 0;
|
||||
int lineCount = 0;
|
||||
String str = null;
|
||||
|
||||
while ( (str = bfr.readLine()) != null ) {
|
||||
StringBuffer line = new StringBuffer(str);
|
||||
//get first ip
|
||||
int first_idx = line.indexOf("|");
|
||||
String first_ip = line.substring(0, first_idx);
|
||||
|
||||
line = new StringBuffer( line.substring(first_idx + 1) );
|
||||
|
||||
//get second ip
|
||||
int second_idx = line.indexOf("|");
|
||||
String second_ip = line.substring(0, second_idx);
|
||||
|
||||
//get addr
|
||||
String source_region = line.substring(second_idx + 1);
|
||||
|
||||
//search from DbSearcher
|
||||
System.out.println("+---Start, start to search");
|
||||
System.out.println("+---[Info]: Source region = "+source_region);
|
||||
|
||||
System.out.println("+---[Info]: Step1, search for first IP: "+first_ip);
|
||||
DataBlock fdata = _searcher.binarySearch(first_ip);
|
||||
if ( ! fdata.getRegion().equalsIgnoreCase( source_region ) ) {
|
||||
System.out.println("[Error]: Search first IP failed, DB region = "+fdata.getRegion());
|
||||
bwr.write("[Source]: Region: "+fdata.getRegion());
|
||||
bwr.newLine();
|
||||
bwr.write("[Source]: First Ip: "+first_ip);
|
||||
bwr.newLine();
|
||||
bwr.write("[DB]: Region: "+fdata.getRegion());
|
||||
bwr.newLine();
|
||||
bwr.flush();
|
||||
errCount++;
|
||||
}
|
||||
|
||||
System.out.println("+---[Info]: Step2, search for second IP: "+second_ip);
|
||||
DataBlock sdata = _searcher.btreeSearch(second_ip);
|
||||
if ( ! sdata.getRegion().equalsIgnoreCase( source_region ) ) {
|
||||
System.out.println("[Error]: Search second IP failed, DB region = "+sdata.getRegion());
|
||||
bwr.write("[Source]: Region: "+sdata.getRegion());
|
||||
bwr.newLine();
|
||||
bwr.write("[Source]: First Ip: "+second_ip);
|
||||
bwr.newLine();
|
||||
bwr.write("[DB]: Region: "+sdata.getRegion());
|
||||
bwr.newLine();
|
||||
bwr.flush();
|
||||
errCount++;
|
||||
}
|
||||
|
||||
lineCount++;
|
||||
}
|
||||
|
||||
bwr.close();
|
||||
bfr.close();
|
||||
System.out.println("+---Done, search complished");
|
||||
System.out.println("+---Statistics, Error count = "+errCount
|
||||
+", Total line = "+lineCount
|
||||
+", Fail ratio = "+((float)(errCount/lineCount))*100+"%");
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (DbMakerConfigException e) {
|
||||
e.printStackTrace();
|
||||
} catch ( Exception e ) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
=======
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
DbSearcher _searcher = new DbSearcher(new DbConfig(), "./data/ip2region.db");
|
||||
@@ -167,6 +92,5 @@ public class TestUnit {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
>>>>>>> 7e51a4909fdb01014f948c2dbc5cfb5fbed9ce56
|
||||
|
||||
}
|
||||
|
||||
@@ -9,53 +9,6 @@ import org.lionsoul.ip2region.Util;
|
||||
*/
|
||||
public class TestUtil
|
||||
{
|
||||
<<<<<<< HEAD
|
||||
public static void main(String[] argv)
|
||||
{
|
||||
/* //1. test the ip2long
|
||||
String[] ipSet = new String[]{
|
||||
"120.24.78.68",
|
||||
"120.24.229.68",
|
||||
"120.24.87.145",
|
||||
"218.17.162.99"
|
||||
};
|
||||
|
||||
for ( String ip : ipSet )
|
||||
{
|
||||
int ipInt = Util.ip2Int(ip);
|
||||
System.out.println("src ip: " + ip + ", ip2Int: " + ipInt + ", int2IP: " + Util.int2IP(ipInt));
|
||||
}*/
|
||||
|
||||
/* int[] arr = new int[]{12344, -1234, 2146789, 0, -1024};
|
||||
byte[] b = new byte[arr.length * 4];
|
||||
|
||||
//write the int
|
||||
System.out.println("+--Testing writeInt ... ");
|
||||
int i, idx = 0;
|
||||
for ( i = 0; i < b.length; i += 4 )
|
||||
{
|
||||
System.out.println("offset: " + i);
|
||||
Util.writeInt(b, i, arr[idx++]);
|
||||
}
|
||||
System.out.println("|----[Ok]");
|
||||
|
||||
//read the int
|
||||
System.out.println("+--Testing getInt ... ");
|
||||
idx = 0;
|
||||
for ( i = 0; i < b.length; i += 4 )
|
||||
{
|
||||
System.out.println(arr[idx++]+", " + Util.getInt(b, i));
|
||||
}
|
||||
System.out.println("|----[Ok]");*/
|
||||
|
||||
/* HeaderBlock headerBlock = new HeaderBlock(241658345, 2134785);
|
||||
byte[] b = headerBlock.getBytes();
|
||||
System.out.println(headerBlock.getIndexStartIp() + ", " + headerBlock.getIndexPtr());
|
||||
System.out.println(Util.getInt(b, 0) + ", " + Util.getInt(b, 4));*/
|
||||
|
||||
System.out.println(Util.ip2long("255.255.255.0"));
|
||||
}
|
||||
=======
|
||||
public static void main(String[] argv)
|
||||
{
|
||||
/* //1. test the ip2long
|
||||
@@ -101,5 +54,4 @@ public class TestUtil
|
||||
|
||||
System.out.println(Util.ip2long("255.255.255.0"));
|
||||
}
|
||||
>>>>>>> 7e51a4909fdb01014f948c2dbc5cfb5fbed9ce56
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user