mirror of
https://github.com/lionsoul2014/ip2region.git
synced 2025-12-08 19:25:22 +00:00
conflicts fixed
This commit is contained in:
parent
9b0b7e8ebd
commit
83d7586c84
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,6 +1,7 @@
|
|||||||
*.class
|
*.class
|
||||||
*.out
|
*.out
|
||||||
*.o
|
*.o
|
||||||
|
*.pyc
|
||||||
META-INF/
|
META-INF/
|
||||||
# Binary Files #
|
# Binary Files #
|
||||||
*.jar
|
*.jar
|
||||||
|
|||||||
@ -1,15 +1,9 @@
|
|||||||
/**
|
/**
|
||||||
* default ip2region implementation
|
* default ip2region implementation
|
||||||
*
|
*
|
||||||
<<<<<<< HEAD
|
|
||||||
* @see #ip2region.h
|
* @see #ip2region.h
|
||||||
* @author chenxin<chenxin619315@gmail.com>
|
* @author chenxin<chenxin619315@gmail.com>
|
||||||
* @date 2015-10-30
|
* @date 2015-10-30
|
||||||
=======
|
|
||||||
* @see #ip2region.h
|
|
||||||
* @author chenxin<chenxin619315@gmail.com>
|
|
||||||
* @date 2015-10-30
|
|
||||||
>>>>>>> 7e51a4909fdb01014f948c2dbc5cfb5fbed9ce56
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "ip2region.h"
|
#include "ip2region.h"
|
||||||
@ -19,7 +13,6 @@
|
|||||||
/**
|
/**
|
||||||
* create a new ip2region object
|
* create a new ip2region object
|
||||||
*
|
*
|
||||||
<<<<<<< HEAD
|
|
||||||
* @param dbFile path
|
* @param dbFile path
|
||||||
*/
|
*/
|
||||||
IP2R_API uint_t ip2region_create(ip2region_t ip2rObj, char *dbFile)
|
IP2R_API uint_t ip2region_create(ip2region_t ip2rObj, char *dbFile)
|
||||||
@ -53,47 +46,11 @@ IP2R_API uint_t ip2region_create(ip2region_t ip2rObj, char *dbFile)
|
|||||||
ip2rObj->totalBlocks = 0;
|
ip2rObj->totalBlocks = 0;
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
=======
|
|
||||||
* @param dbFile path
|
|
||||||
*/
|
|
||||||
IP2R_API uint_t ip2region_create(ip2region_t ip2rObj, char *dbFile)
|
|
||||||
{
|
|
||||||
memset(ip2rObj, 0x00, sizeof(ip2region_entry));
|
|
||||||
ip2rObj->headerLen = 0;
|
|
||||||
ip2rObj->HeaderSip = (uint_t *) IP2R_MALLOC(TOTAL_HEADER_LENGTH);
|
|
||||||
if ( ip2rObj->HeaderSip == NULL )
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
ip2rObj->HeaderPtr = (uint_t *) IP2R_MALLOC(TOTAL_HEADER_LENGTH);
|
|
||||||
if ( ip2rObj->HeaderPtr == NULL )
|
|
||||||
{
|
|
||||||
IP2R_FREE(ip2rObj->HeaderSip);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
//open the db file
|
|
||||||
ip2rObj->dbHandler = fopen(dbFile, "rb");
|
|
||||||
if ( ip2rObj->dbHandler == NULL )
|
|
||||||
{
|
|
||||||
IP2R_FREE(ip2rObj->HeaderSip);
|
|
||||||
IP2R_FREE(ip2rObj->HeaderPtr);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
ip2rObj->firstIndexPtr = 0;
|
|
||||||
ip2rObj->lastIndexPtr = 0;
|
|
||||||
ip2rObj->totalBlocks = 0;
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
>>>>>>> 7e51a4909fdb01014f948c2dbc5cfb5fbed9ce56
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* destroy the specifield ip2region object
|
* destroy the specifield ip2region object
|
||||||
*
|
*
|
||||||
<<<<<<< HEAD
|
|
||||||
* @param ip2region_t
|
* @param ip2region_t
|
||||||
*/
|
*/
|
||||||
IP2R_API uint_t ip2region_destroy(ip2region_t ip2rObj)
|
IP2R_API uint_t ip2region_destroy(ip2region_t ip2rObj)
|
||||||
@ -111,31 +68,11 @@ IP2R_API uint_t ip2region_destroy(ip2region_t ip2rObj)
|
|||||||
}
|
}
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
=======
|
|
||||||
* @param ip2region_t
|
|
||||||
*/
|
|
||||||
IP2R_API uint_t ip2region_destroy(ip2region_t ip2rObj)
|
|
||||||
{
|
|
||||||
IP2R_FREE(ip2rObj->HeaderSip);
|
|
||||||
ip2rObj->HeaderSip = NULL;
|
|
||||||
IP2R_FREE(ip2rObj->HeaderPtr);
|
|
||||||
ip2rObj->HeaderPtr = NULL;
|
|
||||||
|
|
||||||
//close the db file resource
|
|
||||||
if ( ip2rObj->dbHandler != NULL )
|
|
||||||
{
|
|
||||||
fclose(ip2rObj->dbHandler);
|
|
||||||
ip2rObj->dbHandler = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
>>>>>>> 7e51a4909fdb01014f948c2dbc5cfb5fbed9ce56
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get the region associated with the specifield ip address with binary search algorithm
|
* get the region associated with the specifield ip address with binary search algorithm
|
||||||
*
|
*
|
||||||
<<<<<<< HEAD
|
|
||||||
* @param ip2rObj
|
* @param ip2rObj
|
||||||
* @param ip
|
* @param ip
|
||||||
* @param datablock
|
* @param datablock
|
||||||
@ -203,90 +140,16 @@ IP2R_API uint_t ip2region_binary_search(ip2region_t ip2rObj, uint_t ip, databloc
|
|||||||
datablock->region[dataLen] = '\0';
|
datablock->region[dataLen] = '\0';
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
=======
|
|
||||||
* @param ip2rObj
|
|
||||||
* @param ip
|
|
||||||
* @param datablock
|
|
||||||
* @return uint_t
|
|
||||||
*/
|
|
||||||
IP2R_API uint_t ip2region_binary_search(ip2region_t ip2rObj, uint_t ip, datablock_t datablock)
|
|
||||||
{
|
|
||||||
int l, h, m, p;
|
|
||||||
uint_t sip, eip, dptr;
|
|
||||||
char buffer[256];
|
|
||||||
int dataLen, dataptr;
|
|
||||||
|
|
||||||
if ( ip2rObj->totalBlocks == 0 )
|
|
||||||
{
|
|
||||||
fseek(ip2rObj->dbHandler, 0, 0);
|
|
||||||
if ( fread(buffer, 8, 1, ip2rObj->dbHandler) != 1 ) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
ip2rObj->firstIndexPtr = getUnsignedInt(buffer, 0);
|
|
||||||
ip2rObj->lastIndexPtr = getUnsignedInt(buffer, 4);
|
|
||||||
ip2rObj->totalBlocks = (ip2rObj->lastIndexPtr-ip2rObj->firstIndexPtr)/INDEX_BLOCK_LENGTH + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
//binary search the index blocks to define the data block
|
|
||||||
l = 0; h = ip2rObj->totalBlocks; dptr = 0;
|
|
||||||
while ( l <= h ) {
|
|
||||||
m = (l + h) >> 1;
|
|
||||||
p = ip2rObj->firstIndexPtr + m * INDEX_BLOCK_LENGTH;
|
|
||||||
|
|
||||||
fseek(ip2rObj->dbHandler, p, 0);
|
|
||||||
if ( fread(buffer, INDEX_BLOCK_LENGTH, 1, ip2rObj->dbHandler) != 1 ) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
sip = getUnsignedInt(buffer, 0);
|
|
||||||
if ( ip < sip ) {
|
|
||||||
h = m - 1;
|
|
||||||
} else {
|
|
||||||
eip = getUnsignedInt(buffer, 4);
|
|
||||||
if ( ip > eip ) {
|
|
||||||
l = m + 1;
|
|
||||||
} else {
|
|
||||||
dptr = getUnsignedInt(buffer, 8);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( dptr == 0 ) return 0;
|
|
||||||
|
|
||||||
//get the data
|
|
||||||
dataLen = ((dptr >> 24) & 0xFF);
|
|
||||||
dataptr = (dptr & 0x00FFFFFF);
|
|
||||||
|
|
||||||
//memset(data, 0x00, sizeof(data));
|
|
||||||
fseek(ip2rObj->dbHandler, dataptr, 0);
|
|
||||||
if ( fread(buffer, dataLen, 1, ip2rObj->dbHandler) != 1 ) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
//fill the data to the datablock
|
|
||||||
datablock->city_id = getUnsignedInt(buffer, 0);
|
|
||||||
dataLen -= 4; //reduce the length of the city_id
|
|
||||||
memcpy(datablock->region, buffer + 4, dataLen);
|
|
||||||
datablock->region[dataLen] = '\0';
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
>>>>>>> 7e51a4909fdb01014f948c2dbc5cfb5fbed9ce56
|
|
||||||
}
|
}
|
||||||
|
|
||||||
IP2R_API uint_t ip2region_binary_search_string(ip2region_t ip2rObj, char *ip, datablock_t datablock)
|
IP2R_API uint_t ip2region_binary_search_string(ip2region_t ip2rObj, char *ip, datablock_t datablock)
|
||||||
{
|
{
|
||||||
<<<<<<< HEAD
|
|
||||||
return ip2region_binary_search(ip2rObj, ip2long(ip), datablock);
|
return ip2region_binary_search(ip2rObj, ip2long(ip), datablock);
|
||||||
=======
|
|
||||||
return ip2region_binary_search(ip2rObj, ip2long(ip), datablock);
|
|
||||||
>>>>>>> 7e51a4909fdb01014f948c2dbc5cfb5fbed9ce56
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get the region associated with the specifield ip address with b-tree algorithm
|
* get the region associated with the specifield ip address with b-tree algorithm
|
||||||
*
|
*
|
||||||
<<<<<<< HEAD
|
|
||||||
* @param ip2rObj
|
* @param ip2rObj
|
||||||
* @param ip
|
* @param ip
|
||||||
* @param datablock
|
* @param datablock
|
||||||
@ -408,144 +271,16 @@ IP2R_API uint_t ip2region_btree_search(ip2region_t ip2rObj, uint_t ip, datablock
|
|||||||
datablock->region[dataLen] = '\0';
|
datablock->region[dataLen] = '\0';
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
=======
|
|
||||||
* @param ip2rObj
|
|
||||||
* @param ip
|
|
||||||
* @param datablock
|
|
||||||
* @return uint_t
|
|
||||||
*/
|
|
||||||
IP2R_API uint_t ip2region_btree_search(ip2region_t ip2rObj, uint_t ip, datablock_t datablock)
|
|
||||||
{
|
|
||||||
int i, idx;
|
|
||||||
int l, m, h, p, sptr, eptr, indexBlockLen, dataLen, dataptr;
|
|
||||||
uint_t sip, eip, idxptr, dptr;
|
|
||||||
char buffer[TOTAL_HEADER_LENGTH];
|
|
||||||
|
|
||||||
if ( ip2rObj->headerLen == 0 )
|
|
||||||
{
|
|
||||||
idx = 0;
|
|
||||||
fseek(ip2rObj->dbHandler, 8, 0); //pass the super block
|
|
||||||
if ( fread(buffer, TOTAL_HEADER_LENGTH, 1, ip2rObj->dbHandler) != 1 ) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
for ( i = 0; i < TOTAL_HEADER_LENGTH; i += 8 )
|
|
||||||
{
|
|
||||||
sip = getUnsignedInt(buffer, i);
|
|
||||||
idxptr = getUnsignedInt(buffer, i + 4);
|
|
||||||
if ( idxptr == 0 ) break;
|
|
||||||
|
|
||||||
ip2rObj->HeaderSip[idx] = sip;
|
|
||||||
ip2rObj->HeaderPtr[idx] = idxptr;
|
|
||||||
idx++;
|
|
||||||
}
|
|
||||||
|
|
||||||
ip2rObj->headerLen = idx;
|
|
||||||
}
|
|
||||||
|
|
||||||
//search the header block to define the index block
|
|
||||||
l = 0; h = ip2rObj->headerLen; sptr = 0; eptr = 0;
|
|
||||||
while ( l <= h ) {
|
|
||||||
m = ((l + h) >> 1);
|
|
||||||
|
|
||||||
//perfetc matched, just return it
|
|
||||||
if ( ip == ip2rObj->HeaderSip[m] ) {
|
|
||||||
if ( m > 0 ) {
|
|
||||||
sptr = ip2rObj->HeaderPtr[m-1];
|
|
||||||
eptr = ip2rObj->HeaderPtr[m ];
|
|
||||||
} else {
|
|
||||||
sptr = ip2rObj->HeaderPtr[m ];
|
|
||||||
eptr = ip2rObj->HeaderPtr[m+1];
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
//less then the middle value
|
|
||||||
if ( ip < ip2rObj->HeaderSip[m] ) {
|
|
||||||
if ( m == 0 ) {
|
|
||||||
sptr = ip2rObj->HeaderPtr[m ];
|
|
||||||
eptr = ip2rObj->HeaderPtr[m+1];
|
|
||||||
break;
|
|
||||||
} else if ( ip > ip2rObj->HeaderSip[m-1] ) {
|
|
||||||
sptr = ip2rObj->HeaderPtr[m-1];
|
|
||||||
eptr = ip2rObj->HeaderPtr[m ];
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
h = m - 1;
|
|
||||||
} else {
|
|
||||||
if ( m == ip2rObj->headerLen - 1 ) {
|
|
||||||
sptr = ip2rObj->HeaderPtr[m-1];
|
|
||||||
eptr = ip2rObj->HeaderPtr[m ];
|
|
||||||
break;
|
|
||||||
} else if ( ip <= ip2rObj->HeaderSip[m+1] ) {
|
|
||||||
sptr = ip2rObj->HeaderPtr[m ];
|
|
||||||
eptr = ip2rObj->HeaderPtr[m+1];
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
l = m + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//not matched just stop it
|
|
||||||
if ( sptr == 0 ) return 0;
|
|
||||||
|
|
||||||
indexBlockLen = eptr - sptr;
|
|
||||||
fseek(ip2rObj->dbHandler, sptr, 0);
|
|
||||||
if ( fread(buffer, indexBlockLen + INDEX_BLOCK_LENGTH, 1, ip2rObj->dbHandler) != 1 ) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
dptr = 0; l = 0; h = indexBlockLen / INDEX_BLOCK_LENGTH;
|
|
||||||
while ( l <= h ) {
|
|
||||||
m = ((l + h) >> 1);
|
|
||||||
p = m * INDEX_BLOCK_LENGTH;
|
|
||||||
sip = getUnsignedInt(buffer, p);
|
|
||||||
if ( ip < sip ) {
|
|
||||||
h = m - 1;
|
|
||||||
} else {
|
|
||||||
eip = getUnsignedInt(buffer, p + 4);
|
|
||||||
if ( ip > eip ) {
|
|
||||||
l = m + 1;
|
|
||||||
} else {
|
|
||||||
dptr = getUnsignedInt(buffer, p + 8);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( dptr == 0 ) return 0;
|
|
||||||
|
|
||||||
dataLen = ((dptr >> 24) & 0xFF);
|
|
||||||
dataptr = (dptr & 0x00FFFFFF);
|
|
||||||
|
|
||||||
fseek(ip2rObj->dbHandler, dataptr, 0);
|
|
||||||
if ( fread(buffer, dataLen, 1, ip2rObj->dbHandler) != 1 ) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
datablock->city_id = getUnsignedInt(buffer, 0);
|
|
||||||
dataLen -= 4;
|
|
||||||
memcpy(datablock->region, buffer + 4, dataLen);
|
|
||||||
datablock->region[dataLen] = '\0';
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
>>>>>>> 7e51a4909fdb01014f948c2dbc5cfb5fbed9ce56
|
|
||||||
}
|
}
|
||||||
|
|
||||||
IP2R_API uint_t ip2region_btree_search_string(ip2region_t ip2rObj, char *ip, datablock_t datablock)
|
IP2R_API uint_t ip2region_btree_search_string(ip2region_t ip2rObj, char *ip, datablock_t datablock)
|
||||||
{
|
{
|
||||||
<<<<<<< HEAD
|
|
||||||
return ip2region_btree_search(ip2rObj, ip2long(ip), datablock);
|
return ip2region_btree_search(ip2rObj, ip2long(ip), datablock);
|
||||||
=======
|
|
||||||
return ip2region_btree_search(ip2rObj, ip2long(ip), datablock);
|
|
||||||
>>>>>>> 7e51a4909fdb01014f948c2dbc5cfb5fbed9ce56
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get a unsinged long(4bytes) from a specifield buffer start from the specifield offset
|
* get a unsinged long(4bytes) from a specifield buffer start from the specifield offset
|
||||||
*
|
*
|
||||||
<<<<<<< HEAD
|
|
||||||
* @param buffer
|
* @param buffer
|
||||||
* @param offset
|
* @param offset
|
||||||
* @return uint_t
|
* @return uint_t
|
||||||
@ -558,26 +293,11 @@ IP2R_API uint_t getUnsignedInt(char *buffer, int offset)
|
|||||||
((buffer[offset+2] << 16) & 0x00FF0000) |
|
((buffer[offset+2] << 16) & 0x00FF0000) |
|
||||||
((buffer[offset+3] << 24) & 0xFF000000)
|
((buffer[offset+3] << 24) & 0xFF000000)
|
||||||
);
|
);
|
||||||
=======
|
|
||||||
* @param buffer
|
|
||||||
* @param offset
|
|
||||||
* @return uint_t
|
|
||||||
*/
|
|
||||||
IP2R_API uint_t getUnsignedInt(char *buffer, int offset)
|
|
||||||
{
|
|
||||||
return (
|
|
||||||
((buffer[offset ]) & 0x000000FF) |
|
|
||||||
((buffer[offset+1] << 8) & 0x0000FF00) |
|
|
||||||
((buffer[offset+2] << 16) & 0x00FF0000) |
|
|
||||||
((buffer[offset+3] << 24) & 0xFF000000)
|
|
||||||
);
|
|
||||||
>>>>>>> 7e51a4909fdb01014f948c2dbc5cfb5fbed9ce56
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* string ip to long
|
* string ip to long
|
||||||
*
|
*
|
||||||
<<<<<<< HEAD
|
|
||||||
* @param ip
|
* @param ip
|
||||||
* @return uint_t
|
* @return uint_t
|
||||||
*/
|
*/
|
||||||
@ -614,50 +334,11 @@ IP2R_API uint_t ip2long(char *ip)
|
|||||||
ipval |= atoi(buffer);
|
ipval |= atoi(buffer);
|
||||||
|
|
||||||
return ipval;
|
return ipval;
|
||||||
=======
|
|
||||||
* @param ip
|
|
||||||
* @return uint_t
|
|
||||||
*/
|
|
||||||
IP2R_API uint_t ip2long(char *ip)
|
|
||||||
{
|
|
||||||
int i = 0, p = 24;
|
|
||||||
char buffer[4], *cs = ip;
|
|
||||||
uint_t ipval = 0;
|
|
||||||
|
|
||||||
while ( *cs != '\0' )
|
|
||||||
{
|
|
||||||
if ( *cs == '.' ) {
|
|
||||||
//single part length limit
|
|
||||||
if ( i > 3 ) {
|
|
||||||
ipval = 0;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( p < 0 ) break;
|
|
||||||
buffer[i] = '\0';
|
|
||||||
ipval |= (atoi(buffer) << p);
|
|
||||||
p -= 8;
|
|
||||||
i = 0;
|
|
||||||
} else {
|
|
||||||
buffer[i++] = *cs;
|
|
||||||
}
|
|
||||||
|
|
||||||
cs++;
|
|
||||||
}
|
|
||||||
|
|
||||||
//append the rest parts
|
|
||||||
if ( i > 3 ) return 0;
|
|
||||||
buffer[i] = '\0';
|
|
||||||
ipval |= atoi(buffer);
|
|
||||||
|
|
||||||
return ipval;
|
|
||||||
>>>>>>> 7e51a4909fdb01014f948c2dbc5cfb5fbed9ce56
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* long to string ip
|
* long to string ip
|
||||||
*
|
*
|
||||||
<<<<<<< HEAD
|
|
||||||
* @param ip
|
* @param ip
|
||||||
* @param buffer
|
* @param buffer
|
||||||
* @return uint_t(1 for success and 0 for failed)
|
* @return uint_t(1 for success and 0 for failed)
|
||||||
@ -665,13 +346,4 @@ IP2R_API uint_t ip2long(char *ip)
|
|||||||
IP2R_API uint_t long2ip(uint_t ip, char *buffer)
|
IP2R_API uint_t long2ip(uint_t ip, char *buffer)
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
=======
|
|
||||||
* @param ip
|
|
||||||
* @param buffer
|
|
||||||
* @return uint_t(1 for success and 0 for failed)
|
|
||||||
*/
|
|
||||||
IP2R_API uint_t long2ip(uint_t ip, char *buffer)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
>>>>>>> 7e51a4909fdb01014f948c2dbc5cfb5fbed9ce56
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,13 +1,8 @@
|
|||||||
/**
|
/**
|
||||||
* test ip2region searcher program
|
* test ip2region searcher program
|
||||||
*
|
*
|
||||||
<<<<<<< HEAD
|
|
||||||
* @author chenxin<chenxin619315@gmail.com>
|
* @author chenxin<chenxin619315@gmail.com>
|
||||||
* @date 2015-10-30
|
* @date 2015-10-30
|
||||||
=======
|
|
||||||
* @author chenxin<chenxin619315@gmail.com>
|
|
||||||
* @date 2015-10-30
|
|
||||||
>>>>>>> 7e51a4909fdb01014f948c2dbc5cfb5fbed9ce56
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "ip2region.h"
|
#include "ip2region.h"
|
||||||
@ -17,23 +12,15 @@
|
|||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
|
|
||||||
#define __PRINT_ABOUT__ \
|
#define __PRINT_ABOUT__ \
|
||||||
<<<<<<< HEAD
|
|
||||||
println("+-------------------------------------+"); \
|
println("+-------------------------------------+"); \
|
||||||
println("| ip2region test program |"); \
|
println("| ip2region test program |"); \
|
||||||
println("| Author: chenxin619315@gmail.com. |"); \
|
println("| Author: chenxin619315@gmail.com. |"); \
|
||||||
println("| Type 'quit' to exit the program. |"); \
|
println("| Type 'quit' to exit the program. |"); \
|
||||||
=======
|
|
||||||
println("+-------------------------------------+"); \
|
|
||||||
println("| ip2region test program |"); \
|
|
||||||
println("| Author: chenxin619315@gmail.com. |"); \
|
|
||||||
println("| Type 'quit' to exit the program. |"); \
|
|
||||||
>>>>>>> 7e51a4909fdb01014f948c2dbc5cfb5fbed9ce56
|
|
||||||
println("+-------------------------------------+");
|
println("+-------------------------------------+");
|
||||||
|
|
||||||
//read a line from a command line.
|
//read a line from a command line.
|
||||||
static char *getLine( FILE *fp, char *__dst )
|
static char *getLine( FILE *fp, char *__dst )
|
||||||
{
|
{
|
||||||
<<<<<<< HEAD
|
|
||||||
register int c;
|
register int c;
|
||||||
register char *cs;
|
register char *cs;
|
||||||
|
|
||||||
@ -45,41 +32,19 @@ static char *getLine( FILE *fp, char *__dst )
|
|||||||
*cs = '\0';
|
*cs = '\0';
|
||||||
|
|
||||||
return ( c == EOF && cs == __dst ) ? NULL : __dst;
|
return ( c == EOF && cs == __dst ) ? NULL : __dst;
|
||||||
=======
|
|
||||||
register int c;
|
|
||||||
register char *cs;
|
|
||||||
|
|
||||||
cs = __dst;
|
|
||||||
while ( ( c = getc( fp ) ) != EOF ) {
|
|
||||||
if ( c == '\n' ) break;
|
|
||||||
*cs++ = c;
|
|
||||||
}
|
|
||||||
*cs = '\0';
|
|
||||||
|
|
||||||
return ( c == EOF && cs == __dst ) ? NULL : __dst;
|
|
||||||
>>>>>>> 7e51a4909fdb01014f948c2dbc5cfb5fbed9ce56
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static double getTime()
|
static double getTime()
|
||||||
{
|
{
|
||||||
<<<<<<< HEAD
|
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
struct timezone tz;
|
struct timezone tz;
|
||||||
gettimeofday(&tv, &tz);
|
gettimeofday(&tv, &tz);
|
||||||
|
|
||||||
return (tv.tv_sec * 1000 + ((double)tv.tv_usec)/1000);
|
return (tv.tv_sec * 1000 + ((double)tv.tv_usec)/1000);
|
||||||
=======
|
|
||||||
struct timeval tv;
|
|
||||||
struct timezone tz;
|
|
||||||
gettimeofday(&tv, &tz);
|
|
||||||
|
|
||||||
return (tv.tv_sec * 1000 + ((double)tv.tv_usec)/1000);
|
|
||||||
>>>>>>> 7e51a4909fdb01014f948c2dbc5cfb5fbed9ce56
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main( int argc, char **argv )
|
int main( int argc, char **argv )
|
||||||
{
|
{
|
||||||
<<<<<<< HEAD
|
|
||||||
ip2region_entry ip2rEntry;
|
ip2region_entry ip2rEntry;
|
||||||
datablock_entry datablock;
|
datablock_entry datablock;
|
||||||
char *dbFile = NULL, *algorithm = NULL;
|
char *dbFile = NULL, *algorithm = NULL;
|
||||||
@ -131,57 +96,4 @@ int main( int argc, char **argv )
|
|||||||
ip2region_destroy(&ip2rEntry);
|
ip2region_destroy(&ip2rEntry);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
=======
|
|
||||||
ip2region_entry ip2rEntry;
|
|
||||||
datablock_entry datablock;
|
|
||||||
char *dbFile = NULL, *algorithm = NULL;
|
|
||||||
char line[256];
|
|
||||||
uint_t (*func_ptr)(ip2region_t, char *, datablock_t);
|
|
||||||
double s_time, c_time;
|
|
||||||
memset(&datablock, 0x00, sizeof(datablock_entry));
|
|
||||||
|
|
||||||
if ( argc < 2 ) {
|
|
||||||
printf("Usage: a.out [ip2region db file path] [algorithm]");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
dbFile = argv[1];
|
|
||||||
algorithm = "B-tree";
|
|
||||||
func_ptr = ip2region_btree_search_string;
|
|
||||||
if ( argc >= 3 && strcmp(argv[2], "binary") == 0 ) {
|
|
||||||
algorithm = "Binary";
|
|
||||||
func_ptr = ip2region_binary_search_string;
|
|
||||||
}
|
|
||||||
|
|
||||||
//create a new ip2rObj
|
|
||||||
printf("+--initializing %s ... \n", algorithm);
|
|
||||||
if ( ip2region_create(&ip2rEntry, dbFile) == 0 )
|
|
||||||
{
|
|
||||||
println("Error: Fail to create the ip2region object");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
__PRINT_ABOUT__;
|
|
||||||
|
|
||||||
while ( 1 )
|
|
||||||
{
|
|
||||||
print("ip2region>> ");
|
|
||||||
getLine( stdin, line );
|
|
||||||
if ( strlen(line) < 2 ) continue;
|
|
||||||
if ( strcasecmp( line, "quit" ) == 0 ) {
|
|
||||||
println("+--Bye!");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
s_time = getTime();
|
|
||||||
func_ptr(&ip2rEntry, line, &datablock);
|
|
||||||
c_time = getTime() - s_time;
|
|
||||||
printf("%d|%s in %.5f millseconds\n", datablock.city_id, datablock.region, c_time);
|
|
||||||
}
|
|
||||||
|
|
||||||
//destory the ip2rObj
|
|
||||||
ip2region_destroy(&ip2rEntry);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
>>>>>>> 7e51a4909fdb01014f948c2dbc5cfb5fbed9ce56
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,7 +3,6 @@ package org.lionsoul.ip2region;
|
|||||||
/**
|
/**
|
||||||
* data block class
|
* data block class
|
||||||
*
|
*
|
||||||
<<<<<<< HEAD
|
|
||||||
* @author chenxin<chenxin619315@gmail.com>
|
* @author chenxin<chenxin619315@gmail.com>
|
||||||
*/
|
*/
|
||||||
public class DataBlock
|
public class DataBlock
|
||||||
@ -50,52 +49,4 @@ public class DataBlock
|
|||||||
sb.append(city_id).append('|').append(region);
|
sb.append(city_id).append('|').append(region);
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
=======
|
|
||||||
* @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();
|
|
||||||
}
|
|
||||||
>>>>>>> 7e51a4909fdb01014f948c2dbc5cfb5fbed9ce56
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,7 +7,6 @@ package org.lionsoul.ip2region;
|
|||||||
*/
|
*/
|
||||||
public class DbConfig
|
public class DbConfig
|
||||||
{
|
{
|
||||||
<<<<<<< HEAD
|
|
||||||
/**
|
/**
|
||||||
* total header data block size
|
* total header data block size
|
||||||
*/
|
*/
|
||||||
@ -57,55 +56,4 @@ public class DbConfig
|
|||||||
this.indexBlockSize = dataBlockSize;
|
this.indexBlockSize = dataBlockSize;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
=======
|
|
||||||
/**
|
|
||||||
* 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;
|
|
||||||
}
|
|
||||||
>>>>>>> 7e51a4909fdb01014f948c2dbc5cfb5fbed9ce56
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,7 +7,6 @@ package org.lionsoul.ip2region;
|
|||||||
*/
|
*/
|
||||||
public class DbMakerConfigException extends Exception
|
public class DbMakerConfigException extends Exception
|
||||||
{
|
{
|
||||||
<<<<<<< HEAD
|
|
||||||
private static final long serialVersionUID = 4495714680349884838L;
|
private static final long serialVersionUID = 4495714680349884838L;
|
||||||
|
|
||||||
public DbMakerConfigException( String info ) {
|
public DbMakerConfigException( String info ) {
|
||||||
@ -21,19 +20,4 @@ public class DbMakerConfigException extends Exception
|
|||||||
public DbMakerConfigException( String info, Throwable res ) {
|
public DbMakerConfigException( String info, Throwable res ) {
|
||||||
super(info, res);
|
super(info, res);
|
||||||
}
|
}
|
||||||
=======
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
>>>>>>> 7e51a4909fdb01014f948c2dbc5cfb5fbed9ce56
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -11,7 +11,6 @@ import java.io.RandomAccessFile;
|
|||||||
*/
|
*/
|
||||||
public class DbSearcher
|
public class DbSearcher
|
||||||
{
|
{
|
||||||
<<<<<<< HEAD
|
|
||||||
public static final int BTREE_ALGORITHM = 1;
|
public static final int BTREE_ALGORITHM = 1;
|
||||||
public static final int BIN_ALGORITHM = 2;
|
public static final int BIN_ALGORITHM = 2;
|
||||||
|
|
||||||
@ -314,308 +313,4 @@ public class DbSearcher
|
|||||||
HeaderPtr = null;
|
HeaderPtr = null;
|
||||||
raf.close();
|
raf.close();
|
||||||
}
|
}
|
||||||
=======
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
>>>>>>> 7e51a4909fdb01014f948c2dbc5cfb5fbed9ce56
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,7 +7,6 @@ package org.lionsoul.ip2region;
|
|||||||
*/
|
*/
|
||||||
public class HeaderBlock
|
public class HeaderBlock
|
||||||
{
|
{
|
||||||
<<<<<<< HEAD
|
|
||||||
/**
|
/**
|
||||||
* index block start ip address
|
* index block start ip address
|
||||||
*/
|
*/
|
||||||
@ -62,60 +61,4 @@ public class HeaderBlock
|
|||||||
|
|
||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
=======
|
|
||||||
/**
|
|
||||||
* 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;
|
|
||||||
}
|
|
||||||
>>>>>>> 7e51a4909fdb01014f948c2dbc5cfb5fbed9ce56
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,7 +7,6 @@ package org.lionsoul.ip2region;
|
|||||||
*/
|
*/
|
||||||
public class IndexBlock
|
public class IndexBlock
|
||||||
{
|
{
|
||||||
<<<<<<< HEAD
|
|
||||||
private static int LENGTH = 12;
|
private static int LENGTH = 12;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -102,100 +101,4 @@ public class IndexBlock
|
|||||||
|
|
||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
=======
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
>>>>>>> 7e51a4909fdb01014f948c2dbc5cfb5fbed9ce56
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,7 +7,6 @@ package org.lionsoul.ip2region;
|
|||||||
*/
|
*/
|
||||||
public class Util
|
public class Util
|
||||||
{
|
{
|
||||||
<<<<<<< HEAD
|
|
||||||
/**
|
/**
|
||||||
* write specfield bytes to a byte array start from offset
|
* write specfield bytes to a byte array start from offset
|
||||||
*
|
*
|
||||||
@ -143,141 +142,4 @@ public class Util
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
=======
|
|
||||||
/**
|
|
||||||
* 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;
|
|
||||||
}
|
|
||||||
>>>>>>> 7e51a4909fdb01014f948c2dbc5cfb5fbed9ce56
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,7 +18,6 @@ import org.lionsoul.ip2region.Util;
|
|||||||
*/
|
*/
|
||||||
public class TestSearcher
|
public class TestSearcher
|
||||||
{
|
{
|
||||||
<<<<<<< HEAD
|
|
||||||
public static void main(String[] argv)
|
public static void main(String[] argv)
|
||||||
{
|
{
|
||||||
if ( argv.length == 0 ) {
|
if ( argv.length == 0 ) {
|
||||||
@ -80,67 +79,4 @@ public class TestSearcher
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
=======
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
>>>>>>> 7e51a4909fdb01014f948c2dbc5cfb5fbed9ce56
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -19,7 +19,6 @@ import org.lionsoul.ip2region.DbSearcher;
|
|||||||
|
|
||||||
public class TestUnit {
|
public class TestUnit {
|
||||||
|
|
||||||
<<<<<<< HEAD
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
try {
|
try {
|
||||||
DbSearcher _searcher = new DbSearcher(new DbConfig(), "./data/ip2region.db");
|
DbSearcher _searcher = new DbSearcher(new DbConfig(), "./data/ip2region.db");
|
||||||
@ -93,80 +92,5 @@ public class TestUnit {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
=======
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
>>>>>>> 7e51a4909fdb01014f948c2dbc5cfb5fbed9ce56
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,7 +9,6 @@ import org.lionsoul.ip2region.Util;
|
|||||||
*/
|
*/
|
||||||
public class TestUtil
|
public class TestUtil
|
||||||
{
|
{
|
||||||
<<<<<<< HEAD
|
|
||||||
public static void main(String[] argv)
|
public static void main(String[] argv)
|
||||||
{
|
{
|
||||||
/* //1. test the ip2long
|
/* //1. test the ip2long
|
||||||
@ -55,51 +54,4 @@ public class TestUtil
|
|||||||
|
|
||||||
System.out.println(Util.ip2long("255.255.255.0"));
|
System.out.println(Util.ip2long("255.255.255.0"));
|
||||||
}
|
}
|
||||||
=======
|
|
||||||
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"));
|
|
||||||
}
|
|
||||||
>>>>>>> 7e51a4909fdb01014f948c2dbc5cfb5fbed9ce56
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,7 +2,6 @@
|
|||||||
/**
|
/**
|
||||||
* ip2region php seacher client class
|
* ip2region php seacher client class
|
||||||
*
|
*
|
||||||
<<<<<<< HEAD
|
|
||||||
* @author chenxin<chenxin619315@gmail.com>
|
* @author chenxin<chenxin619315@gmail.com>
|
||||||
* @date 2015-10-29
|
* @date 2015-10-29
|
||||||
*/
|
*/
|
||||||
@ -250,254 +249,5 @@ class Ip2Region
|
|||||||
$this->HeaderSip = NULL;
|
$this->HeaderSip = NULL;
|
||||||
$this->HeaderPtr = NULL;
|
$this->HeaderPtr = NULL;
|
||||||
}
|
}
|
||||||
=======
|
|
||||||
* @author chenxin<chenxin619315@gmail.com>
|
|
||||||
* @date 2015-10-29
|
|
||||||
*/
|
|
||||||
|
|
||||||
defined('INDEX_BLOCK_LENGTH') or define('INDEX_BLOCK_LENGTH', 12);
|
|
||||||
defined('TOTAL_HEADER_LENGTH') or define('TOTAL_HEADER_LENGTH', 4096);
|
|
||||||
|
|
||||||
class Ip2Region
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* db file handler
|
|
||||||
*/
|
|
||||||
private $dbFileHandler = NULL;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* header block info
|
|
||||||
*/
|
|
||||||
private $HeaderSip = NULL;
|
|
||||||
private $HeaderPtr = NULL;
|
|
||||||
private $headerLen = 0;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* super block index info
|
|
||||||
*/
|
|
||||||
private $firstIndexPtr = 0;
|
|
||||||
private $lastIndexPtr = 0;
|
|
||||||
private $totalBlocks = 0;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* construct method
|
|
||||||
*
|
|
||||||
* @param ip2regionFile
|
|
||||||
*/
|
|
||||||
public function __construct( $ip2regionFile )
|
|
||||||
{
|
|
||||||
$this->dbFileHandler = fopen($ip2regionFile, 'r');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* get the data block throught the specifield ip address or long ip numeric with binary search algorithm
|
|
||||||
*
|
|
||||||
* @param ip
|
|
||||||
* @return mixed Array or NULL for any error
|
|
||||||
*/
|
|
||||||
public function binarySearch( $ip )
|
|
||||||
{
|
|
||||||
//check and conver the ip address
|
|
||||||
if ( is_string($ip) ) $ip = ip2long($ip);
|
|
||||||
if ( $this->totalBlocks == 0 )
|
|
||||||
{
|
|
||||||
fseek($this->dbFileHandler, 0);
|
|
||||||
$superBlock = fread($this->dbFileHandler, 8);
|
|
||||||
|
|
||||||
$this->firstIndexPtr = self::getLong($superBlock, 0);
|
|
||||||
$this->lastIndexPtr = self::getLong($superBlock, 4);
|
|
||||||
$this->totalBlocks = ($this->lastIndexPtr-$this->firstIndexPtr)/INDEX_BLOCK_LENGTH + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
//binary search to define the data
|
|
||||||
$l = 0;
|
|
||||||
$h = $this->totalBlocks;
|
|
||||||
$dataPtr = 0;
|
|
||||||
while ( $l <= $h )
|
|
||||||
{
|
|
||||||
$m = (($l + $h) >> 1);
|
|
||||||
$p = $m * INDEX_BLOCK_LENGTH;
|
|
||||||
|
|
||||||
fseek($this->dbFileHandler, $this->firstIndexPtr + $p);
|
|
||||||
$buffer = fread($this->dbFileHandler, INDEX_BLOCK_LENGTH);
|
|
||||||
$sip = self::getLong($buffer, 0);
|
|
||||||
if ( $ip < $sip ) {
|
|
||||||
$h = $m - 1;
|
|
||||||
} else {
|
|
||||||
$eip = self::getLong($buffer, 4);
|
|
||||||
if ( $ip > $eip ) {
|
|
||||||
$l = $m + 1;
|
|
||||||
} else {
|
|
||||||
$dataPtr = self::getLong($buffer, 8);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//not matched just stop it here
|
|
||||||
if ( $dataPtr == 0 ) return NULL;
|
|
||||||
|
|
||||||
|
|
||||||
//get the data
|
|
||||||
$dataLen = (($dataPtr >> 24) & 0xFF);
|
|
||||||
$dataPtr = ($dataPtr & 0x00FFFFFF);
|
|
||||||
|
|
||||||
fseek($this->dbFileHandler, $dataPtr);
|
|
||||||
$data = fread($this->dbFileHandler, $dataLen);
|
|
||||||
|
|
||||||
return array(
|
|
||||||
'city_id' => self::getLong($data, 0),
|
|
||||||
'region' => substr($data, 4)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* get the data block associated with the specifield ip with b-tree search algorithm
|
|
||||||
*
|
|
||||||
* @param ip
|
|
||||||
* @return Mixed Array for NULL for any error
|
|
||||||
*/
|
|
||||||
public function btreeSearch( $ip )
|
|
||||||
{
|
|
||||||
if ( is_string($ip) ) $ip = ip2long($ip);
|
|
||||||
|
|
||||||
//check and load the header
|
|
||||||
if ( $this->HeaderSip == NULL )
|
|
||||||
{
|
|
||||||
fseek($this->dbFileHandler, 8);
|
|
||||||
$buffer = fread($this->dbFileHandler, TOTAL_HEADER_LENGTH);
|
|
||||||
|
|
||||||
//fill the header
|
|
||||||
$idx = 0;
|
|
||||||
$this->HeaderSip = array();
|
|
||||||
$this->HeaderPtr = array();
|
|
||||||
for ( $i = 0; $i < TOTAL_HEADER_LENGTH; $i += 8 )
|
|
||||||
{
|
|
||||||
$startIp = self::getLong($buffer, $i);
|
|
||||||
$dataPtr = self::getLong($buffer, $i + 4);
|
|
||||||
if ( $dataPtr == 0 ) break;
|
|
||||||
|
|
||||||
$this->HeaderSip[] = $startIp;
|
|
||||||
$this->HeaderPtr[] = $dataPtr;
|
|
||||||
$idx++;
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->headerLen = $idx;
|
|
||||||
}
|
|
||||||
|
|
||||||
//1. define the index block with the binary search
|
|
||||||
$l = 0; $h = $this->headerLen; $sptr = 0; $eptr = 0;
|
|
||||||
while ( $l <= $h )
|
|
||||||
{
|
|
||||||
$m = (($l + $h) >> 1);
|
|
||||||
|
|
||||||
//perfetc matched, just return it
|
|
||||||
if ( $ip == $this->HeaderSip[$m] ) {
|
|
||||||
if ( $m > 0 ) {
|
|
||||||
$sptr = $this->HeaderPtr[$m-1];
|
|
||||||
$eptr = $this->HeaderPtr[$m ];
|
|
||||||
} else {
|
|
||||||
$sptr = $this->HeaderPtr[$m ];
|
|
||||||
$eptr = $this->HeaderPtr[$m+1];
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
//less then the middle value
|
|
||||||
if ( $ip < $this->HeaderSip[$m] ) {
|
|
||||||
if ( $m == 0 ) {
|
|
||||||
$sptr = $this->HeaderPtr[$m ];
|
|
||||||
$eptr = $this->HeaderPtr[$m+1];
|
|
||||||
break;
|
|
||||||
} else if ( $ip > $this->HeaderSip[$m-1] ) {
|
|
||||||
$sptr = $this->HeaderPtr[$m-1];
|
|
||||||
$eptr = $this->HeaderPtr[$m ];
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
$h = $m - 1;
|
|
||||||
} else {
|
|
||||||
if ( $m == $this->headerLen - 1 ) {
|
|
||||||
$sptr = $this->HeaderPtr[$m-1];
|
|
||||||
$eptr = $this->HeaderPtr[$m ];
|
|
||||||
break;
|
|
||||||
} else if ( $ip <= $this->HeaderSip[$m+1] ) {
|
|
||||||
$sptr = $this->HeaderPtr[$m ];
|
|
||||||
$eptr = $this->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
|
|
||||||
$blockLen = $eptr - $sptr;
|
|
||||||
fseek($this->dbFileHandler, $sptr);
|
|
||||||
$index = fread($this->dbFileHandler, $blockLen + INDEX_BLOCK_LENGTH);
|
|
||||||
|
|
||||||
$dataptr = 0;
|
|
||||||
$l = 0; $h = $blockLen / INDEX_BLOCK_LENGTH;
|
|
||||||
while ( $l <= $h ) {
|
|
||||||
$m = (($l + $h) >> 1);
|
|
||||||
$p = (int)($m * INDEX_BLOCK_LENGTH);
|
|
||||||
$sip = self::getLong($index, $p);
|
|
||||||
if ( $ip < $sip ) {
|
|
||||||
$h = $m - 1;
|
|
||||||
} else {
|
|
||||||
$eip = self::getLong($index, $p + 4);
|
|
||||||
if ( $ip > $eip ) {
|
|
||||||
$l = $m + 1;
|
|
||||||
} else {
|
|
||||||
$dataptr = self::getLong($index, $p + 8);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//not matched
|
|
||||||
if ( $dataptr == 0 ) return NULL;
|
|
||||||
|
|
||||||
//3. get the data
|
|
||||||
$dataLen = (($dataptr >> 24) & 0xFF);
|
|
||||||
$dataPtr = ($dataptr & 0x00FFFFFF);
|
|
||||||
|
|
||||||
fseek($this->dbFileHandler, $dataPtr);
|
|
||||||
$data = fread($this->dbFileHandler, $dataLen);
|
|
||||||
|
|
||||||
return array(
|
|
||||||
'city_id' => self::getLong($data, 0),
|
|
||||||
'region' => substr($data, 4)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* read a long from a byte buffer
|
|
||||||
*
|
|
||||||
* @param b
|
|
||||||
* @param offset
|
|
||||||
*/
|
|
||||||
public static function getLong( $b, $offset )
|
|
||||||
{
|
|
||||||
return (
|
|
||||||
(ord($b[$offset++])) |
|
|
||||||
(ord($b[$offset++]) << 8) |
|
|
||||||
(ord($b[$offset++]) << 16) |
|
|
||||||
(ord($b[$offset ]) << 24)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* destruct method, resource destroy
|
|
||||||
*/
|
|
||||||
public function __destruct()
|
|
||||||
{
|
|
||||||
if ( $this->dbFileHandler != NULL ) fclose($this->dbFileHandler);
|
|
||||||
$this->HeaderSip = NULL;
|
|
||||||
$this->HeaderPtr = NULL;
|
|
||||||
}
|
|
||||||
>>>>>>> 7e51a4909fdb01014f948c2dbc5cfb5fbed9ce56
|
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
|||||||
@ -2,16 +2,11 @@
|
|||||||
/**
|
/**
|
||||||
* Ip2Region php client test script
|
* Ip2Region php client test script
|
||||||
*
|
*
|
||||||
<<<<<<< HEAD
|
|
||||||
* @author chenxin<chenxin619315@gmail.com>
|
* @author chenxin<chenxin619315@gmail.com>
|
||||||
=======
|
|
||||||
* @author chenxin<chenxin619315@gmail.com>
|
|
||||||
>>>>>>> 7e51a4909fdb01014f948c2dbc5cfb5fbed9ce56
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
if ( $argc < 2 )
|
if ( $argc < 2 )
|
||||||
{
|
{
|
||||||
<<<<<<< HEAD
|
|
||||||
$usage = <<<EOF
|
$usage = <<<EOF
|
||||||
Usage: php Test.php [ip2region db file] [alrogrithm]
|
Usage: php Test.php [ip2region db file] [alrogrithm]
|
||||||
+-Algorithm: binary or b-tree\n
|
+-Algorithm: binary or b-tree\n
|
||||||
@ -27,23 +22,6 @@ if ( isset($argv[1])
|
|||||||
&& strtolower($argv[1]) == 'binary' ) {
|
&& strtolower($argv[1]) == 'binary' ) {
|
||||||
$method = 2;
|
$method = 2;
|
||||||
$algorithm = 'Binary';
|
$algorithm = 'Binary';
|
||||||
=======
|
|
||||||
$usage = <<<EOF
|
|
||||||
Usage: php Test.php [ip2region db file] [alrogrithm]
|
|
||||||
+-Algorithm: binary or b-tree\n
|
|
||||||
EOF;
|
|
||||||
exit($usage);
|
|
||||||
}
|
|
||||||
|
|
||||||
array_shift($argv);
|
|
||||||
$dbFile = $argv[0];
|
|
||||||
$method = 1;
|
|
||||||
$algorithm = 'B-tree';
|
|
||||||
if ( isset($argv[1])
|
|
||||||
&& strtolower($argv[1]) == 'binary' ) {
|
|
||||||
$method = 2;
|
|
||||||
$algorithm = 'Binary';
|
|
||||||
>>>>>>> 7e51a4909fdb01014f948c2dbc5cfb5fbed9ce56
|
|
||||||
}
|
}
|
||||||
|
|
||||||
require dirname(__FILE__) . '/Ip2Region.class.php';
|
require dirname(__FILE__) . '/Ip2Region.class.php';
|
||||||
@ -61,7 +39,6 @@ echo $initStr, "\n";
|
|||||||
|
|
||||||
while ( true )
|
while ( true )
|
||||||
{
|
{
|
||||||
<<<<<<< HEAD
|
|
||||||
echo "ip2region>> ";
|
echo "ip2region>> ";
|
||||||
$line = trim(fgets(STDIN));
|
$line = trim(fgets(STDIN));
|
||||||
if ( strlen($line) < 2 ) continue;
|
if ( strlen($line) < 2 ) continue;
|
||||||
@ -75,29 +52,10 @@ while ( true )
|
|||||||
$data = $method==2 ? $ip2regionObj->binarySearch($line) : $ip2regionObj->btreeSearch($line);
|
$data = $method==2 ? $ip2regionObj->binarySearch($line) : $ip2regionObj->btreeSearch($line);
|
||||||
$c_time = getTime() - $s_time;
|
$c_time = getTime() - $s_time;
|
||||||
printf("%s|%s in %.5f millseconds\n", $data['city_id'], $data['region'], $c_time);
|
printf("%s|%s in %.5f millseconds\n", $data['city_id'], $data['region'], $c_time);
|
||||||
=======
|
|
||||||
echo "ip2region>> ";
|
|
||||||
$line = trim(fgets(STDIN));
|
|
||||||
if ( strlen($line) < 2 ) continue;
|
|
||||||
if ( $line == 'quit' ) break;
|
|
||||||
if ( ip2long($line) == NULL ) {
|
|
||||||
echo "Error: invalid ip address\n";
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$s_time = getTime();
|
|
||||||
$data = $method==2 ? $ip2regionObj->binarySearch($line) : $ip2regionObj->btreeSearch($line);
|
|
||||||
$c_time = getTime() - $s_time;
|
|
||||||
printf("%s|%s in %.5f millseconds\n", $data['city_id'], $data['region'], $c_time);
|
|
||||||
>>>>>>> 7e51a4909fdb01014f948c2dbc5cfb5fbed9ce56
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getTime()
|
function getTime()
|
||||||
{
|
{
|
||||||
<<<<<<< HEAD
|
|
||||||
return (microtime(true) * 1000);
|
return (microtime(true) * 1000);
|
||||||
=======
|
|
||||||
return (microtime(true) * 1000);
|
|
||||||
>>>>>>> 7e51a4909fdb01014f948c2dbc5cfb5fbed9ce56
|
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
|||||||
@ -8,7 +8,6 @@
|
|||||||
import struct, io, socket, sys
|
import struct, io, socket, sys
|
||||||
|
|
||||||
class Ip2Region(object):
|
class Ip2Region(object):
|
||||||
<<<<<<< HEAD
|
|
||||||
__headerSip = []
|
__headerSip = []
|
||||||
__headerPtr = []
|
__headerPtr = []
|
||||||
__f = None
|
__f = None
|
||||||
@ -191,187 +190,3 @@ class Ip2Region(object):
|
|||||||
self.__headerPtr = None
|
self.__headerPtr = None
|
||||||
self.__f.close()
|
self.__f.close()
|
||||||
self.__f = None
|
self.__f = None
|
||||||
=======
|
|
||||||
__headerSip = []
|
|
||||||
__headerPtr = []
|
|
||||||
__f = None
|
|
||||||
|
|
||||||
def __init__(self, dbfile):
|
|
||||||
self.initDatabase(dbfile)
|
|
||||||
|
|
||||||
def binarySearch(self, ip):
|
|
||||||
"""
|
|
||||||
" binary search method
|
|
||||||
" param: ip
|
|
||||||
"""
|
|
||||||
if not ip.isdigit(): ip = self.ip2long(ip)
|
|
||||||
|
|
||||||
self.__f.seek(0)
|
|
||||||
b = self.__f.read(8)
|
|
||||||
startPtr = self.getLong(b, 0)
|
|
||||||
endPtr = self.getLong(b, 4)
|
|
||||||
|
|
||||||
indexLen = endPtr - startPtr
|
|
||||||
self.__f.seek(startPtr)
|
|
||||||
b = self.__f.read(indexLen+12)
|
|
||||||
|
|
||||||
l, h, mixPtr = (0, int(indexLen/12), 0)
|
|
||||||
while l <= h:
|
|
||||||
m = int((l+h)/2)
|
|
||||||
ptr = startPtr + m*12
|
|
||||||
self.__f.seek(ptr)
|
|
||||||
|
|
||||||
b = self.__f.read(12)
|
|
||||||
sip = self.getLong(b, 0)
|
|
||||||
eip = self.getLong(b, 4)
|
|
||||||
|
|
||||||
if ip > sip:
|
|
||||||
if ip > eip:
|
|
||||||
l = m + 1
|
|
||||||
else:
|
|
||||||
mixPtr = self.getLong(b, 8)
|
|
||||||
break;
|
|
||||||
else:
|
|
||||||
h = m - 1
|
|
||||||
|
|
||||||
if mixPtr == 0: return "N2"
|
|
||||||
|
|
||||||
dataPtr = mixPtr & 0x00FFFFFFL
|
|
||||||
dataLen = (mixPtr >> 24) & 0xFF
|
|
||||||
|
|
||||||
self.__f.seek(dataPtr)
|
|
||||||
data = self.__f.read(dataLen)
|
|
||||||
return {
|
|
||||||
"city_id": self.getLong(data, 0),
|
|
||||||
"region" : data[4:]
|
|
||||||
}
|
|
||||||
|
|
||||||
def btreeSearch(self, ip):
|
|
||||||
"""
|
|
||||||
" b-tree search method
|
|
||||||
" param: ip
|
|
||||||
"""
|
|
||||||
if not ip.isdigit(): ip = self.ip2long(ip)
|
|
||||||
|
|
||||||
headerLen = len(self.__headerSip) - 1
|
|
||||||
l, h, sptr, eptr = (0, headerLen, 0, 0)
|
|
||||||
while l <= h:
|
|
||||||
m = int((l+h)/2)
|
|
||||||
|
|
||||||
if ip == self.__headerSip[m]:
|
|
||||||
if m > 0:
|
|
||||||
sptr = self.__headerPtr[m-1]
|
|
||||||
eptr = self.__headerPtr[m]
|
|
||||||
break;
|
|
||||||
else:
|
|
||||||
sptr = self.__headerPtr[m]
|
|
||||||
eptr = self.__headerPtr[m+1]
|
|
||||||
break;
|
|
||||||
|
|
||||||
if ip > self.__headerSip[m]:
|
|
||||||
if m == headerLen:
|
|
||||||
sptr = self.__headerPtr[m-1]
|
|
||||||
eptr = self.__headerPtr[m]
|
|
||||||
break;
|
|
||||||
elif ip < self.__headerSip[m+1]:
|
|
||||||
sptr = self.__headerPtr[m]
|
|
||||||
eptr = self.__headerPtr[m+1]
|
|
||||||
break;
|
|
||||||
|
|
||||||
l = m + 1
|
|
||||||
else:
|
|
||||||
if m == 0:
|
|
||||||
sptr = self.__headerPtr[m]
|
|
||||||
eptr = self.__headerPtr[m+1]
|
|
||||||
break;
|
|
||||||
elif ip > self.__headerSip[m-1]:
|
|
||||||
sptr = self.__headerPtr[m-1]
|
|
||||||
eptr = self.__headerPtr[m]
|
|
||||||
break;
|
|
||||||
|
|
||||||
h = m - 1
|
|
||||||
|
|
||||||
if sptr == 0: return "N1"
|
|
||||||
|
|
||||||
indexLen = eptr - sptr
|
|
||||||
self.__f.seek(sptr)
|
|
||||||
b = self.__f.read(indexLen + 12)
|
|
||||||
|
|
||||||
l, h, mixPtr = (0, int(indexLen/12), 0)
|
|
||||||
while l <= h:
|
|
||||||
m = int((l+h)/2)
|
|
||||||
offset = m * 12
|
|
||||||
|
|
||||||
if ip > self.getLong(b, offset):
|
|
||||||
if ip > self.getLong(b, offset+4):
|
|
||||||
l = m + 1
|
|
||||||
else:
|
|
||||||
mixPtr = self.getLong(b, offset+8)
|
|
||||||
break;
|
|
||||||
else:
|
|
||||||
h = m - 1
|
|
||||||
|
|
||||||
if mixPtr == 0: return "N2"
|
|
||||||
|
|
||||||
dataPtr = mixPtr & 0x00FFFFFFL
|
|
||||||
dataLen = (mixPtr >> 24) & 0xFF
|
|
||||||
|
|
||||||
self.__f.seek(dataPtr)
|
|
||||||
data = self.__f.read(dataLen)
|
|
||||||
return {
|
|
||||||
"city_id": self.getLong(data, 0),
|
|
||||||
"region" : data[4:]
|
|
||||||
}
|
|
||||||
|
|
||||||
def initDatabase(self, dbfile):
|
|
||||||
"""
|
|
||||||
" initialize the database for search
|
|
||||||
" param: dbFile
|
|
||||||
"""
|
|
||||||
try:
|
|
||||||
self.__f = io.open(dbfile, "rb")
|
|
||||||
#pass the super block
|
|
||||||
self.__f.seek(8)
|
|
||||||
#read the header block
|
|
||||||
b = self.__f.read(4086)
|
|
||||||
#parse the header block
|
|
||||||
sip = None
|
|
||||||
ptr = None
|
|
||||||
for i in range(0, len(b)-1, 8):
|
|
||||||
sip = self.getLong(b, i)
|
|
||||||
ptr = self.getLong(b, i+4)
|
|
||||||
if ptr == 0:
|
|
||||||
break
|
|
||||||
self.__headerSip.append(sip)
|
|
||||||
self.__headerPtr.append(ptr)
|
|
||||||
|
|
||||||
except IOError, e:
|
|
||||||
print "[Error]: ", e
|
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
def ip2long(self, ip):
|
|
||||||
_ip = socket.inet_aton(ip)
|
|
||||||
return struct.unpack("!L", _ip)[0]
|
|
||||||
|
|
||||||
def isip(self, ip):
|
|
||||||
p = ip.split(".")
|
|
||||||
|
|
||||||
if len(p) != 4 : return False
|
|
||||||
for pp in p:
|
|
||||||
if not pp.isdigit(): return False
|
|
||||||
if len(pp) > 3 : return False
|
|
||||||
if int(pp) > 255 : return False
|
|
||||||
|
|
||||||
return True
|
|
||||||
|
|
||||||
def getLong(self, b, offset):
|
|
||||||
if len( b[offset:offset+4] ) == 4:
|
|
||||||
return struct.unpack('I', b[offset:offset+4])[0]
|
|
||||||
return 0
|
|
||||||
|
|
||||||
def close(self):
|
|
||||||
self.__headerSip = None
|
|
||||||
self.__headerPtr = None
|
|
||||||
self.__f.close()
|
|
||||||
self.__f = None
|
|
||||||
>>>>>>> 7e51a4909fdb01014f948c2dbc5cfb5fbed9ce56
|
|
||||||
|
|||||||
Binary file not shown.
@ -9,7 +9,6 @@ import struct, sys, os, time
|
|||||||
from ip2Region import Ip2Region
|
from ip2Region import Ip2Region
|
||||||
|
|
||||||
def testSearch():
|
def testSearch():
|
||||||
<<<<<<< HEAD
|
|
||||||
"""
|
"""
|
||||||
" ip2region test function
|
" ip2region test function
|
||||||
"""
|
"""
|
||||||
@ -74,69 +73,3 @@ def testSearch():
|
|||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
testSearch()
|
testSearch()
|
||||||
=======
|
|
||||||
"""
|
|
||||||
" ip2region test function
|
|
||||||
"""
|
|
||||||
llen = len(sys.argv)
|
|
||||||
|
|
||||||
if llen < 2:
|
|
||||||
print "Usage: python ip2Region.py [ip2region db file] [alrogrithm]"
|
|
||||||
print "Algorithm: binary or b-tree"
|
|
||||||
return 0
|
|
||||||
|
|
||||||
dbFile = sys.argv[1]
|
|
||||||
method = 1
|
|
||||||
algorithm = "b-tree"
|
|
||||||
|
|
||||||
if (not os.path.isfile(dbFile)) or (not os.path.exists(dbFile)):
|
|
||||||
print "[Error]: Specified db file is not exists."
|
|
||||||
return 0
|
|
||||||
|
|
||||||
if llen > 2:
|
|
||||||
algorithm = sys.argv[2]
|
|
||||||
if algorithm == "binary":
|
|
||||||
method = 2
|
|
||||||
|
|
||||||
print "initializing %s..." % (algorithm)
|
|
||||||
print "+----------------------------------+"
|
|
||||||
print "| ip2region test script |"
|
|
||||||
print "| Author: komazhang@foxmail.com |"
|
|
||||||
print "| Type 'quit' to exit program |"
|
|
||||||
print "+----------------------------------+"
|
|
||||||
|
|
||||||
searcher = Ip2Region(dbFile);
|
|
||||||
|
|
||||||
while True:
|
|
||||||
line = raw_input("ip2region>> ")
|
|
||||||
line = line.strip()
|
|
||||||
|
|
||||||
if line == "":
|
|
||||||
print "[Error]: Invalid ip address."
|
|
||||||
continue
|
|
||||||
|
|
||||||
if line == "quit":
|
|
||||||
print "[Info]: Thanks for your use, Bye."
|
|
||||||
break
|
|
||||||
|
|
||||||
if not searcher.isip(line):
|
|
||||||
print "[Error]: Invalid ip address."
|
|
||||||
continue
|
|
||||||
|
|
||||||
sTime = time.time() * 1000
|
|
||||||
if method == 1:
|
|
||||||
data = searcher.btreeSearch(line)
|
|
||||||
else:
|
|
||||||
data = searcher.binarySearch(line)
|
|
||||||
eTime = time.time() * 1000
|
|
||||||
|
|
||||||
if isinstance(data, dict):
|
|
||||||
print "[Return]: %s|%s in %f millseconds" % (data["city_id"], data["region"], eTime-sTime)
|
|
||||||
else:
|
|
||||||
print "[Error]: ", data
|
|
||||||
|
|
||||||
searcher.close()
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
testSearch()
|
|
||||||
>>>>>>> 7e51a4909fdb01014f948c2dbc5cfb5fbed9ce56
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user