/* * 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 . */ #include "index_cache.h" #include "tcompare.h" #define MAX_INDEX_KEY_LEN 128 // test only, change later static char* getIndexKey(const void *pData) { return NULL; } static int32_t compareKey(const void *l, const void *r) { char *lp = (char *)l; char *rp = (char *)r; // skip total len, not compare int32_t ll, rl; // len memcpy(&ll, lp, sizeof(int32_t)); memcpy(&rl, rp, sizeof(int32_t)); lp += sizeof(int32_t); rp += sizeof(int32_t); // compare field id int16_t lf, rf; // field id memcpy(&lf, lp, sizeof(lf)); memcpy(&rf, rp, sizeof(rf)); if (lf != rf) { return lf < rf ? -1: 1; } lp += sizeof(lf); rp += sizeof(rf); // compare field type int16_t lft, rft; memcpy(&lft, lp, sizeof(lft)); memcpy(&rft, rp, sizeof(rft)); lp += sizeof(lft); rp += sizeof(rft); assert(rft == rft); // skip value len int32_t lfl, rfl; memcpy(&lfl, lp, sizeof(lfl)); memcpy(&rfl, rp, sizeof(rfl)); lp += sizeof(lfl); rp += sizeof(rfl); // compare value int32_t i, j; for (i = 0, j = 0; i < lfl && j < rfl; i++, j++) { if (lp[i] == rp[j]) { continue; } else { return lp[i] < rp[j] ? -1 : 1;} } if (i < lfl) { return 1;} else if (j < rfl) { return -1; } lp += lfl; rp += rfl; // skip uid uint64_t lu, ru; memcpy(&lu, lp, sizeof(lu)); memcpy(&ru, rp, sizeof(ru)); lp += sizeof(lu); rp += sizeof(ru); // compare version, desc order int32_t lv, rv; memcpy(&lv, lp, sizeof(lv)); memcpy(&rv, rp, sizeof(rv)); if (lv != rv) { return lv > rv ? -1 : 1; } lp += sizeof(lv); rp += sizeof(rv); // not care item type return 0; } IndexCache *indexCacheCreate() { IndexCache *cache = calloc(1, sizeof(IndexCache)); cache->skiplist = tSkipListCreate(MAX_SKIP_LIST_LEVEL, TSDB_DATA_TYPE_BINARY, MAX_INDEX_KEY_LEN, compareKey, SL_ALLOW_DUP_KEY, getIndexKey); return cache; } void indexCacheDestroy(IndexCache *cache) { if (cache == NULL) { return; } tSkipListDestroy(cache->skiplist); free(cache); } int indexCachePut(IndexCache *cache, int16_t fieldId, int16_t fieldType, const char *fieldValue, int32_t fvLen, uint32_t version, uint64_t uid, int8_t operType) { if (cache == NULL) { return -1;} // encode data int32_t total = sizeof(int32_t) + sizeof(fieldId) + sizeof(fieldType) + sizeof(fvLen) + fvLen + sizeof(version) + sizeof(uid) + sizeof(operType); char *buf = calloc(1, total); char *p = buf; memcpy(p, &total, sizeof(total)); p += sizeof(total); memcpy(p, &fieldId, sizeof(fieldId)); p += sizeof(fieldId); memcpy(p, &fieldType, sizeof(fieldType)); p += sizeof(fieldType); memcpy(p, &fvLen, sizeof(fvLen)); p += sizeof(fvLen); memcpy(p, fieldValue, fvLen); p += fvLen; memcpy(p, &version, sizeof(version)); p += sizeof(version); memcpy(p, &uid, sizeof(uid)); p += sizeof(uid); memcpy(p, &operType, sizeof(operType)); p += sizeof(operType); tSkipListPut(cache->skiplist, (void *)buf); // encode end } int indexCacheDel(IndexCache *cache, int32_t fieldId, const char *fieldValue, int32_t fvlen, uint64_t uid, int8_t operType) { } int indexCacheSearch(IndexCache *cache, SIndexMultiTermQuery *query, SArray *result) { return 0; }