code tab to 4 space
This commit is contained in:
parent
15d0abb4b4
commit
41617aac4d
|
|
@ -1,9 +1,9 @@
|
||||||
/**
|
/**
|
||||||
* default ip2region implementation
|
* default ip2region implementation
|
||||||
*
|
*
|
||||||
* @see #ip2region.h
|
* @see #ip2region.h
|
||||||
* @author chenxin<chenxin619315@gmail.com>
|
* @author chenxin<chenxin619315@gmail.com>
|
||||||
* @date 2015-10-30
|
* @date 2015-10-30
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "ip2region.h"
|
#include "ip2region.h"
|
||||||
|
|
@ -13,337 +13,337 @@
|
||||||
/**
|
/**
|
||||||
* create a new ip2region object
|
* create a new ip2region object
|
||||||
*
|
*
|
||||||
* @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)
|
||||||
{
|
{
|
||||||
memset(ip2rObj, 0x00, sizeof(ip2region_entry));
|
memset(ip2rObj, 0x00, sizeof(ip2region_entry));
|
||||||
ip2rObj->headerLen = 0;
|
ip2rObj->headerLen = 0;
|
||||||
ip2rObj->HeaderSip = (uint_t *) IP2R_MALLOC(TOTAL_HEADER_LENGTH);
|
ip2rObj->HeaderSip = (uint_t *) IP2R_MALLOC(TOTAL_HEADER_LENGTH);
|
||||||
if ( ip2rObj->HeaderSip == NULL )
|
if ( ip2rObj->HeaderSip == NULL )
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
ip2rObj->HeaderPtr = (uint_t *) IP2R_MALLOC(TOTAL_HEADER_LENGTH);
|
ip2rObj->HeaderPtr = (uint_t *) IP2R_MALLOC(TOTAL_HEADER_LENGTH);
|
||||||
if ( ip2rObj->HeaderPtr == NULL )
|
if ( ip2rObj->HeaderPtr == NULL )
|
||||||
{
|
{
|
||||||
IP2R_FREE(ip2rObj->HeaderSip);
|
IP2R_FREE(ip2rObj->HeaderSip);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
//open the db file
|
//open the db file
|
||||||
ip2rObj->dbHandler = fopen(dbFile, "rb");
|
ip2rObj->dbHandler = fopen(dbFile, "rb");
|
||||||
if ( ip2rObj->dbHandler == NULL )
|
if ( ip2rObj->dbHandler == NULL )
|
||||||
{
|
{
|
||||||
IP2R_FREE(ip2rObj->HeaderSip);
|
IP2R_FREE(ip2rObj->HeaderSip);
|
||||||
IP2R_FREE(ip2rObj->HeaderPtr);
|
IP2R_FREE(ip2rObj->HeaderPtr);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
ip2rObj->firstIndexPtr = 0;
|
ip2rObj->firstIndexPtr = 0;
|
||||||
ip2rObj->lastIndexPtr = 0;
|
ip2rObj->lastIndexPtr = 0;
|
||||||
ip2rObj->totalBlocks = 0;
|
ip2rObj->totalBlocks = 0;
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* destroy the specifield ip2region object
|
* destroy the specifield ip2region object
|
||||||
*
|
*
|
||||||
* @param ip2region_t
|
* @param ip2region_t
|
||||||
*/
|
*/
|
||||||
IP2R_API uint_t ip2region_destroy(ip2region_t ip2rObj)
|
IP2R_API uint_t ip2region_destroy(ip2region_t ip2rObj)
|
||||||
{
|
{
|
||||||
IP2R_FREE(ip2rObj->HeaderSip);
|
IP2R_FREE(ip2rObj->HeaderSip);
|
||||||
ip2rObj->HeaderSip = NULL;
|
ip2rObj->HeaderSip = NULL;
|
||||||
IP2R_FREE(ip2rObj->HeaderPtr);
|
IP2R_FREE(ip2rObj->HeaderPtr);
|
||||||
ip2rObj->HeaderPtr = NULL;
|
ip2rObj->HeaderPtr = NULL;
|
||||||
|
|
||||||
//close the db file resource
|
//close the db file resource
|
||||||
if ( ip2rObj->dbHandler != NULL )
|
if ( ip2rObj->dbHandler != NULL )
|
||||||
{
|
{
|
||||||
fclose(ip2rObj->dbHandler);
|
fclose(ip2rObj->dbHandler);
|
||||||
ip2rObj->dbHandler = NULL;
|
ip2rObj->dbHandler = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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
|
||||||
*
|
*
|
||||||
* @param ip2rObj
|
* @param ip2rObj
|
||||||
* @param ip
|
* @param ip
|
||||||
* @param datablock
|
* @param datablock
|
||||||
* @return uint_t
|
* @return uint_t
|
||||||
*/
|
*/
|
||||||
IP2R_API uint_t ip2region_binary_search(ip2region_t ip2rObj, uint_t ip, datablock_t datablock)
|
IP2R_API uint_t ip2region_binary_search(ip2region_t ip2rObj, uint_t ip, datablock_t datablock)
|
||||||
{
|
{
|
||||||
int l, h, m, p;
|
int l, h, m, p;
|
||||||
uint_t sip, eip, dptr;
|
uint_t sip, eip, dptr;
|
||||||
char buffer[256];
|
char buffer[256];
|
||||||
int dataLen, dataptr;
|
int dataLen, dataptr;
|
||||||
|
|
||||||
if ( ip2rObj->totalBlocks == 0 )
|
if ( ip2rObj->totalBlocks == 0 )
|
||||||
{
|
{
|
||||||
fseek(ip2rObj->dbHandler, 0, 0);
|
fseek(ip2rObj->dbHandler, 0, 0);
|
||||||
if ( fread(buffer, 8, 1, ip2rObj->dbHandler) != 1 ) {
|
if ( fread(buffer, 8, 1, ip2rObj->dbHandler) != 1 ) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
ip2rObj->firstIndexPtr = getUnsignedInt(buffer, 0);
|
ip2rObj->firstIndexPtr = getUnsignedInt(buffer, 0);
|
||||||
ip2rObj->lastIndexPtr = getUnsignedInt(buffer, 4);
|
ip2rObj->lastIndexPtr = getUnsignedInt(buffer, 4);
|
||||||
ip2rObj->totalBlocks = (ip2rObj->lastIndexPtr-ip2rObj->firstIndexPtr)/INDEX_BLOCK_LENGTH + 1;
|
ip2rObj->totalBlocks = (ip2rObj->lastIndexPtr-ip2rObj->firstIndexPtr)/INDEX_BLOCK_LENGTH + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
//binary search the index blocks to define the data block
|
//binary search the index blocks to define the data block
|
||||||
l = 0; h = ip2rObj->totalBlocks; dptr = 0;
|
l = 0; h = ip2rObj->totalBlocks; dptr = 0;
|
||||||
while ( l <= h ) {
|
while ( l <= h ) {
|
||||||
m = (l + h) >> 1;
|
m = (l + h) >> 1;
|
||||||
p = ip2rObj->firstIndexPtr + m * INDEX_BLOCK_LENGTH;
|
p = ip2rObj->firstIndexPtr + m * INDEX_BLOCK_LENGTH;
|
||||||
|
|
||||||
fseek(ip2rObj->dbHandler, p, 0);
|
fseek(ip2rObj->dbHandler, p, 0);
|
||||||
if ( fread(buffer, INDEX_BLOCK_LENGTH, 1, ip2rObj->dbHandler) != 1 ) {
|
if ( fread(buffer, INDEX_BLOCK_LENGTH, 1, ip2rObj->dbHandler) != 1 ) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
sip = getUnsignedInt(buffer, 0);
|
sip = getUnsignedInt(buffer, 0);
|
||||||
if ( ip < sip ) {
|
if ( ip < sip ) {
|
||||||
h = m - 1;
|
h = m - 1;
|
||||||
} else {
|
} else {
|
||||||
eip = getUnsignedInt(buffer, 4);
|
eip = getUnsignedInt(buffer, 4);
|
||||||
if ( ip > eip ) {
|
if ( ip > eip ) {
|
||||||
l = m + 1;
|
l = m + 1;
|
||||||
} else {
|
} else {
|
||||||
dptr = getUnsignedInt(buffer, 8);
|
dptr = getUnsignedInt(buffer, 8);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( dptr == 0 ) return 0;
|
if ( dptr == 0 ) return 0;
|
||||||
|
|
||||||
//get the data
|
//get the data
|
||||||
dataLen = ((dptr >> 24) & 0xFF);
|
dataLen = ((dptr >> 24) & 0xFF);
|
||||||
dataptr = (dptr & 0x00FFFFFF);
|
dataptr = (dptr & 0x00FFFFFF);
|
||||||
|
|
||||||
//memset(data, 0x00, sizeof(data));
|
//memset(data, 0x00, sizeof(data));
|
||||||
fseek(ip2rObj->dbHandler, dataptr, 0);
|
fseek(ip2rObj->dbHandler, dataptr, 0);
|
||||||
if ( fread(buffer, dataLen, 1, ip2rObj->dbHandler) != 1 ) {
|
if ( fread(buffer, dataLen, 1, ip2rObj->dbHandler) != 1 ) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
//fill the data to the datablock
|
//fill the data to the datablock
|
||||||
datablock->city_id = getUnsignedInt(buffer, 0);
|
datablock->city_id = getUnsignedInt(buffer, 0);
|
||||||
dataLen -= 4; //reduce the length of the city_id
|
dataLen -= 4; //reduce the length of the city_id
|
||||||
memcpy(datablock->region, buffer + 4, dataLen);
|
memcpy(datablock->region, buffer + 4, dataLen);
|
||||||
datablock->region[dataLen] = '\0';
|
datablock->region[dataLen] = '\0';
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
{
|
{
|
||||||
return ip2region_binary_search(ip2rObj, ip2long(ip), datablock);
|
return ip2region_binary_search(ip2rObj, ip2long(ip), datablock);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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
|
||||||
*
|
*
|
||||||
* @param ip2rObj
|
* @param ip2rObj
|
||||||
* @param ip
|
* @param ip
|
||||||
* @param datablock
|
* @param datablock
|
||||||
* @return uint_t
|
* @return uint_t
|
||||||
*/
|
*/
|
||||||
IP2R_API uint_t ip2region_btree_search(ip2region_t ip2rObj, uint_t ip, datablock_t datablock)
|
IP2R_API uint_t ip2region_btree_search(ip2region_t ip2rObj, uint_t ip, datablock_t datablock)
|
||||||
{
|
{
|
||||||
int i, idx;
|
int i, idx;
|
||||||
int l, m, h, p, sptr, eptr, indexBlockLen, dataLen, dataptr;
|
int l, m, h, p, sptr, eptr, indexBlockLen, dataLen, dataptr;
|
||||||
uint_t sip, eip, idxptr, dptr;
|
uint_t sip, eip, idxptr, dptr;
|
||||||
char buffer[TOTAL_HEADER_LENGTH];
|
char buffer[TOTAL_HEADER_LENGTH];
|
||||||
|
|
||||||
if ( ip2rObj->headerLen == 0 )
|
if ( ip2rObj->headerLen == 0 )
|
||||||
{
|
{
|
||||||
idx = 0;
|
idx = 0;
|
||||||
fseek(ip2rObj->dbHandler, 8, 0); //pass the super block
|
fseek(ip2rObj->dbHandler, 8, 0); //pass the super block
|
||||||
if ( fread(buffer, TOTAL_HEADER_LENGTH, 1, ip2rObj->dbHandler) != 1 ) {
|
if ( fread(buffer, TOTAL_HEADER_LENGTH, 1, ip2rObj->dbHandler) != 1 ) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
for ( i = 0; i < TOTAL_HEADER_LENGTH; i += 8 )
|
for ( i = 0; i < TOTAL_HEADER_LENGTH; i += 8 )
|
||||||
{
|
{
|
||||||
sip = getUnsignedInt(buffer, i);
|
sip = getUnsignedInt(buffer, i);
|
||||||
idxptr = getUnsignedInt(buffer, i + 4);
|
idxptr = getUnsignedInt(buffer, i + 4);
|
||||||
if ( idxptr == 0 ) break;
|
if ( idxptr == 0 ) break;
|
||||||
|
|
||||||
ip2rObj->HeaderSip[idx] = sip;
|
ip2rObj->HeaderSip[idx] = sip;
|
||||||
ip2rObj->HeaderPtr[idx] = idxptr;
|
ip2rObj->HeaderPtr[idx] = idxptr;
|
||||||
idx++;
|
idx++;
|
||||||
}
|
}
|
||||||
|
|
||||||
ip2rObj->headerLen = idx;
|
ip2rObj->headerLen = idx;
|
||||||
}
|
}
|
||||||
|
|
||||||
//search the header block to define the index block
|
//search the header block to define the index block
|
||||||
l = 0; h = ip2rObj->headerLen; sptr = 0; eptr = 0;
|
l = 0; h = ip2rObj->headerLen; sptr = 0; eptr = 0;
|
||||||
while ( l <= h ) {
|
while ( l <= h ) {
|
||||||
m = ((l + h) >> 1);
|
m = ((l + h) >> 1);
|
||||||
|
|
||||||
//perfetc matched, just return it
|
//perfetc matched, just return it
|
||||||
if ( ip == ip2rObj->HeaderSip[m] ) {
|
if ( ip == ip2rObj->HeaderSip[m] ) {
|
||||||
if ( m > 0 ) {
|
if ( m > 0 ) {
|
||||||
sptr = ip2rObj->HeaderPtr[m-1];
|
sptr = ip2rObj->HeaderPtr[m-1];
|
||||||
eptr = ip2rObj->HeaderPtr[m ];
|
eptr = ip2rObj->HeaderPtr[m ];
|
||||||
} else {
|
} else {
|
||||||
sptr = ip2rObj->HeaderPtr[m ];
|
sptr = ip2rObj->HeaderPtr[m ];
|
||||||
eptr = ip2rObj->HeaderPtr[m+1];
|
eptr = ip2rObj->HeaderPtr[m+1];
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
//less then the middle value
|
//less then the middle value
|
||||||
if ( ip < ip2rObj->HeaderSip[m] ) {
|
if ( ip < ip2rObj->HeaderSip[m] ) {
|
||||||
if ( m == 0 ) {
|
if ( m == 0 ) {
|
||||||
sptr = ip2rObj->HeaderPtr[m ];
|
sptr = ip2rObj->HeaderPtr[m ];
|
||||||
eptr = ip2rObj->HeaderPtr[m+1];
|
eptr = ip2rObj->HeaderPtr[m+1];
|
||||||
break;
|
break;
|
||||||
} else if ( ip > ip2rObj->HeaderSip[m-1] ) {
|
} else if ( ip > ip2rObj->HeaderSip[m-1] ) {
|
||||||
sptr = ip2rObj->HeaderPtr[m-1];
|
sptr = ip2rObj->HeaderPtr[m-1];
|
||||||
eptr = ip2rObj->HeaderPtr[m ];
|
eptr = ip2rObj->HeaderPtr[m ];
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
h = m - 1;
|
h = m - 1;
|
||||||
} else {
|
} else {
|
||||||
if ( m == ip2rObj->headerLen - 1 ) {
|
if ( m == ip2rObj->headerLen - 1 ) {
|
||||||
sptr = ip2rObj->HeaderPtr[m-1];
|
sptr = ip2rObj->HeaderPtr[m-1];
|
||||||
eptr = ip2rObj->HeaderPtr[m ];
|
eptr = ip2rObj->HeaderPtr[m ];
|
||||||
break;
|
break;
|
||||||
} else if ( ip <= ip2rObj->HeaderSip[m+1] ) {
|
} else if ( ip <= ip2rObj->HeaderSip[m+1] ) {
|
||||||
sptr = ip2rObj->HeaderPtr[m ];
|
sptr = ip2rObj->HeaderPtr[m ];
|
||||||
eptr = ip2rObj->HeaderPtr[m+1];
|
eptr = ip2rObj->HeaderPtr[m+1];
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
l = m + 1;
|
l = m + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//not matched just stop it
|
//not matched just stop it
|
||||||
if ( sptr == 0 ) return 0;
|
if ( sptr == 0 ) return 0;
|
||||||
|
|
||||||
indexBlockLen = eptr - sptr;
|
indexBlockLen = eptr - sptr;
|
||||||
fseek(ip2rObj->dbHandler, sptr, 0);
|
fseek(ip2rObj->dbHandler, sptr, 0);
|
||||||
if ( fread(buffer, indexBlockLen + INDEX_BLOCK_LENGTH, 1, ip2rObj->dbHandler) != 1 ) {
|
if ( fread(buffer, indexBlockLen + INDEX_BLOCK_LENGTH, 1, ip2rObj->dbHandler) != 1 ) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
dptr = 0; l = 0; h = indexBlockLen / INDEX_BLOCK_LENGTH;
|
dptr = 0; l = 0; h = indexBlockLen / INDEX_BLOCK_LENGTH;
|
||||||
while ( l <= h ) {
|
while ( l <= h ) {
|
||||||
m = ((l + h) >> 1);
|
m = ((l + h) >> 1);
|
||||||
p = m * INDEX_BLOCK_LENGTH;
|
p = m * INDEX_BLOCK_LENGTH;
|
||||||
sip = getUnsignedInt(buffer, p);
|
sip = getUnsignedInt(buffer, p);
|
||||||
if ( ip < sip ) {
|
if ( ip < sip ) {
|
||||||
h = m - 1;
|
h = m - 1;
|
||||||
} else {
|
} else {
|
||||||
eip = getUnsignedInt(buffer, p + 4);
|
eip = getUnsignedInt(buffer, p + 4);
|
||||||
if ( ip > eip ) {
|
if ( ip > eip ) {
|
||||||
l = m + 1;
|
l = m + 1;
|
||||||
} else {
|
} else {
|
||||||
dptr = getUnsignedInt(buffer, p + 8);
|
dptr = getUnsignedInt(buffer, p + 8);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( dptr == 0 ) return 0;
|
if ( dptr == 0 ) return 0;
|
||||||
|
|
||||||
dataLen = ((dptr >> 24) & 0xFF);
|
dataLen = ((dptr >> 24) & 0xFF);
|
||||||
dataptr = (dptr & 0x00FFFFFF);
|
dataptr = (dptr & 0x00FFFFFF);
|
||||||
|
|
||||||
fseek(ip2rObj->dbHandler, dataptr, 0);
|
fseek(ip2rObj->dbHandler, dataptr, 0);
|
||||||
if ( fread(buffer, dataLen, 1, ip2rObj->dbHandler) != 1 ) {
|
if ( fread(buffer, dataLen, 1, ip2rObj->dbHandler) != 1 ) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
datablock->city_id = getUnsignedInt(buffer, 0);
|
datablock->city_id = getUnsignedInt(buffer, 0);
|
||||||
dataLen -= 4;
|
dataLen -= 4;
|
||||||
memcpy(datablock->region, buffer + 4, dataLen);
|
memcpy(datablock->region, buffer + 4, dataLen);
|
||||||
datablock->region[dataLen] = '\0';
|
datablock->region[dataLen] = '\0';
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
{
|
{
|
||||||
return ip2region_btree_search(ip2rObj, ip2long(ip), datablock);
|
return ip2region_btree_search(ip2rObj, ip2long(ip), datablock);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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
|
||||||
*
|
*
|
||||||
* @param buffer
|
* @param buffer
|
||||||
* @param offset
|
* @param offset
|
||||||
* @return uint_t
|
* @return uint_t
|
||||||
*/
|
*/
|
||||||
IP2R_API uint_t getUnsignedInt(char *buffer, int offset)
|
IP2R_API uint_t getUnsignedInt(char *buffer, int offset)
|
||||||
{
|
{
|
||||||
return (
|
return (
|
||||||
((buffer[offset ]) & 0x000000FF) |
|
((buffer[offset ]) & 0x000000FF) |
|
||||||
((buffer[offset+1] << 8) & 0x0000FF00) |
|
((buffer[offset+1] << 8) & 0x0000FF00) |
|
||||||
((buffer[offset+2] << 16) & 0x00FF0000) |
|
((buffer[offset+2] << 16) & 0x00FF0000) |
|
||||||
((buffer[offset+3] << 24) & 0xFF000000)
|
((buffer[offset+3] << 24) & 0xFF000000)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* string ip to long
|
* string ip to long
|
||||||
*
|
*
|
||||||
* @param ip
|
* @param ip
|
||||||
* @return uint_t
|
* @return uint_t
|
||||||
*/
|
*/
|
||||||
IP2R_API uint_t ip2long(char *ip)
|
IP2R_API uint_t ip2long(char *ip)
|
||||||
{
|
{
|
||||||
int i = 0, p = 24;
|
int i = 0, p = 24;
|
||||||
char buffer[4], *cs = ip;
|
char buffer[4], *cs = ip;
|
||||||
uint_t ipval = 0;
|
uint_t ipval = 0;
|
||||||
|
|
||||||
while ( *cs != '\0' )
|
while ( *cs != '\0' )
|
||||||
{
|
{
|
||||||
if ( *cs == '.' ) {
|
if ( *cs == '.' ) {
|
||||||
//single part length limit
|
//single part length limit
|
||||||
if ( i > 3 ) {
|
if ( i > 3 ) {
|
||||||
ipval = 0;
|
ipval = 0;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( p < 0 ) break;
|
if ( p < 0 ) break;
|
||||||
buffer[i] = '\0';
|
buffer[i] = '\0';
|
||||||
ipval |= (atoi(buffer) << p);
|
ipval |= (atoi(buffer) << p);
|
||||||
p -= 8;
|
p -= 8;
|
||||||
i = 0;
|
i = 0;
|
||||||
} else {
|
} else {
|
||||||
buffer[i++] = *cs;
|
buffer[i++] = *cs;
|
||||||
}
|
}
|
||||||
|
|
||||||
cs++;
|
cs++;
|
||||||
}
|
}
|
||||||
|
|
||||||
//append the rest parts
|
//append the rest parts
|
||||||
if ( i > 3 ) return 0;
|
if ( i > 3 ) return 0;
|
||||||
buffer[i] = '\0';
|
buffer[i] = '\0';
|
||||||
ipval |= atoi(buffer);
|
ipval |= atoi(buffer);
|
||||||
|
|
||||||
return ipval;
|
return ipval;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* long to string ip
|
* long to string ip
|
||||||
*
|
*
|
||||||
* @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)
|
||||||
*/
|
*/
|
||||||
IP2R_API uint_t long2ip(uint_t ip, char *buffer)
|
IP2R_API uint_t long2ip(uint_t ip, char *buffer)
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
/**
|
/**
|
||||||
* test ip2region searcher program
|
* test ip2region searcher program
|
||||||
*
|
*
|
||||||
* @author chenxin<chenxin619315@gmail.com>
|
* @author chenxin<chenxin619315@gmail.com>
|
||||||
* @date 2015-10-30
|
* @date 2015-10-30
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "ip2region.h"
|
#include "ip2region.h"
|
||||||
|
|
@ -12,88 +12,88 @@
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
|
|
||||||
#define __PRINT_ABOUT__ \
|
#define __PRINT_ABOUT__ \
|
||||||
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("+-------------------------------------+");
|
||||||
|
|
||||||
//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 )
|
||||||
{
|
{
|
||||||
register int c;
|
register int c;
|
||||||
register char *cs;
|
register char *cs;
|
||||||
|
|
||||||
cs = __dst;
|
cs = __dst;
|
||||||
while ( ( c = getc( fp ) ) != EOF ) {
|
while ( ( c = getc( fp ) ) != EOF ) {
|
||||||
if ( c == '\n' ) break;
|
if ( c == '\n' ) break;
|
||||||
*cs++ = c;
|
*cs++ = c;
|
||||||
}
|
}
|
||||||
*cs = '\0';
|
*cs = '\0';
|
||||||
|
|
||||||
return ( c == EOF && cs == __dst ) ? NULL : __dst;
|
return ( c == EOF && cs == __dst ) ? NULL : __dst;
|
||||||
}
|
}
|
||||||
|
|
||||||
static double getTime()
|
static double getTime()
|
||||||
{
|
{
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main( int argc, char **argv )
|
int main( int argc, char **argv )
|
||||||
{
|
{
|
||||||
ip2region_entry ip2rEntry;
|
ip2region_entry ip2rEntry;
|
||||||
datablock_entry datablock;
|
datablock_entry datablock;
|
||||||
char *dbFile = NULL, *algorithm = NULL;
|
char *dbFile = NULL, *algorithm = NULL;
|
||||||
char line[256];
|
char line[256];
|
||||||
uint_t (*func_ptr)(ip2region_t, char *, datablock_t);
|
uint_t (*func_ptr)(ip2region_t, char *, datablock_t);
|
||||||
double s_time, c_time;
|
double s_time, c_time;
|
||||||
memset(&datablock, 0x00, sizeof(datablock_entry));
|
memset(&datablock, 0x00, sizeof(datablock_entry));
|
||||||
|
|
||||||
if ( argc < 2 ) {
|
if ( argc < 2 ) {
|
||||||
printf("Usage: a.out [ip2region db file path] [algorithm]");
|
printf("Usage: a.out [ip2region db file path] [algorithm]");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
dbFile = argv[1];
|
dbFile = argv[1];
|
||||||
algorithm = "B-tree";
|
algorithm = "B-tree";
|
||||||
func_ptr = ip2region_btree_search_string;
|
func_ptr = ip2region_btree_search_string;
|
||||||
if ( argc >= 3 && strcmp(argv[2], "binary") == 0 ) {
|
if ( argc >= 3 && strcmp(argv[2], "binary") == 0 ) {
|
||||||
algorithm = "Binary";
|
algorithm = "Binary";
|
||||||
func_ptr = ip2region_binary_search_string;
|
func_ptr = ip2region_binary_search_string;
|
||||||
}
|
}
|
||||||
|
|
||||||
//create a new ip2rObj
|
//create a new ip2rObj
|
||||||
printf("+--initializing %s ... \n", algorithm);
|
printf("+--initializing %s ... \n", algorithm);
|
||||||
if ( ip2region_create(&ip2rEntry, dbFile) == 0 )
|
if ( ip2region_create(&ip2rEntry, dbFile) == 0 )
|
||||||
{
|
{
|
||||||
println("Error: Fail to create the ip2region object");
|
println("Error: Fail to create the ip2region object");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
__PRINT_ABOUT__;
|
__PRINT_ABOUT__;
|
||||||
|
|
||||||
while ( 1 )
|
while ( 1 )
|
||||||
{
|
{
|
||||||
print("ip2region>> ");
|
print("ip2region>> ");
|
||||||
getLine( stdin, line );
|
getLine( stdin, line );
|
||||||
if ( strlen(line) < 2 ) continue;
|
if ( strlen(line) < 2 ) continue;
|
||||||
if ( strcasecmp( line, "quit" ) == 0 ) {
|
if ( strcasecmp( line, "quit" ) == 0 ) {
|
||||||
println("+--Bye!");
|
println("+--Bye!");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
s_time = getTime();
|
s_time = getTime();
|
||||||
func_ptr(&ip2rEntry, line, &datablock);
|
func_ptr(&ip2rEntry, line, &datablock);
|
||||||
c_time = getTime() - s_time;
|
c_time = getTime() - s_time;
|
||||||
printf("%d|%s in %.5f millseconds\n", datablock.city_id, datablock.region, c_time);
|
printf("%d|%s in %.5f millseconds\n", datablock.city_id, datablock.region, c_time);
|
||||||
}
|
}
|
||||||
|
|
||||||
//destory the ip2rObj
|
//destory the ip2rObj
|
||||||
ip2region_destroy(&ip2rEntry);
|
ip2region_destroy(&ip2rEntry);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,50 +3,50 @@ package org.lionsoul.ip2region;
|
||||||
/**
|
/**
|
||||||
* data block class
|
* data block class
|
||||||
*
|
*
|
||||||
* @author chenxin<chenxin619315@gmail.com>
|
* @author chenxin<chenxin619315@gmail.com>
|
||||||
*/
|
*/
|
||||||
public class DataBlock
|
public class DataBlock
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* city id
|
* city id
|
||||||
*/
|
*/
|
||||||
private int city_id;
|
private int city_id;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* region address
|
* region address
|
||||||
*/
|
*/
|
||||||
private String region;
|
private String region;
|
||||||
|
|
||||||
public DataBlock( int city_id, String region )
|
public DataBlock( int city_id, String region )
|
||||||
{
|
{
|
||||||
this.city_id = city_id;
|
this.city_id = city_id;
|
||||||
this.region = region;
|
this.region = region;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getCityId() {
|
public int getCityId() {
|
||||||
return city_id;
|
return city_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public DataBlock setCityId(int city_id) {
|
public DataBlock setCityId(int city_id) {
|
||||||
this.city_id = city_id;
|
this.city_id = city_id;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getRegion() {
|
public String getRegion() {
|
||||||
return region;
|
return region;
|
||||||
}
|
}
|
||||||
|
|
||||||
public DataBlock setRegion(String region) {
|
public DataBlock setRegion(String region) {
|
||||||
this.region = region;
|
this.region = region;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString()
|
public String toString()
|
||||||
{
|
{
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
|
|
||||||
sb.append(city_id).append('|').append(region);
|
sb.append(city_id).append('|').append(region);
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,53 +7,53 @@ package org.lionsoul.ip2region;
|
||||||
*/
|
*/
|
||||||
public class DbConfig
|
public class DbConfig
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* total header data block size
|
* total header data block size
|
||||||
*/
|
*/
|
||||||
private int totalHeaderSize;
|
private int totalHeaderSize;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* max index data block size
|
* max index data block size
|
||||||
* u should always choice the fastest read block size
|
* u should always choice the fastest read block size
|
||||||
*/
|
*/
|
||||||
private int indexBlockSize;
|
private int indexBlockSize;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* construct method
|
* construct method
|
||||||
*
|
*
|
||||||
* @param totalHeaderSize
|
* @param totalHeaderSize
|
||||||
* @param dataBlockSize
|
* @param dataBlockSize
|
||||||
* @throws DbMakerConfigException
|
* @throws DbMakerConfigException
|
||||||
*/
|
*/
|
||||||
public DbConfig( int totalHeaderSize ) throws DbMakerConfigException {
|
public DbConfig( int totalHeaderSize ) throws DbMakerConfigException {
|
||||||
if ( (totalHeaderSize % 8) != 0 )
|
if ( (totalHeaderSize % 8) != 0 )
|
||||||
{
|
{
|
||||||
throw new DbMakerConfigException("totalHeaderSize must be times of 8");
|
throw new DbMakerConfigException("totalHeaderSize must be times of 8");
|
||||||
}
|
}
|
||||||
|
|
||||||
this.totalHeaderSize = totalHeaderSize;
|
this.totalHeaderSize = totalHeaderSize;
|
||||||
this.indexBlockSize = 4096; //4 * 1024
|
this.indexBlockSize = 4096; //4 * 1024
|
||||||
}
|
}
|
||||||
|
|
||||||
public DbConfig() throws DbMakerConfigException {
|
public DbConfig() throws DbMakerConfigException {
|
||||||
this(8192);
|
this(8192);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getTotalHeaderSize() {
|
public int getTotalHeaderSize() {
|
||||||
return totalHeaderSize;
|
return totalHeaderSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
public DbConfig setTotalHeaderSize(int totalHeaderSize) {
|
public DbConfig setTotalHeaderSize(int totalHeaderSize) {
|
||||||
this.totalHeaderSize = totalHeaderSize;
|
this.totalHeaderSize = totalHeaderSize;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getIndexBlockSize() {
|
public int getIndexBlockSize() {
|
||||||
return indexBlockSize;
|
return indexBlockSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
public DbConfig setIndexBlockSize(int dataBlockSize) {
|
public DbConfig setIndexBlockSize(int dataBlockSize) {
|
||||||
this.indexBlockSize = dataBlockSize;
|
this.indexBlockSize = dataBlockSize;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,17 +7,17 @@ package org.lionsoul.ip2region;
|
||||||
*/
|
*/
|
||||||
public class DbMakerConfigException extends Exception
|
public class DbMakerConfigException extends Exception
|
||||||
{
|
{
|
||||||
private static final long serialVersionUID = 4495714680349884838L;
|
private static final long serialVersionUID = 4495714680349884838L;
|
||||||
|
|
||||||
public DbMakerConfigException( String info ) {
|
public DbMakerConfigException( String info ) {
|
||||||
super(info);
|
super(info);
|
||||||
}
|
}
|
||||||
|
|
||||||
public DbMakerConfigException( Throwable res ) {
|
public DbMakerConfigException( Throwable res ) {
|
||||||
super(res);
|
super(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
public DbMakerConfigException( String info, Throwable res ) {
|
public DbMakerConfigException( String info, Throwable res ) {
|
||||||
super(info, res);
|
super(info, res);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,306 +11,306 @@ import java.io.RandomAccessFile;
|
||||||
*/
|
*/
|
||||||
public class DbSearcher
|
public class DbSearcher
|
||||||
{
|
{
|
||||||
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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* db config
|
* db config
|
||||||
*/
|
*/
|
||||||
private DbConfig dbConfig = null;
|
private DbConfig dbConfig = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* db file access handler
|
* db file access handler
|
||||||
*/
|
*/
|
||||||
private RandomAccessFile raf = null;
|
private RandomAccessFile raf = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* header blocks buffer
|
* header blocks buffer
|
||||||
*/
|
*/
|
||||||
private long[] HeaderSip = null;
|
private long[] HeaderSip = null;
|
||||||
private int[] HeaderPtr = null;
|
private int[] HeaderPtr = null;
|
||||||
private int headerLength;
|
private int headerLength;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* super blocks info
|
* super blocks info
|
||||||
*/
|
*/
|
||||||
private long firstIndexPtr = 0;
|
private long firstIndexPtr = 0;
|
||||||
private long lastIndexPtr = 0;
|
private long lastIndexPtr = 0;
|
||||||
private int totalIndexBlocks = 0;
|
private int totalIndexBlocks = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* construct class
|
* construct class
|
||||||
*
|
*
|
||||||
* @param bdConfig
|
* @param bdConfig
|
||||||
* @param dbFile
|
* @param dbFile
|
||||||
* @throws FileNotFoundException
|
* @throws FileNotFoundException
|
||||||
*/
|
*/
|
||||||
public DbSearcher( DbConfig dbConfig, String dbFile ) throws FileNotFoundException
|
public DbSearcher( DbConfig dbConfig, String dbFile ) throws FileNotFoundException
|
||||||
{
|
{
|
||||||
this.dbConfig = dbConfig;
|
this.dbConfig = dbConfig;
|
||||||
raf = new RandomAccessFile(dbFile, "r");
|
raf = new RandomAccessFile(dbFile, "r");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get by index ptr
|
* get by index ptr
|
||||||
*
|
*
|
||||||
* @param indexPtr
|
* @param indexPtr
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public DataBlock getByIndexPtr( long ptr ) throws IOException
|
public DataBlock getByIndexPtr( long ptr ) throws IOException
|
||||||
{
|
{
|
||||||
raf.seek(ptr);
|
raf.seek(ptr);
|
||||||
byte[] buffer = new byte[12];
|
byte[] buffer = new byte[12];
|
||||||
raf.readFully(buffer, 0, buffer.length);
|
raf.readFully(buffer, 0, buffer.length);
|
||||||
//long startIp = Util.getIntLong(buffer, 0);
|
//long startIp = Util.getIntLong(buffer, 0);
|
||||||
//long endIp = Util.getIntLong(buffer, 4);
|
//long endIp = Util.getIntLong(buffer, 4);
|
||||||
long extra = Util.getIntLong(buffer, 8);
|
long extra = Util.getIntLong(buffer, 8);
|
||||||
|
|
||||||
int dataLen = (int)((extra >> 24) & 0xFF);
|
int dataLen = (int)((extra >> 24) & 0xFF);
|
||||||
int dataPtr = (int)((extra & 0x00FFFFFF));
|
int dataPtr = (int)((extra & 0x00FFFFFF));
|
||||||
|
|
||||||
raf.seek(dataPtr);
|
raf.seek(dataPtr);
|
||||||
byte[] data = new byte[dataLen];
|
byte[] data = new byte[dataLen];
|
||||||
raf.readFully(data, 0, data.length);
|
raf.readFully(data, 0, data.length);
|
||||||
|
|
||||||
int city_id = (int)Util.getIntLong(data, 0);
|
int city_id = (int)Util.getIntLong(data, 0);
|
||||||
String region = new String(data, 4, data.length - 4, "UTF-8");
|
String region = new String(data, 4, data.length - 4, "UTF-8");
|
||||||
|
|
||||||
return new DataBlock(city_id, region);
|
return new DataBlock(city_id, region);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get the region with a int ip address with b-tree algorithm
|
* get the region with a int ip address with b-tree algorithm
|
||||||
*
|
*
|
||||||
* @param ip
|
* @param ip
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public DataBlock btreeSearch( long ip ) throws IOException
|
public DataBlock btreeSearch( long ip ) throws IOException
|
||||||
{
|
{
|
||||||
//check and load the header
|
//check and load the header
|
||||||
if ( HeaderSip == null )
|
if ( HeaderSip == null )
|
||||||
{
|
{
|
||||||
raf.seek(8L); //pass the super block
|
raf.seek(8L); //pass the super block
|
||||||
//byte[] b = new byte[dbConfig.getTotalHeaderSize()];
|
//byte[] b = new byte[dbConfig.getTotalHeaderSize()];
|
||||||
byte[] b = new byte[4096];
|
byte[] b = new byte[4096];
|
||||||
raf.readFully(b, 0, b.length);
|
raf.readFully(b, 0, b.length);
|
||||||
|
|
||||||
//fill the header
|
//fill the header
|
||||||
int len = b.length >> 3, idx = 0; //b.lenght / 8
|
int len = b.length >> 3, idx = 0; //b.lenght / 8
|
||||||
HeaderSip = new long[len];
|
HeaderSip = new long[len];
|
||||||
HeaderPtr = new int [len];
|
HeaderPtr = new int [len];
|
||||||
long startIp, dataPtr;
|
long startIp, dataPtr;
|
||||||
for ( int i = 0; i < b.length; i += 8 ) {
|
for ( int i = 0; i < b.length; i += 8 ) {
|
||||||
startIp = Util.getIntLong(b, i);
|
startIp = Util.getIntLong(b, i);
|
||||||
dataPtr = Util.getIntLong(b, i + 4);
|
dataPtr = Util.getIntLong(b, i + 4);
|
||||||
if ( dataPtr == 0 ) break;
|
if ( dataPtr == 0 ) break;
|
||||||
|
|
||||||
HeaderSip[idx] = startIp;
|
HeaderSip[idx] = startIp;
|
||||||
HeaderPtr[idx] = (int)dataPtr;
|
HeaderPtr[idx] = (int)dataPtr;
|
||||||
idx++;
|
idx++;
|
||||||
}
|
}
|
||||||
|
|
||||||
headerLength = idx;
|
headerLength = idx;
|
||||||
}
|
}
|
||||||
|
|
||||||
//1. define the index block with the binary search
|
//1. define the index block with the binary search
|
||||||
if ( ip == HeaderSip[0] ) {
|
if ( ip == HeaderSip[0] ) {
|
||||||
return getByIndexPtr(HeaderPtr[0]);
|
return getByIndexPtr(HeaderPtr[0]);
|
||||||
} else if ( ip == HeaderSip[headerLength-1] ) {
|
} else if ( ip == HeaderSip[headerLength-1] ) {
|
||||||
return getByIndexPtr(HeaderPtr[headerLength-1]);
|
return getByIndexPtr(HeaderPtr[headerLength-1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
int l = 0, h = headerLength, sptr = 0, eptr = 0;
|
int l = 0, h = headerLength, sptr = 0, eptr = 0;
|
||||||
while ( l <= h )
|
while ( l <= h )
|
||||||
{
|
{
|
||||||
int m = (l + h) >> 1;
|
int m = (l + h) >> 1;
|
||||||
|
|
||||||
//perfetc matched, just return it
|
//perfetc matched, just return it
|
||||||
if ( ip == HeaderSip[m] ) {
|
if ( ip == HeaderSip[m] ) {
|
||||||
if ( m > 0 ) {
|
if ( m > 0 ) {
|
||||||
sptr = HeaderPtr[m-1];
|
sptr = HeaderPtr[m-1];
|
||||||
eptr = HeaderPtr[m ];
|
eptr = HeaderPtr[m ];
|
||||||
} else {
|
} else {
|
||||||
sptr = HeaderPtr[m ];
|
sptr = HeaderPtr[m ];
|
||||||
eptr = HeaderPtr[m+1];
|
eptr = HeaderPtr[m+1];
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
//less then the middle value
|
//less then the middle value
|
||||||
if ( ip < HeaderSip[m] ) {
|
if ( ip < HeaderSip[m] ) {
|
||||||
if ( m == 0 ) {
|
if ( m == 0 ) {
|
||||||
sptr = HeaderPtr[m ];
|
sptr = HeaderPtr[m ];
|
||||||
eptr = HeaderPtr[m+1];
|
eptr = HeaderPtr[m+1];
|
||||||
break;
|
break;
|
||||||
} else if ( ip > HeaderSip[m-1] ) {
|
} else if ( ip > HeaderSip[m-1] ) {
|
||||||
sptr = HeaderPtr[m-1];
|
sptr = HeaderPtr[m-1];
|
||||||
eptr = HeaderPtr[m ];
|
eptr = HeaderPtr[m ];
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
h = m - 1;
|
h = m - 1;
|
||||||
} else {
|
} else {
|
||||||
if ( m == headerLength - 1 ) {
|
if ( m == headerLength - 1 ) {
|
||||||
sptr = HeaderPtr[m-1];
|
sptr = HeaderPtr[m-1];
|
||||||
eptr = HeaderPtr[m ];
|
eptr = HeaderPtr[m ];
|
||||||
break;
|
break;
|
||||||
} else if ( ip <= HeaderSip[m+1] ) {
|
} else if ( ip <= HeaderSip[m+1] ) {
|
||||||
sptr = HeaderPtr[m ];
|
sptr = HeaderPtr[m ];
|
||||||
eptr = HeaderPtr[m+1];
|
eptr = HeaderPtr[m+1];
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
l = m + 1;
|
l = m + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//match nothing just stop it
|
//match nothing just stop it
|
||||||
if ( sptr == 0 ) return null;
|
if ( sptr == 0 ) return null;
|
||||||
|
|
||||||
//2. search the index blocks to define the data
|
//2. search the index blocks to define the data
|
||||||
int blockLen = eptr - sptr, blen = IndexBlock.getIndexBlockLength();
|
int blockLen = eptr - sptr, blen = IndexBlock.getIndexBlockLength();
|
||||||
byte[] iBuffer = new byte[blockLen + blen]; //include the right border block
|
byte[] iBuffer = new byte[blockLen + blen]; //include the right border block
|
||||||
raf.seek(sptr);
|
raf.seek(sptr);
|
||||||
raf.readFully(iBuffer, 0, iBuffer.length);
|
raf.readFully(iBuffer, 0, iBuffer.length);
|
||||||
|
|
||||||
l = 0; h = blockLen / blen;
|
l = 0; h = blockLen / blen;
|
||||||
long sip, eip, dataptr = 0;
|
long sip, eip, dataptr = 0;
|
||||||
while ( l <= h ) {
|
while ( l <= h ) {
|
||||||
int m = (l + h) >> 1;
|
int m = (l + h) >> 1;
|
||||||
int p = m * blen;
|
int p = m * blen;
|
||||||
sip = Util.getIntLong(iBuffer, p);
|
sip = Util.getIntLong(iBuffer, p);
|
||||||
if ( ip < sip ) {
|
if ( ip < sip ) {
|
||||||
h = m - 1;
|
h = m - 1;
|
||||||
} else {
|
} else {
|
||||||
eip = Util.getIntLong(iBuffer, p + 4);
|
eip = Util.getIntLong(iBuffer, p + 4);
|
||||||
if ( ip > eip ) {
|
if ( ip > eip ) {
|
||||||
l = m + 1;
|
l = m + 1;
|
||||||
} else {
|
} else {
|
||||||
dataptr = Util.getIntLong(iBuffer, p + 8);
|
dataptr = Util.getIntLong(iBuffer, p + 8);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//not matched
|
//not matched
|
||||||
if ( dataptr == 0 ) return null;
|
if ( dataptr == 0 ) return null;
|
||||||
|
|
||||||
//3. get the data
|
//3. get the data
|
||||||
int dataLen = (int)((dataptr >> 24) & 0xFF);
|
int dataLen = (int)((dataptr >> 24) & 0xFF);
|
||||||
int dataPtr = (int)((dataptr & 0x00FFFFFF));
|
int dataPtr = (int)((dataptr & 0x00FFFFFF));
|
||||||
|
|
||||||
raf.seek(dataPtr);
|
raf.seek(dataPtr);
|
||||||
byte[] data = new byte[dataLen];
|
byte[] data = new byte[dataLen];
|
||||||
raf.readFully(data, 0, data.length);
|
raf.readFully(data, 0, data.length);
|
||||||
|
|
||||||
int city_id = (int)Util.getIntLong(data, 0);
|
int city_id = (int)Util.getIntLong(data, 0);
|
||||||
String region = new String(data, 4, data.length - 4, "UTF-8");
|
String region = new String(data, 4, data.length - 4, "UTF-8");
|
||||||
|
|
||||||
return new DataBlock(city_id, region);
|
return new DataBlock(city_id, region);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get the region throught the ip address with b-tree search algorithm
|
* get the region throught the ip address with b-tree search algorithm
|
||||||
*
|
*
|
||||||
* @param ip
|
* @param ip
|
||||||
* @return DataBlock
|
* @return DataBlock
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public DataBlock btreeSearch( String ip ) throws IOException
|
public DataBlock btreeSearch( String ip ) throws IOException
|
||||||
{
|
{
|
||||||
return btreeSearch(Util.ip2long(ip));
|
return btreeSearch(Util.ip2long(ip));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get the region with a int ip address with binary search algorithm
|
* get the region with a int ip address with binary search algorithm
|
||||||
*
|
*
|
||||||
* @param ip
|
* @param ip
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public DataBlock binarySearch( long ip ) throws IOException
|
public DataBlock binarySearch( long ip ) throws IOException
|
||||||
{
|
{
|
||||||
int blen = IndexBlock.getIndexBlockLength();
|
int blen = IndexBlock.getIndexBlockLength();
|
||||||
if ( totalIndexBlocks == 0 )
|
if ( totalIndexBlocks == 0 )
|
||||||
{
|
{
|
||||||
raf.seek(0L);
|
raf.seek(0L);
|
||||||
byte[] superBytes = new byte[8];
|
byte[] superBytes = new byte[8];
|
||||||
raf.readFully(superBytes, 0, superBytes.length);
|
raf.readFully(superBytes, 0, superBytes.length);
|
||||||
//initialize the global vars
|
//initialize the global vars
|
||||||
firstIndexPtr = Util.getIntLong(superBytes, 0);
|
firstIndexPtr = Util.getIntLong(superBytes, 0);
|
||||||
lastIndexPtr = Util.getIntLong(superBytes, 4);
|
lastIndexPtr = Util.getIntLong(superBytes, 4);
|
||||||
totalIndexBlocks = (int)((lastIndexPtr - firstIndexPtr)/blen) + 1;
|
totalIndexBlocks = (int)((lastIndexPtr - firstIndexPtr)/blen) + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
//search the index blocks to define the data
|
//search the index blocks to define the data
|
||||||
int l = 0, h = totalIndexBlocks;
|
int l = 0, h = totalIndexBlocks;
|
||||||
byte[] buffer = new byte[blen];
|
byte[] buffer = new byte[blen];
|
||||||
long sip, eip, dataptr = 0;
|
long sip, eip, dataptr = 0;
|
||||||
while ( l <= h ) {
|
while ( l <= h ) {
|
||||||
int m = (l + h) >> 1;
|
int m = (l + h) >> 1;
|
||||||
raf.seek(firstIndexPtr + m * blen); //set the file pointer
|
raf.seek(firstIndexPtr + m * blen); //set the file pointer
|
||||||
raf.readFully(buffer, 0, buffer.length);
|
raf.readFully(buffer, 0, buffer.length);
|
||||||
sip = Util.getIntLong(buffer, 0);
|
sip = Util.getIntLong(buffer, 0);
|
||||||
if ( ip < sip ) {
|
if ( ip < sip ) {
|
||||||
h = m - 1;
|
h = m - 1;
|
||||||
} else {
|
} else {
|
||||||
eip = Util.getIntLong(buffer, 4);
|
eip = Util.getIntLong(buffer, 4);
|
||||||
if ( ip > eip ) {
|
if ( ip > eip ) {
|
||||||
l = m + 1;
|
l = m + 1;
|
||||||
} else {
|
} else {
|
||||||
dataptr = Util.getIntLong(buffer, 8);
|
dataptr = Util.getIntLong(buffer, 8);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//not matched
|
//not matched
|
||||||
if ( dataptr == 0 ) return null;
|
if ( dataptr == 0 ) return null;
|
||||||
|
|
||||||
//get the data
|
//get the data
|
||||||
int dataLen = (int)((dataptr >> 24) & 0xFF);
|
int dataLen = (int)((dataptr >> 24) & 0xFF);
|
||||||
int dataPtr = (int)((dataptr & 0x00FFFFFF));
|
int dataPtr = (int)((dataptr & 0x00FFFFFF));
|
||||||
|
|
||||||
raf.seek(dataPtr);
|
raf.seek(dataPtr);
|
||||||
byte[] data = new byte[dataLen];
|
byte[] data = new byte[dataLen];
|
||||||
raf.readFully(data, 0, data.length);
|
raf.readFully(data, 0, data.length);
|
||||||
|
|
||||||
int city_id = (int)Util.getIntLong(data, 0);
|
int city_id = (int)Util.getIntLong(data, 0);
|
||||||
String region = new String(data, 4, data.length - 4, "UTF-8");
|
String region = new String(data, 4, data.length - 4, "UTF-8");
|
||||||
|
|
||||||
return new DataBlock(city_id, region);
|
return new DataBlock(city_id, region);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get the region throught the ip address with binary search algorithm
|
* get the region throught the ip address with binary search algorithm
|
||||||
*
|
*
|
||||||
* @param ip
|
* @param ip
|
||||||
* @return DataBlock
|
* @return DataBlock
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public DataBlock binarySearch( String ip ) throws IOException
|
public DataBlock binarySearch( String ip ) throws IOException
|
||||||
{
|
{
|
||||||
return binarySearch(Util.ip2long(ip));
|
return binarySearch(Util.ip2long(ip));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get the db config
|
* get the db config
|
||||||
*
|
*
|
||||||
* @return DbConfig
|
* @return DbConfig
|
||||||
*/
|
*/
|
||||||
public DbConfig getDbConfig()
|
public DbConfig getDbConfig()
|
||||||
{
|
{
|
||||||
return dbConfig;
|
return dbConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* close the db
|
* close the db
|
||||||
*
|
*
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public void close() throws IOException
|
public void close() throws IOException
|
||||||
{
|
{
|
||||||
HeaderSip = null; //let gc do its work
|
HeaderSip = null; //let gc do its work
|
||||||
HeaderPtr = null;
|
HeaderPtr = null;
|
||||||
raf.close();
|
raf.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,58 +7,58 @@ package org.lionsoul.ip2region;
|
||||||
*/
|
*/
|
||||||
public class HeaderBlock
|
public class HeaderBlock
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* index block start ip address
|
* index block start ip address
|
||||||
*/
|
*/
|
||||||
private long indexStartIp;
|
private long indexStartIp;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ip address
|
* ip address
|
||||||
*/
|
*/
|
||||||
private int indexPtr;
|
private int indexPtr;
|
||||||
|
|
||||||
public HeaderBlock( long indexStartIp, int indexPtr )
|
public HeaderBlock( long indexStartIp, int indexPtr )
|
||||||
{
|
{
|
||||||
this.indexStartIp = indexStartIp;
|
this.indexStartIp = indexStartIp;
|
||||||
this.indexPtr = indexPtr;
|
this.indexPtr = indexPtr;
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getIndexStartIp() {
|
public long getIndexStartIp() {
|
||||||
return indexStartIp;
|
return indexStartIp;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HeaderBlock setIndexStartIp(long indexStartIp) {
|
public HeaderBlock setIndexStartIp(long indexStartIp) {
|
||||||
this.indexStartIp = indexStartIp;
|
this.indexStartIp = indexStartIp;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getIndexPtr() {
|
public int getIndexPtr() {
|
||||||
return indexPtr;
|
return indexPtr;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HeaderBlock setIndexPtr(int indexPtr) {
|
public HeaderBlock setIndexPtr(int indexPtr) {
|
||||||
this.indexPtr = indexPtr;
|
this.indexPtr = indexPtr;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get the bytes for db storage
|
* get the bytes for db storage
|
||||||
*
|
*
|
||||||
* @return byte[]
|
* @return byte[]
|
||||||
*/
|
*/
|
||||||
public byte[] getBytes()
|
public byte[] getBytes()
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
* +------------+-----------+
|
* +------------+-----------+
|
||||||
* | 4bytes | 4bytes |
|
* | 4bytes | 4bytes |
|
||||||
* +------------+-----------+
|
* +------------+-----------+
|
||||||
* start ip index ptr
|
* start ip index ptr
|
||||||
*/
|
*/
|
||||||
byte[] b = new byte[8];
|
byte[] b = new byte[8];
|
||||||
|
|
||||||
Util.writeIntLong(b, 0, indexStartIp);
|
Util.writeIntLong(b, 0, indexStartIp);
|
||||||
Util.writeIntLong(b, 4, indexPtr);
|
Util.writeIntLong(b, 4, indexPtr);
|
||||||
|
|
||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,98 +7,98 @@ package org.lionsoul.ip2region;
|
||||||
*/
|
*/
|
||||||
public class IndexBlock
|
public class IndexBlock
|
||||||
{
|
{
|
||||||
private static int LENGTH = 12;
|
private static int LENGTH = 12;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* start ip address
|
* start ip address
|
||||||
*/
|
*/
|
||||||
private long startIp;
|
private long startIp;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* end ip address
|
* end ip address
|
||||||
*/
|
*/
|
||||||
private long endIp;
|
private long endIp;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* data ptr and data length
|
* data ptr and data length
|
||||||
*/
|
*/
|
||||||
private int dataPtr;
|
private int dataPtr;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* data length
|
* data length
|
||||||
*/
|
*/
|
||||||
private int dataLen;
|
private int dataLen;
|
||||||
|
|
||||||
public IndexBlock(long startIp, long endIp, int dataPtr, int dataLen)
|
public IndexBlock(long startIp, long endIp, int dataPtr, int dataLen)
|
||||||
{
|
{
|
||||||
this.startIp = startIp;
|
this.startIp = startIp;
|
||||||
this.endIp = endIp;
|
this.endIp = endIp;
|
||||||
this.dataPtr = dataPtr;
|
this.dataPtr = dataPtr;
|
||||||
this.dataLen = dataLen;
|
this.dataLen = dataLen;
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getStartIp() {
|
public long getStartIp() {
|
||||||
return startIp;
|
return startIp;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IndexBlock setStartIp(long startIp) {
|
public IndexBlock setStartIp(long startIp) {
|
||||||
this.startIp = startIp;
|
this.startIp = startIp;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getEndIp() {
|
public long getEndIp() {
|
||||||
return endIp;
|
return endIp;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IndexBlock setEndIp(long endIp) {
|
public IndexBlock setEndIp(long endIp) {
|
||||||
this.endIp = endIp;
|
this.endIp = endIp;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getDataPtr() {
|
public int getDataPtr() {
|
||||||
return dataPtr;
|
return dataPtr;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IndexBlock setDataPtr(int dataPtr) {
|
public IndexBlock setDataPtr(int dataPtr) {
|
||||||
this.dataPtr = dataPtr;
|
this.dataPtr = dataPtr;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getDataLen() {
|
public int getDataLen() {
|
||||||
return dataLen;
|
return dataLen;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IndexBlock setDataLen(int dataLen) {
|
public IndexBlock setDataLen(int dataLen) {
|
||||||
this.dataLen = dataLen;
|
this.dataLen = dataLen;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int getIndexBlockLength() {
|
public static int getIndexBlockLength() {
|
||||||
return LENGTH;
|
return LENGTH;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get the bytes for storage
|
* get the bytes for storage
|
||||||
*
|
*
|
||||||
* @return byte[]
|
* @return byte[]
|
||||||
*/
|
*/
|
||||||
public byte[] getBytes()
|
public byte[] getBytes()
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
* +------------+-----------+-----------+
|
* +------------+-----------+-----------+
|
||||||
* | 4bytes | 4bytes | 4bytes |
|
* | 4bytes | 4bytes | 4bytes |
|
||||||
* +------------+-----------+-----------+
|
* +------------+-----------+-----------+
|
||||||
* start ip end ip data ptr + len
|
* start ip end ip data ptr + len
|
||||||
*/
|
*/
|
||||||
byte[] b = new byte[12];
|
byte[] b = new byte[12];
|
||||||
|
|
||||||
Util.writeIntLong(b, 0, startIp); //start ip
|
Util.writeIntLong(b, 0, startIp); //start ip
|
||||||
Util.writeIntLong(b, 4, endIp); //end ip
|
Util.writeIntLong(b, 4, endIp); //end ip
|
||||||
|
|
||||||
//write the data ptr and the length
|
//write the data ptr and the length
|
||||||
long mix = dataPtr | ((dataLen << 24) & 0xFF000000L);
|
long mix = dataPtr | ((dataLen << 24) & 0xFF000000L);
|
||||||
Util.writeIntLong(b, 8, mix);
|
Util.writeIntLong(b, 8, mix);
|
||||||
|
|
||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,139 +7,139 @@ package org.lionsoul.ip2region;
|
||||||
*/
|
*/
|
||||||
public class Util
|
public class Util
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* write specfield bytes to a byte array start from offset
|
* write specfield bytes to a byte array start from offset
|
||||||
*
|
*
|
||||||
* @param b
|
* @param b
|
||||||
* @param offset
|
* @param offset
|
||||||
* @param v
|
* @param v
|
||||||
* @param bytes
|
* @param bytes
|
||||||
*/
|
*/
|
||||||
public static void write( byte[] b, int offset, long v, int bytes)
|
public static void write( byte[] b, int offset, long v, int bytes)
|
||||||
{
|
{
|
||||||
for ( int i = 0; i < bytes; i++ )
|
for ( int i = 0; i < bytes; i++ )
|
||||||
{
|
{
|
||||||
b[offset++] = (byte)((v >>> (8 * i)) & 0xFF);
|
b[offset++] = (byte)((v >>> (8 * i)) & 0xFF);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* write a int to a byte array
|
* write a int to a byte array
|
||||||
*
|
*
|
||||||
* @param b
|
* @param b
|
||||||
* @param offet
|
* @param offet
|
||||||
* @param v
|
* @param v
|
||||||
*/
|
*/
|
||||||
public static void writeIntLong( byte[] b, int offset, long v )
|
public static void writeIntLong( byte[] b, int offset, long v )
|
||||||
{
|
{
|
||||||
b[offset++] = (byte)((v >> 0) & 0xFF);
|
b[offset++] = (byte)((v >> 0) & 0xFF);
|
||||||
b[offset++] = (byte)((v >> 8) & 0xFF);
|
b[offset++] = (byte)((v >> 8) & 0xFF);
|
||||||
b[offset++] = (byte)((v >> 16) & 0xFF);
|
b[offset++] = (byte)((v >> 16) & 0xFF);
|
||||||
b[offset ] = (byte)((v >> 24) & 0xFF);
|
b[offset ] = (byte)((v >> 24) & 0xFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get a int from a byte array start from the specifiled offset
|
* get a int from a byte array start from the specifiled offset
|
||||||
*
|
*
|
||||||
* @param b
|
* @param b
|
||||||
* @param offset
|
* @param offset
|
||||||
*/
|
*/
|
||||||
public static long getIntLong( byte[] b, int offset )
|
public static long getIntLong( byte[] b, int offset )
|
||||||
{
|
{
|
||||||
return (
|
return (
|
||||||
((b[offset++] & 0x000000FFL)) |
|
((b[offset++] & 0x000000FFL)) |
|
||||||
((b[offset++] << 8) & 0x0000FF00L) |
|
((b[offset++] << 8) & 0x0000FF00L) |
|
||||||
((b[offset++] << 16) & 0x00FF0000L) |
|
((b[offset++] << 16) & 0x00FF0000L) |
|
||||||
((b[offset ] << 24) & 0xFF000000L)
|
((b[offset ] << 24) & 0xFF000000L)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get a int from a byte array start from the specifield offset
|
* get a int from a byte array start from the specifield offset
|
||||||
*
|
*
|
||||||
* @param b
|
* @param b
|
||||||
* @param offset
|
* @param offset
|
||||||
*/
|
*/
|
||||||
public static int getInt3( byte[] b, int offset )
|
public static int getInt3( byte[] b, int offset )
|
||||||
{
|
{
|
||||||
return (
|
return (
|
||||||
(b[offset++] & 0x000000FF) |
|
(b[offset++] & 0x000000FF) |
|
||||||
(b[offset++] & 0x0000FF00) |
|
(b[offset++] & 0x0000FF00) |
|
||||||
(b[offset ] & 0x00FF0000)
|
(b[offset ] & 0x00FF0000)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int getInt2( byte[] b, int offset )
|
public static int getInt2( byte[] b, int offset )
|
||||||
{
|
{
|
||||||
return (
|
return (
|
||||||
(b[offset++] & 0x000000FF) |
|
(b[offset++] & 0x000000FF) |
|
||||||
(b[offset ] & 0x0000FF00)
|
(b[offset ] & 0x0000FF00)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int getInt1( byte[] b, int offset )
|
public static int getInt1( byte[] b, int offset )
|
||||||
{
|
{
|
||||||
return (
|
return (
|
||||||
(b[offset] & 0x000000FF)
|
(b[offset] & 0x000000FF)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* string ip to long ip
|
* string ip to long ip
|
||||||
*
|
*
|
||||||
* @param ip
|
* @param ip
|
||||||
* @return long
|
* @return long
|
||||||
*/
|
*/
|
||||||
public static long ip2long( String ip )
|
public static long ip2long( String ip )
|
||||||
{
|
{
|
||||||
String[] p = ip.split("\\.");
|
String[] p = ip.split("\\.");
|
||||||
if ( p.length != 4 ) return 0;
|
if ( p.length != 4 ) return 0;
|
||||||
|
|
||||||
int p1 = ((Integer.valueOf(p[0]) << 24) & 0xFF000000);
|
int p1 = ((Integer.valueOf(p[0]) << 24) & 0xFF000000);
|
||||||
int p2 = ((Integer.valueOf(p[1]) << 16) & 0x00FF0000);
|
int p2 = ((Integer.valueOf(p[1]) << 16) & 0x00FF0000);
|
||||||
int p3 = ((Integer.valueOf(p[2]) << 8) & 0x0000FF00);
|
int p3 = ((Integer.valueOf(p[2]) << 8) & 0x0000FF00);
|
||||||
int p4 = ((Integer.valueOf(p[3]) << 0) & 0x000000FF);
|
int p4 = ((Integer.valueOf(p[3]) << 0) & 0x000000FF);
|
||||||
|
|
||||||
return ((p1 | p2 | p3 | p4) & 0xFFFFFFFFL);
|
return ((p1 | p2 | p3 | p4) & 0xFFFFFFFFL);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* int to ip string
|
* int to ip string
|
||||||
*
|
*
|
||||||
* @param ip
|
* @param ip
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static String long2ip( long ip )
|
public static String long2ip( long ip )
|
||||||
{
|
{
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
|
|
||||||
sb
|
sb
|
||||||
.append((ip >> 24) & 0xFF).append('.')
|
.append((ip >> 24) & 0xFF).append('.')
|
||||||
.append((ip >> 16) & 0xFF).append('.')
|
.append((ip >> 16) & 0xFF).append('.')
|
||||||
.append((ip >> 8) & 0xFF).append('.')
|
.append((ip >> 8) & 0xFF).append('.')
|
||||||
.append((ip >> 0) & 0xFF);
|
.append((ip >> 0) & 0xFF);
|
||||||
|
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* check the validate of the specifeld ip address
|
* check the validate of the specifeld ip address
|
||||||
*
|
*
|
||||||
* @param ip
|
* @param ip
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
public static boolean isIpAddress( String ip )
|
public static boolean isIpAddress( String ip )
|
||||||
{
|
{
|
||||||
String[] p = ip.split("\\.");
|
String[] p = ip.split("\\.");
|
||||||
if ( p.length != 4 ) return false;
|
if ( p.length != 4 ) return false;
|
||||||
|
|
||||||
for ( String pp : p )
|
for ( String pp : p )
|
||||||
{
|
{
|
||||||
if ( pp.length() > 3 ) return false;
|
if ( pp.length() > 3 ) return false;
|
||||||
int val = Integer.valueOf(pp);
|
int val = Integer.valueOf(pp);
|
||||||
if ( val > 255 ) return false;
|
if ( val > 255 ) return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,65 +18,65 @@ import org.lionsoul.ip2region.Util;
|
||||||
*/
|
*/
|
||||||
public class TestSearcher
|
public class TestSearcher
|
||||||
{
|
{
|
||||||
public static void main(String[] argv)
|
public static void main(String[] argv)
|
||||||
{
|
{
|
||||||
if ( argv.length == 0 ) {
|
if ( argv.length == 0 ) {
|
||||||
System.out.println("| Usage: java -jar ip2region-{version}.jar [ip2region db file]");
|
System.out.println("| Usage: java -jar ip2region-{version}.jar [ip2region db file]");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int algorithm = DbSearcher.BTREE_ALGORITHM;
|
int algorithm = DbSearcher.BTREE_ALGORITHM;
|
||||||
File file = new File(argv[0]);
|
File file = new File(argv[0]);
|
||||||
if ( file.exists() == false ) {
|
if ( file.exists() == false ) {
|
||||||
System.out.println("Error: Invalid ip2region.db file");
|
System.out.println("Error: Invalid ip2region.db file");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( argv.length > 1 ) {
|
if ( argv.length > 1 ) {
|
||||||
if ( argv[1].equalsIgnoreCase("binary")) algorithm = DbSearcher.BIN_ALGORITHM;
|
if ( argv[1].equalsIgnoreCase("binary")) algorithm = DbSearcher.BIN_ALGORITHM;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
System.out.println("initializing "+((algorithm==2)?"Binary":"B-tree")+" ... ");
|
System.out.println("initializing "+((algorithm==2)?"Binary":"B-tree")+" ... ");
|
||||||
DbConfig config = new DbConfig();
|
DbConfig config = new DbConfig();
|
||||||
DbSearcher seacher = new DbSearcher(config, argv[0]);
|
DbSearcher seacher = new DbSearcher(config, argv[0]);
|
||||||
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
|
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
|
||||||
|
|
||||||
System.out.println("+----------------------------------+");
|
System.out.println("+----------------------------------+");
|
||||||
System.out.println("| ip2region test shell |");
|
System.out.println("| ip2region test shell |");
|
||||||
System.out.println("| Author: chenxin619315@gmail.com |");
|
System.out.println("| Author: chenxin619315@gmail.com |");
|
||||||
System.out.println("| Type 'quit' to exit program |");
|
System.out.println("| Type 'quit' to exit program |");
|
||||||
System.out.println("+----------------------------------+");
|
System.out.println("+----------------------------------+");
|
||||||
|
|
||||||
double sTime = 0, cTime = 0;
|
double sTime = 0, cTime = 0;
|
||||||
String line = null;
|
String line = null;
|
||||||
DataBlock dataBlock = null;
|
DataBlock dataBlock = null;
|
||||||
while ( true )
|
while ( true )
|
||||||
{
|
{
|
||||||
System.out.print("ip2region>> ");
|
System.out.print("ip2region>> ");
|
||||||
line = reader.readLine().trim();
|
line = reader.readLine().trim();
|
||||||
if ( line.length() < 2 ) continue;
|
if ( line.length() < 2 ) continue;
|
||||||
if ( line.equalsIgnoreCase("quit") ) break;
|
if ( line.equalsIgnoreCase("quit") ) break;
|
||||||
if ( Util.isIpAddress(line) == false ) {
|
if ( Util.isIpAddress(line) == false ) {
|
||||||
System.out.println("Error: Invalid ip address");
|
System.out.println("Error: Invalid ip address");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
sTime = System.nanoTime();
|
sTime = System.nanoTime();
|
||||||
dataBlock = algorithm==2 ? seacher.binarySearch(line) : seacher.btreeSearch(line);
|
dataBlock = algorithm==2 ? seacher.binarySearch(line) : seacher.btreeSearch(line);
|
||||||
cTime = (System.nanoTime() - sTime) / 1000000;
|
cTime = (System.nanoTime() - sTime) / 1000000;
|
||||||
System.out.printf("%s in %.5f millseconds\n", dataBlock, cTime);
|
System.out.printf("%s in %.5f millseconds\n", dataBlock, cTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
reader.close();
|
reader.close();
|
||||||
seacher.close();
|
seacher.close();
|
||||||
System.out.println("+--Bye");
|
System.out.println("+--Bye");
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
// TODO Auto-generated catch block
|
// TODO Auto-generated catch block
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} catch (DbMakerConfigException e) {
|
} catch (DbMakerConfigException e) {
|
||||||
// TODO Auto-generated catch block
|
// TODO Auto-generated catch block
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -19,78 +19,78 @@ import org.lionsoul.ip2region.DbSearcher;
|
||||||
|
|
||||||
public class TestUnit {
|
public class TestUnit {
|
||||||
|
|
||||||
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");
|
||||||
BufferedReader bfr = new BufferedReader(new FileReader("./data/ip.merge.txt"));
|
BufferedReader bfr = new BufferedReader(new FileReader("./data/ip.merge.txt"));
|
||||||
BufferedWriter bwr = new BufferedWriter(new FileWriter("./data/error_log.txt", true));
|
BufferedWriter bwr = new BufferedWriter(new FileWriter("./data/error_log.txt", true));
|
||||||
int errCount = 0;
|
int errCount = 0;
|
||||||
int lineCount = 0;
|
int lineCount = 0;
|
||||||
String str = null;
|
String str = null;
|
||||||
|
|
||||||
while ( (str = bfr.readLine()) != null ) {
|
while ( (str = bfr.readLine()) != null ) {
|
||||||
StringBuffer line = new StringBuffer(str);
|
StringBuffer line = new StringBuffer(str);
|
||||||
//get first ip
|
//get first ip
|
||||||
int first_idx = line.indexOf("|");
|
int first_idx = line.indexOf("|");
|
||||||
String first_ip = line.substring(0, first_idx);
|
String first_ip = line.substring(0, first_idx);
|
||||||
|
|
||||||
line = new StringBuffer( line.substring(first_idx + 1) );
|
line = new StringBuffer( line.substring(first_idx + 1) );
|
||||||
|
|
||||||
//get second ip
|
//get second ip
|
||||||
int second_idx = line.indexOf("|");
|
int second_idx = line.indexOf("|");
|
||||||
String second_ip = line.substring(0, second_idx);
|
String second_ip = line.substring(0, second_idx);
|
||||||
|
|
||||||
//get addr
|
//get addr
|
||||||
String source_region = line.substring(second_idx + 1);
|
String source_region = line.substring(second_idx + 1);
|
||||||
|
|
||||||
//search from DbSearcher
|
//search from DbSearcher
|
||||||
System.out.println("+---Start, start to search");
|
System.out.println("+---Start, start to search");
|
||||||
System.out.println("+---[Info]: Source region = "+source_region);
|
System.out.println("+---[Info]: Source region = "+source_region);
|
||||||
|
|
||||||
System.out.println("+---[Info]: Step1, search for first IP: "+first_ip);
|
System.out.println("+---[Info]: Step1, search for first IP: "+first_ip);
|
||||||
DataBlock fdata = _searcher.binarySearch(first_ip);
|
DataBlock fdata = _searcher.binarySearch(first_ip);
|
||||||
if ( ! fdata.getRegion().equalsIgnoreCase( source_region ) ) {
|
if ( ! fdata.getRegion().equalsIgnoreCase( source_region ) ) {
|
||||||
System.out.println("[Error]: Search first IP failed, DB region = "+fdata.getRegion());
|
System.out.println("[Error]: Search first IP failed, DB region = "+fdata.getRegion());
|
||||||
bwr.write("[Source]: Region: "+fdata.getRegion());
|
bwr.write("[Source]: Region: "+fdata.getRegion());
|
||||||
bwr.newLine();
|
bwr.newLine();
|
||||||
bwr.write("[Source]: First Ip: "+first_ip);
|
bwr.write("[Source]: First Ip: "+first_ip);
|
||||||
bwr.newLine();
|
bwr.newLine();
|
||||||
bwr.write("[DB]: Region: "+fdata.getRegion());
|
bwr.write("[DB]: Region: "+fdata.getRegion());
|
||||||
bwr.newLine();
|
bwr.newLine();
|
||||||
bwr.flush();
|
bwr.flush();
|
||||||
errCount++;
|
errCount++;
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("+---[Info]: Step2, search for second IP: "+second_ip);
|
System.out.println("+---[Info]: Step2, search for second IP: "+second_ip);
|
||||||
DataBlock sdata = _searcher.btreeSearch(second_ip);
|
DataBlock sdata = _searcher.btreeSearch(second_ip);
|
||||||
if ( ! sdata.getRegion().equalsIgnoreCase( source_region ) ) {
|
if ( ! sdata.getRegion().equalsIgnoreCase( source_region ) ) {
|
||||||
System.out.println("[Error]: Search second IP failed, DB region = "+sdata.getRegion());
|
System.out.println("[Error]: Search second IP failed, DB region = "+sdata.getRegion());
|
||||||
bwr.write("[Source]: Region: "+sdata.getRegion());
|
bwr.write("[Source]: Region: "+sdata.getRegion());
|
||||||
bwr.newLine();
|
bwr.newLine();
|
||||||
bwr.write("[Source]: First Ip: "+second_ip);
|
bwr.write("[Source]: First Ip: "+second_ip);
|
||||||
bwr.newLine();
|
bwr.newLine();
|
||||||
bwr.write("[DB]: Region: "+sdata.getRegion());
|
bwr.write("[DB]: Region: "+sdata.getRegion());
|
||||||
bwr.newLine();
|
bwr.newLine();
|
||||||
bwr.flush();
|
bwr.flush();
|
||||||
errCount++;
|
errCount++;
|
||||||
}
|
}
|
||||||
|
|
||||||
lineCount++;
|
lineCount++;
|
||||||
}
|
}
|
||||||
|
|
||||||
bwr.close();
|
bwr.close();
|
||||||
bfr.close();
|
bfr.close();
|
||||||
System.out.println("+---Done, search complished");
|
System.out.println("+---Done, search complished");
|
||||||
System.out.println("+---Statistics, Error count = "+errCount
|
System.out.println("+---Statistics, Error count = "+errCount
|
||||||
+", Total line = "+lineCount
|
+", Total line = "+lineCount
|
||||||
+", Fail ratio = "+((float)(errCount/lineCount))*100+"%");
|
+", Fail ratio = "+((float)(errCount/lineCount))*100+"%");
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} catch (DbMakerConfigException e) {
|
} catch (DbMakerConfigException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} catch ( Exception e ) {
|
} catch ( Exception e ) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,49 +9,49 @@ import org.lionsoul.ip2region.Util;
|
||||||
*/
|
*/
|
||||||
public class TestUtil
|
public class TestUtil
|
||||||
{
|
{
|
||||||
public static void main(String[] argv)
|
public static void main(String[] argv)
|
||||||
{
|
{
|
||||||
/* //1. test the ip2long
|
/* //1. test the ip2long
|
||||||
String[] ipSet = new String[]{
|
String[] ipSet = new String[]{
|
||||||
"120.24.78.68",
|
"120.24.78.68",
|
||||||
"120.24.229.68",
|
"120.24.229.68",
|
||||||
"120.24.87.145",
|
"120.24.87.145",
|
||||||
"218.17.162.99"
|
"218.17.162.99"
|
||||||
};
|
};
|
||||||
|
|
||||||
for ( String ip : ipSet )
|
for ( String ip : ipSet )
|
||||||
{
|
{
|
||||||
int ipInt = Util.ip2Int(ip);
|
int ipInt = Util.ip2Int(ip);
|
||||||
System.out.println("src ip: " + ip + ", ip2Int: " + ipInt + ", int2IP: " + Util.int2IP(ipInt));
|
System.out.println("src ip: " + ip + ", ip2Int: " + ipInt + ", int2IP: " + Util.int2IP(ipInt));
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
/* int[] arr = new int[]{12344, -1234, 2146789, 0, -1024};
|
/* int[] arr = new int[]{12344, -1234, 2146789, 0, -1024};
|
||||||
byte[] b = new byte[arr.length * 4];
|
byte[] b = new byte[arr.length * 4];
|
||||||
|
|
||||||
//write the int
|
//write the int
|
||||||
System.out.println("+--Testing writeInt ... ");
|
System.out.println("+--Testing writeInt ... ");
|
||||||
int i, idx = 0;
|
int i, idx = 0;
|
||||||
for ( i = 0; i < b.length; i += 4 )
|
for ( i = 0; i < b.length; i += 4 )
|
||||||
{
|
{
|
||||||
System.out.println("offset: " + i);
|
System.out.println("offset: " + i);
|
||||||
Util.writeInt(b, i, arr[idx++]);
|
Util.writeInt(b, i, arr[idx++]);
|
||||||
}
|
}
|
||||||
System.out.println("|----[Ok]");
|
System.out.println("|----[Ok]");
|
||||||
|
|
||||||
//read the int
|
//read the int
|
||||||
System.out.println("+--Testing getInt ... ");
|
System.out.println("+--Testing getInt ... ");
|
||||||
idx = 0;
|
idx = 0;
|
||||||
for ( i = 0; i < b.length; i += 4 )
|
for ( i = 0; i < b.length; i += 4 )
|
||||||
{
|
{
|
||||||
System.out.println(arr[idx++]+", " + Util.getInt(b, i));
|
System.out.println(arr[idx++]+", " + Util.getInt(b, i));
|
||||||
}
|
}
|
||||||
System.out.println("|----[Ok]");*/
|
System.out.println("|----[Ok]");*/
|
||||||
|
|
||||||
/* HeaderBlock headerBlock = new HeaderBlock(241658345, 2134785);
|
/* HeaderBlock headerBlock = new HeaderBlock(241658345, 2134785);
|
||||||
byte[] b = headerBlock.getBytes();
|
byte[] b = headerBlock.getBytes();
|
||||||
System.out.println(headerBlock.getIndexStartIp() + ", " + headerBlock.getIndexPtr());
|
System.out.println(headerBlock.getIndexStartIp() + ", " + headerBlock.getIndexPtr());
|
||||||
System.out.println(Util.getInt(b, 0) + ", " + Util.getInt(b, 4));*/
|
System.out.println(Util.getInt(b, 0) + ", " + Util.getInt(b, 4));*/
|
||||||
|
|
||||||
System.out.println(Util.ip2long("255.255.255.0"));
|
System.out.println(Util.ip2long("255.255.255.0"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,252 +2,252 @@
|
||||||
/**
|
/**
|
||||||
* ip2region php seacher client class
|
* ip2region php seacher client class
|
||||||
*
|
*
|
||||||
* @author chenxin<chenxin619315@gmail.com>
|
* @author chenxin<chenxin619315@gmail.com>
|
||||||
* @date 2015-10-29
|
* @date 2015-10-29
|
||||||
*/
|
*/
|
||||||
|
|
||||||
defined('INDEX_BLOCK_LENGTH') or define('INDEX_BLOCK_LENGTH', 12);
|
defined('INDEX_BLOCK_LENGTH') or define('INDEX_BLOCK_LENGTH', 12);
|
||||||
defined('TOTAL_HEADER_LENGTH') or define('TOTAL_HEADER_LENGTH', 4096);
|
defined('TOTAL_HEADER_LENGTH') or define('TOTAL_HEADER_LENGTH', 4096);
|
||||||
|
|
||||||
class Ip2Region
|
class Ip2Region
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* db file handler
|
* db file handler
|
||||||
*/
|
*/
|
||||||
private $dbFileHandler = NULL;
|
private $dbFileHandler = NULL;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* header block info
|
* header block info
|
||||||
*/
|
*/
|
||||||
private $HeaderSip = NULL;
|
private $HeaderSip = NULL;
|
||||||
private $HeaderPtr = NULL;
|
private $HeaderPtr = NULL;
|
||||||
private $headerLen = 0;
|
private $headerLen = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* super block index info
|
* super block index info
|
||||||
*/
|
*/
|
||||||
private $firstIndexPtr = 0;
|
private $firstIndexPtr = 0;
|
||||||
private $lastIndexPtr = 0;
|
private $lastIndexPtr = 0;
|
||||||
private $totalBlocks = 0;
|
private $totalBlocks = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* construct method
|
* construct method
|
||||||
*
|
*
|
||||||
* @param ip2regionFile
|
* @param ip2regionFile
|
||||||
*/
|
*/
|
||||||
public function __construct( $ip2regionFile )
|
public function __construct( $ip2regionFile )
|
||||||
{
|
{
|
||||||
$this->dbFileHandler = fopen($ip2regionFile, 'r');
|
$this->dbFileHandler = fopen($ip2regionFile, 'r');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get the data block throught the specifield ip address or long ip numeric with binary search algorithm
|
* get the data block throught the specifield ip address or long ip numeric with binary search algorithm
|
||||||
*
|
*
|
||||||
* @param ip
|
* @param ip
|
||||||
* @return mixed Array or NULL for any error
|
* @return mixed Array or NULL for any error
|
||||||
*/
|
*/
|
||||||
public function binarySearch( $ip )
|
public function binarySearch( $ip )
|
||||||
{
|
{
|
||||||
//check and conver the ip address
|
//check and conver the ip address
|
||||||
if ( is_string($ip) ) $ip = ip2long($ip);
|
if ( is_string($ip) ) $ip = ip2long($ip);
|
||||||
if ( $this->totalBlocks == 0 )
|
if ( $this->totalBlocks == 0 )
|
||||||
{
|
{
|
||||||
fseek($this->dbFileHandler, 0);
|
fseek($this->dbFileHandler, 0);
|
||||||
$superBlock = fread($this->dbFileHandler, 8);
|
$superBlock = fread($this->dbFileHandler, 8);
|
||||||
|
|
||||||
$this->firstIndexPtr = self::getLong($superBlock, 0);
|
$this->firstIndexPtr = self::getLong($superBlock, 0);
|
||||||
$this->lastIndexPtr = self::getLong($superBlock, 4);
|
$this->lastIndexPtr = self::getLong($superBlock, 4);
|
||||||
$this->totalBlocks = ($this->lastIndexPtr-$this->firstIndexPtr)/INDEX_BLOCK_LENGTH + 1;
|
$this->totalBlocks = ($this->lastIndexPtr-$this->firstIndexPtr)/INDEX_BLOCK_LENGTH + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
//binary search to define the data
|
//binary search to define the data
|
||||||
$l = 0;
|
$l = 0;
|
||||||
$h = $this->totalBlocks;
|
$h = $this->totalBlocks;
|
||||||
$dataPtr = 0;
|
$dataPtr = 0;
|
||||||
while ( $l <= $h )
|
while ( $l <= $h )
|
||||||
{
|
{
|
||||||
$m = (($l + $h) >> 1);
|
$m = (($l + $h) >> 1);
|
||||||
$p = $m * INDEX_BLOCK_LENGTH;
|
$p = $m * INDEX_BLOCK_LENGTH;
|
||||||
|
|
||||||
fseek($this->dbFileHandler, $this->firstIndexPtr + $p);
|
fseek($this->dbFileHandler, $this->firstIndexPtr + $p);
|
||||||
$buffer = fread($this->dbFileHandler, INDEX_BLOCK_LENGTH);
|
$buffer = fread($this->dbFileHandler, INDEX_BLOCK_LENGTH);
|
||||||
$sip = self::getLong($buffer, 0);
|
$sip = self::getLong($buffer, 0);
|
||||||
if ( $ip < $sip ) {
|
if ( $ip < $sip ) {
|
||||||
$h = $m - 1;
|
$h = $m - 1;
|
||||||
} else {
|
} else {
|
||||||
$eip = self::getLong($buffer, 4);
|
$eip = self::getLong($buffer, 4);
|
||||||
if ( $ip > $eip ) {
|
if ( $ip > $eip ) {
|
||||||
$l = $m + 1;
|
$l = $m + 1;
|
||||||
} else {
|
} else {
|
||||||
$dataPtr = self::getLong($buffer, 8);
|
$dataPtr = self::getLong($buffer, 8);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//not matched just stop it here
|
//not matched just stop it here
|
||||||
if ( $dataPtr == 0 ) return NULL;
|
if ( $dataPtr == 0 ) return NULL;
|
||||||
|
|
||||||
|
|
||||||
//get the data
|
//get the data
|
||||||
$dataLen = (($dataPtr >> 24) & 0xFF);
|
$dataLen = (($dataPtr >> 24) & 0xFF);
|
||||||
$dataPtr = ($dataPtr & 0x00FFFFFF);
|
$dataPtr = ($dataPtr & 0x00FFFFFF);
|
||||||
|
|
||||||
fseek($this->dbFileHandler, $dataPtr);
|
fseek($this->dbFileHandler, $dataPtr);
|
||||||
$data = fread($this->dbFileHandler, $dataLen);
|
$data = fread($this->dbFileHandler, $dataLen);
|
||||||
|
|
||||||
return array(
|
return array(
|
||||||
'city_id' => self::getLong($data, 0),
|
'city_id' => self::getLong($data, 0),
|
||||||
'region' => substr($data, 4)
|
'region' => substr($data, 4)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get the data block associated with the specifield ip with b-tree search algorithm
|
* get the data block associated with the specifield ip with b-tree search algorithm
|
||||||
*
|
*
|
||||||
* @param ip
|
* @param ip
|
||||||
* @return Mixed Array for NULL for any error
|
* @return Mixed Array for NULL for any error
|
||||||
*/
|
*/
|
||||||
public function btreeSearch( $ip )
|
public function btreeSearch( $ip )
|
||||||
{
|
{
|
||||||
if ( is_string($ip) ) $ip = ip2long($ip);
|
if ( is_string($ip) ) $ip = ip2long($ip);
|
||||||
|
|
||||||
//check and load the header
|
//check and load the header
|
||||||
if ( $this->HeaderSip == NULL )
|
if ( $this->HeaderSip == NULL )
|
||||||
{
|
{
|
||||||
fseek($this->dbFileHandler, 8);
|
fseek($this->dbFileHandler, 8);
|
||||||
$buffer = fread($this->dbFileHandler, TOTAL_HEADER_LENGTH);
|
$buffer = fread($this->dbFileHandler, TOTAL_HEADER_LENGTH);
|
||||||
|
|
||||||
//fill the header
|
//fill the header
|
||||||
$idx = 0;
|
$idx = 0;
|
||||||
$this->HeaderSip = array();
|
$this->HeaderSip = array();
|
||||||
$this->HeaderPtr = array();
|
$this->HeaderPtr = array();
|
||||||
for ( $i = 0; $i < TOTAL_HEADER_LENGTH; $i += 8 )
|
for ( $i = 0; $i < TOTAL_HEADER_LENGTH; $i += 8 )
|
||||||
{
|
{
|
||||||
$startIp = self::getLong($buffer, $i);
|
$startIp = self::getLong($buffer, $i);
|
||||||
$dataPtr = self::getLong($buffer, $i + 4);
|
$dataPtr = self::getLong($buffer, $i + 4);
|
||||||
if ( $dataPtr == 0 ) break;
|
if ( $dataPtr == 0 ) break;
|
||||||
|
|
||||||
$this->HeaderSip[] = $startIp;
|
$this->HeaderSip[] = $startIp;
|
||||||
$this->HeaderPtr[] = $dataPtr;
|
$this->HeaderPtr[] = $dataPtr;
|
||||||
$idx++;
|
$idx++;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->headerLen = $idx;
|
$this->headerLen = $idx;
|
||||||
}
|
}
|
||||||
|
|
||||||
//1. define the index block with the binary search
|
//1. define the index block with the binary search
|
||||||
$l = 0; $h = $this->headerLen; $sptr = 0; $eptr = 0;
|
$l = 0; $h = $this->headerLen; $sptr = 0; $eptr = 0;
|
||||||
while ( $l <= $h )
|
while ( $l <= $h )
|
||||||
{
|
{
|
||||||
$m = (($l + $h) >> 1);
|
$m = (($l + $h) >> 1);
|
||||||
|
|
||||||
//perfetc matched, just return it
|
//perfetc matched, just return it
|
||||||
if ( $ip == $this->HeaderSip[$m] ) {
|
if ( $ip == $this->HeaderSip[$m] ) {
|
||||||
if ( $m > 0 ) {
|
if ( $m > 0 ) {
|
||||||
$sptr = $this->HeaderPtr[$m-1];
|
$sptr = $this->HeaderPtr[$m-1];
|
||||||
$eptr = $this->HeaderPtr[$m ];
|
$eptr = $this->HeaderPtr[$m ];
|
||||||
} else {
|
} else {
|
||||||
$sptr = $this->HeaderPtr[$m ];
|
$sptr = $this->HeaderPtr[$m ];
|
||||||
$eptr = $this->HeaderPtr[$m+1];
|
$eptr = $this->HeaderPtr[$m+1];
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
//less then the middle value
|
//less then the middle value
|
||||||
if ( $ip < $this->HeaderSip[$m] ) {
|
if ( $ip < $this->HeaderSip[$m] ) {
|
||||||
if ( $m == 0 ) {
|
if ( $m == 0 ) {
|
||||||
$sptr = $this->HeaderPtr[$m ];
|
$sptr = $this->HeaderPtr[$m ];
|
||||||
$eptr = $this->HeaderPtr[$m+1];
|
$eptr = $this->HeaderPtr[$m+1];
|
||||||
break;
|
break;
|
||||||
} else if ( $ip > $this->HeaderSip[$m-1] ) {
|
} else if ( $ip > $this->HeaderSip[$m-1] ) {
|
||||||
$sptr = $this->HeaderPtr[$m-1];
|
$sptr = $this->HeaderPtr[$m-1];
|
||||||
$eptr = $this->HeaderPtr[$m ];
|
$eptr = $this->HeaderPtr[$m ];
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
$h = $m - 1;
|
$h = $m - 1;
|
||||||
} else {
|
} else {
|
||||||
if ( $m == $this->headerLen - 1 ) {
|
if ( $m == $this->headerLen - 1 ) {
|
||||||
$sptr = $this->HeaderPtr[$m-1];
|
$sptr = $this->HeaderPtr[$m-1];
|
||||||
$eptr = $this->HeaderPtr[$m ];
|
$eptr = $this->HeaderPtr[$m ];
|
||||||
break;
|
break;
|
||||||
} else if ( $ip <= $this->HeaderSip[$m+1] ) {
|
} else if ( $ip <= $this->HeaderSip[$m+1] ) {
|
||||||
$sptr = $this->HeaderPtr[$m ];
|
$sptr = $this->HeaderPtr[$m ];
|
||||||
$eptr = $this->HeaderPtr[$m+1];
|
$eptr = $this->HeaderPtr[$m+1];
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
$l = $m + 1;
|
$l = $m + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//match nothing just stop it
|
//match nothing just stop it
|
||||||
if ( $sptr == 0 ) return NULL;
|
if ( $sptr == 0 ) return NULL;
|
||||||
|
|
||||||
//2. search the index blocks to define the data
|
//2. search the index blocks to define the data
|
||||||
$blockLen = $eptr - $sptr;
|
$blockLen = $eptr - $sptr;
|
||||||
fseek($this->dbFileHandler, $sptr);
|
fseek($this->dbFileHandler, $sptr);
|
||||||
$index = fread($this->dbFileHandler, $blockLen + INDEX_BLOCK_LENGTH);
|
$index = fread($this->dbFileHandler, $blockLen + INDEX_BLOCK_LENGTH);
|
||||||
|
|
||||||
$dataptr = 0;
|
$dataptr = 0;
|
||||||
$l = 0; $h = $blockLen / INDEX_BLOCK_LENGTH;
|
$l = 0; $h = $blockLen / INDEX_BLOCK_LENGTH;
|
||||||
while ( $l <= $h ) {
|
while ( $l <= $h ) {
|
||||||
$m = (($l + $h) >> 1);
|
$m = (($l + $h) >> 1);
|
||||||
$p = (int)($m * INDEX_BLOCK_LENGTH);
|
$p = (int)($m * INDEX_BLOCK_LENGTH);
|
||||||
$sip = self::getLong($index, $p);
|
$sip = self::getLong($index, $p);
|
||||||
if ( $ip < $sip ) {
|
if ( $ip < $sip ) {
|
||||||
$h = $m - 1;
|
$h = $m - 1;
|
||||||
} else {
|
} else {
|
||||||
$eip = self::getLong($index, $p + 4);
|
$eip = self::getLong($index, $p + 4);
|
||||||
if ( $ip > $eip ) {
|
if ( $ip > $eip ) {
|
||||||
$l = $m + 1;
|
$l = $m + 1;
|
||||||
} else {
|
} else {
|
||||||
$dataptr = self::getLong($index, $p + 8);
|
$dataptr = self::getLong($index, $p + 8);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//not matched
|
//not matched
|
||||||
if ( $dataptr == 0 ) return NULL;
|
if ( $dataptr == 0 ) return NULL;
|
||||||
|
|
||||||
//3. get the data
|
//3. get the data
|
||||||
$dataLen = (($dataptr >> 24) & 0xFF);
|
$dataLen = (($dataptr >> 24) & 0xFF);
|
||||||
$dataPtr = ($dataptr & 0x00FFFFFF);
|
$dataPtr = ($dataptr & 0x00FFFFFF);
|
||||||
|
|
||||||
fseek($this->dbFileHandler, $dataPtr);
|
fseek($this->dbFileHandler, $dataPtr);
|
||||||
$data = fread($this->dbFileHandler, $dataLen);
|
$data = fread($this->dbFileHandler, $dataLen);
|
||||||
|
|
||||||
return array(
|
return array(
|
||||||
'city_id' => self::getLong($data, 0),
|
'city_id' => self::getLong($data, 0),
|
||||||
'region' => substr($data, 4)
|
'region' => substr($data, 4)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* read a long from a byte buffer
|
* read a long from a byte buffer
|
||||||
*
|
*
|
||||||
* @param b
|
* @param b
|
||||||
* @param offset
|
* @param offset
|
||||||
*/
|
*/
|
||||||
public static function getLong( $b, $offset )
|
public static function getLong( $b, $offset )
|
||||||
{
|
{
|
||||||
return (
|
return (
|
||||||
(ord($b[$offset++])) |
|
(ord($b[$offset++])) |
|
||||||
(ord($b[$offset++]) << 8) |
|
(ord($b[$offset++]) << 8) |
|
||||||
(ord($b[$offset++]) << 16) |
|
(ord($b[$offset++]) << 16) |
|
||||||
(ord($b[$offset ]) << 24)
|
(ord($b[$offset ]) << 24)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* destruct method, resource destroy
|
* destruct method, resource destroy
|
||||||
*/
|
*/
|
||||||
public function __destruct()
|
public function __destruct()
|
||||||
{
|
{
|
||||||
if ( $this->dbFileHandler != NULL ) fclose($this->dbFileHandler);
|
if ( $this->dbFileHandler != NULL ) fclose($this->dbFileHandler);
|
||||||
$this->HeaderSip = NULL;
|
$this->HeaderSip = NULL;
|
||||||
$this->HeaderPtr = NULL;
|
$this->HeaderPtr = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
|
|
||||||
|
|
@ -2,26 +2,26 @@
|
||||||
/**
|
/**
|
||||||
* Ip2Region php client test script
|
* Ip2Region php client test script
|
||||||
*
|
*
|
||||||
* @author chenxin<chenxin619315@gmail.com>
|
* @author chenxin<chenxin619315@gmail.com>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
if ( $argc < 2 )
|
if ( $argc < 2 )
|
||||||
{
|
{
|
||||||
$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
|
||||||
EOF;
|
EOF;
|
||||||
exit($usage);
|
exit($usage);
|
||||||
}
|
}
|
||||||
|
|
||||||
array_shift($argv);
|
array_shift($argv);
|
||||||
$dbFile = $argv[0];
|
$dbFile = $argv[0];
|
||||||
$method = 1;
|
$method = 1;
|
||||||
$algorithm = 'B-tree';
|
$algorithm = 'B-tree';
|
||||||
if ( isset($argv[1])
|
if ( isset($argv[1])
|
||||||
&& strtolower($argv[1]) == 'binary' ) {
|
&& strtolower($argv[1]) == 'binary' ) {
|
||||||
$method = 2;
|
$method = 2;
|
||||||
$algorithm = 'Binary';
|
$algorithm = 'Binary';
|
||||||
}
|
}
|
||||||
|
|
||||||
require dirname(__FILE__) . '/Ip2Region.class.php';
|
require dirname(__FILE__) . '/Ip2Region.class.php';
|
||||||
|
|
@ -39,23 +39,23 @@ echo $initStr, "\n";
|
||||||
|
|
||||||
while ( true )
|
while ( true )
|
||||||
{
|
{
|
||||||
echo "ip2region>> ";
|
echo "ip2region>> ";
|
||||||
$line = trim(fgets(STDIN));
|
$line = trim(fgets(STDIN));
|
||||||
if ( strlen($line) < 2 ) continue;
|
if ( strlen($line) < 2 ) continue;
|
||||||
if ( $line == 'quit' ) break;
|
if ( $line == 'quit' ) break;
|
||||||
if ( ip2long($line) == NULL ) {
|
if ( ip2long($line) == NULL ) {
|
||||||
echo "Error: invalid ip address\n";
|
echo "Error: invalid ip address\n";
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$s_time = getTime();
|
$s_time = getTime();
|
||||||
$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);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getTime()
|
function getTime()
|
||||||
{
|
{
|
||||||
return (microtime(true) * 1000);
|
return (microtime(true) * 1000);
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
|
|
||||||
|
|
@ -8,185 +8,185 @@
|
||||||
import struct, io, socket, sys
|
import struct, io, socket, sys
|
||||||
|
|
||||||
class Ip2Region(object):
|
class Ip2Region(object):
|
||||||
__headerSip = []
|
__headerSip = []
|
||||||
__headerPtr = []
|
__headerPtr = []
|
||||||
__f = None
|
__f = None
|
||||||
|
|
||||||
def __init__(self, dbfile):
|
def __init__(self, dbfile):
|
||||||
self.initDatabase(dbfile)
|
self.initDatabase(dbfile)
|
||||||
|
|
||||||
def binarySearch(self, ip):
|
def binarySearch(self, ip):
|
||||||
"""
|
"""
|
||||||
" binary search method
|
" binary search method
|
||||||
" param: ip
|
" param: ip
|
||||||
"""
|
"""
|
||||||
if not ip.isdigit(): ip = self.ip2long(ip)
|
if not ip.isdigit(): ip = self.ip2long(ip)
|
||||||
|
|
||||||
self.__f.seek(0)
|
self.__f.seek(0)
|
||||||
b = self.__f.read(8)
|
b = self.__f.read(8)
|
||||||
startPtr = self.getLong(b, 0)
|
startPtr = self.getLong(b, 0)
|
||||||
endPtr = self.getLong(b, 4)
|
endPtr = self.getLong(b, 4)
|
||||||
|
|
||||||
indexLen = endPtr - startPtr
|
indexLen = endPtr - startPtr
|
||||||
self.__f.seek(startPtr)
|
self.__f.seek(startPtr)
|
||||||
b = self.__f.read(indexLen+12)
|
b = self.__f.read(indexLen+12)
|
||||||
|
|
||||||
l, h, mixPtr = (0, int(indexLen/12), 0)
|
l, h, mixPtr = (0, int(indexLen/12), 0)
|
||||||
while l <= h:
|
while l <= h:
|
||||||
m = int((l+h)/2)
|
m = int((l+h)/2)
|
||||||
ptr = startPtr + m*12
|
ptr = startPtr + m*12
|
||||||
self.__f.seek(ptr)
|
self.__f.seek(ptr)
|
||||||
|
|
||||||
b = self.__f.read(12)
|
b = self.__f.read(12)
|
||||||
sip = self.getLong(b, 0)
|
sip = self.getLong(b, 0)
|
||||||
eip = self.getLong(b, 4)
|
eip = self.getLong(b, 4)
|
||||||
|
|
||||||
if ip > sip:
|
if ip > sip:
|
||||||
if ip > eip:
|
if ip > eip:
|
||||||
l = m + 1
|
l = m + 1
|
||||||
else:
|
else:
|
||||||
mixPtr = self.getLong(b, 8)
|
mixPtr = self.getLong(b, 8)
|
||||||
break;
|
break;
|
||||||
else:
|
else:
|
||||||
h = m - 1
|
h = m - 1
|
||||||
|
|
||||||
if mixPtr == 0: return "N2"
|
if mixPtr == 0: return "N2"
|
||||||
|
|
||||||
dataPtr = mixPtr & 0x00FFFFFFL
|
dataPtr = mixPtr & 0x00FFFFFFL
|
||||||
dataLen = (mixPtr >> 24) & 0xFF
|
dataLen = (mixPtr >> 24) & 0xFF
|
||||||
|
|
||||||
self.__f.seek(dataPtr)
|
self.__f.seek(dataPtr)
|
||||||
data = self.__f.read(dataLen)
|
data = self.__f.read(dataLen)
|
||||||
return {
|
return {
|
||||||
"city_id": self.getLong(data, 0),
|
"city_id": self.getLong(data, 0),
|
||||||
"region" : data[4:]
|
"region" : data[4:]
|
||||||
}
|
}
|
||||||
|
|
||||||
def btreeSearch(self, ip):
|
def btreeSearch(self, ip):
|
||||||
"""
|
"""
|
||||||
" b-tree search method
|
" b-tree search method
|
||||||
" param: ip
|
" param: ip
|
||||||
"""
|
"""
|
||||||
if not ip.isdigit(): ip = self.ip2long(ip)
|
if not ip.isdigit(): ip = self.ip2long(ip)
|
||||||
|
|
||||||
headerLen = len(self.__headerSip) - 1
|
headerLen = len(self.__headerSip) - 1
|
||||||
l, h, sptr, eptr = (0, headerLen, 0, 0)
|
l, h, sptr, eptr = (0, headerLen, 0, 0)
|
||||||
while l <= h:
|
while l <= h:
|
||||||
m = int((l+h)/2)
|
m = int((l+h)/2)
|
||||||
|
|
||||||
if ip == self.__headerSip[m]:
|
if ip == self.__headerSip[m]:
|
||||||
if m > 0:
|
if m > 0:
|
||||||
sptr = self.__headerPtr[m-1]
|
sptr = self.__headerPtr[m-1]
|
||||||
eptr = self.__headerPtr[m]
|
eptr = self.__headerPtr[m]
|
||||||
break;
|
break;
|
||||||
else:
|
else:
|
||||||
sptr = self.__headerPtr[m]
|
sptr = self.__headerPtr[m]
|
||||||
eptr = self.__headerPtr[m+1]
|
eptr = self.__headerPtr[m+1]
|
||||||
break;
|
break;
|
||||||
|
|
||||||
if ip > self.__headerSip[m]:
|
if ip > self.__headerSip[m]:
|
||||||
if m == headerLen:
|
if m == headerLen:
|
||||||
sptr = self.__headerPtr[m-1]
|
sptr = self.__headerPtr[m-1]
|
||||||
eptr = self.__headerPtr[m]
|
eptr = self.__headerPtr[m]
|
||||||
break;
|
break;
|
||||||
elif ip < self.__headerSip[m+1]:
|
elif ip < self.__headerSip[m+1]:
|
||||||
sptr = self.__headerPtr[m]
|
sptr = self.__headerPtr[m]
|
||||||
eptr = self.__headerPtr[m+1]
|
eptr = self.__headerPtr[m+1]
|
||||||
break;
|
break;
|
||||||
|
|
||||||
l = m + 1
|
l = m + 1
|
||||||
else:
|
else:
|
||||||
if m == 0:
|
if m == 0:
|
||||||
sptr = self.__headerPtr[m]
|
sptr = self.__headerPtr[m]
|
||||||
eptr = self.__headerPtr[m+1]
|
eptr = self.__headerPtr[m+1]
|
||||||
break;
|
break;
|
||||||
elif ip > self.__headerSip[m-1]:
|
elif ip > self.__headerSip[m-1]:
|
||||||
sptr = self.__headerPtr[m-1]
|
sptr = self.__headerPtr[m-1]
|
||||||
eptr = self.__headerPtr[m]
|
eptr = self.__headerPtr[m]
|
||||||
break;
|
break;
|
||||||
|
|
||||||
h = m - 1
|
h = m - 1
|
||||||
|
|
||||||
if sptr == 0: return "N1"
|
if sptr == 0: return "N1"
|
||||||
|
|
||||||
indexLen = eptr - sptr
|
indexLen = eptr - sptr
|
||||||
self.__f.seek(sptr)
|
self.__f.seek(sptr)
|
||||||
b = self.__f.read(indexLen + 12)
|
b = self.__f.read(indexLen + 12)
|
||||||
|
|
||||||
l, h, mixPtr = (0, int(indexLen/12), 0)
|
l, h, mixPtr = (0, int(indexLen/12), 0)
|
||||||
while l <= h:
|
while l <= h:
|
||||||
m = int((l+h)/2)
|
m = int((l+h)/2)
|
||||||
offset = m * 12
|
offset = m * 12
|
||||||
|
|
||||||
if ip > self.getLong(b, offset):
|
if ip > self.getLong(b, offset):
|
||||||
if ip > self.getLong(b, offset+4):
|
if ip > self.getLong(b, offset+4):
|
||||||
l = m + 1
|
l = m + 1
|
||||||
else:
|
else:
|
||||||
mixPtr = self.getLong(b, offset+8)
|
mixPtr = self.getLong(b, offset+8)
|
||||||
break;
|
break;
|
||||||
else:
|
else:
|
||||||
h = m - 1
|
h = m - 1
|
||||||
|
|
||||||
if mixPtr == 0: return "N2"
|
if mixPtr == 0: return "N2"
|
||||||
|
|
||||||
dataPtr = mixPtr & 0x00FFFFFFL
|
dataPtr = mixPtr & 0x00FFFFFFL
|
||||||
dataLen = (mixPtr >> 24) & 0xFF
|
dataLen = (mixPtr >> 24) & 0xFF
|
||||||
|
|
||||||
self.__f.seek(dataPtr)
|
self.__f.seek(dataPtr)
|
||||||
data = self.__f.read(dataLen)
|
data = self.__f.read(dataLen)
|
||||||
return {
|
return {
|
||||||
"city_id": self.getLong(data, 0),
|
"city_id": self.getLong(data, 0),
|
||||||
"region" : data[4:]
|
"region" : data[4:]
|
||||||
}
|
}
|
||||||
|
|
||||||
def initDatabase(self, dbfile):
|
def initDatabase(self, dbfile):
|
||||||
"""
|
"""
|
||||||
" initialize the database for search
|
" initialize the database for search
|
||||||
" param: dbFile
|
" param: dbFile
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
self.__f = io.open(dbfile, "rb")
|
self.__f = io.open(dbfile, "rb")
|
||||||
#pass the super block
|
#pass the super block
|
||||||
self.__f.seek(8)
|
self.__f.seek(8)
|
||||||
#read the header block
|
#read the header block
|
||||||
b = self.__f.read(4086)
|
b = self.__f.read(4086)
|
||||||
#parse the header block
|
#parse the header block
|
||||||
sip = None
|
sip = None
|
||||||
ptr = None
|
ptr = None
|
||||||
for i in range(0, len(b)-1, 8):
|
for i in range(0, len(b)-1, 8):
|
||||||
sip = self.getLong(b, i)
|
sip = self.getLong(b, i)
|
||||||
ptr = self.getLong(b, i+4)
|
ptr = self.getLong(b, i+4)
|
||||||
if ptr == 0:
|
if ptr == 0:
|
||||||
break
|
break
|
||||||
self.__headerSip.append(sip)
|
self.__headerSip.append(sip)
|
||||||
self.__headerPtr.append(ptr)
|
self.__headerPtr.append(ptr)
|
||||||
|
|
||||||
except IOError, e:
|
except IOError, e:
|
||||||
print "[Error]: ", e
|
print "[Error]: ", e
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
def ip2long(self, ip):
|
def ip2long(self, ip):
|
||||||
_ip = socket.inet_aton(ip)
|
_ip = socket.inet_aton(ip)
|
||||||
return struct.unpack("!L", _ip)[0]
|
return struct.unpack("!L", _ip)[0]
|
||||||
|
|
||||||
def isip(self, ip):
|
def isip(self, ip):
|
||||||
p = ip.split(".")
|
p = ip.split(".")
|
||||||
|
|
||||||
if len(p) != 4 : return False
|
if len(p) != 4 : return False
|
||||||
for pp in p:
|
for pp in p:
|
||||||
if not pp.isdigit(): return False
|
if not pp.isdigit(): return False
|
||||||
if len(pp) > 3 : return False
|
if len(pp) > 3 : return False
|
||||||
if int(pp) > 255 : return False
|
if int(pp) > 255 : return False
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def getLong(self, b, offset):
|
def getLong(self, b, offset):
|
||||||
if len( b[offset:offset+4] ) == 4:
|
if len( b[offset:offset+4] ) == 4:
|
||||||
return struct.unpack('I', b[offset:offset+4])[0]
|
return struct.unpack('I', b[offset:offset+4])[0]
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
self.__headerSip = None
|
self.__headerSip = None
|
||||||
self.__headerPtr = None
|
self.__headerPtr = None
|
||||||
self.__f.close()
|
self.__f.close()
|
||||||
self.__f = None
|
self.__f = None
|
||||||
|
|
|
||||||
Binary file not shown.
|
|
@ -9,67 +9,67 @@ import struct, sys, os, time
|
||||||
from ip2Region import Ip2Region
|
from ip2Region import Ip2Region
|
||||||
|
|
||||||
def testSearch():
|
def testSearch():
|
||||||
"""
|
"""
|
||||||
" ip2region test function
|
" ip2region test function
|
||||||
"""
|
"""
|
||||||
llen = len(sys.argv)
|
llen = len(sys.argv)
|
||||||
|
|
||||||
if llen < 2:
|
if llen < 2:
|
||||||
print "Usage: python ip2Region.py [ip2region db file] [alrogrithm]"
|
print "Usage: python ip2Region.py [ip2region db file] [alrogrithm]"
|
||||||
print "Algorithm: binary or b-tree"
|
print "Algorithm: binary or b-tree"
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
dbFile = sys.argv[1]
|
dbFile = sys.argv[1]
|
||||||
method = 1
|
method = 1
|
||||||
algorithm = "b-tree"
|
algorithm = "b-tree"
|
||||||
|
|
||||||
if (not os.path.isfile(dbFile)) or (not os.path.exists(dbFile)):
|
if (not os.path.isfile(dbFile)) or (not os.path.exists(dbFile)):
|
||||||
print "[Error]: Specified db file is not exists."
|
print "[Error]: Specified db file is not exists."
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
if llen > 2:
|
if llen > 2:
|
||||||
algorithm = sys.argv[2]
|
algorithm = sys.argv[2]
|
||||||
if algorithm == "binary":
|
if algorithm == "binary":
|
||||||
method = 2
|
method = 2
|
||||||
|
|
||||||
print "initializing %s..." % (algorithm)
|
print "initializing %s..." % (algorithm)
|
||||||
print "+----------------------------------+"
|
print "+----------------------------------+"
|
||||||
print "| ip2region test script |"
|
print "| ip2region test script |"
|
||||||
print "| Author: komazhang@foxmail.com |"
|
print "| Author: komazhang@foxmail.com |"
|
||||||
print "| Type 'quit' to exit program |"
|
print "| Type 'quit' to exit program |"
|
||||||
print "+----------------------------------+"
|
print "+----------------------------------+"
|
||||||
|
|
||||||
searcher = Ip2Region(dbFile);
|
searcher = Ip2Region(dbFile);
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
line = raw_input("ip2region>> ")
|
line = raw_input("ip2region>> ")
|
||||||
line = line.strip()
|
line = line.strip()
|
||||||
|
|
||||||
if line == "":
|
if line == "":
|
||||||
print "[Error]: Invalid ip address."
|
print "[Error]: Invalid ip address."
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if line == "quit":
|
if line == "quit":
|
||||||
print "[Info]: Thanks for your use, Bye."
|
print "[Info]: Thanks for your use, Bye."
|
||||||
break
|
break
|
||||||
|
|
||||||
if not searcher.isip(line):
|
if not searcher.isip(line):
|
||||||
print "[Error]: Invalid ip address."
|
print "[Error]: Invalid ip address."
|
||||||
continue
|
continue
|
||||||
|
|
||||||
sTime = time.time() * 1000
|
sTime = time.time() * 1000
|
||||||
if method == 1:
|
if method == 1:
|
||||||
data = searcher.btreeSearch(line)
|
data = searcher.btreeSearch(line)
|
||||||
else:
|
else:
|
||||||
data = searcher.binarySearch(line)
|
data = searcher.binarySearch(line)
|
||||||
eTime = time.time() * 1000
|
eTime = time.time() * 1000
|
||||||
|
|
||||||
if isinstance(data, dict):
|
if isinstance(data, dict):
|
||||||
print "[Return]: %s|%s in %f millseconds" % (data["city_id"], data["region"], eTime-sTime)
|
print "[Return]: %s|%s in %f millseconds" % (data["city_id"], data["region"], eTime-sTime)
|
||||||
else:
|
else:
|
||||||
print "[Error]: ", data
|
print "[Error]: ", data
|
||||||
|
|
||||||
searcher.close()
|
searcher.close()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
testSearch()
|
testSearch()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue