From 41617aac4d9f4ddbaf63c71425fcb619f64209fc Mon Sep 17 00:00:00 2001 From: chenxin Date: Mon, 7 Dec 2015 11:27:14 +0800 Subject: [PATCH] code tab to 4 space --- binding/c/ip2region.c | 482 +++++++------- binding/c/testSearcher.c | 124 ++-- .../src/org/lionsoul/ip2region/DataBlock.java | 78 +-- .../src/org/lionsoul/ip2region/DbConfig.java | 90 +-- .../ip2region/DbMakerConfigException.java | 26 +- .../org/lionsoul/ip2region/DbSearcher.java | 602 +++++++++--------- .../org/lionsoul/ip2region/HeaderBlock.java | 100 +-- .../org/lionsoul/ip2region/IndexBlock.java | 172 ++--- .../java/src/org/lionsoul/ip2region/Util.java | 270 ++++---- .../lionsoul/ip2region/test/TestSearcher.java | 122 ++-- .../org/lionsoul/ip2region/test/TestUnit.java | 146 ++--- .../org/lionsoul/ip2region/test/TestUtil.java | 90 +-- binding/php/Ip2Region.class.php | 448 ++++++------- binding/php/testSeacher.php | 44 +- binding/python/ip2Region.py | 308 ++++----- binding/python/ip2Region.pyc | Bin 0 -> 4951 bytes binding/python/testSearcher.py | 100 +-- 17 files changed, 1601 insertions(+), 1601 deletions(-) create mode 100644 binding/python/ip2Region.pyc diff --git a/binding/c/ip2region.c b/binding/c/ip2region.c index 67931b3..cc16d77 100644 --- a/binding/c/ip2region.c +++ b/binding/c/ip2region.c @@ -1,9 +1,9 @@ /** * default ip2region implementation * - * @see #ip2region.h - * @author chenxin - * @date 2015-10-30 + * @see #ip2region.h + * @author chenxin + * @date 2015-10-30 */ #include "ip2region.h" @@ -13,337 +13,337 @@ /** * create a new ip2region object * - * @param dbFile path + * @param dbFile path */ IP2R_API uint_t ip2region_create(ip2region_t ip2rObj, char *dbFile) { - memset(ip2rObj, 0x00, sizeof(ip2region_entry)); - ip2rObj->headerLen = 0; - ip2rObj->HeaderSip = (uint_t *) IP2R_MALLOC(TOTAL_HEADER_LENGTH); - if ( ip2rObj->HeaderSip == NULL ) - { - return 0; - } + memset(ip2rObj, 0x00, sizeof(ip2region_entry)); + ip2rObj->headerLen = 0; + ip2rObj->HeaderSip = (uint_t *) IP2R_MALLOC(TOTAL_HEADER_LENGTH); + if ( ip2rObj->HeaderSip == NULL ) + { + return 0; + } - ip2rObj->HeaderPtr = (uint_t *) IP2R_MALLOC(TOTAL_HEADER_LENGTH); - if ( ip2rObj->HeaderPtr == NULL ) - { - IP2R_FREE(ip2rObj->HeaderSip); - return 0; - } + ip2rObj->HeaderPtr = (uint_t *) IP2R_MALLOC(TOTAL_HEADER_LENGTH); + if ( ip2rObj->HeaderPtr == NULL ) + { + IP2R_FREE(ip2rObj->HeaderSip); + return 0; + } - //open the db file - ip2rObj->dbHandler = fopen(dbFile, "rb"); - if ( ip2rObj->dbHandler == NULL ) - { - IP2R_FREE(ip2rObj->HeaderSip); - IP2R_FREE(ip2rObj->HeaderPtr); - return 0; - } + //open the db file + ip2rObj->dbHandler = fopen(dbFile, "rb"); + if ( ip2rObj->dbHandler == NULL ) + { + IP2R_FREE(ip2rObj->HeaderSip); + IP2R_FREE(ip2rObj->HeaderPtr); + return 0; + } - ip2rObj->firstIndexPtr = 0; - ip2rObj->lastIndexPtr = 0; - ip2rObj->totalBlocks = 0; + ip2rObj->firstIndexPtr = 0; + ip2rObj->lastIndexPtr = 0; + ip2rObj->totalBlocks = 0; - return 1; + return 1; } /** * destroy the specifield ip2region object * - * @param ip2region_t + * @param ip2region_t */ IP2R_API uint_t ip2region_destroy(ip2region_t ip2rObj) { - IP2R_FREE(ip2rObj->HeaderSip); - ip2rObj->HeaderSip = NULL; - IP2R_FREE(ip2rObj->HeaderPtr); - ip2rObj->HeaderPtr = NULL; + IP2R_FREE(ip2rObj->HeaderSip); + ip2rObj->HeaderSip = NULL; + IP2R_FREE(ip2rObj->HeaderPtr); + ip2rObj->HeaderPtr = NULL; - //close the db file resource - if ( ip2rObj->dbHandler != NULL ) - { - fclose(ip2rObj->dbHandler); - ip2rObj->dbHandler = NULL; - } + //close the db file resource + if ( ip2rObj->dbHandler != NULL ) + { + fclose(ip2rObj->dbHandler); + ip2rObj->dbHandler = NULL; + } - return 1; + return 1; } /** * get the region associated with the specifield ip address with binary search algorithm * - * @param ip2rObj - * @param ip - * @param datablock - * @return uint_t + * @param ip2rObj + * @param ip + * @param datablock + * @return uint_t */ IP2R_API uint_t ip2region_binary_search(ip2region_t ip2rObj, uint_t ip, datablock_t datablock) { - int l, h, m, p; - uint_t sip, eip, dptr; - char buffer[256]; - int dataLen, dataptr; + int l, h, m, p; + uint_t sip, eip, dptr; + char buffer[256]; + int dataLen, dataptr; - if ( ip2rObj->totalBlocks == 0 ) - { - fseek(ip2rObj->dbHandler, 0, 0); - if ( fread(buffer, 8, 1, ip2rObj->dbHandler) != 1 ) { - return 0; - } + if ( ip2rObj->totalBlocks == 0 ) + { + fseek(ip2rObj->dbHandler, 0, 0); + if ( fread(buffer, 8, 1, ip2rObj->dbHandler) != 1 ) { + return 0; + } - ip2rObj->firstIndexPtr = getUnsignedInt(buffer, 0); - ip2rObj->lastIndexPtr = getUnsignedInt(buffer, 4); - ip2rObj->totalBlocks = (ip2rObj->lastIndexPtr-ip2rObj->firstIndexPtr)/INDEX_BLOCK_LENGTH + 1; - } + ip2rObj->firstIndexPtr = getUnsignedInt(buffer, 0); + ip2rObj->lastIndexPtr = getUnsignedInt(buffer, 4); + ip2rObj->totalBlocks = (ip2rObj->lastIndexPtr-ip2rObj->firstIndexPtr)/INDEX_BLOCK_LENGTH + 1; + } - //binary search the index blocks to define the data block - l = 0; h = ip2rObj->totalBlocks; dptr = 0; - while ( l <= h ) { - m = (l + h) >> 1; - p = ip2rObj->firstIndexPtr + m * INDEX_BLOCK_LENGTH; + //binary search the index blocks to define the data block + l = 0; h = ip2rObj->totalBlocks; dptr = 0; + while ( l <= h ) { + m = (l + h) >> 1; + p = ip2rObj->firstIndexPtr + m * INDEX_BLOCK_LENGTH; - fseek(ip2rObj->dbHandler, p, 0); - if ( fread(buffer, INDEX_BLOCK_LENGTH, 1, ip2rObj->dbHandler) != 1 ) { - return 0; - } - sip = getUnsignedInt(buffer, 0); - if ( ip < sip ) { - h = m - 1; - } else { - eip = getUnsignedInt(buffer, 4); - if ( ip > eip ) { - l = m + 1; - } else { - dptr = getUnsignedInt(buffer, 8); - break; - } - } - } + fseek(ip2rObj->dbHandler, p, 0); + if ( fread(buffer, INDEX_BLOCK_LENGTH, 1, ip2rObj->dbHandler) != 1 ) { + return 0; + } + sip = getUnsignedInt(buffer, 0); + if ( ip < sip ) { + h = m - 1; + } else { + eip = getUnsignedInt(buffer, 4); + if ( ip > eip ) { + l = m + 1; + } else { + dptr = getUnsignedInt(buffer, 8); + break; + } + } + } - if ( dptr == 0 ) return 0; + if ( dptr == 0 ) return 0; - //get the data - dataLen = ((dptr >> 24) & 0xFF); - dataptr = (dptr & 0x00FFFFFF); + //get the data + dataLen = ((dptr >> 24) & 0xFF); + dataptr = (dptr & 0x00FFFFFF); - //memset(data, 0x00, sizeof(data)); - fseek(ip2rObj->dbHandler, dataptr, 0); - if ( fread(buffer, dataLen, 1, ip2rObj->dbHandler) != 1 ) { - return 0; - } + //memset(data, 0x00, sizeof(data)); + fseek(ip2rObj->dbHandler, dataptr, 0); + if ( fread(buffer, dataLen, 1, ip2rObj->dbHandler) != 1 ) { + return 0; + } - //fill the data to the datablock - datablock->city_id = getUnsignedInt(buffer, 0); - dataLen -= 4; //reduce the length of the city_id - memcpy(datablock->region, buffer + 4, dataLen); - datablock->region[dataLen] = '\0'; + //fill the data to the datablock + datablock->city_id = getUnsignedInt(buffer, 0); + dataLen -= 4; //reduce the length of the city_id + memcpy(datablock->region, buffer + 4, dataLen); + datablock->region[dataLen] = '\0'; - return 1; + return 1; } 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 * - * @param ip2rObj - * @param ip - * @param datablock - * @return uint_t + * @param ip2rObj + * @param ip + * @param datablock + * @return uint_t */ IP2R_API uint_t ip2region_btree_search(ip2region_t ip2rObj, uint_t ip, datablock_t datablock) { - int i, idx; - int l, m, h, p, sptr, eptr, indexBlockLen, dataLen, dataptr; - uint_t sip, eip, idxptr, dptr; - char buffer[TOTAL_HEADER_LENGTH]; + int i, idx; + int l, m, h, p, sptr, eptr, indexBlockLen, dataLen, dataptr; + uint_t sip, eip, idxptr, dptr; + char buffer[TOTAL_HEADER_LENGTH]; - if ( ip2rObj->headerLen == 0 ) - { - idx = 0; - fseek(ip2rObj->dbHandler, 8, 0); //pass the super block - if ( fread(buffer, TOTAL_HEADER_LENGTH, 1, ip2rObj->dbHandler) != 1 ) { - return 0; - } + if ( ip2rObj->headerLen == 0 ) + { + idx = 0; + fseek(ip2rObj->dbHandler, 8, 0); //pass the super block + if ( fread(buffer, TOTAL_HEADER_LENGTH, 1, ip2rObj->dbHandler) != 1 ) { + return 0; + } - for ( i = 0; i < TOTAL_HEADER_LENGTH; i += 8 ) - { - sip = getUnsignedInt(buffer, i); - idxptr = getUnsignedInt(buffer, i + 4); - if ( idxptr == 0 ) break; + for ( i = 0; i < TOTAL_HEADER_LENGTH; i += 8 ) + { + sip = getUnsignedInt(buffer, i); + idxptr = getUnsignedInt(buffer, i + 4); + if ( idxptr == 0 ) break; - ip2rObj->HeaderSip[idx] = sip; - ip2rObj->HeaderPtr[idx] = idxptr; - idx++; - } + ip2rObj->HeaderSip[idx] = sip; + ip2rObj->HeaderPtr[idx] = idxptr; + idx++; + } - ip2rObj->headerLen = idx; - } + ip2rObj->headerLen = idx; + } - //search the header block to define the index block - l = 0; h = ip2rObj->headerLen; sptr = 0; eptr = 0; - while ( l <= h ) { - m = ((l + h) >> 1); + //search the header block to define the index block + l = 0; h = ip2rObj->headerLen; sptr = 0; eptr = 0; + while ( l <= h ) { + m = ((l + h) >> 1); - //perfetc matched, just return it - if ( ip == ip2rObj->HeaderSip[m] ) { - if ( m > 0 ) { - sptr = ip2rObj->HeaderPtr[m-1]; - eptr = ip2rObj->HeaderPtr[m ]; - } else { - sptr = ip2rObj->HeaderPtr[m ]; - eptr = ip2rObj->HeaderPtr[m+1]; - } + //perfetc matched, just return it + if ( ip == ip2rObj->HeaderSip[m] ) { + if ( m > 0 ) { + sptr = ip2rObj->HeaderPtr[m-1]; + eptr = ip2rObj->HeaderPtr[m ]; + } else { + sptr = ip2rObj->HeaderPtr[m ]; + eptr = ip2rObj->HeaderPtr[m+1]; + } - break; - } + break; + } - //less then the middle value - if ( ip < ip2rObj->HeaderSip[m] ) { - if ( m == 0 ) { - sptr = ip2rObj->HeaderPtr[m ]; - eptr = ip2rObj->HeaderPtr[m+1]; - break; - } else if ( ip > ip2rObj->HeaderSip[m-1] ) { - sptr = ip2rObj->HeaderPtr[m-1]; - eptr = ip2rObj->HeaderPtr[m ]; - break; - } - h = m - 1; - } else { - if ( m == ip2rObj->headerLen - 1 ) { - sptr = ip2rObj->HeaderPtr[m-1]; - eptr = ip2rObj->HeaderPtr[m ]; - break; - } else if ( ip <= ip2rObj->HeaderSip[m+1] ) { - sptr = ip2rObj->HeaderPtr[m ]; - eptr = ip2rObj->HeaderPtr[m+1]; - break; - } - l = m + 1; - } - } + //less then the middle value + if ( ip < ip2rObj->HeaderSip[m] ) { + if ( m == 0 ) { + sptr = ip2rObj->HeaderPtr[m ]; + eptr = ip2rObj->HeaderPtr[m+1]; + break; + } else if ( ip > ip2rObj->HeaderSip[m-1] ) { + sptr = ip2rObj->HeaderPtr[m-1]; + eptr = ip2rObj->HeaderPtr[m ]; + break; + } + h = m - 1; + } else { + if ( m == ip2rObj->headerLen - 1 ) { + sptr = ip2rObj->HeaderPtr[m-1]; + eptr = ip2rObj->HeaderPtr[m ]; + break; + } else if ( ip <= ip2rObj->HeaderSip[m+1] ) { + sptr = ip2rObj->HeaderPtr[m ]; + eptr = ip2rObj->HeaderPtr[m+1]; + break; + } + l = m + 1; + } + } - //not matched just stop it - if ( sptr == 0 ) return 0; + //not matched just stop it + if ( sptr == 0 ) return 0; - indexBlockLen = eptr - sptr; - fseek(ip2rObj->dbHandler, sptr, 0); - if ( fread(buffer, indexBlockLen + INDEX_BLOCK_LENGTH, 1, ip2rObj->dbHandler) != 1 ) { - return 0; - } + indexBlockLen = eptr - sptr; + fseek(ip2rObj->dbHandler, sptr, 0); + if ( fread(buffer, indexBlockLen + INDEX_BLOCK_LENGTH, 1, ip2rObj->dbHandler) != 1 ) { + return 0; + } - dptr = 0; l = 0; h = indexBlockLen / INDEX_BLOCK_LENGTH; - while ( l <= h ) { - m = ((l + h) >> 1); - p = m * INDEX_BLOCK_LENGTH; - sip = getUnsignedInt(buffer, p); - if ( ip < sip ) { - h = m - 1; - } else { - eip = getUnsignedInt(buffer, p + 4); - if ( ip > eip ) { - l = m + 1; - } else { - dptr = getUnsignedInt(buffer, p + 8); - break; - } - } - } + dptr = 0; l = 0; h = indexBlockLen / INDEX_BLOCK_LENGTH; + while ( l <= h ) { + m = ((l + h) >> 1); + p = m * INDEX_BLOCK_LENGTH; + sip = getUnsignedInt(buffer, p); + if ( ip < sip ) { + h = m - 1; + } else { + eip = getUnsignedInt(buffer, p + 4); + if ( ip > eip ) { + l = m + 1; + } else { + dptr = getUnsignedInt(buffer, p + 8); + break; + } + } + } - if ( dptr == 0 ) return 0; + if ( dptr == 0 ) return 0; - dataLen = ((dptr >> 24) & 0xFF); - dataptr = (dptr & 0x00FFFFFF); + dataLen = ((dptr >> 24) & 0xFF); + dataptr = (dptr & 0x00FFFFFF); - fseek(ip2rObj->dbHandler, dataptr, 0); - if ( fread(buffer, dataLen, 1, ip2rObj->dbHandler) != 1 ) { - return 0; - } + fseek(ip2rObj->dbHandler, dataptr, 0); + if ( fread(buffer, dataLen, 1, ip2rObj->dbHandler) != 1 ) { + return 0; + } - datablock->city_id = getUnsignedInt(buffer, 0); - dataLen -= 4; - memcpy(datablock->region, buffer + 4, dataLen); - datablock->region[dataLen] = '\0'; + datablock->city_id = getUnsignedInt(buffer, 0); + dataLen -= 4; + memcpy(datablock->region, buffer + 4, dataLen); + datablock->region[dataLen] = '\0'; - return 1; + return 1; } 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 * - * @param buffer - * @param offset - * @return uint_t + * @param buffer + * @param offset + * @return uint_t */ IP2R_API uint_t getUnsignedInt(char *buffer, int offset) { - return ( - ((buffer[offset ]) & 0x000000FF) | - ((buffer[offset+1] << 8) & 0x0000FF00) | - ((buffer[offset+2] << 16) & 0x00FF0000) | - ((buffer[offset+3] << 24) & 0xFF000000) - ); + return ( + ((buffer[offset ]) & 0x000000FF) | + ((buffer[offset+1] << 8) & 0x0000FF00) | + ((buffer[offset+2] << 16) & 0x00FF0000) | + ((buffer[offset+3] << 24) & 0xFF000000) + ); } /** * string ip to long * - * @param ip - * @return uint_t + * @param ip + * @return uint_t */ IP2R_API uint_t ip2long(char *ip) { - int i = 0, p = 24; - char buffer[4], *cs = ip; - uint_t ipval = 0; + int i = 0, p = 24; + char buffer[4], *cs = ip; + uint_t ipval = 0; - while ( *cs != '\0' ) - { - if ( *cs == '.' ) { - //single part length limit - if ( i > 3 ) { - ipval = 0; - break; - } + while ( *cs != '\0' ) + { + if ( *cs == '.' ) { + //single part length limit + if ( i > 3 ) { + ipval = 0; + break; + } - if ( p < 0 ) break; - buffer[i] = '\0'; - ipval |= (atoi(buffer) << p); - p -= 8; - i = 0; - } else { - buffer[i++] = *cs; - } + if ( p < 0 ) break; + buffer[i] = '\0'; + ipval |= (atoi(buffer) << p); + p -= 8; + i = 0; + } else { + buffer[i++] = *cs; + } - cs++; - } + cs++; + } - //append the rest parts - if ( i > 3 ) return 0; - buffer[i] = '\0'; - ipval |= atoi(buffer); + //append the rest parts + if ( i > 3 ) return 0; + buffer[i] = '\0'; + ipval |= atoi(buffer); - return ipval; + return ipval; } /** * long to string ip * - * @param ip - * @param buffer - * @return uint_t(1 for success and 0 for failed) + * @param ip + * @param buffer + * @return uint_t(1 for success and 0 for failed) */ IP2R_API uint_t long2ip(uint_t ip, char *buffer) { - return 0; + return 0; } diff --git a/binding/c/testSearcher.c b/binding/c/testSearcher.c index f1646a2..53abbe4 100644 --- a/binding/c/testSearcher.c +++ b/binding/c/testSearcher.c @@ -1,8 +1,8 @@ /** * test ip2region searcher program * - * @author chenxin - * @date 2015-10-30 + * @author chenxin + * @date 2015-10-30 */ #include "ip2region.h" @@ -12,88 +12,88 @@ #include #define __PRINT_ABOUT__ \ - println("+-------------------------------------+"); \ - println("| ip2region test program |"); \ - println("| Author: chenxin619315@gmail.com. |"); \ - println("| Type 'quit' to exit the program. |"); \ + println("+-------------------------------------+"); \ + println("| ip2region test program |"); \ + println("| Author: chenxin619315@gmail.com. |"); \ + println("| Type 'quit' to exit the program. |"); \ println("+-------------------------------------+"); //read a line from a command line. static char *getLine( FILE *fp, char *__dst ) { - register int c; - register char *cs; + register int c; + register char *cs; - cs = __dst; - while ( ( c = getc( fp ) ) != EOF ) { - if ( c == '\n' ) break; - *cs++ = c; - } - *cs = '\0'; + cs = __dst; + while ( ( c = getc( fp ) ) != EOF ) { + if ( c == '\n' ) break; + *cs++ = c; + } + *cs = '\0'; - return ( c == EOF && cs == __dst ) ? NULL : __dst; + return ( c == EOF && cs == __dst ) ? NULL : __dst; } static double getTime() { - struct timeval tv; - struct timezone tz; - gettimeofday(&tv, &tz); + struct timeval tv; + struct timezone 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 ) { - ip2region_entry ip2rEntry; - datablock_entry datablock; - char *dbFile = NULL, *algorithm = NULL; - char line[256]; - uint_t (*func_ptr)(ip2region_t, char *, datablock_t); - double s_time, c_time; - memset(&datablock, 0x00, sizeof(datablock_entry)); + ip2region_entry ip2rEntry; + datablock_entry datablock; + char *dbFile = NULL, *algorithm = NULL; + char line[256]; + uint_t (*func_ptr)(ip2region_t, char *, datablock_t); + double s_time, c_time; + memset(&datablock, 0x00, sizeof(datablock_entry)); - if ( argc < 2 ) { - printf("Usage: a.out [ip2region db file path] [algorithm]"); - return 0; - } + if ( argc < 2 ) { + printf("Usage: a.out [ip2region db file path] [algorithm]"); + return 0; + } - dbFile = argv[1]; - algorithm = "B-tree"; - func_ptr = ip2region_btree_search_string; - if ( argc >= 3 && strcmp(argv[2], "binary") == 0 ) { - algorithm = "Binary"; - func_ptr = ip2region_binary_search_string; - } + dbFile = argv[1]; + algorithm = "B-tree"; + func_ptr = ip2region_btree_search_string; + if ( argc >= 3 && strcmp(argv[2], "binary") == 0 ) { + algorithm = "Binary"; + func_ptr = ip2region_binary_search_string; + } - //create a new ip2rObj - printf("+--initializing %s ... \n", algorithm); - if ( ip2region_create(&ip2rEntry, dbFile) == 0 ) - { - println("Error: Fail to create the ip2region object"); - return 0; - } + //create a new ip2rObj + printf("+--initializing %s ... \n", algorithm); + if ( ip2region_create(&ip2rEntry, dbFile) == 0 ) + { + println("Error: Fail to create the ip2region object"); + return 0; + } - __PRINT_ABOUT__; + __PRINT_ABOUT__; - while ( 1 ) - { - print("ip2region>> "); - getLine( stdin, line ); - if ( strlen(line) < 2 ) continue; - if ( strcasecmp( line, "quit" ) == 0 ) { - println("+--Bye!"); - break; - } + while ( 1 ) + { + print("ip2region>> "); + getLine( stdin, line ); + if ( strlen(line) < 2 ) continue; + if ( strcasecmp( line, "quit" ) == 0 ) { + println("+--Bye!"); + break; + } - s_time = getTime(); - func_ptr(&ip2rEntry, line, &datablock); - c_time = getTime() - s_time; - printf("%d|%s in %.5f millseconds\n", datablock.city_id, datablock.region, c_time); - } + s_time = getTime(); + func_ptr(&ip2rEntry, line, &datablock); + c_time = getTime() - s_time; + printf("%d|%s in %.5f millseconds\n", datablock.city_id, datablock.region, c_time); + } - //destory the ip2rObj - ip2region_destroy(&ip2rEntry); + //destory the ip2rObj + ip2region_destroy(&ip2rEntry); - return 0; + return 0; } diff --git a/binding/java/src/org/lionsoul/ip2region/DataBlock.java b/binding/java/src/org/lionsoul/ip2region/DataBlock.java index e29fbe4..fdf8311 100644 --- a/binding/java/src/org/lionsoul/ip2region/DataBlock.java +++ b/binding/java/src/org/lionsoul/ip2region/DataBlock.java @@ -3,50 +3,50 @@ package org.lionsoul.ip2region; /** * data block class * - * @author chenxin + * @author chenxin */ public class DataBlock { - /** - * city id - */ - private int city_id; - - /** - * region address - */ - private String region; - - public DataBlock( int city_id, String region ) - { - this.city_id = city_id; - this.region = region; - } + /** + * city id + */ + private int city_id; + + /** + * region address + */ + private String region; + + public DataBlock( int city_id, String region ) + { + this.city_id = city_id; + this.region = region; + } - public int getCityId() { - return city_id; - } + public int getCityId() { + return city_id; + } - public DataBlock setCityId(int city_id) { - this.city_id = city_id; - return this; - } + public DataBlock setCityId(int city_id) { + this.city_id = city_id; + return this; + } - public String getRegion() { - return region; - } + public String getRegion() { + return region; + } - public DataBlock setRegion(String region) { - this.region = region; - return this; - } - - @Override - public String toString() - { - StringBuilder sb = new StringBuilder(); - - sb.append(city_id).append('|').append(region); - return sb.toString(); - } + public DataBlock setRegion(String region) { + this.region = region; + return this; + } + + @Override + public String toString() + { + StringBuilder sb = new StringBuilder(); + + sb.append(city_id).append('|').append(region); + return sb.toString(); + } } diff --git a/binding/java/src/org/lionsoul/ip2region/DbConfig.java b/binding/java/src/org/lionsoul/ip2region/DbConfig.java index 5df40c1..acc2d4a 100644 --- a/binding/java/src/org/lionsoul/ip2region/DbConfig.java +++ b/binding/java/src/org/lionsoul/ip2region/DbConfig.java @@ -7,53 +7,53 @@ package org.lionsoul.ip2region; */ public class DbConfig { - /** - * total header data block size - */ - private int totalHeaderSize; - - /** - * max index data block size - * u should always choice the fastest read block size - */ - private int indexBlockSize; - - /** - * construct method - * - * @param totalHeaderSize - * @param dataBlockSize - * @throws DbMakerConfigException - */ - public DbConfig( int totalHeaderSize ) throws DbMakerConfigException { - if ( (totalHeaderSize % 8) != 0 ) - { - throw new DbMakerConfigException("totalHeaderSize must be times of 8"); - } - - this.totalHeaderSize = totalHeaderSize; - this.indexBlockSize = 4096; //4 * 1024 - } - - public DbConfig() throws DbMakerConfigException { - this(8192); - } + /** + * total header data block size + */ + private int totalHeaderSize; + + /** + * max index data block size + * u should always choice the fastest read block size + */ + private int indexBlockSize; + + /** + * construct method + * + * @param totalHeaderSize + * @param dataBlockSize + * @throws DbMakerConfigException + */ + public DbConfig( int totalHeaderSize ) throws DbMakerConfigException { + if ( (totalHeaderSize % 8) != 0 ) + { + throw new DbMakerConfigException("totalHeaderSize must be times of 8"); + } + + this.totalHeaderSize = totalHeaderSize; + this.indexBlockSize = 4096; //4 * 1024 + } + + public DbConfig() throws DbMakerConfigException { + this(8192); + } - public int getTotalHeaderSize() { - return totalHeaderSize; - } + public int getTotalHeaderSize() { + return totalHeaderSize; + } - public DbConfig setTotalHeaderSize(int totalHeaderSize) { - this.totalHeaderSize = totalHeaderSize; - return this; - } + public DbConfig setTotalHeaderSize(int totalHeaderSize) { + this.totalHeaderSize = totalHeaderSize; + return this; + } - public int getIndexBlockSize() { - return indexBlockSize; - } + public int getIndexBlockSize() { + return indexBlockSize; + } - public DbConfig setIndexBlockSize(int dataBlockSize) { - this.indexBlockSize = dataBlockSize; - return this; - } + public DbConfig setIndexBlockSize(int dataBlockSize) { + this.indexBlockSize = dataBlockSize; + return this; + } } diff --git a/binding/java/src/org/lionsoul/ip2region/DbMakerConfigException.java b/binding/java/src/org/lionsoul/ip2region/DbMakerConfigException.java index b84334f..6ab87cc 100644 --- a/binding/java/src/org/lionsoul/ip2region/DbMakerConfigException.java +++ b/binding/java/src/org/lionsoul/ip2region/DbMakerConfigException.java @@ -7,17 +7,17 @@ package org.lionsoul.ip2region; */ public class DbMakerConfigException extends Exception { - private static final long serialVersionUID = 4495714680349884838L; - - public DbMakerConfigException( String info ) { - super(info); - } - - public DbMakerConfigException( Throwable res ) { - super(res); - } - - public DbMakerConfigException( String info, Throwable res ) { - super(info, res); - } + private static final long serialVersionUID = 4495714680349884838L; + + public DbMakerConfigException( String info ) { + super(info); + } + + public DbMakerConfigException( Throwable res ) { + super(res); + } + + public DbMakerConfigException( String info, Throwable res ) { + super(info, res); + } } diff --git a/binding/java/src/org/lionsoul/ip2region/DbSearcher.java b/binding/java/src/org/lionsoul/ip2region/DbSearcher.java index 01f69cb..6312727 100644 --- a/binding/java/src/org/lionsoul/ip2region/DbSearcher.java +++ b/binding/java/src/org/lionsoul/ip2region/DbSearcher.java @@ -11,306 +11,306 @@ import java.io.RandomAccessFile; */ public class DbSearcher { - public static final int BTREE_ALGORITHM = 1; - public static final int BIN_ALGORITHM = 2; - - /** - * db config - */ - private DbConfig dbConfig = null; - - /** - * db file access handler - */ - private RandomAccessFile raf = null; - - /** - * header blocks buffer - */ - private long[] HeaderSip = null; - private int[] HeaderPtr = null; - private int headerLength; - - /** - * super blocks info - */ - private long firstIndexPtr = 0; - private long lastIndexPtr = 0; - private int totalIndexBlocks = 0; - - /** - * construct class - * - * @param bdConfig - * @param dbFile - * @throws FileNotFoundException - */ - public DbSearcher( DbConfig dbConfig, String dbFile ) throws FileNotFoundException - { - this.dbConfig = dbConfig; - raf = new RandomAccessFile(dbFile, "r"); - } - - /** - * get by index ptr - * - * @param indexPtr - * @throws IOException - */ - public DataBlock getByIndexPtr( long ptr ) throws IOException - { - raf.seek(ptr); - byte[] buffer = new byte[12]; - raf.readFully(buffer, 0, buffer.length); - //long startIp = Util.getIntLong(buffer, 0); - //long endIp = Util.getIntLong(buffer, 4); - long extra = Util.getIntLong(buffer, 8); - - int dataLen = (int)((extra >> 24) & 0xFF); - int dataPtr = (int)((extra & 0x00FFFFFF)); - - raf.seek(dataPtr); - byte[] data = new byte[dataLen]; - raf.readFully(data, 0, data.length); - - int city_id = (int)Util.getIntLong(data, 0); - String region = new String(data, 4, data.length - 4, "UTF-8"); - - return new DataBlock(city_id, region); - } - - /** - * get the region with a int ip address with b-tree algorithm - * - * @param ip - * @throws IOException - */ - public DataBlock btreeSearch( long ip ) throws IOException - { - //check and load the header - if ( HeaderSip == null ) - { - raf.seek(8L); //pass the super block - //byte[] b = new byte[dbConfig.getTotalHeaderSize()]; - byte[] b = new byte[4096]; - raf.readFully(b, 0, b.length); - - //fill the header - int len = b.length >> 3, idx = 0; //b.lenght / 8 - HeaderSip = new long[len]; - HeaderPtr = new int [len]; - long startIp, dataPtr; - for ( int i = 0; i < b.length; i += 8 ) { - startIp = Util.getIntLong(b, i); - dataPtr = Util.getIntLong(b, i + 4); - if ( dataPtr == 0 ) break; - - HeaderSip[idx] = startIp; - HeaderPtr[idx] = (int)dataPtr; - idx++; - } - - headerLength = idx; - } - - //1. define the index block with the binary search - if ( ip == HeaderSip[0] ) { - return getByIndexPtr(HeaderPtr[0]); - } else if ( ip == HeaderSip[headerLength-1] ) { - return getByIndexPtr(HeaderPtr[headerLength-1]); - } - - int l = 0, h = headerLength, sptr = 0, eptr = 0; - while ( l <= h ) - { - int m = (l + h) >> 1; - - //perfetc matched, just return it - if ( ip == HeaderSip[m] ) { - if ( m > 0 ) { - sptr = HeaderPtr[m-1]; - eptr = HeaderPtr[m ]; - } else { - sptr = HeaderPtr[m ]; - eptr = HeaderPtr[m+1]; - } - - break; - } - - //less then the middle value - if ( ip < HeaderSip[m] ) { - if ( m == 0 ) { - sptr = HeaderPtr[m ]; - eptr = HeaderPtr[m+1]; - break; - } else if ( ip > HeaderSip[m-1] ) { - sptr = HeaderPtr[m-1]; - eptr = HeaderPtr[m ]; - break; - } - h = m - 1; - } else { - if ( m == headerLength - 1 ) { - sptr = HeaderPtr[m-1]; - eptr = HeaderPtr[m ]; - break; - } else if ( ip <= HeaderSip[m+1] ) { - sptr = HeaderPtr[m ]; - eptr = HeaderPtr[m+1]; - break; - } - l = m + 1; - } - } - - //match nothing just stop it - if ( sptr == 0 ) return null; - - //2. search the index blocks to define the data - int blockLen = eptr - sptr, blen = IndexBlock.getIndexBlockLength(); - byte[] iBuffer = new byte[blockLen + blen]; //include the right border block - raf.seek(sptr); - raf.readFully(iBuffer, 0, iBuffer.length); - - l = 0; h = blockLen / blen; - long sip, eip, dataptr = 0; - while ( l <= h ) { - int m = (l + h) >> 1; - int p = m * blen; - sip = Util.getIntLong(iBuffer, p); - if ( ip < sip ) { - h = m - 1; - } else { - eip = Util.getIntLong(iBuffer, p + 4); - if ( ip > eip ) { - l = m + 1; - } else { - dataptr = Util.getIntLong(iBuffer, p + 8); - break; - } - } - } - - //not matched - if ( dataptr == 0 ) return null; - - //3. get the data - int dataLen = (int)((dataptr >> 24) & 0xFF); - int dataPtr = (int)((dataptr & 0x00FFFFFF)); - - raf.seek(dataPtr); - byte[] data = new byte[dataLen]; - raf.readFully(data, 0, data.length); - - int city_id = (int)Util.getIntLong(data, 0); - String region = new String(data, 4, data.length - 4, "UTF-8"); - - return new DataBlock(city_id, region); - } - - /** - * get the region throught the ip address with b-tree search algorithm - * - * @param ip - * @return DataBlock - * @throws IOException - */ - public DataBlock btreeSearch( String ip ) throws IOException - { - return btreeSearch(Util.ip2long(ip)); - } - - /** - * get the region with a int ip address with binary search algorithm - * - * @param ip - * @throws IOException - */ - public DataBlock binarySearch( long ip ) throws IOException - { - int blen = IndexBlock.getIndexBlockLength(); - if ( totalIndexBlocks == 0 ) - { - raf.seek(0L); - byte[] superBytes = new byte[8]; - raf.readFully(superBytes, 0, superBytes.length); - //initialize the global vars - firstIndexPtr = Util.getIntLong(superBytes, 0); - lastIndexPtr = Util.getIntLong(superBytes, 4); - totalIndexBlocks = (int)((lastIndexPtr - firstIndexPtr)/blen) + 1; - } - - //search the index blocks to define the data - int l = 0, h = totalIndexBlocks; - byte[] buffer = new byte[blen]; - long sip, eip, dataptr = 0; - while ( l <= h ) { - int m = (l + h) >> 1; - raf.seek(firstIndexPtr + m * blen); //set the file pointer - raf.readFully(buffer, 0, buffer.length); - sip = Util.getIntLong(buffer, 0); - if ( ip < sip ) { - h = m - 1; - } else { - eip = Util.getIntLong(buffer, 4); - if ( ip > eip ) { - l = m + 1; - } else { - dataptr = Util.getIntLong(buffer, 8); - break; - } - } - } - - //not matched - if ( dataptr == 0 ) return null; - - //get the data - int dataLen = (int)((dataptr >> 24) & 0xFF); - int dataPtr = (int)((dataptr & 0x00FFFFFF)); - - raf.seek(dataPtr); - byte[] data = new byte[dataLen]; - raf.readFully(data, 0, data.length); - - int city_id = (int)Util.getIntLong(data, 0); - String region = new String(data, 4, data.length - 4, "UTF-8"); - - return new DataBlock(city_id, region); - } - - /** - * get the region throught the ip address with binary search algorithm - * - * @param ip - * @return DataBlock - * @throws IOException - */ - public DataBlock binarySearch( String ip ) throws IOException - { - return binarySearch(Util.ip2long(ip)); - } + public static final int BTREE_ALGORITHM = 1; + public static final int BIN_ALGORITHM = 2; + + /** + * db config + */ + private DbConfig dbConfig = null; + + /** + * db file access handler + */ + private RandomAccessFile raf = null; + + /** + * header blocks buffer + */ + private long[] HeaderSip = null; + private int[] HeaderPtr = null; + private int headerLength; + + /** + * super blocks info + */ + private long firstIndexPtr = 0; + private long lastIndexPtr = 0; + private int totalIndexBlocks = 0; + + /** + * construct class + * + * @param bdConfig + * @param dbFile + * @throws FileNotFoundException + */ + public DbSearcher( DbConfig dbConfig, String dbFile ) throws FileNotFoundException + { + this.dbConfig = dbConfig; + raf = new RandomAccessFile(dbFile, "r"); + } + + /** + * get by index ptr + * + * @param indexPtr + * @throws IOException + */ + public DataBlock getByIndexPtr( long ptr ) throws IOException + { + raf.seek(ptr); + byte[] buffer = new byte[12]; + raf.readFully(buffer, 0, buffer.length); + //long startIp = Util.getIntLong(buffer, 0); + //long endIp = Util.getIntLong(buffer, 4); + long extra = Util.getIntLong(buffer, 8); + + int dataLen = (int)((extra >> 24) & 0xFF); + int dataPtr = (int)((extra & 0x00FFFFFF)); + + raf.seek(dataPtr); + byte[] data = new byte[dataLen]; + raf.readFully(data, 0, data.length); + + int city_id = (int)Util.getIntLong(data, 0); + String region = new String(data, 4, data.length - 4, "UTF-8"); + + return new DataBlock(city_id, region); + } + + /** + * get the region with a int ip address with b-tree algorithm + * + * @param ip + * @throws IOException + */ + public DataBlock btreeSearch( long ip ) throws IOException + { + //check and load the header + if ( HeaderSip == null ) + { + raf.seek(8L); //pass the super block + //byte[] b = new byte[dbConfig.getTotalHeaderSize()]; + byte[] b = new byte[4096]; + raf.readFully(b, 0, b.length); + + //fill the header + int len = b.length >> 3, idx = 0; //b.lenght / 8 + HeaderSip = new long[len]; + HeaderPtr = new int [len]; + long startIp, dataPtr; + for ( int i = 0; i < b.length; i += 8 ) { + startIp = Util.getIntLong(b, i); + dataPtr = Util.getIntLong(b, i + 4); + if ( dataPtr == 0 ) break; + + HeaderSip[idx] = startIp; + HeaderPtr[idx] = (int)dataPtr; + idx++; + } + + headerLength = idx; + } + + //1. define the index block with the binary search + if ( ip == HeaderSip[0] ) { + return getByIndexPtr(HeaderPtr[0]); + } else if ( ip == HeaderSip[headerLength-1] ) { + return getByIndexPtr(HeaderPtr[headerLength-1]); + } + + int l = 0, h = headerLength, sptr = 0, eptr = 0; + while ( l <= h ) + { + int m = (l + h) >> 1; + + //perfetc matched, just return it + if ( ip == HeaderSip[m] ) { + if ( m > 0 ) { + sptr = HeaderPtr[m-1]; + eptr = HeaderPtr[m ]; + } else { + sptr = HeaderPtr[m ]; + eptr = HeaderPtr[m+1]; + } + + break; + } + + //less then the middle value + if ( ip < HeaderSip[m] ) { + if ( m == 0 ) { + sptr = HeaderPtr[m ]; + eptr = HeaderPtr[m+1]; + break; + } else if ( ip > HeaderSip[m-1] ) { + sptr = HeaderPtr[m-1]; + eptr = HeaderPtr[m ]; + break; + } + h = m - 1; + } else { + if ( m == headerLength - 1 ) { + sptr = HeaderPtr[m-1]; + eptr = HeaderPtr[m ]; + break; + } else if ( ip <= HeaderSip[m+1] ) { + sptr = HeaderPtr[m ]; + eptr = HeaderPtr[m+1]; + break; + } + l = m + 1; + } + } + + //match nothing just stop it + if ( sptr == 0 ) return null; + + //2. search the index blocks to define the data + int blockLen = eptr - sptr, blen = IndexBlock.getIndexBlockLength(); + byte[] iBuffer = new byte[blockLen + blen]; //include the right border block + raf.seek(sptr); + raf.readFully(iBuffer, 0, iBuffer.length); + + l = 0; h = blockLen / blen; + long sip, eip, dataptr = 0; + while ( l <= h ) { + int m = (l + h) >> 1; + int p = m * blen; + sip = Util.getIntLong(iBuffer, p); + if ( ip < sip ) { + h = m - 1; + } else { + eip = Util.getIntLong(iBuffer, p + 4); + if ( ip > eip ) { + l = m + 1; + } else { + dataptr = Util.getIntLong(iBuffer, p + 8); + break; + } + } + } + + //not matched + if ( dataptr == 0 ) return null; + + //3. get the data + int dataLen = (int)((dataptr >> 24) & 0xFF); + int dataPtr = (int)((dataptr & 0x00FFFFFF)); + + raf.seek(dataPtr); + byte[] data = new byte[dataLen]; + raf.readFully(data, 0, data.length); + + int city_id = (int)Util.getIntLong(data, 0); + String region = new String(data, 4, data.length - 4, "UTF-8"); + + return new DataBlock(city_id, region); + } + + /** + * get the region throught the ip address with b-tree search algorithm + * + * @param ip + * @return DataBlock + * @throws IOException + */ + public DataBlock btreeSearch( String ip ) throws IOException + { + return btreeSearch(Util.ip2long(ip)); + } + + /** + * get the region with a int ip address with binary search algorithm + * + * @param ip + * @throws IOException + */ + public DataBlock binarySearch( long ip ) throws IOException + { + int blen = IndexBlock.getIndexBlockLength(); + if ( totalIndexBlocks == 0 ) + { + raf.seek(0L); + byte[] superBytes = new byte[8]; + raf.readFully(superBytes, 0, superBytes.length); + //initialize the global vars + firstIndexPtr = Util.getIntLong(superBytes, 0); + lastIndexPtr = Util.getIntLong(superBytes, 4); + totalIndexBlocks = (int)((lastIndexPtr - firstIndexPtr)/blen) + 1; + } + + //search the index blocks to define the data + int l = 0, h = totalIndexBlocks; + byte[] buffer = new byte[blen]; + long sip, eip, dataptr = 0; + while ( l <= h ) { + int m = (l + h) >> 1; + raf.seek(firstIndexPtr + m * blen); //set the file pointer + raf.readFully(buffer, 0, buffer.length); + sip = Util.getIntLong(buffer, 0); + if ( ip < sip ) { + h = m - 1; + } else { + eip = Util.getIntLong(buffer, 4); + if ( ip > eip ) { + l = m + 1; + } else { + dataptr = Util.getIntLong(buffer, 8); + break; + } + } + } + + //not matched + if ( dataptr == 0 ) return null; + + //get the data + int dataLen = (int)((dataptr >> 24) & 0xFF); + int dataPtr = (int)((dataptr & 0x00FFFFFF)); + + raf.seek(dataPtr); + byte[] data = new byte[dataLen]; + raf.readFully(data, 0, data.length); + + int city_id = (int)Util.getIntLong(data, 0); + String region = new String(data, 4, data.length - 4, "UTF-8"); + + return new DataBlock(city_id, region); + } + + /** + * get the region throught the ip address with binary search algorithm + * + * @param ip + * @return DataBlock + * @throws IOException + */ + public DataBlock binarySearch( String ip ) throws IOException + { + return binarySearch(Util.ip2long(ip)); + } - /** - * get the db config - * - * @return DbConfig - */ - public DbConfig getDbConfig() - { - return dbConfig; - } - - /** - * close the db - * - * @throws IOException - */ - public void close() throws IOException - { - HeaderSip = null; //let gc do its work - HeaderPtr = null; - raf.close(); - } + /** + * get the db config + * + * @return DbConfig + */ + public DbConfig getDbConfig() + { + return dbConfig; + } + + /** + * close the db + * + * @throws IOException + */ + public void close() throws IOException + { + HeaderSip = null; //let gc do its work + HeaderPtr = null; + raf.close(); + } } diff --git a/binding/java/src/org/lionsoul/ip2region/HeaderBlock.java b/binding/java/src/org/lionsoul/ip2region/HeaderBlock.java index c772602..28b04ce 100644 --- a/binding/java/src/org/lionsoul/ip2region/HeaderBlock.java +++ b/binding/java/src/org/lionsoul/ip2region/HeaderBlock.java @@ -7,58 +7,58 @@ package org.lionsoul.ip2region; */ public class HeaderBlock { - /** - * index block start ip address - */ - private long indexStartIp; - - /** - * ip address - */ - private int indexPtr; - - public HeaderBlock( long indexStartIp, int indexPtr ) - { - this.indexStartIp = indexStartIp; - this.indexPtr = indexPtr; - } + /** + * index block start ip address + */ + private long indexStartIp; + + /** + * ip address + */ + private int indexPtr; + + public HeaderBlock( long indexStartIp, int indexPtr ) + { + this.indexStartIp = indexStartIp; + this.indexPtr = indexPtr; + } - public long getIndexStartIp() { - return indexStartIp; - } + public long getIndexStartIp() { + return indexStartIp; + } - public HeaderBlock setIndexStartIp(long indexStartIp) { - this.indexStartIp = indexStartIp; - return this; - } + public HeaderBlock setIndexStartIp(long indexStartIp) { + this.indexStartIp = indexStartIp; + return this; + } - public int getIndexPtr() { - return indexPtr; - } + public int getIndexPtr() { + return indexPtr; + } - public HeaderBlock setIndexPtr(int indexPtr) { - this.indexPtr = indexPtr; - return this; - } - - /** - * get the bytes for db storage - * - * @return byte[] - */ - public byte[] getBytes() - { - /* - * +------------+-----------+ - * | 4bytes | 4bytes | - * +------------+-----------+ - * start ip index ptr - */ - byte[] b = new byte[8]; - - Util.writeIntLong(b, 0, indexStartIp); - Util.writeIntLong(b, 4, indexPtr); - - return b; - } + public HeaderBlock setIndexPtr(int indexPtr) { + this.indexPtr = indexPtr; + return this; + } + + /** + * get the bytes for db storage + * + * @return byte[] + */ + public byte[] getBytes() + { + /* + * +------------+-----------+ + * | 4bytes | 4bytes | + * +------------+-----------+ + * start ip index ptr + */ + byte[] b = new byte[8]; + + Util.writeIntLong(b, 0, indexStartIp); + Util.writeIntLong(b, 4, indexPtr); + + return b; + } } diff --git a/binding/java/src/org/lionsoul/ip2region/IndexBlock.java b/binding/java/src/org/lionsoul/ip2region/IndexBlock.java index ae9ef74..5d6b741 100644 --- a/binding/java/src/org/lionsoul/ip2region/IndexBlock.java +++ b/binding/java/src/org/lionsoul/ip2region/IndexBlock.java @@ -7,98 +7,98 @@ package org.lionsoul.ip2region; */ public class IndexBlock { - private static int LENGTH = 12; - - /** - * start ip address - */ - private long startIp; - - /** - * end ip address - */ - private long endIp; - - /** - * data ptr and data length - */ - private int dataPtr; - - /** - * data length - */ - private int dataLen; - - public IndexBlock(long startIp, long endIp, int dataPtr, int dataLen) - { - this.startIp = startIp; - this.endIp = endIp; - this.dataPtr = dataPtr; - this.dataLen = dataLen; - } + private static int LENGTH = 12; + + /** + * start ip address + */ + private long startIp; + + /** + * end ip address + */ + private long endIp; + + /** + * data ptr and data length + */ + private int dataPtr; + + /** + * data length + */ + private int dataLen; + + public IndexBlock(long startIp, long endIp, int dataPtr, int dataLen) + { + this.startIp = startIp; + this.endIp = endIp; + this.dataPtr = dataPtr; + this.dataLen = dataLen; + } - public long getStartIp() { - return startIp; - } + public long getStartIp() { + return startIp; + } - public IndexBlock setStartIp(long startIp) { - this.startIp = startIp; - return this; - } + public IndexBlock setStartIp(long startIp) { + this.startIp = startIp; + return this; + } - public long getEndIp() { - return endIp; - } + public long getEndIp() { + return endIp; + } - public IndexBlock setEndIp(long endIp) { - this.endIp = endIp; - return this; - } + public IndexBlock setEndIp(long endIp) { + this.endIp = endIp; + return this; + } - public int getDataPtr() { - return dataPtr; - } + public int getDataPtr() { + return dataPtr; + } - public IndexBlock setDataPtr(int dataPtr) { - this.dataPtr = dataPtr; - return this; - } + public IndexBlock setDataPtr(int dataPtr) { + this.dataPtr = dataPtr; + return this; + } - public int getDataLen() { - return dataLen; - } + public int getDataLen() { + return dataLen; + } - public IndexBlock setDataLen(int dataLen) { - this.dataLen = dataLen; - return this; - } - - public static int getIndexBlockLength() { - return LENGTH; - } - - /** - * get the bytes for storage - * - * @return byte[] - */ - public byte[] getBytes() - { - /* - * +------------+-----------+-----------+ - * | 4bytes | 4bytes | 4bytes | - * +------------+-----------+-----------+ - * start ip end ip data ptr + len - */ - byte[] b = new byte[12]; - - Util.writeIntLong(b, 0, startIp); //start ip - Util.writeIntLong(b, 4, endIp); //end ip - - //write the data ptr and the length - long mix = dataPtr | ((dataLen << 24) & 0xFF000000L); - Util.writeIntLong(b, 8, mix); - - return b; - } + public IndexBlock setDataLen(int dataLen) { + this.dataLen = dataLen; + return this; + } + + public static int getIndexBlockLength() { + return LENGTH; + } + + /** + * get the bytes for storage + * + * @return byte[] + */ + public byte[] getBytes() + { + /* + * +------------+-----------+-----------+ + * | 4bytes | 4bytes | 4bytes | + * +------------+-----------+-----------+ + * start ip end ip data ptr + len + */ + byte[] b = new byte[12]; + + Util.writeIntLong(b, 0, startIp); //start ip + Util.writeIntLong(b, 4, endIp); //end ip + + //write the data ptr and the length + long mix = dataPtr | ((dataLen << 24) & 0xFF000000L); + Util.writeIntLong(b, 8, mix); + + return b; + } } diff --git a/binding/java/src/org/lionsoul/ip2region/Util.java b/binding/java/src/org/lionsoul/ip2region/Util.java index 0f9d40e..0a0b153 100644 --- a/binding/java/src/org/lionsoul/ip2region/Util.java +++ b/binding/java/src/org/lionsoul/ip2region/Util.java @@ -7,139 +7,139 @@ package org.lionsoul.ip2region; */ public class Util { - /** - * write specfield bytes to a byte array start from offset - * - * @param b - * @param offset - * @param v - * @param bytes - */ - public static void write( byte[] b, int offset, long v, int bytes) - { - for ( int i = 0; i < bytes; i++ ) - { - b[offset++] = (byte)((v >>> (8 * i)) & 0xFF); - } - } - - /** - * write a int to a byte array - * - * @param b - * @param offet - * @param v - */ - public static void writeIntLong( byte[] b, int offset, long v ) - { - b[offset++] = (byte)((v >> 0) & 0xFF); - b[offset++] = (byte)((v >> 8) & 0xFF); - b[offset++] = (byte)((v >> 16) & 0xFF); - b[offset ] = (byte)((v >> 24) & 0xFF); - } - - /** - * get a int from a byte array start from the specifiled offset - * - * @param b - * @param offset - */ - public static long getIntLong( byte[] b, int offset ) - { - return ( - ((b[offset++] & 0x000000FFL)) | - ((b[offset++] << 8) & 0x0000FF00L) | - ((b[offset++] << 16) & 0x00FF0000L) | - ((b[offset ] << 24) & 0xFF000000L) - ); - } - - /** - * get a int from a byte array start from the specifield offset - * - * @param b - * @param offset - */ - public static int getInt3( byte[] b, int offset ) - { - return ( - (b[offset++] & 0x000000FF) | - (b[offset++] & 0x0000FF00) | - (b[offset ] & 0x00FF0000) - ); - } - - public static int getInt2( byte[] b, int offset ) - { - return ( - (b[offset++] & 0x000000FF) | - (b[offset ] & 0x0000FF00) - ); - } - - public static int getInt1( byte[] b, int offset ) - { - return ( - (b[offset] & 0x000000FF) - ); - } - - /** - * string ip to long ip - * - * @param ip - * @return long - */ - public static long ip2long( String ip ) - { - String[] p = ip.split("\\."); - if ( p.length != 4 ) return 0; - - int p1 = ((Integer.valueOf(p[0]) << 24) & 0xFF000000); - int p2 = ((Integer.valueOf(p[1]) << 16) & 0x00FF0000); - int p3 = ((Integer.valueOf(p[2]) << 8) & 0x0000FF00); - int p4 = ((Integer.valueOf(p[3]) << 0) & 0x000000FF); - - return ((p1 | p2 | p3 | p4) & 0xFFFFFFFFL); - } - - /** - * int to ip string - * - * @param ip - * @return string - */ - public static String long2ip( long ip ) - { - StringBuilder sb = new StringBuilder(); - - sb - .append((ip >> 24) & 0xFF).append('.') - .append((ip >> 16) & 0xFF).append('.') - .append((ip >> 8) & 0xFF).append('.') - .append((ip >> 0) & 0xFF); - - return sb.toString(); - } - - /** - * check the validate of the specifeld ip address - * - * @param ip - * @return boolean - */ - public static boolean isIpAddress( String ip ) - { - String[] p = ip.split("\\."); - if ( p.length != 4 ) return false; - - for ( String pp : p ) - { - if ( pp.length() > 3 ) return false; - int val = Integer.valueOf(pp); - if ( val > 255 ) return false; - } - - return true; - } + /** + * write specfield bytes to a byte array start from offset + * + * @param b + * @param offset + * @param v + * @param bytes + */ + public static void write( byte[] b, int offset, long v, int bytes) + { + for ( int i = 0; i < bytes; i++ ) + { + b[offset++] = (byte)((v >>> (8 * i)) & 0xFF); + } + } + + /** + * write a int to a byte array + * + * @param b + * @param offet + * @param v + */ + public static void writeIntLong( byte[] b, int offset, long v ) + { + b[offset++] = (byte)((v >> 0) & 0xFF); + b[offset++] = (byte)((v >> 8) & 0xFF); + b[offset++] = (byte)((v >> 16) & 0xFF); + b[offset ] = (byte)((v >> 24) & 0xFF); + } + + /** + * get a int from a byte array start from the specifiled offset + * + * @param b + * @param offset + */ + public static long getIntLong( byte[] b, int offset ) + { + return ( + ((b[offset++] & 0x000000FFL)) | + ((b[offset++] << 8) & 0x0000FF00L) | + ((b[offset++] << 16) & 0x00FF0000L) | + ((b[offset ] << 24) & 0xFF000000L) + ); + } + + /** + * get a int from a byte array start from the specifield offset + * + * @param b + * @param offset + */ + public static int getInt3( byte[] b, int offset ) + { + return ( + (b[offset++] & 0x000000FF) | + (b[offset++] & 0x0000FF00) | + (b[offset ] & 0x00FF0000) + ); + } + + public static int getInt2( byte[] b, int offset ) + { + return ( + (b[offset++] & 0x000000FF) | + (b[offset ] & 0x0000FF00) + ); + } + + public static int getInt1( byte[] b, int offset ) + { + return ( + (b[offset] & 0x000000FF) + ); + } + + /** + * string ip to long ip + * + * @param ip + * @return long + */ + public static long ip2long( String ip ) + { + String[] p = ip.split("\\."); + if ( p.length != 4 ) return 0; + + int p1 = ((Integer.valueOf(p[0]) << 24) & 0xFF000000); + int p2 = ((Integer.valueOf(p[1]) << 16) & 0x00FF0000); + int p3 = ((Integer.valueOf(p[2]) << 8) & 0x0000FF00); + int p4 = ((Integer.valueOf(p[3]) << 0) & 0x000000FF); + + return ((p1 | p2 | p3 | p4) & 0xFFFFFFFFL); + } + + /** + * int to ip string + * + * @param ip + * @return string + */ + public static String long2ip( long ip ) + { + StringBuilder sb = new StringBuilder(); + + sb + .append((ip >> 24) & 0xFF).append('.') + .append((ip >> 16) & 0xFF).append('.') + .append((ip >> 8) & 0xFF).append('.') + .append((ip >> 0) & 0xFF); + + return sb.toString(); + } + + /** + * check the validate of the specifeld ip address + * + * @param ip + * @return boolean + */ + public static boolean isIpAddress( String ip ) + { + String[] p = ip.split("\\."); + if ( p.length != 4 ) return false; + + for ( String pp : p ) + { + if ( pp.length() > 3 ) return false; + int val = Integer.valueOf(pp); + if ( val > 255 ) return false; + } + + return true; + } } diff --git a/binding/java/src/org/lionsoul/ip2region/test/TestSearcher.java b/binding/java/src/org/lionsoul/ip2region/test/TestSearcher.java index 619623b..73228a8 100644 --- a/binding/java/src/org/lionsoul/ip2region/test/TestSearcher.java +++ b/binding/java/src/org/lionsoul/ip2region/test/TestSearcher.java @@ -18,65 +18,65 @@ import org.lionsoul.ip2region.Util; */ public class TestSearcher { - public static void main(String[] argv) - { - if ( argv.length == 0 ) { - System.out.println("| Usage: java -jar ip2region-{version}.jar [ip2region db file]"); - return; - } - - int algorithm = DbSearcher.BTREE_ALGORITHM; - File file = new File(argv[0]); - if ( file.exists() == false ) { - System.out.println("Error: Invalid ip2region.db file"); - return; - } - - if ( argv.length > 1 ) { - if ( argv[1].equalsIgnoreCase("binary")) algorithm = DbSearcher.BIN_ALGORITHM; - } - - try { - System.out.println("initializing "+((algorithm==2)?"Binary":"B-tree")+" ... "); - DbConfig config = new DbConfig(); - DbSearcher seacher = new DbSearcher(config, argv[0]); - BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); - - System.out.println("+----------------------------------+"); - System.out.println("| ip2region test shell |"); - System.out.println("| Author: chenxin619315@gmail.com |"); - System.out.println("| Type 'quit' to exit program |"); - System.out.println("+----------------------------------+"); - - double sTime = 0, cTime = 0; - String line = null; - DataBlock dataBlock = null; - while ( true ) - { - System.out.print("ip2region>> "); - line = reader.readLine().trim(); - if ( line.length() < 2 ) continue; - if ( line.equalsIgnoreCase("quit") ) break; - if ( Util.isIpAddress(line) == false ) { - System.out.println("Error: Invalid ip address"); - continue; - } - - sTime = System.nanoTime(); - dataBlock = algorithm==2 ? seacher.binarySearch(line) : seacher.btreeSearch(line); - cTime = (System.nanoTime() - sTime) / 1000000; - System.out.printf("%s in %.5f millseconds\n", dataBlock, cTime); - } - - reader.close(); - seacher.close(); - System.out.println("+--Bye"); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (DbMakerConfigException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } + public static void main(String[] argv) + { + if ( argv.length == 0 ) { + System.out.println("| Usage: java -jar ip2region-{version}.jar [ip2region db file]"); + return; + } + + int algorithm = DbSearcher.BTREE_ALGORITHM; + File file = new File(argv[0]); + if ( file.exists() == false ) { + System.out.println("Error: Invalid ip2region.db file"); + return; + } + + if ( argv.length > 1 ) { + if ( argv[1].equalsIgnoreCase("binary")) algorithm = DbSearcher.BIN_ALGORITHM; + } + + try { + System.out.println("initializing "+((algorithm==2)?"Binary":"B-tree")+" ... "); + DbConfig config = new DbConfig(); + DbSearcher seacher = new DbSearcher(config, argv[0]); + BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); + + System.out.println("+----------------------------------+"); + System.out.println("| ip2region test shell |"); + System.out.println("| Author: chenxin619315@gmail.com |"); + System.out.println("| Type 'quit' to exit program |"); + System.out.println("+----------------------------------+"); + + double sTime = 0, cTime = 0; + String line = null; + DataBlock dataBlock = null; + while ( true ) + { + System.out.print("ip2region>> "); + line = reader.readLine().trim(); + if ( line.length() < 2 ) continue; + if ( line.equalsIgnoreCase("quit") ) break; + if ( Util.isIpAddress(line) == false ) { + System.out.println("Error: Invalid ip address"); + continue; + } + + sTime = System.nanoTime(); + dataBlock = algorithm==2 ? seacher.binarySearch(line) : seacher.btreeSearch(line); + cTime = (System.nanoTime() - sTime) / 1000000; + System.out.printf("%s in %.5f millseconds\n", dataBlock, cTime); + } + + reader.close(); + seacher.close(); + System.out.println("+--Bye"); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (DbMakerConfigException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } } diff --git a/binding/java/src/org/lionsoul/ip2region/test/TestUnit.java b/binding/java/src/org/lionsoul/ip2region/test/TestUnit.java index 79572a3..5a1a197 100644 --- a/binding/java/src/org/lionsoul/ip2region/test/TestUnit.java +++ b/binding/java/src/org/lionsoul/ip2region/test/TestUnit.java @@ -19,78 +19,78 @@ import org.lionsoul.ip2region.DbSearcher; public class TestUnit { - public static void main(String[] args) { - try { - DbSearcher _searcher = new DbSearcher(new DbConfig(), "./data/ip2region.db"); - BufferedReader bfr = new BufferedReader(new FileReader("./data/ip.merge.txt")); - BufferedWriter bwr = new BufferedWriter(new FileWriter("./data/error_log.txt", true)); - int errCount = 0; - int lineCount = 0; - String str = null; - - while ( (str = bfr.readLine()) != null ) { - StringBuffer line = new StringBuffer(str); - //get first ip - int first_idx = line.indexOf("|"); - String first_ip = line.substring(0, first_idx); - - line = new StringBuffer( line.substring(first_idx + 1) ); - - //get second ip - int second_idx = line.indexOf("|"); - String second_ip = line.substring(0, second_idx); - - //get addr - String source_region = line.substring(second_idx + 1); - - //search from DbSearcher - System.out.println("+---Start, start to search"); - System.out.println("+---[Info]: Source region = "+source_region); - - System.out.println("+---[Info]: Step1, search for first IP: "+first_ip); - DataBlock fdata = _searcher.binarySearch(first_ip); - if ( ! fdata.getRegion().equalsIgnoreCase( source_region ) ) { - System.out.println("[Error]: Search first IP failed, DB region = "+fdata.getRegion()); - bwr.write("[Source]: Region: "+fdata.getRegion()); - bwr.newLine(); - bwr.write("[Source]: First Ip: "+first_ip); - bwr.newLine(); - bwr.write("[DB]: Region: "+fdata.getRegion()); - bwr.newLine(); - bwr.flush(); - errCount++; - } - - System.out.println("+---[Info]: Step2, search for second IP: "+second_ip); - DataBlock sdata = _searcher.btreeSearch(second_ip); - if ( ! sdata.getRegion().equalsIgnoreCase( source_region ) ) { - System.out.println("[Error]: Search second IP failed, DB region = "+sdata.getRegion()); - bwr.write("[Source]: Region: "+sdata.getRegion()); - bwr.newLine(); - bwr.write("[Source]: First Ip: "+second_ip); - bwr.newLine(); - bwr.write("[DB]: Region: "+sdata.getRegion()); - bwr.newLine(); - bwr.flush(); - errCount++; - } - - lineCount++; - } - - bwr.close(); - bfr.close(); - System.out.println("+---Done, search complished"); - System.out.println("+---Statistics, Error count = "+errCount - +", Total line = "+lineCount - +", Fail ratio = "+((float)(errCount/lineCount))*100+"%"); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (DbMakerConfigException e) { - e.printStackTrace(); - } catch ( Exception e ) { - e.printStackTrace(); - } - } + public static void main(String[] args) { + try { + DbSearcher _searcher = new DbSearcher(new DbConfig(), "./data/ip2region.db"); + BufferedReader bfr = new BufferedReader(new FileReader("./data/ip.merge.txt")); + BufferedWriter bwr = new BufferedWriter(new FileWriter("./data/error_log.txt", true)); + int errCount = 0; + int lineCount = 0; + String str = null; + + while ( (str = bfr.readLine()) != null ) { + StringBuffer line = new StringBuffer(str); + //get first ip + int first_idx = line.indexOf("|"); + String first_ip = line.substring(0, first_idx); + + line = new StringBuffer( line.substring(first_idx + 1) ); + + //get second ip + int second_idx = line.indexOf("|"); + String second_ip = line.substring(0, second_idx); + + //get addr + String source_region = line.substring(second_idx + 1); + + //search from DbSearcher + System.out.println("+---Start, start to search"); + System.out.println("+---[Info]: Source region = "+source_region); + + System.out.println("+---[Info]: Step1, search for first IP: "+first_ip); + DataBlock fdata = _searcher.binarySearch(first_ip); + if ( ! fdata.getRegion().equalsIgnoreCase( source_region ) ) { + System.out.println("[Error]: Search first IP failed, DB region = "+fdata.getRegion()); + bwr.write("[Source]: Region: "+fdata.getRegion()); + bwr.newLine(); + bwr.write("[Source]: First Ip: "+first_ip); + bwr.newLine(); + bwr.write("[DB]: Region: "+fdata.getRegion()); + bwr.newLine(); + bwr.flush(); + errCount++; + } + + System.out.println("+---[Info]: Step2, search for second IP: "+second_ip); + DataBlock sdata = _searcher.btreeSearch(second_ip); + if ( ! sdata.getRegion().equalsIgnoreCase( source_region ) ) { + System.out.println("[Error]: Search second IP failed, DB region = "+sdata.getRegion()); + bwr.write("[Source]: Region: "+sdata.getRegion()); + bwr.newLine(); + bwr.write("[Source]: First Ip: "+second_ip); + bwr.newLine(); + bwr.write("[DB]: Region: "+sdata.getRegion()); + bwr.newLine(); + bwr.flush(); + errCount++; + } + + lineCount++; + } + + bwr.close(); + bfr.close(); + System.out.println("+---Done, search complished"); + System.out.println("+---Statistics, Error count = "+errCount + +", Total line = "+lineCount + +", Fail ratio = "+((float)(errCount/lineCount))*100+"%"); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (DbMakerConfigException e) { + e.printStackTrace(); + } catch ( Exception e ) { + e.printStackTrace(); + } + } } diff --git a/binding/java/src/org/lionsoul/ip2region/test/TestUtil.java b/binding/java/src/org/lionsoul/ip2region/test/TestUtil.java index c10cc53..14429d8 100644 --- a/binding/java/src/org/lionsoul/ip2region/test/TestUtil.java +++ b/binding/java/src/org/lionsoul/ip2region/test/TestUtil.java @@ -9,49 +9,49 @@ import org.lionsoul.ip2region.Util; */ public class TestUtil { - public static void main(String[] argv) - { -/* //1. test the ip2long - String[] ipSet = new String[]{ - "120.24.78.68", - "120.24.229.68", - "120.24.87.145", - "218.17.162.99" - }; - - for ( String ip : ipSet ) - { - int ipInt = Util.ip2Int(ip); - System.out.println("src ip: " + ip + ", ip2Int: " + ipInt + ", int2IP: " + Util.int2IP(ipInt)); - }*/ - -/* int[] arr = new int[]{12344, -1234, 2146789, 0, -1024}; - byte[] b = new byte[arr.length * 4]; - - //write the int - System.out.println("+--Testing writeInt ... "); - int i, idx = 0; - for ( i = 0; i < b.length; i += 4 ) - { - System.out.println("offset: " + i); - Util.writeInt(b, i, arr[idx++]); - } - System.out.println("|----[Ok]"); - - //read the int - System.out.println("+--Testing getInt ... "); - idx = 0; - for ( i = 0; i < b.length; i += 4 ) - { - System.out.println(arr[idx++]+", " + Util.getInt(b, i)); - } - System.out.println("|----[Ok]");*/ - -/* HeaderBlock headerBlock = new HeaderBlock(241658345, 2134785); - byte[] b = headerBlock.getBytes(); - System.out.println(headerBlock.getIndexStartIp() + ", " + headerBlock.getIndexPtr()); - System.out.println(Util.getInt(b, 0) + ", " + Util.getInt(b, 4));*/ - - System.out.println(Util.ip2long("255.255.255.0")); - } + public static void main(String[] argv) + { +/* //1. test the ip2long + String[] ipSet = new String[]{ + "120.24.78.68", + "120.24.229.68", + "120.24.87.145", + "218.17.162.99" + }; + + for ( String ip : ipSet ) + { + int ipInt = Util.ip2Int(ip); + System.out.println("src ip: " + ip + ", ip2Int: " + ipInt + ", int2IP: " + Util.int2IP(ipInt)); + }*/ + +/* int[] arr = new int[]{12344, -1234, 2146789, 0, -1024}; + byte[] b = new byte[arr.length * 4]; + + //write the int + System.out.println("+--Testing writeInt ... "); + int i, idx = 0; + for ( i = 0; i < b.length; i += 4 ) + { + System.out.println("offset: " + i); + Util.writeInt(b, i, arr[idx++]); + } + System.out.println("|----[Ok]"); + + //read the int + System.out.println("+--Testing getInt ... "); + idx = 0; + for ( i = 0; i < b.length; i += 4 ) + { + System.out.println(arr[idx++]+", " + Util.getInt(b, i)); + } + System.out.println("|----[Ok]");*/ + +/* HeaderBlock headerBlock = new HeaderBlock(241658345, 2134785); + byte[] b = headerBlock.getBytes(); + System.out.println(headerBlock.getIndexStartIp() + ", " + headerBlock.getIndexPtr()); + System.out.println(Util.getInt(b, 0) + ", " + Util.getInt(b, 4));*/ + + System.out.println(Util.ip2long("255.255.255.0")); + } } diff --git a/binding/php/Ip2Region.class.php b/binding/php/Ip2Region.class.php index 8bfe64e..066930a 100644 --- a/binding/php/Ip2Region.class.php +++ b/binding/php/Ip2Region.class.php @@ -2,252 +2,252 @@ /** * ip2region php seacher client class * - * @author chenxin - * @date 2015-10-29 + * @author chenxin + * @date 2015-10-29 */ -defined('INDEX_BLOCK_LENGTH') or define('INDEX_BLOCK_LENGTH', 12); -defined('TOTAL_HEADER_LENGTH') or define('TOTAL_HEADER_LENGTH', 4096); +defined('INDEX_BLOCK_LENGTH') or define('INDEX_BLOCK_LENGTH', 12); +defined('TOTAL_HEADER_LENGTH') or define('TOTAL_HEADER_LENGTH', 4096); class Ip2Region { - /** - * db file handler - */ - private $dbFileHandler = NULL; + /** + * db file handler + */ + private $dbFileHandler = NULL; - /** - * header block info - */ - private $HeaderSip = NULL; - private $HeaderPtr = NULL; - private $headerLen = 0; + /** + * header block info + */ + private $HeaderSip = NULL; + private $HeaderPtr = NULL; + private $headerLen = 0; - /** - * super block index info - */ - private $firstIndexPtr = 0; - private $lastIndexPtr = 0; - private $totalBlocks = 0; - - /** - * construct method - * - * @param ip2regionFile - */ - public function __construct( $ip2regionFile ) - { - $this->dbFileHandler = fopen($ip2regionFile, 'r'); - } + /** + * super block index info + */ + private $firstIndexPtr = 0; + private $lastIndexPtr = 0; + private $totalBlocks = 0; + + /** + * construct method + * + * @param ip2regionFile + */ + public function __construct( $ip2regionFile ) + { + $this->dbFileHandler = fopen($ip2regionFile, 'r'); + } - /** - * get the data block throught the specifield ip address or long ip numeric with binary search algorithm - * - * @param ip - * @return mixed Array or NULL for any error - */ - public function binarySearch( $ip ) - { - //check and conver the ip address - if ( is_string($ip) ) $ip = ip2long($ip); - if ( $this->totalBlocks == 0 ) - { - fseek($this->dbFileHandler, 0); - $superBlock = fread($this->dbFileHandler, 8); + /** + * get the data block throught the specifield ip address or long ip numeric with binary search algorithm + * + * @param ip + * @return mixed Array or NULL for any error + */ + public function binarySearch( $ip ) + { + //check and conver the ip address + if ( is_string($ip) ) $ip = ip2long($ip); + if ( $this->totalBlocks == 0 ) + { + fseek($this->dbFileHandler, 0); + $superBlock = fread($this->dbFileHandler, 8); - $this->firstIndexPtr = self::getLong($superBlock, 0); - $this->lastIndexPtr = self::getLong($superBlock, 4); - $this->totalBlocks = ($this->lastIndexPtr-$this->firstIndexPtr)/INDEX_BLOCK_LENGTH + 1; - } + $this->firstIndexPtr = self::getLong($superBlock, 0); + $this->lastIndexPtr = self::getLong($superBlock, 4); + $this->totalBlocks = ($this->lastIndexPtr-$this->firstIndexPtr)/INDEX_BLOCK_LENGTH + 1; + } - //binary search to define the data - $l = 0; - $h = $this->totalBlocks; - $dataPtr = 0; - while ( $l <= $h ) - { - $m = (($l + $h) >> 1); - $p = $m * INDEX_BLOCK_LENGTH; + //binary search to define the data + $l = 0; + $h = $this->totalBlocks; + $dataPtr = 0; + while ( $l <= $h ) + { + $m = (($l + $h) >> 1); + $p = $m * INDEX_BLOCK_LENGTH; - fseek($this->dbFileHandler, $this->firstIndexPtr + $p); - $buffer = fread($this->dbFileHandler, INDEX_BLOCK_LENGTH); - $sip = self::getLong($buffer, 0); - if ( $ip < $sip ) { - $h = $m - 1; - } else { - $eip = self::getLong($buffer, 4); - if ( $ip > $eip ) { - $l = $m + 1; - } else { - $dataPtr = self::getLong($buffer, 8); - break; - } - } - } + fseek($this->dbFileHandler, $this->firstIndexPtr + $p); + $buffer = fread($this->dbFileHandler, INDEX_BLOCK_LENGTH); + $sip = self::getLong($buffer, 0); + if ( $ip < $sip ) { + $h = $m - 1; + } else { + $eip = self::getLong($buffer, 4); + if ( $ip > $eip ) { + $l = $m + 1; + } else { + $dataPtr = self::getLong($buffer, 8); + break; + } + } + } - //not matched just stop it here - if ( $dataPtr == 0 ) return NULL; + //not matched just stop it here + if ( $dataPtr == 0 ) return NULL; - //get the data - $dataLen = (($dataPtr >> 24) & 0xFF); - $dataPtr = ($dataPtr & 0x00FFFFFF); + //get the data + $dataLen = (($dataPtr >> 24) & 0xFF); + $dataPtr = ($dataPtr & 0x00FFFFFF); - fseek($this->dbFileHandler, $dataPtr); - $data = fread($this->dbFileHandler, $dataLen); + fseek($this->dbFileHandler, $dataPtr); + $data = fread($this->dbFileHandler, $dataLen); - return array( - 'city_id' => self::getLong($data, 0), - 'region' => substr($data, 4) - ); - } + return array( + 'city_id' => self::getLong($data, 0), + 'region' => substr($data, 4) + ); + } - /** - * get the data block associated with the specifield ip with b-tree search algorithm - * - * @param ip - * @return Mixed Array for NULL for any error - */ - public function btreeSearch( $ip ) - { - if ( is_string($ip) ) $ip = ip2long($ip); + /** + * get the data block associated with the specifield ip with b-tree search algorithm + * + * @param ip + * @return Mixed Array for NULL for any error + */ + public function btreeSearch( $ip ) + { + if ( is_string($ip) ) $ip = ip2long($ip); - //check and load the header - if ( $this->HeaderSip == NULL ) - { - fseek($this->dbFileHandler, 8); - $buffer = fread($this->dbFileHandler, TOTAL_HEADER_LENGTH); - - //fill the header - $idx = 0; - $this->HeaderSip = array(); - $this->HeaderPtr = array(); - for ( $i = 0; $i < TOTAL_HEADER_LENGTH; $i += 8 ) - { - $startIp = self::getLong($buffer, $i); - $dataPtr = self::getLong($buffer, $i + 4); - if ( $dataPtr == 0 ) break; + //check and load the header + if ( $this->HeaderSip == NULL ) + { + fseek($this->dbFileHandler, 8); + $buffer = fread($this->dbFileHandler, TOTAL_HEADER_LENGTH); + + //fill the header + $idx = 0; + $this->HeaderSip = array(); + $this->HeaderPtr = array(); + for ( $i = 0; $i < TOTAL_HEADER_LENGTH; $i += 8 ) + { + $startIp = self::getLong($buffer, $i); + $dataPtr = self::getLong($buffer, $i + 4); + if ( $dataPtr == 0 ) break; - $this->HeaderSip[] = $startIp; - $this->HeaderPtr[] = $dataPtr; - $idx++; - } + $this->HeaderSip[] = $startIp; + $this->HeaderPtr[] = $dataPtr; + $idx++; + } - $this->headerLen = $idx; - } - - //1. define the index block with the binary search - $l = 0; $h = $this->headerLen; $sptr = 0; $eptr = 0; - while ( $l <= $h ) - { - $m = (($l + $h) >> 1); - - //perfetc matched, just return it - if ( $ip == $this->HeaderSip[$m] ) { - if ( $m > 0 ) { - $sptr = $this->HeaderPtr[$m-1]; - $eptr = $this->HeaderPtr[$m ]; - } else { - $sptr = $this->HeaderPtr[$m ]; - $eptr = $this->HeaderPtr[$m+1]; - } - - break; - } - - //less then the middle value - if ( $ip < $this->HeaderSip[$m] ) { - if ( $m == 0 ) { - $sptr = $this->HeaderPtr[$m ]; - $eptr = $this->HeaderPtr[$m+1]; - break; - } else if ( $ip > $this->HeaderSip[$m-1] ) { - $sptr = $this->HeaderPtr[$m-1]; - $eptr = $this->HeaderPtr[$m ]; - break; - } - $h = $m - 1; - } else { - if ( $m == $this->headerLen - 1 ) { - $sptr = $this->HeaderPtr[$m-1]; - $eptr = $this->HeaderPtr[$m ]; - break; - } else if ( $ip <= $this->HeaderSip[$m+1] ) { - $sptr = $this->HeaderPtr[$m ]; - $eptr = $this->HeaderPtr[$m+1]; - break; - } - $l = $m + 1; - } - } - - //match nothing just stop it - if ( $sptr == 0 ) return NULL; - - //2. search the index blocks to define the data - $blockLen = $eptr - $sptr; - fseek($this->dbFileHandler, $sptr); - $index = fread($this->dbFileHandler, $blockLen + INDEX_BLOCK_LENGTH); - - $dataptr = 0; - $l = 0; $h = $blockLen / INDEX_BLOCK_LENGTH; - while ( $l <= $h ) { - $m = (($l + $h) >> 1); - $p = (int)($m * INDEX_BLOCK_LENGTH); - $sip = self::getLong($index, $p); - if ( $ip < $sip ) { - $h = $m - 1; - } else { - $eip = self::getLong($index, $p + 4); - if ( $ip > $eip ) { - $l = $m + 1; - } else { - $dataptr = self::getLong($index, $p + 8); - break; - } - } - } - - //not matched - if ( $dataptr == 0 ) return NULL; - - //3. get the data - $dataLen = (($dataptr >> 24) & 0xFF); - $dataPtr = ($dataptr & 0x00FFFFFF); - - fseek($this->dbFileHandler, $dataPtr); - $data = fread($this->dbFileHandler, $dataLen); + $this->headerLen = $idx; + } + + //1. define the index block with the binary search + $l = 0; $h = $this->headerLen; $sptr = 0; $eptr = 0; + while ( $l <= $h ) + { + $m = (($l + $h) >> 1); + + //perfetc matched, just return it + if ( $ip == $this->HeaderSip[$m] ) { + if ( $m > 0 ) { + $sptr = $this->HeaderPtr[$m-1]; + $eptr = $this->HeaderPtr[$m ]; + } else { + $sptr = $this->HeaderPtr[$m ]; + $eptr = $this->HeaderPtr[$m+1]; + } + + break; + } + + //less then the middle value + if ( $ip < $this->HeaderSip[$m] ) { + if ( $m == 0 ) { + $sptr = $this->HeaderPtr[$m ]; + $eptr = $this->HeaderPtr[$m+1]; + break; + } else if ( $ip > $this->HeaderSip[$m-1] ) { + $sptr = $this->HeaderPtr[$m-1]; + $eptr = $this->HeaderPtr[$m ]; + break; + } + $h = $m - 1; + } else { + if ( $m == $this->headerLen - 1 ) { + $sptr = $this->HeaderPtr[$m-1]; + $eptr = $this->HeaderPtr[$m ]; + break; + } else if ( $ip <= $this->HeaderSip[$m+1] ) { + $sptr = $this->HeaderPtr[$m ]; + $eptr = $this->HeaderPtr[$m+1]; + break; + } + $l = $m + 1; + } + } + + //match nothing just stop it + if ( $sptr == 0 ) return NULL; + + //2. search the index blocks to define the data + $blockLen = $eptr - $sptr; + fseek($this->dbFileHandler, $sptr); + $index = fread($this->dbFileHandler, $blockLen + INDEX_BLOCK_LENGTH); + + $dataptr = 0; + $l = 0; $h = $blockLen / INDEX_BLOCK_LENGTH; + while ( $l <= $h ) { + $m = (($l + $h) >> 1); + $p = (int)($m * INDEX_BLOCK_LENGTH); + $sip = self::getLong($index, $p); + if ( $ip < $sip ) { + $h = $m - 1; + } else { + $eip = self::getLong($index, $p + 4); + if ( $ip > $eip ) { + $l = $m + 1; + } else { + $dataptr = self::getLong($index, $p + 8); + break; + } + } + } + + //not matched + if ( $dataptr == 0 ) return NULL; + + //3. get the data + $dataLen = (($dataptr >> 24) & 0xFF); + $dataPtr = ($dataptr & 0x00FFFFFF); + + fseek($this->dbFileHandler, $dataPtr); + $data = fread($this->dbFileHandler, $dataLen); - return array( - 'city_id' => self::getLong($data, 0), - 'region' => substr($data, 4) - ); - } + return array( + 'city_id' => self::getLong($data, 0), + 'region' => substr($data, 4) + ); + } - /** - * read a long from a byte buffer - * - * @param b - * @param offset - */ - public static function getLong( $b, $offset ) - { - return ( - (ord($b[$offset++])) | - (ord($b[$offset++]) << 8) | - (ord($b[$offset++]) << 16) | - (ord($b[$offset ]) << 24) - ); - } + /** + * read a long from a byte buffer + * + * @param b + * @param offset + */ + public static function getLong( $b, $offset ) + { + return ( + (ord($b[$offset++])) | + (ord($b[$offset++]) << 8) | + (ord($b[$offset++]) << 16) | + (ord($b[$offset ]) << 24) + ); + } - /** - * destruct method, resource destroy - */ - public function __destruct() - { - if ( $this->dbFileHandler != NULL ) fclose($this->dbFileHandler); - $this->HeaderSip = NULL; - $this->HeaderPtr = NULL; - } + /** + * destruct method, resource destroy + */ + public function __destruct() + { + if ( $this->dbFileHandler != NULL ) fclose($this->dbFileHandler); + $this->HeaderSip = NULL; + $this->HeaderPtr = NULL; + } } ?> diff --git a/binding/php/testSeacher.php b/binding/php/testSeacher.php index d3c991f..9c1768b 100644 --- a/binding/php/testSeacher.php +++ b/binding/php/testSeacher.php @@ -2,26 +2,26 @@ /** * Ip2Region php client test script * - * @author chenxin + * @author chenxin */ if ( $argc < 2 ) { - $usage = <<> "; - $line = trim(fgets(STDIN)); - if ( strlen($line) < 2 ) continue; - if ( $line == 'quit' ) break; - if ( ip2long($line) == NULL ) { - echo "Error: invalid ip address\n"; - continue; - } + echo "ip2region>> "; + $line = trim(fgets(STDIN)); + if ( strlen($line) < 2 ) continue; + if ( $line == 'quit' ) break; + if ( ip2long($line) == NULL ) { + echo "Error: invalid ip address\n"; + continue; + } - $s_time = getTime(); - $data = $method==2 ? $ip2regionObj->binarySearch($line) : $ip2regionObj->btreeSearch($line); - $c_time = getTime() - $s_time; - printf("%s|%s in %.5f millseconds\n", $data['city_id'], $data['region'], $c_time); + $s_time = getTime(); + $data = $method==2 ? $ip2regionObj->binarySearch($line) : $ip2regionObj->btreeSearch($line); + $c_time = getTime() - $s_time; + printf("%s|%s in %.5f millseconds\n", $data['city_id'], $data['region'], $c_time); } function getTime() { - return (microtime(true) * 1000); + return (microtime(true) * 1000); } ?> diff --git a/binding/python/ip2Region.py b/binding/python/ip2Region.py index 087ae3c..6fa1e65 100644 --- a/binding/python/ip2Region.py +++ b/binding/python/ip2Region.py @@ -8,185 +8,185 @@ import struct, io, socket, sys class Ip2Region(object): - __headerSip = [] - __headerPtr = [] - __f = None + __headerSip = [] + __headerPtr = [] + __f = None - def __init__(self, dbfile): - self.initDatabase(dbfile) + def __init__(self, dbfile): + self.initDatabase(dbfile) - def binarySearch(self, ip): - """ - " binary search method - " param: ip - """ - if not ip.isdigit(): ip = self.ip2long(ip) + def binarySearch(self, ip): + """ + " binary search method + " param: ip + """ + if not ip.isdigit(): ip = self.ip2long(ip) - self.__f.seek(0) - b = self.__f.read(8) - startPtr = self.getLong(b, 0) - endPtr = self.getLong(b, 4) + self.__f.seek(0) + b = self.__f.read(8) + startPtr = self.getLong(b, 0) + endPtr = self.getLong(b, 4) - indexLen = endPtr - startPtr - self.__f.seek(startPtr) - b = self.__f.read(indexLen+12) + indexLen = endPtr - startPtr + self.__f.seek(startPtr) + b = self.__f.read(indexLen+12) - l, h, mixPtr = (0, int(indexLen/12), 0) - while l <= h: - m = int((l+h)/2) - ptr = startPtr + m*12 - self.__f.seek(ptr) + l, h, mixPtr = (0, int(indexLen/12), 0) + while l <= h: + m = int((l+h)/2) + ptr = startPtr + m*12 + self.__f.seek(ptr) - b = self.__f.read(12) - sip = self.getLong(b, 0) - eip = self.getLong(b, 4) + b = self.__f.read(12) + sip = self.getLong(b, 0) + eip = self.getLong(b, 4) - if ip > sip: - if ip > eip: - l = m + 1 - else: - mixPtr = self.getLong(b, 8) - break; - else: - h = m - 1 + if ip > sip: + if ip > eip: + l = m + 1 + else: + mixPtr = self.getLong(b, 8) + break; + else: + h = m - 1 - if mixPtr == 0: return "N2" + if mixPtr == 0: return "N2" - dataPtr = mixPtr & 0x00FFFFFFL - dataLen = (mixPtr >> 24) & 0xFF - - self.__f.seek(dataPtr) - data = self.__f.read(dataLen) - return { - "city_id": self.getLong(data, 0), - "region" : data[4:] - } + dataPtr = mixPtr & 0x00FFFFFFL + dataLen = (mixPtr >> 24) & 0xFF + + self.__f.seek(dataPtr) + data = self.__f.read(dataLen) + return { + "city_id": self.getLong(data, 0), + "region" : data[4:] + } - def btreeSearch(self, ip): - """ - " b-tree search method - " param: ip - """ - if not ip.isdigit(): ip = self.ip2long(ip) + def btreeSearch(self, ip): + """ + " b-tree search method + " param: ip + """ + if not ip.isdigit(): ip = self.ip2long(ip) - headerLen = len(self.__headerSip) - 1 - l, h, sptr, eptr = (0, headerLen, 0, 0) - while l <= h: - m = int((l+h)/2) + headerLen = len(self.__headerSip) - 1 + l, h, sptr, eptr = (0, headerLen, 0, 0) + while l <= h: + m = int((l+h)/2) - if ip == self.__headerSip[m]: - if m > 0: - sptr = self.__headerPtr[m-1] - eptr = self.__headerPtr[m] - break; - else: - sptr = self.__headerPtr[m] - eptr = self.__headerPtr[m+1] - break; + if ip == self.__headerSip[m]: + if m > 0: + sptr = self.__headerPtr[m-1] + eptr = self.__headerPtr[m] + break; + else: + sptr = self.__headerPtr[m] + eptr = self.__headerPtr[m+1] + break; - if ip > self.__headerSip[m]: - if m == headerLen: - sptr = self.__headerPtr[m-1] - eptr = self.__headerPtr[m] - break; - elif ip < self.__headerSip[m+1]: - sptr = self.__headerPtr[m] - eptr = self.__headerPtr[m+1] - break; + if ip > self.__headerSip[m]: + if m == headerLen: + sptr = self.__headerPtr[m-1] + eptr = self.__headerPtr[m] + break; + elif ip < self.__headerSip[m+1]: + sptr = self.__headerPtr[m] + eptr = self.__headerPtr[m+1] + break; - l = m + 1 - else: - if m == 0: - sptr = self.__headerPtr[m] - eptr = self.__headerPtr[m+1] - break; - elif ip > self.__headerSip[m-1]: - sptr = self.__headerPtr[m-1] - eptr = self.__headerPtr[m] - break; + l = m + 1 + else: + if m == 0: + sptr = self.__headerPtr[m] + eptr = self.__headerPtr[m+1] + break; + elif ip > self.__headerSip[m-1]: + sptr = self.__headerPtr[m-1] + eptr = self.__headerPtr[m] + break; - h = m - 1 + h = m - 1 - if sptr == 0: return "N1" + if sptr == 0: return "N1" - indexLen = eptr - sptr - self.__f.seek(sptr) - b = self.__f.read(indexLen + 12) - - l, h, mixPtr = (0, int(indexLen/12), 0) - while l <= h: - m = int((l+h)/2) - offset = m * 12 + indexLen = eptr - sptr + self.__f.seek(sptr) + b = self.__f.read(indexLen + 12) + + l, h, mixPtr = (0, int(indexLen/12), 0) + while l <= h: + m = int((l+h)/2) + offset = m * 12 - if ip > self.getLong(b, offset): - if ip > self.getLong(b, offset+4): - l = m + 1 - else: - mixPtr = self.getLong(b, offset+8) - break; - else: - h = m - 1 + if ip > self.getLong(b, offset): + if ip > self.getLong(b, offset+4): + l = m + 1 + else: + mixPtr = self.getLong(b, offset+8) + break; + else: + h = m - 1 - if mixPtr == 0: return "N2" + if mixPtr == 0: return "N2" - dataPtr = mixPtr & 0x00FFFFFFL - dataLen = (mixPtr >> 24) & 0xFF - - self.__f.seek(dataPtr) - data = self.__f.read(dataLen) - return { - "city_id": self.getLong(data, 0), - "region" : data[4:] - } + dataPtr = mixPtr & 0x00FFFFFFL + dataLen = (mixPtr >> 24) & 0xFF + + self.__f.seek(dataPtr) + data = self.__f.read(dataLen) + return { + "city_id": self.getLong(data, 0), + "region" : data[4:] + } - def initDatabase(self, dbfile): - """ - " initialize the database for search - " param: dbFile - """ - try: - self.__f = io.open(dbfile, "rb") - #pass the super block - self.__f.seek(8) - #read the header block - b = self.__f.read(4086) - #parse the header block - sip = None - ptr = None - for i in range(0, len(b)-1, 8): - sip = self.getLong(b, i) - ptr = self.getLong(b, i+4) - if ptr == 0: - break - self.__headerSip.append(sip) - self.__headerPtr.append(ptr) + def initDatabase(self, dbfile): + """ + " initialize the database for search + " param: dbFile + """ + try: + self.__f = io.open(dbfile, "rb") + #pass the super block + self.__f.seek(8) + #read the header block + b = self.__f.read(4086) + #parse the header block + sip = None + ptr = None + for i in range(0, len(b)-1, 8): + sip = self.getLong(b, i) + ptr = self.getLong(b, i+4) + if ptr == 0: + break + self.__headerSip.append(sip) + self.__headerPtr.append(ptr) - except IOError, e: - print "[Error]: ", e - sys.exit() + except IOError, e: + print "[Error]: ", e + sys.exit() - def ip2long(self, ip): - _ip = socket.inet_aton(ip) - return struct.unpack("!L", _ip)[0] + def ip2long(self, ip): + _ip = socket.inet_aton(ip) + return struct.unpack("!L", _ip)[0] - def isip(self, ip): - p = ip.split(".") + def isip(self, ip): + p = ip.split(".") - if len(p) != 4 : return False - for pp in p: - if not pp.isdigit(): return False - if len(pp) > 3 : return False - if int(pp) > 255 : return False + if len(p) != 4 : return False + for pp in p: + if not pp.isdigit(): return False + if len(pp) > 3 : return False + if int(pp) > 255 : return False - return True + return True - def getLong(self, b, offset): - if len( b[offset:offset+4] ) == 4: - return struct.unpack('I', b[offset:offset+4])[0] - return 0 + def getLong(self, b, offset): + if len( b[offset:offset+4] ) == 4: + return struct.unpack('I', b[offset:offset+4])[0] + return 0 - def close(self): - self.__headerSip = None - self.__headerPtr = None - self.__f.close() - self.__f = None + def close(self): + self.__headerSip = None + self.__headerPtr = None + self.__f.close() + self.__f = None diff --git a/binding/python/ip2Region.pyc b/binding/python/ip2Region.pyc new file mode 100644 index 0000000000000000000000000000000000000000..dbf7cc5b848ec913ca6366647d652d5fedfd21b8 GIT binary patch literal 4951 zcmb_gOK%(36+U-3q)3XC9NE^RO&r=mQ?|7&OGOOSFkHoU9u8ba7#3Bk#vP0}BWWax zq-I8e6p#o|chyBVU3bx47u^*_HtnKl(RDW27U&=7CP26D_nk|Mv|~3wR7rE@+{c{P zJ?EP<%6(BS-(SA}la>sBa=5SIO4mU`kxft|p(AZ0n_w*O$fh&mbF!Hm@vdyT66Pez z%Qkkp@=KA&B0t?MP(-tyr{oBve5DrmR+DHa?sjUulWZ5aGzwa~QBrHQ4!Yq{ zJ1Sqn);Eu^;Z|+Gdl1|v`FJRdCCD!AHUJdmzoXmQO>yycKuCxU;itL(T;o zmg|@(;xXT+GT)yCpHh_=HToPlDpjNZ3Hx9!jKbYNhlV_kA&PxFFa(M61r z8xFl3IQmRp`kFIR}?DN=!wMlt>oRKRFRwfP!0kKmTF@<(yU1*roZGw zg<}Y(fAjAj07iInMPijj6{!a-88-3)Qk$p#!Ul=4- zljEYS4XQ$}BtHzx7|l$WU47c@{*0PEEAeUgXDJd2!)dwM#~>%Kh84NltXFZ?>32cO zrP7revL;AQSfEL3w{{Sr8be*^1xavl3#E)vjNKA5iDG6-teMLcikZ$CxyEXnw#}9!>+}c*Ut9uUO zWA5Sffhy=>L`5m&@*d$%FGAEVcaC@pV~9Af6|3aymM!CK52&f2%sJ=G z9Dkj%IcMgbOJ>$wFf-XU3_Bi(NDNM9!SIg-;+LYTv_^uE4ARUHAwq zi$bduskMkoN6E9y^>5pi4_y{JsvBjE>MZ zZV)f6qyI8MwkAsV(rfPZy(h@+>0k=z9!Uq4(O zZd;_!tIkFrZ)@xG=MF%$$#Dtj3AhGV29{1+WQ9B%fyy%xP;3R-4zUzUXDE_b3IOGx z04%K%OJ`U*V=TRvB~kQMSV~Z_fO-m6jTH;23a$huk9GMRK=s%Zcm$hznVjxr`=OxJ z2KZdWU=lrd0hdwIjC&q!eZevi%u{*O6iU?bh{AOughHxEPP;8D4>0Fl;3Cy2;H5-J z+8=@O7PxkvL{WL3yJ&iMdpnIX@0(nCndB9av*RSq-eT-6OumU>nt)iCJZ}^lo$~<1 zmrNCaSjF8PkIe#RIa752ndC5IcaETQ46tQq(Yy>`oqsW~@_bH#^$QG6{vjFC4JDwR z3o;4W>{~pwy_EJ&THm&pQj{ga773}Ot{nFnMAR|fHoz}-K`3TsW=`%u`Z-aTMS>cJ zWRx`u#I|0enPG836hIkrh=9M7b;5=g*B%<9aT7Kz9BV&h4qLLZ(Rp25G_Ct(N&YPc z0L)(H9bn2k@8YGshZY1aWyi<|EIVzA)7x-fy9u_QanO!Gj%wL%RO9QBEpKhRn+)q^ zvgru7-bUMt8GV%}TekK150cOpj+PLUEoJWi?5!l}CXa5_8k(^7_21QG?DmkTPiMBy zH@clD;}IqJ+K9XwIL&zPGC0lDpod38?+V8}&j;_T7TW}zq&C%$<4k)QTO;fXY`!m% zV;7_yM65JAbDDc}``5VA8i?d`=Awg_y;Cp+^BNj~dE~c?R$$SYH6{Csr%Al-;Ywcv zIZZ>`Lb&`;<6UQ{H7aShwI5{)1TiA$2U)jc7qaB2l_`7F z=>@HQl`?@H6za#lvj>4E{(D?0jS}=cEy5@VRB;bi`Vj~!ZBM^%hmNB5Y{}r=kdx$F z>=K*D?{W72qq|xuIc;w6{9Rqdso82kyTW(X2XYqYLO0UGcVqP^ZhL;z! zNgcfkCRAwj;hkkHWFt`ki`YJ#1wfBK?TISh_ed_25I_`x^sHO$F23`rMi2Jvo~%LC zpK+x{5JB0OaDWZ%ONJl)o!}ty{fr>!`}Q9%zCU4`nyL`#VvBc)kjc>;ih12IR6D(Wqs-Z literal 0 HcmV?d00001 diff --git a/binding/python/testSearcher.py b/binding/python/testSearcher.py index af02480..9ecbd17 100644 --- a/binding/python/testSearcher.py +++ b/binding/python/testSearcher.py @@ -9,67 +9,67 @@ import struct, sys, os, time from ip2Region import Ip2Region def testSearch(): - """ - " ip2region test function - """ - llen = len(sys.argv) + """ + " ip2region test function + """ + llen = len(sys.argv) - if llen < 2: - print "Usage: python ip2Region.py [ip2region db file] [alrogrithm]" - print "Algorithm: binary or b-tree" - return 0 + if llen < 2: + print "Usage: python ip2Region.py [ip2region db file] [alrogrithm]" + print "Algorithm: binary or b-tree" + return 0 - dbFile = sys.argv[1] - method = 1 - algorithm = "b-tree" - - if (not os.path.isfile(dbFile)) or (not os.path.exists(dbFile)): - print "[Error]: Specified db file is not exists." - return 0 + dbFile = sys.argv[1] + method = 1 + algorithm = "b-tree" + + if (not os.path.isfile(dbFile)) or (not os.path.exists(dbFile)): + print "[Error]: Specified db file is not exists." + return 0 - if llen > 2: - algorithm = sys.argv[2] - if algorithm == "binary": - method = 2 + if llen > 2: + algorithm = sys.argv[2] + if algorithm == "binary": + method = 2 - print "initializing %s..." % (algorithm) - print "+----------------------------------+" - print "| ip2region test script |" - print "| Author: komazhang@foxmail.com |" - print "| Type 'quit' to exit program |" - print "+----------------------------------+" + print "initializing %s..." % (algorithm) + print "+----------------------------------+" + print "| ip2region test script |" + print "| Author: komazhang@foxmail.com |" + print "| Type 'quit' to exit program |" + print "+----------------------------------+" - searcher = Ip2Region(dbFile); + searcher = Ip2Region(dbFile); - while True: - line = raw_input("ip2region>> ") - line = line.strip() + while True: + line = raw_input("ip2region>> ") + line = line.strip() - if line == "": - print "[Error]: Invalid ip address." - continue + if line == "": + print "[Error]: Invalid ip address." + continue - if line == "quit": - print "[Info]: Thanks for your use, Bye." - break + if line == "quit": + print "[Info]: Thanks for your use, Bye." + break - if not searcher.isip(line): - print "[Error]: Invalid ip address." - continue + if not searcher.isip(line): + print "[Error]: Invalid ip address." + continue - sTime = time.time() * 1000 - if method == 1: - data = searcher.btreeSearch(line) - else: - data = searcher.binarySearch(line) - eTime = time.time() * 1000 + sTime = time.time() * 1000 + if method == 1: + data = searcher.btreeSearch(line) + else: + data = searcher.binarySearch(line) + eTime = time.time() * 1000 - if isinstance(data, dict): - print "[Return]: %s|%s in %f millseconds" % (data["city_id"], data["region"], eTime-sTime) - else: - print "[Error]: ", data + if isinstance(data, dict): + print "[Return]: %s|%s in %f millseconds" % (data["city_id"], data["region"], eTime-sTime) + else: + print "[Error]: ", data - searcher.close() + searcher.close() if __name__ == "__main__": - testSearch() \ No newline at end of file + testSearch()