diff --git a/source/libs/index/inc/index_util.h b/source/libs/index/inc/index_util.h new file mode 100644 index 0000000000..4ab517ecfa --- /dev/null +++ b/source/libs/index/inc/index_util.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * This program is free software: you can use, redistribute, and/or modify + * it under the terms of the GNU Affero General Public License, version 3 + * or later ("AGPL"), as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +#ifndef __INDEX_UTIL_H__ +#define __INDEX_UTIL_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#define SERIALIZE_MEM_TO_BUF(buf, key, mem) \ + do { \ + memcpy((void *)buf, (void *)(&key->mem), sizeof(key->mem)); \ + buf += sizeof(key->mem); \ + } while (0) + +#define SERIALIZE_STR_MEM_TO_BUF(buf, key, mem, len) \ + do { \ + memcpy((void *)buf, (void *)key->mem, len); \ + buf += len; \ + } while (0) + +#define SERIALIZE_VAR_TO_BUF(buf, var, type) \ + do { \ + type c = var; \ + assert(sizeof(var) == sizeof(type));\ + memcpy((void *)buf, (void *)&c, sizeof(c)); \ + buf += sizeof(c); \ + } while (0) + +#define SERIALIZE_STR_VAR_TO_BUF(buf, var, len) \ + do { \ + memcpy((void *)buf, (void *)var, len); \ + buf += len;\ + } while (0) + + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/source/libs/index/src/index_cache.c b/source/libs/index/src/index_cache.c index 3c52275a4c..5813c99164 100644 --- a/source/libs/index/src/index_cache.c +++ b/source/libs/index/src/index_cache.c @@ -15,6 +15,7 @@ #include "index_cache.h" #include "tcompare.h" +#include "index_util.h" #define MAX_INDEX_KEY_LEN 256// test only, change later @@ -110,35 +111,22 @@ int indexCachePut(void *cache, SIndexTerm *term, int16_t colId, int32_t version, if (cache == NULL) { return -1;} IndexCache *pCache = cache; - // encode data int32_t total = CACHE_KEY_LEN(term); - char *buf = calloc(1, total); char *p = buf; - memcpy(p, &total, sizeof(total)); - p += sizeof(total); + SERIALIZE_VAR_TO_BUF(p, total,int32_t); + SERIALIZE_VAR_TO_BUF(p, colId, int16_t); - memcpy(p, &colId, sizeof(colId)); - p += sizeof(colId); - - memcpy(p, &term->colType, sizeof(term->colType)); - p += sizeof(term->colType); + SERIALIZE_MEM_TO_BUF(p, term, colType); + SERIALIZE_MEM_TO_BUF(p, term, nColVal); + SERIALIZE_STR_MEM_TO_BUF(p, term, colVal, term->nColVal); - memcpy(p, &term->nColVal, sizeof(term->nColVal)); - p += sizeof(term->nColVal); - memcpy(p, term->colVal, term->nColVal); - p += term->nColVal; + SERIALIZE_VAR_TO_BUF(p, version, int32_t); + SERIALIZE_VAR_TO_BUF(p, uid, uint64_t); - memcpy(p, &version, sizeof(version)); - p += sizeof(version); - - memcpy(p, &uid, sizeof(uid)); - p += sizeof(uid); - - memcpy(p, &term->operType, sizeof(term->operType)); - p += sizeof(term->operType); + SERIALIZE_MEM_TO_BUF(p, term, operType); tSkipListPut(pCache->skiplist, (void *)buf); return 0; diff --git a/source/libs/index/src/index_tfile.c b/source/libs/index/src/index_tfile.c index c47aa9b381..81a7f9f443 100644 --- a/source/libs/index/src/index_tfile.c +++ b/source/libs/index/src/index_tfile.c @@ -15,36 +15,18 @@ #include "index_tfile.h" #include "index_fst.h" +#include "index_util.h" -#define SERIALIZE_TO_BUF(buf, key, mem) \ - do { \ - memcpy(buf, &key->mem, sizeof(key->mem)); \ - buf += sizeof(key->mem); \ - } while (0) - -#define SERIALIZE_STR_TO_BUF(buf, key, mem, len) \ - do { \ - memcpy(buf, key->mem, len); \ - buf += len; \ - } while (0) - -#define SERIALIZE_DELIMITER_TO_BUF(buf, delim) \ - do { \ - char c = delim; \ - memcpy(buf, &c, sizeof(c)); \ - buf += sizeof(c); \ - } while (0) - static void tfileSerialCacheKey(TFileCacheKey *key, char *buf) { - SERIALIZE_TO_BUF(buf, key, suid); - SERIALIZE_DELIMITER_TO_BUF(buf, '_'); - SERIALIZE_TO_BUF(buf, key, colType); - SERIALIZE_DELIMITER_TO_BUF(buf, '_'); - SERIALIZE_TO_BUF(buf, key, version); - SERIALIZE_DELIMITER_TO_BUF(buf, '_'); - SERIALIZE_STR_TO_BUF(buf, key, colName, key->nColName); + SERIALIZE_MEM_TO_BUF(buf, key, suid); + SERIALIZE_VAR_TO_BUF(buf, '_', char); + SERIALIZE_MEM_TO_BUF(buf, key, colType); + SERIALIZE_VAR_TO_BUF(buf, '_', char); + SERIALIZE_MEM_TO_BUF(buf, key, version); + SERIALIZE_VAR_TO_BUF(buf, '_', char); + SERIALIZE_STR_MEM_TO_BUF(buf, key, colName, key->nColName); } TFileCache *tfileCacheCreate() { @@ -57,6 +39,8 @@ TFileCache *tfileCacheCreate() { } void tfileCacheDestroy(TFileCache *tcache) { + free(tcache); + } TFileReader *tfileCacheGet(TFileCache *tcache, TFileCacheKey *key) { @@ -86,7 +70,7 @@ void IndexTFileDestroy(IndexTFile *tfile) { int indexTFileSearch(void *tfile, SIndexTermQuery *query, SArray *result) { IndexTFile *ptfile = (IndexTFile *)tfile; - + return 0; } int indexTFilePut(void *tfile, SIndexTerm *term, uint64_t uid) {