diff --git a/binding/c/Makefile b/binding/c/Makefile index 3031f3b..f4724ba 100644 --- a/binding/c/Makefile +++ b/binding/c/Makefile @@ -1,24 +1,27 @@ -all: xdb_searcher util_test +all: xdb_searcher test_util -xdb_searcher: xdb_searcher.h xdb_searcher.c main.c - gcc -O2 -I./ xdb_searcher.c main.c -o xdb_searcher +xdb_searcher: xdb_api.h xdb_util.c xdb_searcher.c main.c + gcc -O2 -I./ xdb_util.c xdb_searcher.c main.c -o xdb_searcher -util_test: xdb_searcher.h xdb_searcher.c util_test.c - gcc -O2 -I./ xdb_searcher.c util_test.c -o util_test +test_util: xdb_api.h xdb_util.c test_util.c + gcc -O2 -I./ xdb_util.c test_util.c -o test_util xdb_searcher.o: xdb_searcher.c - gcc -c -o xdb_searcher.o xdb_searcher.c + gcc -c xdb_searcher.c -xdb_searcher_lib: xdb_searcher.o +xdb_util.o: xdb_util.c + gcc -c xdb_util.c + +xdb_searcher_lib: xdb_util.o xdb_searcher.o mkdir -p build/lib mkdir -p build/include - ar -rc build/lib/libxdb_searcher.a `find . -name *.o` - cp xdb_searcher.h build/include + ar -rc build/lib/libxdb.a `find . -name *.o` + cp xdb_api.h build/include clean: find ./ -name \*.o | xargs rm -f - find ./ -name util_test | xargs rm -f + find ./ -name test_util | xargs rm -f find ./ -name xdb_searcher | xargs rm -f rm -rf build -.PHONY: all clean xdb_searcher util_test +.PHONY: all clean xdb_searcher test_util diff --git a/binding/c/ReadMe.md b/binding/c/ReadMe.md index 591f905..b684f5a 100644 --- a/binding/c/ReadMe.md +++ b/binding/c/ReadMe.md @@ -7,7 +7,7 @@ ```c #include -#include "xdb_searcher.h" +#include "xdb_api.h" int main(int argc, char *argv[]) { char *db_path = "ip2region.xdb file path"; @@ -45,7 +45,7 @@ int main(int argc, char *argv[]) { 我们可以提前从 xdb 文件中加载出来 VectorIndex 数据,然后全局缓存,每次创建 Searcher 对象的时候使用全局的 VectorIndex 缓存可以减少一次固定的 IO 操作,从而加速查询,减少 IO 压力。 ```c #include -#include "xdb_searcher.h" +#include "xdb_api.h" int main(int argc, char *argv[]) { char *db_path = "ip2region.xdb file path"; @@ -94,7 +94,7 @@ int main(int argc, char *argv[]) { 我们也可以预先加载整个 ip2region.xdb 的数据到内存,然后基于这个数据创建查询对象来实现完全基于文件的查询,类似之前的 memory search。 ```c #include -#include "xdb_searcher.h" +#include "xdb_api.h" int main(int argc, char *argv[]) { char *db_path = "ip2region.xdb file path"; diff --git a/binding/c/main.c b/binding/c/main.c index 49a0f13..8a2219e 100644 --- a/binding/c/main.c +++ b/binding/c/main.c @@ -7,7 +7,7 @@ // @Date 2022/06/28 #include "stdio.h" -#include "xdb_searcher.h" +#include "xdb_api.h" struct searcher_test_entry { xdb_searcher_t searcher; @@ -64,13 +64,13 @@ void destroy_searcher_test(searcher_test_t *test) { // check and free the vector index if (test->v_index != NULL) { - xdb_close_vector_index(test->v_index); + xdb_free_vector_index(test->v_index); test->v_index = NULL; } // check and free the content buffer if (test->c_buffer != NULL) { - xdb_close_content(test->c_buffer); + xdb_free_content(test->c_buffer); test->c_buffer = NULL; } } diff --git a/binding/c/test_util b/binding/c/test_util new file mode 100755 index 0000000..625cdc9 Binary files /dev/null and b/binding/c/test_util differ diff --git a/binding/c/util_test.c b/binding/c/test_util.c similarity index 93% rename from binding/c/util_test.c rename to binding/c/test_util.c index f6828d2..9a9fcb6 100644 --- a/binding/c/util_test.c +++ b/binding/c/test_util.c @@ -7,7 +7,7 @@ // @Date 2022/06/27 #include "stdio.h" -#include "xdb_searcher.h" +#include "xdb_api.h" void test_check_ip() { char *ip_list[] = { @@ -37,7 +37,7 @@ void test_check_ip() { } void test_load_header() { - xdb_header_t *header = xdb_load_header_from_file("../../data/ip2region.xdb"); + xdb_header_t *header = xdb_load_header_from_file("../../data/ip2region_v4.xdb"); if (header == NULL) { printf("failed to load header"); } else { @@ -54,29 +54,29 @@ void test_load_header() { ); } - xdb_close_header(header); + xdb_free_header(header); } void test_load_vector_index() { - xdb_vector_index_t *v_index = xdb_load_vector_index_from_file("../../data/ip2region.xdb"); + xdb_vector_index_t *v_index = xdb_load_vector_index_from_file("../../data/ip2region_v4.xdb"); if (v_index == NULL) { printf("failed to load vector index from file\n"); } else { printf("vector index loaded from file, length=%d\n", v_index->length); } - xdb_close_vector_index(v_index); + xdb_free_vector_index(v_index); } void test_load_content() { - xdb_content_t *content = xdb_load_content_from_file("../../data/ip2region.xdb"); + xdb_content_t *content = xdb_load_content_from_file("../../data/ip2region_v4.xdb"); if (content == NULL) { printf("failed to load content from file\n"); } else { printf("content loaded from file, length=%d\n", content->length); } - xdb_close_content(content); + xdb_free_content(content); } // valgrind --tool=memcheck --leak-check=full ./a.out @@ -98,4 +98,4 @@ int main(int argc, char *argv[]) { printf("|--done\n\n"); return 0; -} \ No newline at end of file +} diff --git a/binding/c/xdb_searcher.h b/binding/c/xdb_api.h similarity index 95% rename from binding/c/xdb_searcher.h rename to binding/c/xdb_api.h index 8be9429..22b1d5e 100644 --- a/binding/c/xdb_searcher.h +++ b/binding/c/xdb_api.h @@ -6,8 +6,8 @@ // @Author Lion // @Date 2022/06/27 -#ifndef C_XDB_SEARCHER_H -#define C_XDB_SEARCHER_H +#ifndef C_IP2REGION_XDB_H +#define C_IP2REGION_XDB_H #include #include @@ -59,7 +59,7 @@ XDB_PUBLIC(xdb_header_t *) xdb_load_header(FILE *); XDB_PUBLIC(xdb_header_t *) xdb_load_header_from_file(const char *); -XDB_PUBLIC(void) xdb_close_header(void *); +XDB_PUBLIC(void) xdb_free_header(void *); // --- vector index buffer @@ -73,7 +73,7 @@ XDB_PUBLIC(xdb_vector_index_t *) xdb_load_vector_index(FILE *); XDB_PUBLIC(xdb_vector_index_t *) xdb_load_vector_index_from_file(const char *); -XDB_PUBLIC(void) xdb_close_vector_index(void *); +XDB_PUBLIC(void) xdb_free_vector_index(void *); // --- content buffer @@ -87,7 +87,7 @@ XDB_PUBLIC(xdb_content_t *) xdb_load_content(FILE *); XDB_PUBLIC(xdb_content_t *) xdb_load_content_from_file(const char *); -XDB_PUBLIC(void) xdb_close_content(void *); +XDB_PUBLIC(void) xdb_free_content(void *); // --- End buffer load @@ -146,4 +146,4 @@ XDB_PUBLIC(unsigned int) xdb_mip(unsigned long, unsigned long); XDB_PUBLIC(long) xdb_now(); -#endif //C_XDB_SEARCHER_H +#endif // C_IP2REGION_XDB_H diff --git a/binding/c/xdb_searcher.c b/binding/c/xdb_searcher.c index 53f9af6..7ad4d16 100644 --- a/binding/c/xdb_searcher.c +++ b/binding/c/xdb_searcher.c @@ -6,35 +6,7 @@ // @Author Lion // @Date 2022/06/27 -#include "xdb_searcher.h" - -// for Linux -#ifdef XDB_LINUX -#include "sys/time.h" -#endif - -// @Note: since 2023/10/13 to compatible with the windows system -#ifdef XDB_WINDOWS -#include -XDB_PRIVATE(int) gettimeofday(struct timeval* tp, void* tzp) { - time_t clock; - struct tm tm; - SYSTEMTIME wtm; - GetLocalTime(&wtm); - tm.tm_year = wtm.wYear - 1900; - tm.tm_mon = wtm.wMonth - 1; - tm.tm_mday = wtm.wDay; - tm.tm_hour = wtm.wHour; - tm.tm_min = wtm.wMinute; - tm.tm_sec = wtm.wSecond; - tm.tm_isdst = -1; - clock = mktime(&tm); - tp->tv_sec = clock; - tp->tv_usec = wtm.wMilliseconds * 1000; - return (0); -} -#endif - +#include "xdb_api.h" // internal function prototype define XDB_PRIVATE(int) read(xdb_searcher_t *, long offset, char *, size_t length); @@ -196,234 +168,3 @@ XDB_PRIVATE(int) read(xdb_searcher_t *xdb, long offset, char *buffer, size_t len XDB_PUBLIC(int) xdb_get_io_count(xdb_searcher_t *xdb) { return xdb->io_count; } - - -// --- buffer load util functions - -XDB_PUBLIC(xdb_header_t *) xdb_load_header(FILE *handle) { - xdb_header_t *header; - unsigned int size = xdb_header_info_length; - - // entry alloc - header = (xdb_header_t *) xdb_malloc(sizeof(xdb_header_t)); - if (header == NULL) { - return NULL; - } - - if (fseek(handle, 0, SEEK_SET) == -1) { - xdb_free(header); - return NULL; - } - - if (fread(header->buffer, 1,size, handle) != size) { - xdb_free(header); - return NULL; - } - - // fill the fields - header->length = size; - header->version = (unsigned short) xdb_get_ushort(header->buffer, 0); - header->index_policy = (unsigned short) xdb_get_ushort(header->buffer, 2); - header->created_at = xdb_get_uint(header->buffer, 4); - header->start_index_ptr = xdb_get_uint(header->buffer, 8); - header->end_index_ptr = xdb_get_uint(header->buffer,12); - - return header; -} - -XDB_PUBLIC(xdb_header_t *) xdb_load_header_from_file(const char *db_path) { - xdb_header_t *header; - FILE *handle = fopen(db_path, "rb"); - if (handle == NULL) { - return NULL; - } - - header = xdb_load_header(handle); - fclose(handle); - return header; -} - -XDB_PUBLIC(void) xdb_close_header(void *ptr) { - xdb_header_t *header = (xdb_header_t *) ptr; - if (header->length > 0) { - header->length = 0; - xdb_free(header); - } -} - -// --- vector index - -XDB_PUBLIC(xdb_vector_index_t *) xdb_load_vector_index(FILE *handle) { - xdb_vector_index_t *v_index; - unsigned int size = xdb_vector_index_length; - - // seek to the vector index offset - if (fseek(handle, xdb_header_info_length, SEEK_SET) == -1) { - return NULL; - } - - // do the buffer read - v_index = (xdb_vector_index_t *) xdb_malloc(sizeof(xdb_vector_index_t)); - if (v_index == NULL) { - return NULL; - } - - v_index->length = size; - if (fread(v_index->buffer, 1, size, handle) != size) { - xdb_free(v_index); - return NULL; - } - - return v_index; -} - -XDB_PUBLIC(xdb_vector_index_t *) xdb_load_vector_index_from_file(const char *db_path) { - xdb_vector_index_t *v_index; - FILE *handle = fopen(db_path, "rb"); - if (handle == NULL) { - return NULL; - } - - v_index = xdb_load_vector_index(handle); - fclose(handle); - return v_index; -} - -XDB_PUBLIC(void) xdb_close_vector_index(void *ptr) { - xdb_vector_index_t *v_index = (xdb_vector_index_t *) ptr; - if (v_index->length > 0) { - v_index->length = 0; - xdb_free(v_index); - } -} - -// --- content buffer - -XDB_PUBLIC(xdb_content_t *) xdb_load_content(FILE *handle) { - unsigned int size; - xdb_content_t *content; - - // determine the file size - if (fseek(handle, 0, SEEK_END) == -1) { - return NULL; - } - - size = (unsigned int) ftell(handle); - if (fseek(handle, 0, SEEK_SET) == -1) { - return NULL; - } - - // do the file read - content = (xdb_content_t *) xdb_malloc(sizeof(xdb_content_t)); - if (content == NULL) { - return NULL; - } - - // do the buffer alloc - content->buffer = (char *) xdb_malloc(size); - if (content->buffer == NULL) { - xdb_free(content); - return NULL; - } - - // read the content into the buffer - content->length = size; - if (fread(content->buffer, 1, size, handle) != size) { - xdb_free(content); - return NULL; - } - - return content; -} - -XDB_PUBLIC(xdb_content_t *) xdb_load_content_from_file(const char *db_path) { - xdb_content_t *content; - FILE *handle = fopen(db_path, "rb"); - if (handle == NULL) { - return NULL; - } - - content = xdb_load_content(handle); - fclose(handle); - return content; -} - -XDB_PUBLIC(void) xdb_close_content(void *ptr) { - xdb_content_t *content = (xdb_content_t *) ptr; - if (content->length > 0) { - content->length = 0; - xdb_free(content->buffer); - content->buffer = NULL; - xdb_free(content); - } -} - -// --- End - -// get unsigned long (4bytes) from a specified buffer start from the specified offset -XDB_PUBLIC(unsigned int) xdb_get_uint(const char *buffer, int offset) { - return ( - ((buffer[offset ]) & 0x000000FF) | - ((buffer[offset+1] << 8) & 0x0000FF00) | - ((buffer[offset+2] << 16) & 0x00FF0000) | - ((buffer[offset+3] << 24) & 0xFF000000) - ); -} - -// get unsigned short (2bytes) from a specified buffer start from the specified offset -XDB_PUBLIC(int) xdb_get_ushort(const char *buffer, int offset) { - return ( - ((buffer[offset ]) & 0x000000FF) | - ((buffer[offset+1] << 8) & 0x0000FF00) - ); -} - -// string ip to unsigned int -static int shiftIndex[4] = {24, 16, 8, 0}; -XDB_PUBLIC(int) xdb_check_ip(const char *src_ip, unsigned int *dst_ip) { - char c; - int i, n, ip = 0; - const char *ptr = src_ip; - for (i = 0; i < 4; i++) { - n = 0; - while (1) { - c = *ptr; - ptr++; - if (c >= '0' && c <= '9') { - n *= 10; - n += c - '0'; - } else if ((i < 3 && c == '.') || i == 3) { - // stopping at the '.' but ignore the tailing chars - // after the 3rd one (auto clean the tailing none-integer ?). - break; - } else { - return 1; - } - } - - if (n > 0xFF) { - return 2; - } - - ip |= (n << shiftIndex[i]); - } - - *dst_ip = ip; - return 0; -} - -// unsigned int ip to string ip -XDB_PUBLIC(void) xdb_long2ip(unsigned int ip, char *buffer) { - sprintf(buffer, "%d.%d.%d.%d", (ip >> 24) & 0xFF, (ip >> 16) & 0xFF, (ip >> 8) & 0xFF, ip & 0xFF); -} - -// get the middle ip of a and b -XDB_PUBLIC(unsigned int) xdb_mip(unsigned long a, unsigned long b) { - return (unsigned int) ((a + b) >> 1); -} - -XDB_PUBLIC(long) xdb_now() { - struct timeval c_time; - gettimeofday(&c_time, NULL); - return c_time.tv_sec * (int)1e6 + c_time.tv_usec; -} diff --git a/binding/c/xdb_util.c b/binding/c/xdb_util.c new file mode 100644 index 0000000..2d6c9e9 --- /dev/null +++ b/binding/c/xdb_util.c @@ -0,0 +1,264 @@ +// Copyright 2022 The Ip2Region Authors. All rights reserved. +// Use of this source code is governed by a Apache2.0-style +// license that can be found in the LICENSE file. + +// --- +// @Author Lion +// @Date 2022/06/27 + +#include "xdb_api.h" + +// for Linux +#ifdef XDB_LINUX +#include "sys/time.h" +#endif + +// @Note: since 2023/10/13 to compatible with the windows system +#ifdef XDB_WINDOWS +#include +XDB_PRIVATE(int) gettimeofday(struct timeval* tp, void* tzp) { + time_t clock; + struct tm tm; + SYSTEMTIME wtm; + GetLocalTime(&wtm); + tm.tm_year = wtm.wYear - 1900; + tm.tm_mon = wtm.wMonth - 1; + tm.tm_mday = wtm.wDay; + tm.tm_hour = wtm.wHour; + tm.tm_min = wtm.wMinute; + tm.tm_sec = wtm.wSecond; + tm.tm_isdst = -1; + clock = mktime(&tm); + tp->tv_sec = clock; + tp->tv_usec = wtm.wMilliseconds * 1000; + return (0); +} +#endif + +XDB_PUBLIC(xdb_header_t *) xdb_load_header(FILE *handle) { + xdb_header_t *header; + unsigned int size = xdb_header_info_length; + + // entry alloc + header = (xdb_header_t *) xdb_malloc(sizeof(xdb_header_t)); + if (header == NULL) { + return NULL; + } + + if (fseek(handle, 0, SEEK_SET) == -1) { + xdb_free(header); + return NULL; + } + + if (fread(header->buffer, 1,size, handle) != size) { + xdb_free(header); + return NULL; + } + + // fill the fields + header->length = size; + header->version = (unsigned short) xdb_get_ushort(header->buffer, 0); + header->index_policy = (unsigned short) xdb_get_ushort(header->buffer, 2); + header->created_at = xdb_get_uint(header->buffer, 4); + header->start_index_ptr = xdb_get_uint(header->buffer, 8); + header->end_index_ptr = xdb_get_uint(header->buffer,12); + + return header; +} + +XDB_PUBLIC(xdb_header_t *) xdb_load_header_from_file(const char *db_path) { + xdb_header_t *header; + FILE *handle = fopen(db_path, "rb"); + if (handle == NULL) { + return NULL; + } + + header = xdb_load_header(handle); + fclose(handle); + return header; +} + +XDB_PUBLIC(void) xdb_free_header(void *ptr) { + xdb_header_t *header = (xdb_header_t *) ptr; + if (header->length > 0) { + header->length = 0; + xdb_free(header); + } +} + +// --- vector index + +XDB_PUBLIC(xdb_vector_index_t *) xdb_load_vector_index(FILE *handle) { + xdb_vector_index_t *v_index; + unsigned int size = xdb_vector_index_length; + + // seek to the vector index offset + if (fseek(handle, xdb_header_info_length, SEEK_SET) == -1) { + return NULL; + } + + // do the buffer read + v_index = (xdb_vector_index_t *) xdb_malloc(sizeof(xdb_vector_index_t)); + if (v_index == NULL) { + return NULL; + } + + v_index->length = size; + if (fread(v_index->buffer, 1, size, handle) != size) { + xdb_free(v_index); + return NULL; + } + + return v_index; +} + +XDB_PUBLIC(xdb_vector_index_t *) xdb_load_vector_index_from_file(const char *db_path) { + xdb_vector_index_t *v_index; + FILE *handle = fopen(db_path, "rb"); + if (handle == NULL) { + return NULL; + } + + v_index = xdb_load_vector_index(handle); + fclose(handle); + return v_index; +} + +XDB_PUBLIC(void) xdb_free_vector_index(void *ptr) { + xdb_vector_index_t *v_index = (xdb_vector_index_t *) ptr; + if (v_index->length > 0) { + v_index->length = 0; + xdb_free(v_index); + } +} + +// --- content buffer + +XDB_PUBLIC(xdb_content_t *) xdb_load_content(FILE *handle) { + unsigned int size; + xdb_content_t *content; + + // determine the file size + if (fseek(handle, 0, SEEK_END) == -1) { + return NULL; + } + + size = (unsigned int) ftell(handle); + if (fseek(handle, 0, SEEK_SET) == -1) { + return NULL; + } + + // do the file read + content = (xdb_content_t *) xdb_malloc(sizeof(xdb_content_t)); + if (content == NULL) { + return NULL; + } + + // do the buffer alloc + content->buffer = (char *) xdb_malloc(size); + if (content->buffer == NULL) { + xdb_free(content); + return NULL; + } + + // read the content into the buffer + content->length = size; + if (fread(content->buffer, 1, size, handle) != size) { + xdb_free(content); + return NULL; + } + + return content; +} + +XDB_PUBLIC(xdb_content_t *) xdb_load_content_from_file(const char *db_path) { + xdb_content_t *content; + FILE *handle = fopen(db_path, "rb"); + if (handle == NULL) { + return NULL; + } + + content = xdb_load_content(handle); + fclose(handle); + return content; +} + +XDB_PUBLIC(void) xdb_free_content(void *ptr) { + xdb_content_t *content = (xdb_content_t *) ptr; + if (content->length > 0) { + content->length = 0; + xdb_free(content->buffer); + content->buffer = NULL; + xdb_free(content); + } +} + +// --- End + +// get unsigned long (4bytes) from a specified buffer start from the specified offset +XDB_PUBLIC(unsigned int) xdb_get_uint(const char *buffer, int offset) { + return ( + ((buffer[offset ]) & 0x000000FF) | + ((buffer[offset+1] << 8) & 0x0000FF00) | + ((buffer[offset+2] << 16) & 0x00FF0000) | + ((buffer[offset+3] << 24) & 0xFF000000) + ); +} + +// get unsigned short (2bytes) from a specified buffer start from the specified offset +XDB_PUBLIC(int) xdb_get_ushort(const char *buffer, int offset) { + return ( + ((buffer[offset ]) & 0x000000FF) | + ((buffer[offset+1] << 8) & 0x0000FF00) + ); +} + +// string ip to unsigned int +static int shiftIndex[4] = {24, 16, 8, 0}; +XDB_PUBLIC(int) xdb_check_ip(const char *src_ip, unsigned int *dst_ip) { + char c; + int i, n, ip = 0; + const char *ptr = src_ip; + for (i = 0; i < 4; i++) { + n = 0; + while (1) { + c = *ptr; + ptr++; + if (c >= '0' && c <= '9') { + n *= 10; + n += c - '0'; + } else if ((i < 3 && c == '.') || i == 3) { + // stopping at the '.' but ignore the tailing chars + // after the 3rd one (auto clean the tailing none-integer ?). + break; + } else { + return 1; + } + } + + if (n > 0xFF) { + return 2; + } + + ip |= (n << shiftIndex[i]); + } + + *dst_ip = ip; + return 0; +} + +// unsigned int ip to string ip +XDB_PUBLIC(void) xdb_long2ip(unsigned int ip, char *buffer) { + sprintf(buffer, "%d.%d.%d.%d", (ip >> 24) & 0xFF, (ip >> 16) & 0xFF, (ip >> 8) & 0xFF, ip & 0xFF); +} + +// get the middle ip of a and b +XDB_PUBLIC(unsigned int) xdb_mip(unsigned long a, unsigned long b) { + return (unsigned int) ((a + b) >> 1); +} + +XDB_PUBLIC(long) xdb_now() { + struct timeval c_time; + gettimeofday(&c_time, NULL); + return c_time.tv_sec * (int)1e6 + c_time.tv_usec; +}