streamBackendRocksdb.c 74.1 KB
Newer Older
dengyihao's avatar
dengyihao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * 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 <http://www.gnu.org/licenses/>.
 */

16
// #include "streamStateRocksdb.h"
dengyihao's avatar
dengyihao 已提交
17
#include "streamBackendRocksdb.h"
dengyihao's avatar
dengyihao 已提交
18
#include "tcommon.h"
dengyihao's avatar
dengyihao 已提交
19

20 21 22 23
typedef struct SCompactFilteFactory {
  void* status;
} SCompactFilteFactory;

dengyihao's avatar
dengyihao 已提交
24 25 26 27 28 29 30 31 32 33 34 35
typedef struct {
  rocksdb_t*                       db;
  rocksdb_column_family_handle_t** pHandle;
  rocksdb_writeoptions_t*          wOpt;
  rocksdb_readoptions_t*           rOpt;
  rocksdb_options_t**              cfOpt;
  rocksdb_options_t*               dbOpt;
  void*                            param;
  void*                            pBackendHandle;
  SListNode*                       pCompareNode;
} RocksdbCfInst;

dengyihao's avatar
dengyihao 已提交
36 37 38
int32_t streamStateOpenBackendCf(void* backend, char* name, SHashObj* ids);

void destroyRocksdbCfInst(RocksdbCfInst* inst);
dengyihao's avatar
dengyihao 已提交
39

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
void          destroyCompactFilteFactory(void* arg);
void          destroyCompactFilte(void* arg);
const char*   compactFilteFactoryName(void* arg);
const char*   compactFilteName(void* arg);
unsigned char compactFilte(void* arg, int level, const char* key, size_t klen, const char* val, size_t vlen,
                           char** newval, size_t* newvlen, unsigned char* value_changed);
rocksdb_compactionfilter_t* compactFilteFactoryCreateFilter(void* arg, rocksdb_compactionfiltercontext_t* ctx);

typedef struct {
  void* tableOpt;
} RocksdbCfParam;
const char* cfName[] = {"default", "state", "fill", "sess", "func", "parname", "partag"};

typedef int (*EncodeFunc)(void* key, char* buf);
typedef int (*DecodeFunc)(void* key, char* buf);
typedef int (*ToStringFunc)(void* key, char* buf);
typedef const char* (*CompareName)(void* statue);
typedef int (*BackendCmpFunc)(void* state, const char* aBuf, size_t aLen, const char* bBuf, size_t bLen);
typedef void (*DestroyFunc)(void* state);
typedef int32_t (*EncodeValueFunc)(void* value, int32_t vlen, int64_t ttl, char** dest);
typedef int32_t (*DecodeValueFunc)(void* value, int32_t vlen, int64_t* ttl, char** dest);

const char* compareDefaultName(void* name);
const char* compareStateName(void* name);
const char* compareWinKeyName(void* name);
const char* compareSessionKeyName(void* name);
const char* compareFuncKeyName(void* name);
const char* compareParKeyName(void* name);
const char* comparePartagKeyName(void* name);

void* streamBackendInit(const char* path) {
dengyihao's avatar
dengyihao 已提交
71
  qDebug("init stream backend");
72 73 74
  SBackendHandle* pHandle = calloc(1, sizeof(SBackendHandle));
  pHandle->list = tdListNew(sizeof(SCfComparator));
  taosThreadMutexInit(&pHandle->mutex, NULL);
dengyihao's avatar
dengyihao 已提交
75 76
  taosThreadMutexInit(&pHandle->cfMutex, NULL);
  pHandle->cfInst = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

  rocksdb_env_t* env = rocksdb_create_default_env();  // rocksdb_envoptions_create();
  rocksdb_env_set_low_priority_background_threads(env, 4);
  rocksdb_env_set_high_priority_background_threads(env, 2);

  rocksdb_cache_t* cache = rocksdb_cache_create_lru(128 << 20);

  rocksdb_options_t* opts = rocksdb_options_create();
  rocksdb_options_set_env(opts, env);
  rocksdb_options_set_create_if_missing(opts, 1);
  rocksdb_options_set_create_missing_column_families(opts, 1);
  rocksdb_options_set_write_buffer_size(opts, 128 << 20);
  rocksdb_options_set_max_total_wal_size(opts, 128 << 20);
  rocksdb_options_set_recycle_log_file_num(opts, 6);
  rocksdb_options_set_max_write_buffer_number(opts, 3);
dengyihao's avatar
dengyihao 已提交
92
  rocksdb_options_set_info_log_level(opts, 0);
93 94 95 96 97 98 99 100

  pHandle->env = env;
  pHandle->dbOpt = opts;
  pHandle->cache = cache;
  pHandle->filterFactory = rocksdb_compactionfilterfactory_create(
      NULL, destroyCompactFilteFactory, compactFilteFactoryCreateFilter, compactFilteFactoryName);
  rocksdb_options_set_compaction_filter_factory(pHandle->dbOpt, pHandle->filterFactory);

dengyihao's avatar
dengyihao 已提交
101 102 103 104
  char*  err = NULL;
  size_t nCf = 0;

  char** cfs = rocksdb_list_column_families(opts, path, &nCf, &err);
dengyihao's avatar
dengyihao 已提交
105
  if (nCf == 0 || nCf == 1 || err != NULL) {
106
    taosMemoryFreeClear(err);
dengyihao's avatar
dengyihao 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    pHandle->db = rocksdb_open(opts, path, &err);
    if (err != NULL) {
      qError("failed to open rocksdb, path:%s, reason:%s", path, err);
      taosMemoryFreeClear(err);
    }
  } else {
    int64_t   streamId;
    int32_t   taskId, dummpy = 0;
    SHashObj* tbl = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
    for (size_t i = 0; i < nCf; i++) {
      char* cf = cfs[i];
      char  suffix[64] = {0};
      if (3 == sscanf(cf, "0x%" PRIx64 "-%d_%s", &streamId, &taskId, suffix)) {
        char idstr[128] = {0};
        sprintf(idstr, "0x%" PRIx64 "-%d", streamId, taskId);
dengyihao's avatar
dengyihao 已提交
122 123 124
        // qError("make cf name %s", idstr);
        if (taosHashGet(tbl, idstr, strlen(idstr) + 1) == NULL) {
          taosHashPut(tbl, idstr, strlen(idstr) + 1, &dummpy, sizeof(dummpy));
dengyihao's avatar
dengyihao 已提交
125 126 127 128 129
        }
      } else {
        continue;
      }
    }
dengyihao's avatar
dengyihao 已提交
130
    streamStateOpenBackendCf(pHandle, (char*)path, tbl);
dengyihao's avatar
dengyihao 已提交
131
    taosHashCleanup(tbl);
132
  }
dengyihao's avatar
dengyihao 已提交
133
  rocksdb_list_column_families_destroy(cfs, nCf);
134 135 136 137 138 139 140

  return (void*)pHandle;
_EXIT:
  rocksdb_options_destroy(opts);
  rocksdb_cache_destroy(cache);
  rocksdb_env_destroy(env);
  taosThreadMutexDestroy(&pHandle->mutex);
dengyihao's avatar
dengyihao 已提交
141 142
  taosThreadMutexDestroy(&pHandle->cfMutex);
  taosHashCleanup(pHandle->cfInst);
143 144 145 146 147 148
  rocksdb_compactionfilterfactory_destroy(pHandle->filterFactory);
  tdListFree(pHandle->list);
  free(pHandle);
  return NULL;
}
void streamBackendCleanup(void* arg) {
dengyihao's avatar
dengyihao 已提交
149
  SBackendHandle* pHandle = (SBackendHandle*)arg;
dengyihao's avatar
dengyihao 已提交
150 151 152 153 154 155 156
  RocksdbCfInst** pIter = (RocksdbCfInst**)taosHashIterate(pHandle->cfInst, NULL);
  while (pIter != NULL) {
    RocksdbCfInst* inst = *pIter;
    destroyRocksdbCfInst(inst);
    taosHashIterate(pHandle->cfInst, pIter);
  }
  taosHashCleanup(pHandle->cfInst);
dengyihao's avatar
dengyihao 已提交
157

dengyihao's avatar
dengyihao 已提交
158 159 160 161 162 163 164 165 166
  rocksdb_flushoptions_t* flushOpt = rocksdb_flushoptions_create();
  char*                   err = NULL;
  rocksdb_flush(pHandle->db, flushOpt, &err);
  if (err != NULL) {
    qError("failed to flush db before streamBackend clean up, reason:%s", err);
    taosMemoryFree(err);
  }
  rocksdb_flushoptions_destroy(flushOpt);

167 168 169 170 171 172 173 174 175 176 177 178 179 180
  rocksdb_close(pHandle->db);
  rocksdb_options_destroy(pHandle->dbOpt);
  rocksdb_env_destroy(pHandle->env);
  rocksdb_cache_destroy(pHandle->cache);

  taosThreadMutexDestroy(&pHandle->mutex);
  SListNode* head = tdListPopHead(pHandle->list);
  while (head != NULL) {
    streamStateDestroyCompar(head->data);
    taosMemoryFree(head);
    head = tdListPopHead(pHandle->list);
  }
  // rocksdb_compactionfilterfactory_destroy(pHandle->filterFactory);
  tdListFree(pHandle->list);
dengyihao's avatar
dengyihao 已提交
181
  taosThreadMutexDestroy(&pHandle->cfMutex);
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206

  taosMemoryFree(pHandle);

  return;
}
SListNode* streamBackendAddCompare(void* backend, void* arg) {
  SBackendHandle* pHandle = (SBackendHandle*)backend;
  SListNode*      node = NULL;
  taosThreadMutexLock(&pHandle->mutex);
  node = tdListAdd(pHandle->list, arg);
  taosThreadMutexUnlock(&pHandle->mutex);
  return node;
}
void streamBackendDelCompare(void* backend, void* arg) {
  SBackendHandle* pHandle = (SBackendHandle*)backend;
  SListNode*      node = NULL;
  taosThreadMutexLock(&pHandle->mutex);
  node = tdListPopNode(pHandle->list, arg);
  taosThreadMutexUnlock(&pHandle->mutex);
  if (node) {
    streamStateDestroyCompar(node->data);
    taosMemoryFree(node);
  }
}
void        streamStateDestroy_rocksdb(SStreamState* pState, bool remove) { streamStateCloseBackend(pState, remove); }
dengyihao's avatar
dengyihao 已提交
207 208 209 210 211 212 213 214 215
static bool streamStateIterSeekAndValid(rocksdb_iterator_t* iter, char* buf, size_t len);
int         streamGetInit(const char* funcName);

// |key|-----value------|
// |key|ttl|len|userData|

static rocksdb_iterator_t* streamStateIterCreate(SStreamState* pState, const char* cfName,
                                                 rocksdb_snapshot_t** snapshot, rocksdb_readoptions_t** readOpt);

dengyihao's avatar
dengyihao 已提交
216
int defaultKeyComp(void* state, const char* aBuf, size_t aLen, const char* bBuf, size_t bLen) {
dengyihao's avatar
dengyihao 已提交
217 218 219 220 221 222 223 224 225 226 227
  int ret = memcmp(aBuf, bBuf, aLen);
  if (ret == 0) {
    if (aLen < bLen)
      return -1;
    else if (aLen > bLen)
      return 1;
    else
      return 0;
  } else {
    return ret;
  }
dengyihao's avatar
dengyihao 已提交
228
}
dengyihao's avatar
dengyihao 已提交
229 230 231
int streamStateValueIsStale(char* vv) {
  int64_t ts = 0;
  taosDecodeFixedI64(vv, &ts);
dengyihao's avatar
dengyihao 已提交
232
  return (ts != 0 && ts < taosGetTimestampMs()) ? 1 : 0;
dengyihao's avatar
dengyihao 已提交
233
}
dengyihao's avatar
dengyihao 已提交
234
int iterValueIsStale(rocksdb_iterator_t* iter) {
dengyihao's avatar
dengyihao 已提交
235 236 237
  size_t len;
  char*  v = (char*)rocksdb_iter_value(iter, &len);
  return streamStateValueIsStale(v);
dengyihao's avatar
dengyihao 已提交
238
}
dengyihao's avatar
dengyihao 已提交
239 240 241 242 243 244 245 246 247 248 249 250 251 252
int defaultKeyEncode(void* k, char* buf) {
  int len = strlen((char*)k);
  memcpy(buf, (char*)k, len);
  return len;
}
int defaultKeyDecode(void* k, char* buf) {
  int len = strlen(buf);
  memcpy(k, buf, len);
  return len;
}
int defaultKeyToString(void* k, char* buf) {
  // just to debug
  return sprintf(buf, "key: %s", (char*)k);
}
dengyihao's avatar
dengyihao 已提交
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
//
//  SStateKey
//  |--groupid--|---ts------|--opNum----|
//  |--uint64_t-|-uint64_t--|--int64_t--|
//
//
//
int stateKeyDBComp(void* state, const char* aBuf, size_t aLen, const char* bBuf, size_t bLen) {
  SStateKey key1, key2;
  memset(&key1, 0, sizeof(key1));
  memset(&key2, 0, sizeof(key2));

  char* p1 = (char*)aBuf;
  char* p2 = (char*)bBuf;

  p1 = taosDecodeFixedU64(p1, &key1.key.groupId);
  p2 = taosDecodeFixedU64(p2, &key2.key.groupId);

  p1 = taosDecodeFixedI64(p1, &key1.key.ts);
  p2 = taosDecodeFixedI64(p2, &key2.key.ts);

  taosDecodeFixedI64(p1, &key1.opNum);
  taosDecodeFixedI64(p2, &key2.opNum);

  return stateKeyCmpr(&key1, sizeof(key1), &key2, sizeof(key2));
}

int stateKeyEncode(void* k, char* buf) {
  SStateKey* key = k;
  int        len = 0;
  len += taosEncodeFixedU64((void**)&buf, key->key.groupId);
  len += taosEncodeFixedI64((void**)&buf, key->key.ts);
dengyihao's avatar
dengyihao 已提交
285
  len += taosEncodeFixedI64((void**)&buf, key->opNum);
dengyihao's avatar
dengyihao 已提交
286 287 288 289 290 291 292 293 294 295 296 297
  return len;
}
int stateKeyDecode(void* k, char* buf) {
  SStateKey* key = k;
  int        len = 0;
  char*      p = buf;
  p = taosDecodeFixedU64(p, &key->key.groupId);
  p = taosDecodeFixedI64(p, &key->key.ts);
  p = taosDecodeFixedI64(p, &key->opNum);
  return p - buf;
}

dengyihao's avatar
dengyihao 已提交
298 299 300
int stateKeyToString(void* k, char* buf) {
  SStateKey* key = k;
  int        n = 0;
dengyihao's avatar
dengyihao 已提交
301 302 303
  n += sprintf(buf + n, "[groupId:%" PRId64 ",", key->key.groupId);
  n += sprintf(buf + n, "ts:%" PRIi64 ",", key->key.ts);
  n += sprintf(buf + n, "opNum:%" PRIi64 "]", key->opNum);
dengyihao's avatar
dengyihao 已提交
304 305 306
  return n;
}

dengyihao's avatar
dengyihao 已提交
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
//
// SStateSessionKey
//  |-----------SSessionKey----------|
//  |-----STimeWindow-----|
//  |---skey--|---ekey----|--groupId-|--opNum--|
//  |---int64-|--int64_t--|--uint64--|--int64_t|
// |
//
int stateSessionKeyDBComp(void* state, const char* aBuf, size_t aLen, const char* bBuf, size_t bLen) {
  SStateSessionKey w1, w2;
  memset(&w1, 0, sizeof(w1));
  memset(&w2, 0, sizeof(w2));

  char* p1 = (char*)aBuf;
  char* p2 = (char*)bBuf;

  p1 = taosDecodeFixedI64(p1, &w1.key.win.skey);
  p2 = taosDecodeFixedI64(p2, &w2.key.win.skey);

  p1 = taosDecodeFixedI64(p1, &w1.key.win.ekey);
  p2 = taosDecodeFixedI64(p2, &w2.key.win.ekey);

  p1 = taosDecodeFixedU64(p1, &w1.key.groupId);
  p2 = taosDecodeFixedU64(p2, &w2.key.groupId);

  p1 = taosDecodeFixedI64(p1, &w1.opNum);
  p2 = taosDecodeFixedI64(p2, &w2.opNum);

  return stateSessionKeyCmpr(&w1, sizeof(w1), &w2, sizeof(w2));
}
int stateSessionKeyEncode(void* ses, char* buf) {
  SStateSessionKey* sess = ses;
  int               len = 0;
  len += taosEncodeFixedI64((void**)&buf, sess->key.win.skey);
  len += taosEncodeFixedI64((void**)&buf, sess->key.win.ekey);
  len += taosEncodeFixedU64((void**)&buf, sess->key.groupId);
  len += taosEncodeFixedI64((void**)&buf, sess->opNum);
  return len;
}
int stateSessionKeyDecode(void* ses, char* buf) {
  SStateSessionKey* sess = ses;
  int               len = 0;

  char* p = buf;
  p = taosDecodeFixedI64(p, &sess->key.win.skey);
  p = taosDecodeFixedI64(p, &sess->key.win.ekey);
  p = taosDecodeFixedU64(p, &sess->key.groupId);
  p = taosDecodeFixedI64(p, &sess->opNum);
  return p - buf;
}
dengyihao's avatar
dengyihao 已提交
357 358 359
int stateSessionKeyToString(void* k, char* buf) {
  SStateSessionKey* key = k;
  int               n = 0;
dengyihao's avatar
dengyihao 已提交
360 361 362 363
  n += sprintf(buf + n, "[skey:%" PRIi64 ",", key->key.win.skey);
  n += sprintf(buf + n, "ekey:%" PRIi64 ",", key->key.win.ekey);
  n += sprintf(buf + n, "groupId:%" PRIu64 ",", key->key.groupId);
  n += sprintf(buf + n, "opNum:%" PRIi64 "]", key->opNum);
dengyihao's avatar
dengyihao 已提交
364 365
  return n;
}
dengyihao's avatar
dengyihao 已提交
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404

/**
 *  SWinKey
 *  |------groupId------|-----ts------|
 *  |------uint64-------|----int64----|
 */
int winKeyDBComp(void* state, const char* aBuf, size_t aLen, const char* bBuf, size_t bLen) {
  SWinKey w1, w2;
  memset(&w1, 0, sizeof(w1));
  memset(&w2, 0, sizeof(w2));

  char* p1 = (char*)aBuf;
  char* p2 = (char*)bBuf;

  p1 = taosDecodeFixedU64(p1, &w1.groupId);
  p2 = taosDecodeFixedU64(p2, &w2.groupId);

  p1 = taosDecodeFixedI64(p1, &w1.ts);
  p2 = taosDecodeFixedI64(p2, &w2.ts);

  return winKeyCmpr(&w1, sizeof(w1), &w2, sizeof(w2));
}

int winKeyEncode(void* k, char* buf) {
  SWinKey* key = k;
  int      len = 0;
  len += taosEncodeFixedU64((void**)&buf, key->groupId);
  len += taosEncodeFixedI64((void**)&buf, key->ts);
  return len;
}

int winKeyDecode(void* k, char* buf) {
  SWinKey* key = k;
  int      len = 0;
  char*    p = buf;
  p = taosDecodeFixedU64(p, &key->groupId);
  p = taosDecodeFixedI64(p, &key->ts);
  return len;
}
dengyihao's avatar
dengyihao 已提交
405 406 407 408

int winKeyToString(void* k, char* buf) {
  SWinKey* key = k;
  int      n = 0;
dengyihao's avatar
dengyihao 已提交
409 410
  n += sprintf(buf + n, "[groupId:%" PRIu64 ",", key->groupId);
  n += sprintf(buf + n, "ts:%" PRIi64 "]", key->ts);
dengyihao's avatar
dengyihao 已提交
411 412
  return n;
}
dengyihao's avatar
dengyihao 已提交
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
/*
 * STupleKey
 * |---groupId---|---ts---|---exprIdx---|
 * |---uint64--|---int64--|---int32-----|
 */
int tupleKeyDBComp(void* state, const char* aBuf, size_t aLen, const char* bBuf, size_t bLen) {
  STupleKey w1, w2;
  memset(&w1, 0, sizeof(w1));
  memset(&w2, 0, sizeof(w2));

  char* p1 = (char*)aBuf;
  char* p2 = (char*)bBuf;

  p1 = taosDecodeFixedU64(p1, &w1.groupId);
  p2 = taosDecodeFixedU64(p2, &w2.groupId);

  p1 = taosDecodeFixedI64(p1, &w1.ts);
  p2 = taosDecodeFixedI64(p2, &w2.ts);

  p1 = taosDecodeFixedI32(p1, &w1.exprIdx);
  p2 = taosDecodeFixedI32(p2, &w2.exprIdx);

  return STupleKeyCmpr(&w1, sizeof(w1), &w2, sizeof(w2));
}

int tupleKeyEncode(void* k, char* buf) {
  STupleKey* key = k;
  int        len = 0;
  len += taosEncodeFixedU64((void**)&buf, key->groupId);
  len += taosEncodeFixedI64((void**)&buf, key->ts);
  len += taosEncodeFixedI32((void**)&buf, key->exprIdx);
  return len;
}
int tupleKeyDecode(void* k, char* buf) {
  STupleKey* key = k;
  int        len = 0;
  char*      p = buf;
  p = taosDecodeFixedU64(p, &key->groupId);
  p = taosDecodeFixedI64(p, &key->ts);
  p = taosDecodeFixedI32(p, &key->exprIdx);
  return len;
}
dengyihao's avatar
dengyihao 已提交
455 456 457
int tupleKeyToString(void* k, char* buf) {
  int        n = 0;
  STupleKey* key = k;
dengyihao's avatar
dengyihao 已提交
458 459 460
  n += sprintf(buf + n, "[groupId:%" PRIu64 ",", key->groupId);
  n += sprintf(buf + n, "ts:%" PRIi64 ",", key->ts);
  n += sprintf(buf + n, "exprIdx:%d]", key->exprIdx);
dengyihao's avatar
dengyihao 已提交
461 462
  return n;
}
dengyihao's avatar
dengyihao 已提交
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490

int parKeyDBComp(void* state, const char* aBuf, size_t aLen, const char* bBuf, size_t bLen) {
  int64_t w1, w2;
  memset(&w1, 0, sizeof(w1));
  memset(&w2, 0, sizeof(w2));
  char* p1 = (char*)aBuf;
  char* p2 = (char*)bBuf;

  taosDecodeFixedI64(p1, &w1);
  taosDecodeFixedI64(p2, &w2);
  if (w1 == w2) {
    return 0;
  } else {
    return w1 < w2 ? -1 : 1;
  }
}
int parKeyEncode(void* k, char* buf) {
  int64_t* groupid = k;
  int      len = taosEncodeFixedI64((void**)&buf, *groupid);
  return len;
}
int parKeyDecode(void* k, char* buf) {
  char*    p = buf;
  int64_t* groupid = k;

  p = taosDecodeFixedI64(p, groupid);
  return p - buf;
}
dengyihao's avatar
dengyihao 已提交
491 492 493
int parKeyToString(void* k, char* buf) {
  int64_t* key = k;
  int      n = 0;
dengyihao's avatar
dengyihao 已提交
494
  n = sprintf(buf + n, "[groupId:%" PRIi64 "]", *key);
dengyihao's avatar
dengyihao 已提交
495 496
  return n;
}
dengyihao's avatar
dengyihao 已提交
497
int stremaValueEncode(void* k, char* buf) {
dengyihao's avatar
dengyihao 已提交
498 499
  int           len = 0;
  SStreamValue* key = k;
dengyihao's avatar
dengyihao 已提交
500 501 502 503 504 505
  len += taosEncodeFixedI64((void**)&buf, key->unixTimestamp);
  len += taosEncodeFixedI32((void**)&buf, key->len);
  len += taosEncodeBinary((void**)&buf, key->data, key->len);
  return len;
}
int streamValueDecode(void* k, char* buf) {
dengyihao's avatar
dengyihao 已提交
506 507
  SStreamValue* key = k;
  char*         p = buf;
dengyihao's avatar
dengyihao 已提交
508 509 510 511 512 513
  p = taosDecodeFixedI64(p, &key->unixTimestamp);
  p = taosDecodeFixedI32(p, &key->len);
  p = taosDecodeBinary(p, (void**)&key->data, key->len);
  return p - buf;
}
int32_t streamValueToString(void* k, char* buf) {
dengyihao's avatar
dengyihao 已提交
514 515
  SStreamValue* key = k;
  int           n = 0;
dengyihao's avatar
dengyihao 已提交
516 517 518 519 520 521
  n += sprintf(buf + n, "[unixTimestamp:%" PRIi64 ",", key->unixTimestamp);
  n += sprintf(buf + n, "len:%d,", key->len);
  n += sprintf(buf + n, "data:%s]", key->data);
  return n;
}

dengyihao's avatar
dengyihao 已提交
522
/*1: stale, 0: no stale*/
dengyihao's avatar
dengyihao 已提交
523
int32_t streaValueIsStale(void* k, int64_t ts) {
dengyihao's avatar
dengyihao 已提交
524
  SStreamValue* key = k;
dengyihao's avatar
dengyihao 已提交
525 526 527 528 529
  if (key->unixTimestamp < ts) {
    return 1;
  }
  return 0;
}
dengyihao's avatar
dengyihao 已提交
530

dengyihao's avatar
dengyihao 已提交
531 532 533 534
void destroyFunc(void* arg) {
  (void)arg;
  return;
}
dengyihao's avatar
dengyihao 已提交
535

dengyihao's avatar
dengyihao 已提交
536
typedef struct {
dengyihao's avatar
dengyihao 已提交
537 538 539 540 541 542 543 544 545 546 547
  const char*     key;
  int32_t         len;
  int             idx;
  BackendCmpFunc  cmpFunc;
  EncodeFunc      enFunc;
  DecodeFunc      deFunc;
  ToStringFunc    toStrFunc;
  CompareName     cmpName;
  DestroyFunc     detroyFunc;
  EncodeValueFunc enValueFunc;
  DecodeValueFunc deValueFunc;
dengyihao's avatar
dengyihao 已提交
548

dengyihao's avatar
dengyihao 已提交
549 550
} SCfInit;

dengyihao's avatar
dengyihao 已提交
551
#define GEN_COLUMN_FAMILY_NAME(name, idstr, SUFFIX) sprintf(name, "%s_%s", idstr, (SUFFIX));
dengyihao's avatar
dengyihao 已提交
552

dengyihao's avatar
dengyihao 已提交
553 554 555 556 557 558 559 560 561 562 563 564
int32_t encodeValueFunc(void* value, int32_t vlen, int64_t ttl, char** dest) {
  SStreamValue key = {.unixTimestamp = ttl, .len = vlen, .data = (char*)(value)};

  char*   p = taosMemoryCalloc(1, sizeof(int64_t) + sizeof(int32_t) + key.len);
  char*   buf = p;
  int32_t len = 0;
  len += taosEncodeFixedI64((void**)&buf, key.unixTimestamp);
  len += taosEncodeFixedI32((void**)&buf, key.len);
  len += taosEncodeBinary((void**)&buf, (char*)value, vlen);
  *dest = p;
  return len;
}
dengyihao's avatar
dengyihao 已提交
565 566 567 568
/*
 *  ret >= 0 : found valid value
 *  ret < 0 : error or timeout
 */
dengyihao's avatar
dengyihao 已提交
569 570 571
int32_t decodeValueFunc(void* value, int32_t vlen, int64_t* ttl, char** dest) {
  SStreamValue key = {0};
  char*        p = value;
dengyihao's avatar
dengyihao 已提交
572
  if (streamStateValueIsStale(p)) {
dengyihao's avatar
dengyihao 已提交
573 574 575
    *dest = NULL;
    return -1;
  }
dengyihao's avatar
dengyihao 已提交
576 577 578 579 580 581 582 583 584
  int64_t now = taosGetTimestampMs();
  p = taosDecodeFixedI64(p, &key.unixTimestamp);
  p = taosDecodeFixedI32(p, &key.len);
  if (key.len == 0) {
    key.data = NULL;
  } else {
    p = taosDecodeBinary(p, (void**)&(key.data), key.len);
  }

dengyihao's avatar
dengyihao 已提交
585 586 587 588 589 590 591 592
  if (ttl != NULL) {
    *ttl = key.unixTimestamp == 0 ? 0 : key.unixTimestamp - now;
  }
  if (dest != NULL) {
    *dest = key.data;
  } else {
    taosMemoryFree(key.data);
  }
dengyihao's avatar
dengyihao 已提交
593 594
  return key.len;
}
dengyihao's avatar
dengyihao 已提交
595
SCfInit ginitDict[] = {
dengyihao's avatar
dengyihao 已提交
596
    {"default", 7, 0, defaultKeyComp, defaultKeyEncode, defaultKeyDecode, defaultKeyToString, compareDefaultName,
dengyihao's avatar
dengyihao 已提交
597 598 599 600 601
     destroyFunc, encodeValueFunc, decodeValueFunc},
    {"state", 5, 1, stateKeyDBComp, stateKeyEncode, stateKeyDecode, stateKeyToString, compareStateName, destroyFunc,
     encodeValueFunc, decodeValueFunc},
    {"fill", 4, 2, winKeyDBComp, winKeyEncode, winKeyDecode, winKeyToString, compareWinKeyName, destroyFunc,
     encodeValueFunc, decodeValueFunc},
dengyihao's avatar
dengyihao 已提交
602
    {"sess", 4, 3, stateSessionKeyDBComp, stateSessionKeyEncode, stateSessionKeyDecode, stateSessionKeyToString,
dengyihao's avatar
dengyihao 已提交
603 604 605 606 607 608 609
     compareSessionKeyName, destroyFunc, encodeValueFunc, decodeValueFunc},
    {"func", 4, 4, tupleKeyDBComp, tupleKeyEncode, tupleKeyDecode, tupleKeyToString, compareFuncKeyName, destroyFunc,
     encodeValueFunc, decodeValueFunc},
    {"parname", 7, 5, parKeyDBComp, parKeyEncode, parKeyDecode, parKeyToString, compareParKeyName, destroyFunc,
     encodeValueFunc, decodeValueFunc},
    {"partag", 6, 6, parKeyDBComp, parKeyEncode, parKeyDecode, parKeyToString, comparePartagKeyName, destroyFunc,
     encodeValueFunc, decodeValueFunc},
dengyihao's avatar
dengyihao 已提交
610 611
};

dengyihao's avatar
dengyihao 已提交
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
const char* compareDefaultName(void* arg) {
  (void)arg;
  return ginitDict[0].key;
}
const char* compareStateName(void* arg) {
  (void)arg;
  return ginitDict[1].key;
}
const char* compareWinKeyName(void* arg) {
  (void)arg;
  return ginitDict[2].key;
}
const char* compareSessionKeyName(void* arg) {
  (void)arg;
  return ginitDict[3].key;
}
const char* compareFuncKeyName(void* arg) {
  (void)arg;
  return ginitDict[4].key;
}
const char* compareParKeyName(void* arg) {
  (void)arg;
  return ginitDict[5].key;
}
const char* comparePartagKeyName(void* arg) {
  (void)arg;
  return ginitDict[6].key;
}
dengyihao's avatar
dengyihao 已提交
640

dengyihao's avatar
dengyihao 已提交
641 642 643 644 645 646 647 648
void destroyCompactFilteFactory(void* arg) {
  if (arg == NULL) return;
}
const char* compactFilteFactoryName(void* arg) {
  SCompactFilteFactory* state = arg;
  return "stream_compact_filter";
}

dengyihao's avatar
dengyihao 已提交
649
void          destroyCompactFilte(void* arg) { (void)arg; }
dengyihao's avatar
dengyihao 已提交
650 651
unsigned char compactFilte(void* arg, int level, const char* key, size_t klen, const char* val, size_t vlen,
                           char** newval, size_t* newvlen, unsigned char* value_changed) {
dengyihao's avatar
dengyihao 已提交
652
  if (streamStateValueIsStale((char*)val)) {
dengyihao's avatar
dengyihao 已提交
653 654 655 656 657 658 659 660 661 662 663 664
    return 1;
  }
  return 0;
}
const char* compactFilteName(void* arg) { return "stream_filte"; }

rocksdb_compactionfilter_t* compactFilteFactoryCreateFilter(void* arg, rocksdb_compactionfiltercontext_t* ctx) {
  SCompactFilteFactory*       state = arg;
  rocksdb_compactionfilter_t* filter =
      rocksdb_compactionfilter_create(NULL, destroyCompactFilte, compactFilte, compactFilteName);
  return filter;
}
dengyihao's avatar
dengyihao 已提交
665

dengyihao's avatar
dengyihao 已提交
666 667
void destroyRocksdbCfInst(RocksdbCfInst* inst) {
  int cfLen = sizeof(ginitDict) / sizeof(ginitDict[0]);
dengyihao's avatar
dengyihao 已提交
668
  for (int i = 0; i < cfLen; i++) {
dengyihao's avatar
dengyihao 已提交
669
    rocksdb_column_family_handle_destroy(inst->pHandle[i]);
dengyihao's avatar
dengyihao 已提交
670 671
  }

dengyihao's avatar
dengyihao 已提交
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
  rocksdb_writeoptions_destroy(inst->wOpt);
  inst->wOpt = NULL;

  rocksdb_readoptions_destroy(inst->rOpt);
  taosMemoryFree(inst->cfOpt);
  taosMemoryFree(inst->param);
  taosMemoryFreeClear(inst->param);
  taosMemoryFree(inst);
}

int32_t streamStateOpenBackendCf(void* backend, char* name, SHashObj* ids) {
  SBackendHandle* handle = backend;
  char*           err = NULL;
  size_t          nSize = taosHashGetSize(ids);
  int             cfLen = sizeof(ginitDict) / sizeof(ginitDict[0]);

  char** cfNames = taosMemoryCalloc(nSize * cfLen + 1, sizeof(char*));
  void*  pIter = taosHashIterate(ids, NULL);
  size_t keyLen = 0;
  char*  idstr = taosHashGetKey(pIter, &keyLen);
  for (int i = 0; i < nSize * cfLen + 1; i++) {
    cfNames[i] = (char*)taosMemoryCalloc(1, 128);
    if (i == 0) {
      memcpy(cfNames[0], "default", strlen("default"));
      continue;
    }

    GEN_COLUMN_FAMILY_NAME(cfNames[i], idstr, ginitDict[(i - 1) % (cfLen)].key);
    if (i % cfLen == 0) {
      pIter = taosHashIterate(ids, pIter);
      if (pIter != NULL) idstr = taosHashGetKey(pIter, &keyLen);
    }
  }
  rocksdb_options_t** cfOpts = taosMemoryCalloc(nSize * cfLen + 1, sizeof(rocksdb_options_t*));
  RocksdbCfParam*     params = taosMemoryCalloc(nSize * cfLen + 1, sizeof(RocksdbCfParam*));
  for (int i = 0; i < nSize * cfLen + 1; i++) {
    cfOpts[i] = rocksdb_options_create_copy(handle->dbOpt);
    if (i == 0) {
      continue;
    }
dengyihao's avatar
dengyihao 已提交
712 713 714 715 716 717 718
    // refactor later
    rocksdb_block_based_table_options_t* tableOpt = rocksdb_block_based_options_create();
    rocksdb_block_based_options_set_block_cache(tableOpt, handle->cache);

    rocksdb_filterpolicy_t* filter = rocksdb_filterpolicy_create_bloom(15);
    rocksdb_block_based_options_set_filter_policy(tableOpt, filter);

dengyihao's avatar
dengyihao 已提交
719 720
    rocksdb_options_set_block_based_table_factory((rocksdb_options_t*)cfOpts[i], tableOpt);
    params[i].tableOpt = tableOpt;
dengyihao's avatar
dengyihao 已提交
721 722
  };

dengyihao's avatar
dengyihao 已提交
723 724 725 726 727 728
  rocksdb_comparator_t** pCompare = taosMemoryCalloc(nSize * cfLen + 1, sizeof(rocksdb_comparator_t**));
  for (int i = 0; i < nSize * cfLen + 1; i++) {
    if (i == 0) {
      continue;
    }
    SCfInit* cf = &ginitDict[(i - 1) % cfLen];
dengyihao's avatar
dengyihao 已提交
729 730

    rocksdb_comparator_t* compare = rocksdb_comparator_create(NULL, cf->detroyFunc, cf->cmpFunc, cf->cmpName);
dengyihao's avatar
dengyihao 已提交
731
    rocksdb_options_set_comparator((rocksdb_options_t*)cfOpts[i], compare);
dengyihao's avatar
dengyihao 已提交
732 733
    pCompare[i] = compare;
  }
dengyihao's avatar
dengyihao 已提交
734 735 736 737
  rocksdb_column_family_handle_t** cfHandle =
      taosMemoryCalloc(nSize * cfLen + 1, sizeof(rocksdb_column_family_handle_t*));
  rocksdb_t* db = rocksdb_open_column_families(handle->dbOpt, name, nSize * cfLen + 1, (const char* const*)cfNames,
                                               (const rocksdb_options_t* const*)cfOpts, cfHandle, &err);
dengyihao's avatar
dengyihao 已提交
738 739 740 741 742 743 744
  if (err != NULL) {
    qError("failed to open rocksdb cf, reason:%s", err);
    taosMemoryFree(err);
  } else {
    qDebug("succ to open rocksdb cf, reason:%s", err);
  }

dengyihao's avatar
dengyihao 已提交
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
  pIter = taosHashIterate(ids, NULL);
  idstr = taosHashGetKey(pIter, &keyLen);
  for (int i = 0; i < nSize; i++) {
    RocksdbCfInst*                   inst = taosMemoryCalloc(1, sizeof(RocksdbCfInst));
    rocksdb_column_family_handle_t** subCf = taosMemoryCalloc(cfLen, sizeof(rocksdb_column_family_handle_t*));
    rocksdb_comparator_t**           subCompare = taosMemoryCalloc(cfLen, sizeof(rocksdb_comparator_t*));
    RocksdbCfParam*                  subParam = taosMemoryCalloc(cfLen, sizeof(RocksdbCfParam));
    rocksdb_options_t**              subOpt = taosMemoryCalloc(cfLen, sizeof(rocksdb_options_t*));
    for (int j = 0; j < cfLen; j++) {
      subCf[j] = cfHandle[i * cfLen + j + 1];
      subCompare[j] = pCompare[i * cfLen + j + 1];
      subParam[j] = params[i * cfLen + j + 1];
      subOpt[j] = cfOpts[i * cfLen + j + 1];
    }
    inst->db = db;
    inst->pHandle = subCf;
    inst->wOpt = rocksdb_writeoptions_create();
    inst->rOpt = rocksdb_readoptions_create();
    inst->cfOpt = (rocksdb_options_t**)subOpt;
    inst->dbOpt = handle->dbOpt;
    inst->param = subParam;
    inst->pBackendHandle = handle;
    handle->db = db;
    SCfComparator compare = {.comp = subCompare, .numOfComp = cfLen};
    inst->pCompareNode = streamBackendAddCompare(handle, &compare);
    rocksdb_writeoptions_disable_WAL(inst->wOpt, 1);

    taosHashPut(handle->cfInst, idstr, keyLen, &inst, sizeof(void*));

    pIter = taosHashIterate(ids, pIter);
    if (pIter != NULL) idstr = taosHashGetKey(pIter, &keyLen);
  }
  rocksdb_column_family_handle_destroy(cfHandle[0]);
  rocksdb_options_destroy(cfOpts[0]);
dengyihao's avatar
dengyihao 已提交
779

dengyihao's avatar
dengyihao 已提交
780 781 782 783 784 785 786 787
  for (int i = 0; i < nSize * cfLen + 1; i++) {
    taosMemoryFree(cfNames[i]);
  }
  taosMemoryFree(cfNames);
  taosMemoryFree(cfHandle);
  taosMemoryFree(pCompare);
  taosMemoryFree(params);
  taosMemoryFree(cfOpts);
dengyihao's avatar
dengyihao 已提交
788

dengyihao's avatar
dengyihao 已提交
789
  return 0;
dengyihao's avatar
dengyihao 已提交
790
}
dengyihao's avatar
dengyihao 已提交
791
int streamStateOpenBackend(void* backend, SStreamState* pState) {
dengyihao's avatar
dengyihao 已提交
792
  qInfo("start to open backend, %p 0x%" PRIx64 "-%d", pState, pState->streamId, pState->taskId);
dengyihao's avatar
dengyihao 已提交
793
  SBackendHandle* handle = backend;
dengyihao's avatar
dengyihao 已提交
794

dengyihao's avatar
dengyihao 已提交
795
  sprintf(pState->pTdbState->idstr, "0x%" PRIx64 "-%d", pState->streamId, pState->taskId);
dengyihao's avatar
dengyihao 已提交
796
  taosThreadMutexLock(&handle->cfMutex);
dengyihao's avatar
dengyihao 已提交
797
  RocksdbCfInst** ppInst = taosHashGet(handle->cfInst, pState->pTdbState->idstr, strlen(pState->pTdbState->idstr) + 1);
dengyihao's avatar
dengyihao 已提交
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
  if (ppInst != NULL && *ppInst != NULL) {
    RocksdbCfInst* inst = *ppInst;
    pState->pTdbState->rocksdb = inst->db;
    pState->pTdbState->pHandle = inst->pHandle;
    pState->pTdbState->writeOpts = inst->wOpt;
    pState->pTdbState->readOpts = inst->rOpt;
    pState->pTdbState->cfOpts = inst->cfOpt;
    pState->pTdbState->dbOpt = handle->dbOpt;
    pState->pTdbState->param = inst->param;
    pState->pTdbState->pBackendHandle = handle;
    pState->pTdbState->pComparNode = inst->pCompareNode;
    taosThreadMutexUnlock(&handle->cfMutex);
    return 0;
  }
  taosThreadMutexUnlock(&handle->cfMutex);

dengyihao's avatar
dengyihao 已提交
814
  char* err = NULL;
dengyihao's avatar
dengyihao 已提交
815
  int   cfLen = sizeof(ginitDict) / sizeof(ginitDict[0]);
dengyihao's avatar
dengyihao 已提交
816

dengyihao's avatar
dengyihao 已提交
817
  RocksdbCfParam*           param = taosMemoryCalloc(cfLen, sizeof(RocksdbCfParam));
dengyihao's avatar
dengyihao 已提交
818 819
  const rocksdb_options_t** cfOpt = taosMemoryCalloc(cfLen, sizeof(rocksdb_options_t*));
  for (int i = 0; i < cfLen; i++) {
820
    cfOpt[i] = rocksdb_options_create_copy(handle->dbOpt);
dengyihao's avatar
dengyihao 已提交
821
    // refactor later
dengyihao's avatar
dengyihao 已提交
822
    rocksdb_block_based_table_options_t* tableOpt = rocksdb_block_based_options_create();
dengyihao's avatar
dengyihao 已提交
823
    rocksdb_block_based_options_set_block_cache(tableOpt, handle->cache);
dengyihao's avatar
dengyihao 已提交
824

dengyihao's avatar
dengyihao 已提交
825
    rocksdb_filterpolicy_t* filter = rocksdb_filterpolicy_create_bloom(15);
dengyihao's avatar
dengyihao 已提交
826
    rocksdb_block_based_options_set_filter_policy(tableOpt, filter);
dengyihao's avatar
dengyihao 已提交
827 828

    rocksdb_options_set_block_based_table_factory((rocksdb_options_t*)cfOpt[i], tableOpt);
dengyihao's avatar
dengyihao 已提交
829

dengyihao's avatar
dengyihao 已提交
830
    param[i].tableOpt = tableOpt;
dengyihao's avatar
dengyihao 已提交
831
  };
dengyihao's avatar
dengyihao 已提交
832

dengyihao's avatar
dengyihao 已提交
833
  rocksdb_comparator_t** pCompare = taosMemoryCalloc(cfLen, sizeof(rocksdb_comparator_t**));
dengyihao's avatar
dengyihao 已提交
834
  for (int i = 0; i < cfLen; i++) {
dengyihao's avatar
dengyihao 已提交
835 836
    SCfInit* cf = &ginitDict[i];

dengyihao's avatar
dengyihao 已提交
837 838 839 840
    rocksdb_comparator_t* compare = rocksdb_comparator_create(NULL, cf->detroyFunc, cf->cmpFunc, cf->cmpName);
    rocksdb_options_set_comparator((rocksdb_options_t*)cfOpt[i], compare);
    pCompare[i] = compare;
  }
dengyihao's avatar
dengyihao 已提交
841
  rocksdb_column_family_handle_t** cfHandle = taosMemoryMalloc(cfLen * sizeof(rocksdb_column_family_handle_t*));
dengyihao's avatar
dengyihao 已提交
842
  for (int i = 0; i < cfLen; i++) {
dengyihao's avatar
dengyihao 已提交
843
    char buf[128] = {0};
dengyihao's avatar
dengyihao 已提交
844
    GEN_COLUMN_FAMILY_NAME(buf, pState->pTdbState->idstr, ginitDict[i].key);
dengyihao's avatar
dengyihao 已提交
845 846
    cfHandle[i] = rocksdb_create_column_family(handle->db, cfOpt[i], buf, &err);
    if (err != NULL) {
dengyihao's avatar
dengyihao 已提交
847 848
      qError("failed to create cf:%s_%s, reason:%s", pState->pTdbState->idstr, ginitDict[i].key, err);
      taosMemoryFreeClear(err);
dengyihao's avatar
dengyihao 已提交
849 850 851
    }
  }
  pState->pTdbState->rocksdb = handle->db;
dengyihao's avatar
dengyihao 已提交
852
  pState->pTdbState->pHandle = cfHandle;
dengyihao's avatar
dengyihao 已提交
853 854 855
  pState->pTdbState->writeOpts = rocksdb_writeoptions_create();
  pState->pTdbState->readOpts = rocksdb_readoptions_create();
  pState->pTdbState->cfOpts = (rocksdb_options_t**)cfOpt;
dengyihao's avatar
dengyihao 已提交
856
  pState->pTdbState->dbOpt = handle->dbOpt;
dengyihao's avatar
dengyihao 已提交
857
  pState->pTdbState->param = param;
dengyihao's avatar
dengyihao 已提交
858
  pState->pTdbState->pBackendHandle = handle;
dengyihao's avatar
dengyihao 已提交
859 860 861

  SCfComparator compare = {.comp = pCompare, .numOfComp = cfLen};
  pState->pTdbState->pComparNode = streamBackendAddCompare(handle, &compare);
dengyihao's avatar
dengyihao 已提交
862
  // rocksdb_writeoptions_disable_WAL(pState->pTdbState->writeOpts, 1);
dengyihao's avatar
dengyihao 已提交
863
  qInfo("succ to open backend, %p, 0x%" PRIx64 "-%d", pState, pState->streamId, pState->taskId);
dengyihao's avatar
dengyihao 已提交
864 865
  return 0;
}
866

dengyihao's avatar
dengyihao 已提交
867
void streamStateCloseBackend(SStreamState* pState, bool remove) {
dengyihao's avatar
dengyihao 已提交
868 869
  SBackendHandle* pHandle = pState->pTdbState->pBackendHandle;
  taosThreadMutexLock(&pHandle->cfMutex);
dengyihao's avatar
dengyihao 已提交
870
  RocksdbCfInst** ppInst = taosHashGet(pHandle->cfInst, pState->pTdbState->idstr, strlen(pState->pTdbState->idstr) + 1);
dengyihao's avatar
dengyihao 已提交
871 872 873
  if (ppInst != NULL && *ppInst != NULL) {
    RocksdbCfInst* inst = *ppInst;
    taosMemoryFree(inst);
dengyihao's avatar
dengyihao 已提交
874
    taosHashRemove(pHandle->cfInst, pState->pTdbState->idstr, strlen(pState->pTdbState->idstr) + 1);
dengyihao's avatar
dengyihao 已提交
875 876
  }
  taosThreadMutexUnlock(&pHandle->cfMutex);
dengyihao's avatar
dengyihao 已提交
877

dengyihao's avatar
dengyihao 已提交
878
  char* status[] = {"close", "drop"};
dengyihao's avatar
dengyihao 已提交
879 880
  qInfo("start to %s backend, %p, 0x%" PRIx64 "-%d", status[remove == false ? 0 : 1], pState, pState->streamId,
        pState->taskId);
dengyihao's avatar
dengyihao 已提交
881 882 883
  if (pState->pTdbState->rocksdb == NULL) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
884

dengyihao's avatar
dengyihao 已提交
885
  int cfLen = sizeof(ginitDict) / sizeof(ginitDict[0]);
dengyihao's avatar
dengyihao 已提交
886

dengyihao's avatar
dengyihao 已提交
887 888 889
  char* err = NULL;
  if (remove) {
    for (int i = 0; i < cfLen; i++) {
dengyihao's avatar
dengyihao 已提交
890
      rocksdb_drop_column_family(pState->pTdbState->rocksdb, pState->pTdbState->pHandle[i], &err);
dengyihao's avatar
dengyihao 已提交
891 892 893 894 895 896 897 898
      if (err != NULL) {
        qError("failed to create cf:%s_%s, reason:%s", pState->pTdbState->idstr, ginitDict[i].key, err);
        taosMemoryFreeClear(err);
      }
    }
  } else {
    rocksdb_flushoptions_t* flushOpt = rocksdb_flushoptions_create();
    for (int i = 0; i < cfLen; i++) {
dengyihao's avatar
dengyihao 已提交
899
      rocksdb_flush_cf(pState->pTdbState->rocksdb, flushOpt, pState->pTdbState->pHandle[i], &err);
dengyihao's avatar
dengyihao 已提交
900 901 902 903
      if (err != NULL) {
        qError("failed to create cf:%s_%s, reason:%s", pState->pTdbState->idstr, ginitDict[i].key, err);
        taosMemoryFreeClear(err);
      }
dengyihao's avatar
dengyihao 已提交
904
    }
dengyihao's avatar
dengyihao 已提交
905
    rocksdb_flushoptions_destroy(flushOpt);
dengyihao's avatar
dengyihao 已提交
906 907
  }

dengyihao's avatar
dengyihao 已提交
908 909 910
  for (int i = 0; i < cfLen; i++) {
    rocksdb_column_family_handle_destroy(pState->pTdbState->pHandle[i]);
  }
dengyihao's avatar
dengyihao 已提交
911
  taosMemoryFreeClear(pState->pTdbState->pHandle);
dengyihao's avatar
dengyihao 已提交
912 913
  for (int i = 0; i < cfLen; i++) {
    rocksdb_options_destroy(pState->pTdbState->cfOpts[i]);
dengyihao's avatar
dengyihao 已提交
914
    rocksdb_block_based_options_destroy(((RocksdbCfParam*)pState->pTdbState->param)[i].tableOpt);
dengyihao's avatar
dengyihao 已提交
915
  }
dengyihao's avatar
dengyihao 已提交
916

dengyihao's avatar
dengyihao 已提交
917 918 919
  if (remove) {
    streamBackendDelCompare(pState->pTdbState->pBackendHandle, pState->pTdbState->pComparNode);
  }
dengyihao's avatar
dengyihao 已提交
920 921 922 923 924
  rocksdb_writeoptions_destroy(pState->pTdbState->writeOpts);
  pState->pTdbState->writeOpts = NULL;

  rocksdb_readoptions_destroy(pState->pTdbState->readOpts);
  pState->pTdbState->readOpts = NULL;
dengyihao's avatar
dengyihao 已提交
925
  taosMemoryFreeClear(pState->pTdbState->cfOpts);
dengyihao's avatar
dengyihao 已提交
926
  taosMemoryFreeClear(pState->pTdbState->param);
dengyihao's avatar
dengyihao 已提交
927
  pState->pTdbState->rocksdb = NULL;
dengyihao's avatar
dengyihao 已提交
928
}
dengyihao's avatar
dengyihao 已提交
929 930 931 932 933 934 935
void streamStateDestroyCompar(void* arg) {
  SCfComparator* comp = (SCfComparator*)arg;
  for (int i = 0; i < comp->numOfComp; i++) {
    rocksdb_comparator_destroy(comp->comp[i]);
  }
  taosMemoryFree(comp->comp);
}
936

dengyihao's avatar
dengyihao 已提交
937
int streamGetInit(const char* funcName) {
dengyihao's avatar
dengyihao 已提交
938
  size_t len = strlen(funcName);
dengyihao's avatar
dengyihao 已提交
939
  for (int i = 0; i < sizeof(ginitDict) / sizeof(ginitDict[0]); i++) {
dengyihao's avatar
dengyihao 已提交
940
    if (len == ginitDict[i].len && strncmp(funcName, ginitDict[i].key, strlen(funcName)) == 0) {
dengyihao's avatar
dengyihao 已提交
941 942 943 944 945
      return i;
    }
  }
  return -1;
}
dengyihao's avatar
dengyihao 已提交
946
bool streamStateIterSeekAndValid(rocksdb_iterator_t* iter, char* buf, size_t len) {
dengyihao's avatar
dengyihao 已提交
947
  rocksdb_iter_seek(iter, buf, len);
dengyihao's avatar
dengyihao 已提交
948
  if (!rocksdb_iter_valid(iter)) {
dengyihao's avatar
dengyihao 已提交
949 950 951 952
    rocksdb_iter_seek_for_prev(iter, buf, len);
    if (!rocksdb_iter_valid(iter)) {
      return false;
    }
dengyihao's avatar
dengyihao 已提交
953
  }
dengyihao's avatar
dengyihao 已提交
954
  return true;
dengyihao's avatar
dengyihao 已提交
955
}
dengyihao's avatar
dengyihao 已提交
956 957
rocksdb_iterator_t* streamStateIterCreate(SStreamState* pState, const char* cfName, rocksdb_snapshot_t** snapshot,
                                          rocksdb_readoptions_t** readOpt) {
dengyihao's avatar
dengyihao 已提交
958
  int idx = streamGetInit(cfName);
dengyihao's avatar
dengyihao 已提交
959

dengyihao's avatar
dengyihao 已提交
960
  if (snapshot != NULL) {
dengyihao's avatar
dengyihao 已提交
961
    *snapshot = (rocksdb_snapshot_t*)rocksdb_create_snapshot(pState->pTdbState->rocksdb);
dengyihao's avatar
dengyihao 已提交
962
  }
dengyihao's avatar
dengyihao 已提交
963 964
  rocksdb_readoptions_t* rOpt = rocksdb_readoptions_create();
  *readOpt = rOpt;
dengyihao's avatar
dengyihao 已提交
965

dengyihao's avatar
dengyihao 已提交
966
  rocksdb_readoptions_set_snapshot(rOpt, *snapshot);
dengyihao's avatar
dengyihao 已提交
967 968 969
  rocksdb_readoptions_set_fill_cache(rOpt, 0);

  return rocksdb_create_iterator_cf(pState->pTdbState->rocksdb, rOpt, pState->pTdbState->pHandle[idx]);
dengyihao's avatar
dengyihao 已提交
970 971
}

dengyihao's avatar
dengyihao 已提交
972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
#define STREAM_STATE_PUT_ROCKSDB(pState, funcname, key, value, vLen)                                     \
  do {                                                                                                   \
    code = 0;                                                                                            \
    char  buf[128] = {0};                                                                                \
    char* err = NULL;                                                                                    \
    int   i = streamGetInit(funcname);                                                                   \
    if (i < 0) {                                                                                         \
      qWarn("streamState failed to get cf name: %s", funcname);                                          \
      code = -1;                                                                                         \
      break;                                                                                             \
    }                                                                                                    \
    char toString[128] = {0};                                                                            \
    if (qDebugFlag & DEBUG_TRACE) ginitDict[i].toStrFunc((void*)key, toString);                          \
    int32_t                         klen = ginitDict[i].enFunc((void*)key, buf);                         \
    rocksdb_column_family_handle_t* pHandle = pState->pTdbState->pHandle[ginitDict[i].idx];              \
    rocksdb_t*                      db = pState->pTdbState->rocksdb;                                     \
    rocksdb_writeoptions_t*         opts = pState->pTdbState->writeOpts;                                 \
    char*                           ttlV = NULL;                                                         \
    int32_t                         ttlVLen = ginitDict[i].enValueFunc((char*)value, vLen, 0, &ttlV);    \
    rocksdb_put_cf(db, opts, pHandle, (const char*)buf, klen, (const char*)ttlV, (size_t)ttlVLen, &err); \
    if (err != NULL) {                                                                                   \
      taosMemoryFree(err);                                                                               \
      qDebug("streamState str: %s failed to write to %s, err: %s", toString, funcname, err);             \
      code = -1;                                                                                         \
    } else {                                                                                             \
      qDebug("streamState str:%s succ to write to %s, valLen:%d", toString, funcname, vLen);             \
    }                                                                                                    \
    taosMemoryFree(ttlV);                                                                                \
dengyihao's avatar
dengyihao 已提交
1000 1001
  } while (0);

dengyihao's avatar
dengyihao 已提交
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
#define STREAM_STATE_GET_ROCKSDB(pState, funcname, key, pVal, vLen)                                                    \
  do {                                                                                                                 \
    code = 0;                                                                                                          \
    char  buf[128] = {0};                                                                                              \
    char* err = NULL;                                                                                                  \
    int   i = streamGetInit(funcname);                                                                                 \
    if (i < 0) {                                                                                                       \
      qWarn("streamState failed to get cf name: %s", funcname);                                                        \
      code = -1;                                                                                                       \
      break;                                                                                                           \
    }                                                                                                                  \
    char toString[128] = {0};                                                                                          \
    if (qDebugFlag & DEBUG_TRACE) ginitDict[i].toStrFunc((void*)key, toString);                                        \
    int32_t                         klen = ginitDict[i].enFunc((void*)key, buf);                                       \
    rocksdb_column_family_handle_t* pHandle = pState->pTdbState->pHandle[ginitDict[i].idx];                            \
    rocksdb_t*                      db = pState->pTdbState->rocksdb;                                                   \
    rocksdb_readoptions_t*          opts = pState->pTdbState->readOpts;                                                \
    size_t                          len = 0;                                                                           \
    char* val = rocksdb_get_cf(db, opts, pHandle, (const char*)buf, klen, (size_t*)&len, &err);                        \
    if (val == NULL) {                                                                                                 \
      if (err == NULL) {                                                                                               \
        qDebug("streamState str: %s failed to read from %s_%s, err: not exist", toString, pState->pTdbState->idstr,    \
               funcname);                                                                                              \
      } else {                                                                                                         \
        qDebug("streamState str: %s failed to read from %s_%s, err: %s", toString, pState->pTdbState->idstr, funcname, \
               err);                                                                                                   \
        taosMemoryFreeClear(err);                                                                                      \
      }                                                                                                                \
      code = -1;                                                                                                       \
    } else {                                                                                                           \
      char*   p = NULL;                                                                                                \
      int32_t len = ginitDict[i].deValueFunc(val, len, NULL, (char**)pVal);                                            \
      if (len < 0) {                                                                                                   \
        qDebug("streamState str: %s failed to read from %s_%s, err: already ttl ", toString, pState->pTdbState->idstr, \
               funcname);                                                                                              \
        code = -1;                                                                                                     \
      } else {                                                                                                         \
        qDebug("streamState str: %s succ to read from %s_%s, valLen:%d", toString, pState->pTdbState->idstr, funcname, \
               len);                                                                                                   \
      }                                                                                                                \
      taosMemoryFree(val);                                                                                             \
      if (vLen != NULL) *vLen = len;                                                                                   \
    }                                                                                                                  \
    if (code == 0)                                                                                                     \
      qDebug("streamState str: %s succ to read from %s_%s", toString, pState->pTdbState->idstr, funcname);             \
dengyihao's avatar
dengyihao 已提交
1047 1048
  } while (0);

dengyihao's avatar
dengyihao 已提交
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
#define STREAM_STATE_DEL_ROCKSDB(pState, funcname, key)                                                             \
  do {                                                                                                              \
    code = 0;                                                                                                       \
    char  buf[128] = {0};                                                                                           \
    char* err = NULL;                                                                                               \
    int   i = streamGetInit(funcname);                                                                              \
    if (i < 0) {                                                                                                    \
      qWarn("streamState failed to get cf name: %s_%s", pState->pTdbState->idstr, funcname);                        \
      code = -1;                                                                                                    \
      break;                                                                                                        \
    }                                                                                                               \
    char toString[128] = {0};                                                                                       \
    if (qDebugFlag & DEBUG_TRACE) ginitDict[i].toStrFunc((void*)key, toString);                                     \
    int32_t                         klen = ginitDict[i].enFunc((void*)key, buf);                                    \
    rocksdb_column_family_handle_t* pHandle = pState->pTdbState->pHandle[ginitDict[i].idx];                         \
    rocksdb_t*                      db = pState->pTdbState->rocksdb;                                                \
    rocksdb_writeoptions_t*         opts = pState->pTdbState->writeOpts;                                            \
    rocksdb_delete_cf(db, opts, pHandle, (const char*)buf, klen, &err);                                             \
    if (err != NULL) {                                                                                              \
      qError("streamState str: %s failed to del from %s_%s, err: %s", toString, pState->pTdbState->idstr, funcname, \
             err);                                                                                                  \
      taosMemoryFree(err);                                                                                          \
      code = -1;                                                                                                    \
    } else {                                                                                                        \
      qDebug("streamState str: %s succ to del from %s_%s", toString, pState->pTdbState->idstr, funcname);           \
    }                                                                                                               \
dengyihao's avatar
dengyihao 已提交
1075 1076
  } while (0);

dengyihao's avatar
dengyihao 已提交
1077
// state cf
dengyihao's avatar
dengyihao 已提交
1078 1079 1080 1081
int32_t streamStatePut_rocksdb(SStreamState* pState, const SWinKey* key, const void* value, int32_t vLen) {
  int code = 0;

  SStateKey sKey = {.key = *key, .opNum = pState->number};
dengyihao's avatar
dengyihao 已提交
1082
  STREAM_STATE_PUT_ROCKSDB(pState, "state", &sKey, (void*)value, vLen);
dengyihao's avatar
dengyihao 已提交
1083 1084 1085 1086 1087
  return code;
}
int32_t streamStateGet_rocksdb(SStreamState* pState, const SWinKey* key, void** pVal, int32_t* pVLen) {
  int       code = 0;
  SStateKey sKey = {.key = *key, .opNum = pState->number};
dengyihao's avatar
dengyihao 已提交
1088
  STREAM_STATE_GET_ROCKSDB(pState, "state", &sKey, pVal, pVLen);
dengyihao's avatar
dengyihao 已提交
1089 1090 1091 1092 1093
  return code;
}
int32_t streamStateDel_rocksdb(SStreamState* pState, const SWinKey* key) {
  int       code = 0;
  SStateKey sKey = {.key = *key, .opNum = pState->number};
dengyihao's avatar
dengyihao 已提交
1094
  STREAM_STATE_DEL_ROCKSDB(pState, "state", &sKey);
dengyihao's avatar
dengyihao 已提交
1095 1096
  return code;
}
dengyihao's avatar
dengyihao 已提交
1097
int32_t streamStateClear_rocksdb(SStreamState* pState) {
dengyihao's avatar
dengyihao 已提交
1098
  qDebug("streamStateClear_rocksdb");
dengyihao's avatar
dengyihao 已提交
1099

dengyihao's avatar
dengyihao 已提交
1100 1101 1102 1103
  SStateKey sKey = {.key = {.ts = 0, .groupId = 0}, .opNum = pState->number};
  SStateKey eKey = {.key = {.ts = INT64_MAX, .groupId = UINT64_MAX}, .opNum = pState->number};
  char      sKeyStr[128] = {0};
  char      eKeyStr[128] = {0};
dengyihao's avatar
dengyihao 已提交
1104

dengyihao's avatar
dengyihao 已提交
1105 1106
  int sLen = stateKeyEncode(&sKey, sKeyStr);
  int eLen = stateKeyEncode(&eKey, eKeyStr);
dengyihao's avatar
dengyihao 已提交
1107

dengyihao's avatar
dengyihao 已提交
1108 1109 1110 1111 1112 1113
  char toStringStart[128] = {0};
  char toStringEnd[128] = {0};
  if (qDebugFlag & DEBUG_TRACE) {
    stateKeyToString(&sKey, toStringStart);
    stateKeyToString(&eKey, toStringEnd);
  }
dengyihao's avatar
dengyihao 已提交
1114

dengyihao's avatar
dengyihao 已提交
1115
  char* err = NULL;
dengyihao's avatar
dengyihao 已提交
1116
  rocksdb_delete_range_cf(pState->pTdbState->rocksdb, pState->pTdbState->writeOpts, pState->pTdbState->pHandle[1],
dengyihao's avatar
dengyihao 已提交
1117
                          sKeyStr, sLen, eKeyStr, eLen, &err);
1118 1119
  // rocksdb_compact_range_cf(pState->pTdbState->rocksdb, pState->pTdbState->pHandle[0], sKeyStr, sLen, eKeyStr,
  // eLen);
dengyihao's avatar
dengyihao 已提交
1120
  if (err != NULL) {
dengyihao's avatar
dengyihao 已提交
1121
    qWarn("failed to delete range cf(state) start: %s, end:%s, reason:%s", toStringStart, toStringEnd, err);
dengyihao's avatar
dengyihao 已提交
1122 1123 1124
    taosMemoryFree(err);
  }

dengyihao's avatar
dengyihao 已提交
1125
  return 0;
dengyihao's avatar
dengyihao 已提交
1126
}
dengyihao's avatar
dengyihao 已提交
1127 1128
int32_t streamStateCurNext_rocksdb(SStreamState* pState, SStreamStateCur* pCur) {
  if (!pCur) {
dengyihao's avatar
dengyihao 已提交
1129 1130
    return -1;
  }
dengyihao's avatar
dengyihao 已提交
1131
  rocksdb_iter_next(pCur->iter);
dengyihao's avatar
dengyihao 已提交
1132 1133
  return 0;
}
dengyihao's avatar
dengyihao 已提交
1134 1135 1136 1137 1138 1139 1140 1141
int32_t streamStateGetFirst_rocksdb(SStreamState* pState, SWinKey* key) {
  qDebug("streamStateGetFirst_rocksdb");
  SWinKey tmp = {.ts = 0, .groupId = 0};
  streamStatePut_rocksdb(pState, &tmp, NULL, 0);
  SStreamStateCur* pCur = streamStateSeekKeyNext_rocksdb(pState, &tmp);
  int32_t          code = streamStateGetKVByCur_rocksdb(pCur, key, NULL, 0);
  streamStateFreeCur(pCur);
  streamStateDel_rocksdb(pState, &tmp);
dengyihao's avatar
dengyihao 已提交
1142 1143 1144
  return code;
}

dengyihao's avatar
dengyihao 已提交
1145 1146 1147 1148
int32_t streamStateGetGroupKVByCur_rocksdb(SStreamStateCur* pCur, SWinKey* pKey, const void** pVal, int32_t* pVLen) {
  qDebug("streamStateGetGroupKVByCur_rocksdb");
  if (!pCur) {
    return -1;
dengyihao's avatar
dengyihao 已提交
1149
  }
dengyihao's avatar
dengyihao 已提交
1150
  uint64_t groupId = pKey->groupId;
dengyihao's avatar
dengyihao 已提交
1151

dengyihao's avatar
dengyihao 已提交
1152 1153 1154 1155 1156
  int32_t code = streamStateFillGetKVByCur_rocksdb(pCur, pKey, pVal, pVLen);
  if (code == 0) {
    if (pKey->groupId == groupId) {
      return 0;
    }
dengyihao's avatar
dengyihao 已提交
1157
  }
dengyihao's avatar
dengyihao 已提交
1158
  return -1;
dengyihao's avatar
dengyihao 已提交
1159
}
dengyihao's avatar
dengyihao 已提交
1160 1161 1162 1163 1164
int32_t streamStateAddIfNotExist_rocksdb(SStreamState* pState, const SWinKey* key, void** pVal, int32_t* pVLen) {
  qDebug("streamStateAddIfNotExist_rocksdb");
  int32_t size = *pVLen;
  if (streamStateGet_rocksdb(pState, key, pVal, pVLen) == 0) {
    return 0;
dengyihao's avatar
dengyihao 已提交
1165
  }
dengyihao's avatar
dengyihao 已提交
1166 1167 1168
  *pVal = taosMemoryMalloc(size);
  memset(*pVal, 0, size);
  return 0;
dengyihao's avatar
dengyihao 已提交
1169
}
dengyihao's avatar
dengyihao 已提交
1170 1171 1172
int32_t streamStateCurPrev_rocksdb(SStreamState* pState, SStreamStateCur* pCur) {
  qDebug("streamStateCurPrev_rocksdb");
  if (!pCur) return -1;
dengyihao's avatar
dengyihao 已提交
1173

dengyihao's avatar
dengyihao 已提交
1174 1175
  rocksdb_iter_prev(pCur->iter);
  return 0;
dengyihao's avatar
dengyihao 已提交
1176
}
dengyihao's avatar
dengyihao 已提交
1177 1178 1179 1180 1181
int32_t streamStateGetKVByCur_rocksdb(SStreamStateCur* pCur, SWinKey* pKey, const void** pVal, int32_t* pVLen) {
  qDebug("streamStateGetKVByCur_rocksdb");
  if (!pCur) return -1;
  SStateKey  tkey;
  SStateKey* pKtmp = &tkey;
dengyihao's avatar
dengyihao 已提交
1182

dengyihao's avatar
dengyihao 已提交
1183
  if (rocksdb_iter_valid(pCur->iter) && !iterValueIsStale(pCur->iter)) {
dengyihao's avatar
dengyihao 已提交
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
    size_t tlen;
    char*  keyStr = (char*)rocksdb_iter_key(pCur->iter, &tlen);
    stateKeyDecode((void*)pKtmp, keyStr);
    if (pKtmp->opNum != pCur->number) {
      return -1;
    }
    size_t vlen = 0;
    if (pVal != NULL) *pVal = (char*)rocksdb_iter_value(pCur->iter, &vlen);
    if (pVLen != NULL) *pVLen = vlen;
    *pKey = pKtmp->key;
    return 0;
dengyihao's avatar
dengyihao 已提交
1195
  }
dengyihao's avatar
dengyihao 已提交
1196
  return -1;
dengyihao's avatar
dengyihao 已提交
1197
}
dengyihao's avatar
dengyihao 已提交
1198 1199 1200 1201 1202 1203
SStreamStateCur* streamStateGetAndCheckCur_rocksdb(SStreamState* pState, SWinKey* key) {
  qDebug("streamStateGetAndCheckCur_rocksdb");
  SStreamStateCur* pCur = streamStateFillGetCur_rocksdb(pState, key);
  if (pCur) {
    int32_t code = streamStateGetGroupKVByCur_rocksdb(pCur, key, NULL, 0);
    if (code == 0) return pCur;
dengyihao's avatar
dengyihao 已提交
1204 1205
    streamStateFreeCur(pCur);
  }
dengyihao's avatar
dengyihao 已提交
1206
  return NULL;
dengyihao's avatar
dengyihao 已提交
1207
}
dengyihao's avatar
dengyihao 已提交
1208

dengyihao's avatar
dengyihao 已提交
1209 1210
SStreamStateCur* streamStateSeekKeyNext_rocksdb(SStreamState* pState, const SWinKey* key) {
  qDebug("streamStateSeekKeyNext_rocksdb");
dengyihao's avatar
dengyihao 已提交
1211 1212 1213 1214 1215
  SStreamStateCur* pCur = taosMemoryCalloc(1, sizeof(SStreamStateCur));
  if (pCur == NULL) {
    return NULL;
  }
  pCur->number = pState->number;
dengyihao's avatar
dengyihao 已提交
1216 1217
  pCur->db = pState->pTdbState->rocksdb;
  pCur->iter = streamStateIterCreate(pState, "state", &pCur->snapshot, &pCur->readOpt);
dengyihao's avatar
dengyihao 已提交
1218

dengyihao's avatar
dengyihao 已提交
1219 1220 1221
  SStateKey sKey = {.key = *key, .opNum = pState->number};
  char      buf[128] = {0};
  int       len = stateKeyEncode((void*)&sKey, buf);
dengyihao's avatar
dengyihao 已提交
1222
  if (!streamStateIterSeekAndValid(pCur->iter, buf, len)) {
dengyihao's avatar
dengyihao 已提交
1223 1224
    streamStateFreeCur(pCur);
    return NULL;
dengyihao's avatar
dengyihao 已提交
1225
  }
dengyihao's avatar
dengyihao 已提交
1226
  // skip ttl expired data
dengyihao's avatar
dengyihao 已提交
1227
  while (rocksdb_iter_valid(pCur->iter) && iterValueIsStale(pCur->iter)) {
dengyihao's avatar
dengyihao 已提交
1228
    rocksdb_iter_next(pCur->iter);
dengyihao's avatar
dengyihao 已提交
1229
  }
dengyihao's avatar
dengyihao 已提交
1230

dengyihao's avatar
dengyihao 已提交
1231 1232 1233 1234 1235 1236 1237
  if (rocksdb_iter_valid(pCur->iter)) {
    SStateKey curKey;
    size_t    kLen;
    char*     keyStr = (char*)rocksdb_iter_key(pCur->iter, &kLen);
    stateKeyDecode((void*)&curKey, keyStr);
    if (stateKeyCmpr(&sKey, sizeof(sKey), &curKey, sizeof(curKey)) > 0) {
      return pCur;
dengyihao's avatar
dengyihao 已提交
1238
    }
dengyihao's avatar
dengyihao 已提交
1239 1240
    rocksdb_iter_next(pCur->iter);
    return pCur;
dengyihao's avatar
dengyihao 已提交
1241
  }
dengyihao's avatar
dengyihao 已提交
1242 1243
  streamStateFreeCur(pCur);
  return NULL;
dengyihao's avatar
dengyihao 已提交
1244 1245
}

dengyihao's avatar
dengyihao 已提交
1246 1247
SStreamStateCur* streamStateSeekToLast_rocksdb(SStreamState* pState, const SWinKey* key) {
  qDebug("streamStateGetCur_rocksdb");
dengyihao's avatar
dengyihao 已提交
1248 1249 1250 1251 1252
  int32_t         code = 0;
  const SStateKey maxStateKey = {.key = {.groupId = UINT64_MAX, .ts = INT64_MAX}, .opNum = INT64_MAX};
  STREAM_STATE_PUT_ROCKSDB(pState, "state", &maxStateKey, "", 0);
  char    buf[128] = {0};
  int32_t klen = stateKeyEncode((void*)&maxStateKey, buf);
dengyihao's avatar
dengyihao 已提交
1253

dengyihao's avatar
dengyihao 已提交
1254
  SStreamStateCur* pCur = taosMemoryCalloc(1, sizeof(SStreamStateCur));
dengyihao's avatar
dengyihao 已提交
1255 1256 1257
  if (pCur == NULL) return NULL;
  pCur->db = pState->pTdbState->rocksdb;
  pCur->iter = streamStateIterCreate(pState, "state", &pCur->snapshot, &pCur->readOpt);
dengyihao's avatar
dengyihao 已提交
1258
  rocksdb_iter_seek(pCur->iter, buf, (size_t)klen);
dengyihao's avatar
dengyihao 已提交
1259

dengyihao's avatar
dengyihao 已提交
1260
  rocksdb_iter_prev(pCur->iter);
dengyihao's avatar
dengyihao 已提交
1261
  while (rocksdb_iter_valid(pCur->iter) && iterValueIsStale(pCur->iter)) {
dengyihao's avatar
dengyihao 已提交
1262 1263
    rocksdb_iter_prev(pCur->iter);
  }
dengyihao's avatar
dengyihao 已提交
1264

dengyihao's avatar
dengyihao 已提交
1265 1266 1267 1268
  if (!rocksdb_iter_valid(pCur->iter)) {
    streamStateFreeCur(pCur);
    pCur = NULL;
  }
dengyihao's avatar
dengyihao 已提交
1269
  STREAM_STATE_DEL_ROCKSDB(pState, "state", &maxStateKey);
dengyihao's avatar
dengyihao 已提交
1270
  return pCur;
dengyihao's avatar
dengyihao 已提交
1271 1272
}

dengyihao's avatar
dengyihao 已提交
1273
SStreamStateCur* streamStateGetCur_rocksdb(SStreamState* pState, const SWinKey* key) {
dengyihao's avatar
dengyihao 已提交
1274
  qDebug("streamStateGetCur_rocksdb");
dengyihao's avatar
dengyihao 已提交
1275
  SStreamStateCur* pCur = taosMemoryCalloc(1, sizeof(SStreamStateCur));
dengyihao's avatar
dengyihao 已提交
1276

dengyihao's avatar
dengyihao 已提交
1277
  if (pCur == NULL) return NULL;
dengyihao's avatar
dengyihao 已提交
1278
  pCur->db = pState->pTdbState->rocksdb;
dengyihao's avatar
dengyihao 已提交
1279
  pCur->iter = streamStateIterCreate(pState, "state", &pCur->snapshot, &pCur->readOpt);
dengyihao's avatar
dengyihao 已提交
1280

dengyihao's avatar
dengyihao 已提交
1281
  SStateKey sKey = {.key = *key, .opNum = pState->number};
dengyihao's avatar
dengyihao 已提交
1282
  char      buf[128] = {0};
dengyihao's avatar
dengyihao 已提交
1283
  int       len = stateKeyEncode((void*)&sKey, buf);
dengyihao's avatar
dengyihao 已提交
1284

dengyihao's avatar
dengyihao 已提交
1285
  rocksdb_iter_seek(pCur->iter, buf, len);
dengyihao's avatar
dengyihao 已提交
1286

dengyihao's avatar
dengyihao 已提交
1287
  if (rocksdb_iter_valid(pCur->iter) && !iterValueIsStale(pCur->iter)) {
dengyihao's avatar
dengyihao 已提交
1288 1289
    size_t vlen;
    char*  val = (char*)rocksdb_iter_value(pCur->iter, &vlen);
dengyihao's avatar
dengyihao 已提交
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
    if (!streamStateValueIsStale(val)) {
      SStateKey curKey;
      size_t    kLen = 0;
      char*     keyStr = (char*)rocksdb_iter_key(pCur->iter, &kLen);
      stateKeyDecode((void*)&curKey, keyStr);

      if (stateKeyCmpr(&sKey, sizeof(sKey), &curKey, sizeof(curKey)) == 0) {
        pCur->number = pState->number;
        return pCur;
      }
dengyihao's avatar
dengyihao 已提交
1300
    }
dengyihao's avatar
dengyihao 已提交
1301
  }
dengyihao's avatar
dengyihao 已提交
1302 1303 1304
  streamStateFreeCur(pCur);
  return NULL;
}
dengyihao's avatar
dengyihao 已提交
1305

dengyihao's avatar
dengyihao 已提交
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
// func cf
int32_t streamStateFuncPut_rocksdb(SStreamState* pState, const STupleKey* key, const void* value, int32_t vLen) {
  int code = 0;
  STREAM_STATE_PUT_ROCKSDB(pState, "func", key, (void*)value, vLen);
  return code;
}
int32_t streamStateFuncGet_rocksdb(SStreamState* pState, const STupleKey* key, void** pVal, int32_t* pVLen) {
  int code = 0;
  STREAM_STATE_GET_ROCKSDB(pState, "func", key, pVal, pVLen);
  return 0;
}
int32_t streamStateFuncDel_rocksdb(SStreamState* pState, const STupleKey* key) {
  int code = 0;
  STREAM_STATE_DEL_ROCKSDB(pState, "func", key);
dengyihao's avatar
dengyihao 已提交
1320 1321
  return 0;
}
dengyihao's avatar
dengyihao 已提交
1322

dengyihao's avatar
dengyihao 已提交
1323
// session cf
dengyihao's avatar
dengyihao 已提交
1324 1325 1326
int32_t streamStateSessionPut_rocksdb(SStreamState* pState, const SSessionKey* key, const void* value, int32_t vLen) {
  int              code = 0;
  SStateSessionKey sKey = {.key = *key, .opNum = pState->number};
dengyihao's avatar
dengyihao 已提交
1327
  STREAM_STATE_PUT_ROCKSDB(pState, "sess", &sKey, value, vLen);
dengyihao's avatar
dengyihao 已提交
1328 1329
  if (code == -1) {
  }
dengyihao's avatar
dengyihao 已提交
1330 1331
  return code;
}
dengyihao's avatar
dengyihao 已提交
1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
int32_t streamStateSessionGet_rocksdb(SStreamState* pState, SSessionKey* key, void** pVal, int32_t* pVLen) {
  qDebug("streamStateSessionGet_rocksdb");
  int              code = 0;
  SStreamStateCur* pCur = streamStateSessionSeekKeyCurrentNext_rocksdb(pState, key);
  SSessionKey      resKey = *key;
  void*            tmp = NULL;
  int32_t          vLen = 0;
  code = streamStateSessionGetKVByCur_rocksdb(pCur, &resKey, &tmp, &vLen);
  if (code == 0) {
    if (pVLen != NULL) *pVLen = vLen;
dengyihao's avatar
dengyihao 已提交
1342

dengyihao's avatar
dengyihao 已提交
1343 1344 1345 1346 1347 1348
    if (key->win.skey != resKey.win.skey) {
      code = -1;
    } else {
      *key = resKey;
      *pVal = taosMemoryCalloc(1, *pVLen);
      memcpy(*pVal, tmp, *pVLen);
dengyihao's avatar
dengyihao 已提交
1349 1350
    }
  }
dengyihao's avatar
dengyihao 已提交
1351
  taosMemoryFree(tmp);
dengyihao's avatar
dengyihao 已提交
1352 1353 1354
  streamStateFreeCur(pCur);
  // impl later
  return code;
dengyihao's avatar
dengyihao 已提交
1355
}
dengyihao's avatar
dengyihao 已提交
1356 1357 1358 1359 1360 1361 1362

int32_t streamStateSessionDel_rocksdb(SStreamState* pState, const SSessionKey* key) {
  int              code = 0;
  SStateSessionKey sKey = {.key = *key, .opNum = pState->number};
  STREAM_STATE_DEL_ROCKSDB(pState, "sess", &sKey);
  return code;
}
dengyihao's avatar
dengyihao 已提交
1363
SStreamStateCur* streamStateSessionSeekKeyCurrentPrev_rocksdb(SStreamState* pState, const SSessionKey* key) {
dengyihao's avatar
dengyihao 已提交
1364
  qDebug("streamStateSessionSeekKeyCurrentPrev_rocksdb");
dengyihao's avatar
dengyihao 已提交
1365 1366 1367 1368 1369
  SStreamStateCur* pCur = taosMemoryCalloc(1, sizeof(SStreamStateCur));
  if (pCur == NULL) {
    return NULL;
  }
  pCur->number = pState->number;
dengyihao's avatar
dengyihao 已提交
1370 1371
  pCur->db = pState->pTdbState->rocksdb;
  pCur->iter = streamStateIterCreate(pState, "sess", &pCur->snapshot, &pCur->readOpt);
dengyihao's avatar
dengyihao 已提交
1372

dengyihao's avatar
dengyihao 已提交
1373
  char             buf[128] = {0};
dengyihao's avatar
dengyihao 已提交
1374 1375
  SStateSessionKey sKey = {.key = *key, .opNum = pState->number};
  int              len = stateSessionKeyEncode(&sKey, buf);
dengyihao's avatar
dengyihao 已提交
1376
  if (!streamStateIterSeekAndValid(pCur->iter, buf, len)) {
dengyihao's avatar
dengyihao 已提交
1377
    streamStateFreeCur(pCur);
dengyihao's avatar
dengyihao 已提交
1378
    return NULL;
dengyihao's avatar
dengyihao 已提交
1379
  }
dengyihao's avatar
dengyihao 已提交
1380 1381
  while (rocksdb_iter_valid(pCur->iter) && iterValueIsStale(pCur->iter)) rocksdb_iter_prev(pCur->iter);

dengyihao's avatar
dengyihao 已提交
1382
  if (!rocksdb_iter_valid(pCur->iter)) {
dengyihao's avatar
dengyihao 已提交
1383 1384
    streamStateFreeCur(pCur);
    return NULL;
dengyihao's avatar
dengyihao 已提交
1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
  }

  int32_t          c = 0;
  size_t           klen;
  const char*      iKey = rocksdb_iter_key(pCur->iter, &klen);
  SStateSessionKey curKey = {0};
  stateSessionKeyDecode(&curKey, (char*)iKey);
  if (stateSessionKeyCmpr(&sKey, sizeof(sKey), &curKey, sizeof(curKey)) >= 0) return pCur;

  rocksdb_iter_prev(pCur->iter);
dengyihao's avatar
dengyihao 已提交
1395
  if (!rocksdb_iter_valid(pCur->iter)) {
dengyihao's avatar
dengyihao 已提交
1396 1397
    // qWarn("streamState failed to seek key prev
    // %s", toString);
dengyihao's avatar
dengyihao 已提交
1398 1399 1400
    streamStateFreeCur(pCur);
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
1401 1402
  return pCur;
}
dengyihao's avatar
dengyihao 已提交
1403
SStreamStateCur* streamStateSessionSeekKeyCurrentNext_rocksdb(SStreamState* pState, SSessionKey* key) {
dengyihao's avatar
dengyihao 已提交
1404
  qDebug("streamStateSessionSeekKeyCurrentNext_rocksdb");
dengyihao's avatar
dengyihao 已提交
1405 1406 1407 1408
  SStreamStateCur* pCur = taosMemoryCalloc(1, sizeof(SStreamStateCur));
  if (pCur == NULL) {
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
1409 1410
  pCur->db = pState->pTdbState->rocksdb;
  pCur->iter = streamStateIterCreate(pState, "sess", &pCur->snapshot, &pCur->readOpt);
dengyihao's avatar
dengyihao 已提交
1411
  pCur->number = pState->number;
dengyihao's avatar
dengyihao 已提交
1412

dengyihao's avatar
dengyihao 已提交
1413
  char buf[128] = {0};
dengyihao's avatar
dengyihao 已提交
1414

dengyihao's avatar
dengyihao 已提交
1415
  SStateSessionKey sKey = {.key = *key, .opNum = pState->number};
dengyihao's avatar
dengyihao 已提交
1416
  int              len = stateSessionKeyEncode(&sKey, buf);
dengyihao's avatar
dengyihao 已提交
1417
  if (!streamStateIterSeekAndValid(pCur->iter, buf, len)) {
dengyihao's avatar
dengyihao 已提交
1418 1419
    streamStateFreeCur(pCur);
    return NULL;
dengyihao's avatar
dengyihao 已提交
1420
  }
dengyihao's avatar
dengyihao 已提交
1421
  if (iterValueIsStale(pCur->iter)) {
dengyihao's avatar
dengyihao 已提交
1422 1423 1424
    streamStateFreeCur(pCur);
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
1425 1426 1427 1428 1429 1430 1431
  size_t           klen;
  const char*      iKey = rocksdb_iter_key(pCur->iter, &klen);
  SStateSessionKey curKey = {0};
  stateSessionKeyDecode(&curKey, (char*)iKey);
  if (stateSessionKeyCmpr(&sKey, sizeof(sKey), &curKey, sizeof(curKey)) <= 0) return pCur;

  rocksdb_iter_next(pCur->iter);
dengyihao's avatar
dengyihao 已提交
1432 1433 1434 1435
  if (!rocksdb_iter_valid(pCur->iter)) {
    streamStateFreeCur(pCur);
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
1436 1437
  return pCur;
}
dengyihao's avatar
dengyihao 已提交
1438

dengyihao's avatar
dengyihao 已提交
1439
SStreamStateCur* streamStateSessionSeekKeyNext_rocksdb(SStreamState* pState, const SSessionKey* key) {
dengyihao's avatar
dengyihao 已提交
1440
  qDebug("streamStateSessionSeekKeyNext_rocksdb");
dengyihao's avatar
dengyihao 已提交
1441 1442 1443 1444
  SStreamStateCur* pCur = taosMemoryCalloc(1, sizeof(SStreamStateCur));
  if (pCur == NULL) {
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
1445 1446
  pCur->db = pState->pTdbState->rocksdb;
  pCur->iter = streamStateIterCreate(pState, "sess", &pCur->snapshot, &pCur->readOpt);
dengyihao's avatar
dengyihao 已提交
1447
  pCur->number = pState->number;
dengyihao's avatar
dengyihao 已提交
1448

dengyihao's avatar
dengyihao 已提交
1449
  SStateSessionKey sKey = {.key = *key, .opNum = pState->number};
dengyihao's avatar
dengyihao 已提交
1450

dengyihao's avatar
dengyihao 已提交
1451
  char buf[128] = {0};
dengyihao's avatar
dengyihao 已提交
1452
  int  len = stateSessionKeyEncode(&sKey, buf);
dengyihao's avatar
dengyihao 已提交
1453
  if (!streamStateIterSeekAndValid(pCur->iter, buf, len)) {
dengyihao's avatar
dengyihao 已提交
1454 1455
    streamStateFreeCur(pCur);
    return NULL;
dengyihao's avatar
dengyihao 已提交
1456
  }
dengyihao's avatar
dengyihao 已提交
1457 1458 1459 1460 1461
  while (rocksdb_iter_valid(pCur->iter) && iterValueIsStale(pCur->iter)) rocksdb_iter_next(pCur->iter);
  if (!rocksdb_iter_valid(pCur->iter)) {
    streamStateFreeCur(pCur);
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
1462 1463 1464 1465 1466 1467 1468
  size_t           klen;
  const char*      iKey = rocksdb_iter_key(pCur->iter, &klen);
  SStateSessionKey curKey = {0};
  stateSessionKeyDecode(&curKey, (char*)iKey);
  if (stateSessionKeyCmpr(&sKey, sizeof(sKey), &curKey, sizeof(curKey)) < 0) return pCur;

  rocksdb_iter_next(pCur->iter);
dengyihao's avatar
dengyihao 已提交
1469 1470 1471 1472
  if (!rocksdb_iter_valid(pCur->iter)) {
    streamStateFreeCur(pCur);
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
1473 1474
  return pCur;
}
dengyihao's avatar
dengyihao 已提交
1475
int32_t streamStateSessionGetKVByCur_rocksdb(SStreamStateCur* pCur, SSessionKey* pKey, void** pVal, int32_t* pVLen) {
dengyihao's avatar
dengyihao 已提交
1476
  qDebug("streamStateSessionGetKVByCur_rocksdb");
dengyihao's avatar
dengyihao 已提交
1477 1478 1479
  if (!pCur) {
    return -1;
  }
dengyihao's avatar
dengyihao 已提交
1480
  SStateSessionKey ktmp = {0};
dengyihao's avatar
dengyihao 已提交
1481
  size_t           kLen = 0, vLen = 0;
dengyihao's avatar
dengyihao 已提交
1482

dengyihao's avatar
dengyihao 已提交
1483
  if (!rocksdb_iter_valid(pCur->iter) || iterValueIsStale(pCur->iter)) {
dengyihao's avatar
dengyihao 已提交
1484 1485 1486 1487 1488
    return -1;
  }
  const char* curKey = rocksdb_iter_key(pCur->iter, (size_t*)&kLen);
  stateSessionKeyDecode((void*)&ktmp, (char*)curKey);

dengyihao's avatar
dengyihao 已提交
1489
  SStateSessionKey* pKTmp = &ktmp;
dengyihao's avatar
dengyihao 已提交
1490 1491 1492 1493 1494
  const char*       vval = rocksdb_iter_value(pCur->iter, (size_t*)&vLen);
  char*             val = NULL;
  int32_t           len = decodeValueFunc((void*)vval, vLen, NULL, &val);
  if (len < 0) {
    return -1;
dengyihao's avatar
dengyihao 已提交
1495
  }
dengyihao's avatar
dengyihao 已提交
1496 1497 1498 1499 1500
  if (pVal != NULL) {
    *pVal = (char*)val;
  } else {
    taosMemoryFree(val);
  }
dengyihao's avatar
dengyihao 已提交
1501
  if (pVLen != NULL) *pVLen = len;
dengyihao's avatar
dengyihao 已提交
1502 1503 1504 1505 1506 1507 1508 1509 1510 1511

  if (pKTmp->opNum != pCur->number) {
    return -1;
  }
  if (pKey->groupId != 0 && pKey->groupId != pKTmp->key.groupId) {
    return -1;
  }
  *pKey = pKTmp->key;
  return 0;
}
dengyihao's avatar
dengyihao 已提交
1512 1513 1514
// fill cf
int32_t streamStateFillPut_rocksdb(SStreamState* pState, const SWinKey* key, const void* value, int32_t vLen) {
  int code = 0;
dengyihao's avatar
dengyihao 已提交
1515

dengyihao's avatar
dengyihao 已提交
1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
  STREAM_STATE_PUT_ROCKSDB(pState, "fill", key, value, vLen);
  return code;
}

int32_t streamStateFillGet_rocksdb(SStreamState* pState, const SWinKey* key, void** pVal, int32_t* pVLen) {
  int code = 0;
  STREAM_STATE_GET_ROCKSDB(pState, "fill", key, pVal, pVLen);
  return code;
}
int32_t streamStateFillDel_rocksdb(SStreamState* pState, const SWinKey* key) {
  int code = 0;
  STREAM_STATE_DEL_ROCKSDB(pState, "fill", key);
  return code;
}
dengyihao's avatar
dengyihao 已提交
1530

dengyihao's avatar
dengyihao 已提交
1531 1532
SStreamStateCur* streamStateFillGetCur_rocksdb(SStreamState* pState, const SWinKey* key) {
  qDebug("streamStateFillGetCur_rocksdb");
dengyihao's avatar
dengyihao 已提交
1533
  SStreamStateCur* pCur = taosMemoryCalloc(1, sizeof(SStreamStateCur));
dengyihao's avatar
dengyihao 已提交
1534 1535 1536

  if (pCur == NULL) return NULL;

dengyihao's avatar
dengyihao 已提交
1537
  pCur->db = pState->pTdbState->rocksdb;
dengyihao's avatar
dengyihao 已提交
1538
  pCur->iter = streamStateIterCreate(pState, "fill", &pCur->snapshot, &pCur->readOpt);
dengyihao's avatar
dengyihao 已提交
1539

dengyihao's avatar
dengyihao 已提交
1540 1541
  char buf[128] = {0};
  int  len = winKeyEncode((void*)key, buf);
dengyihao's avatar
dengyihao 已提交
1542 1543 1544
  if (!streamStateIterSeekAndValid(pCur->iter, buf, len)) {
    streamStateFreeCur(pCur);
    return NULL;
dengyihao's avatar
dengyihao 已提交
1545
  }
dengyihao's avatar
dengyihao 已提交
1546 1547 1548
  if (iterValueIsStale(pCur->iter)) {
    streamStateFreeCur(pCur);
    return NULL;
dengyihao's avatar
dengyihao 已提交
1549
  }
dengyihao's avatar
dengyihao 已提交
1550

dengyihao's avatar
dengyihao 已提交
1551
  if (rocksdb_iter_valid(pCur->iter)) {
dengyihao's avatar
dengyihao 已提交
1552 1553 1554 1555 1556
    size_t  kLen;
    SWinKey curKey;
    char*   keyStr = (char*)rocksdb_iter_key(pCur->iter, &kLen);
    winKeyDecode((void*)&curKey, keyStr);
    if (winKeyCmpr(key, sizeof(*key), &curKey, sizeof(curKey)) == 0) {
dengyihao's avatar
dengyihao 已提交
1557 1558 1559
      return pCur;
    }
  }
dengyihao's avatar
dengyihao 已提交
1560

dengyihao's avatar
dengyihao 已提交
1561 1562 1563
  streamStateFreeCur(pCur);
  return NULL;
}
dengyihao's avatar
dengyihao 已提交
1564 1565 1566 1567 1568 1569
int32_t streamStateFillGetKVByCur_rocksdb(SStreamStateCur* pCur, SWinKey* pKey, const void** pVal, int32_t* pVLen) {
  qDebug("streamStateFillGetKVByCur_rocksdb");
  if (!pCur) {
    return -1;
  }
  SWinKey winKey;
dengyihao's avatar
dengyihao 已提交
1570 1571 1572
  if (!rocksdb_iter_valid(pCur->iter) || iterValueIsStale(pCur->iter)) {
    return -1;
  }
dengyihao's avatar
dengyihao 已提交
1573 1574
  size_t klen, vlen;
  char*  keyStr = (char*)rocksdb_iter_key(pCur->iter, &klen);
dengyihao's avatar
dengyihao 已提交
1575
  winKeyDecode(&winKey, keyStr);
dengyihao's avatar
dengyihao 已提交
1576

dengyihao's avatar
dengyihao 已提交
1577
  const char* valStr = rocksdb_iter_value(pCur->iter, &vlen);
dengyihao's avatar
dengyihao 已提交
1578 1579
  // char*       dst = NULL;
  int32_t len = decodeValueFunc((void*)valStr, vlen, NULL, (char**)pVal);
dengyihao's avatar
dengyihao 已提交
1580
  if (len < 0) {
dengyihao's avatar
dengyihao 已提交
1581 1582
    return -1;
  }
dengyihao's avatar
dengyihao 已提交
1583
  if (pVLen != NULL) *pVLen = len;
dengyihao's avatar
dengyihao 已提交
1584

dengyihao's avatar
dengyihao 已提交
1585 1586 1587 1588
  *pKey = winKey;
  return 0;
}

dengyihao's avatar
dengyihao 已提交
1589
SStreamStateCur* streamStateFillSeekKeyNext_rocksdb(SStreamState* pState, const SWinKey* key) {
dengyihao's avatar
dengyihao 已提交
1590
  qDebug("streamStateFillSeekKeyNext_rocksdb");
dengyihao's avatar
dengyihao 已提交
1591 1592 1593 1594
  SStreamStateCur* pCur = taosMemoryCalloc(1, sizeof(SStreamStateCur));
  if (!pCur) {
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
1595

dengyihao's avatar
dengyihao 已提交
1596 1597
  pCur->db = pState->pTdbState->rocksdb;
  pCur->iter = streamStateIterCreate(pState, "fill", &pCur->snapshot, &pCur->readOpt);
dengyihao's avatar
dengyihao 已提交
1598

dengyihao's avatar
dengyihao 已提交
1599
  char buf[128] = {0};
dengyihao's avatar
dengyihao 已提交
1600
  int  len = winKeyEncode((void*)key, buf);
dengyihao's avatar
dengyihao 已提交
1601 1602 1603
  if (!streamStateIterSeekAndValid(pCur->iter, buf, len)) {
    streamStateFreeCur(pCur);
    return NULL;
dengyihao's avatar
dengyihao 已提交
1604
  }
dengyihao's avatar
dengyihao 已提交
1605 1606 1607 1608
  // skip stale data
  while (rocksdb_iter_valid(pCur->iter) && iterValueIsStale(pCur->iter)) {
    rocksdb_iter_next(pCur->iter);
  }
dengyihao's avatar
dengyihao 已提交
1609

dengyihao's avatar
dengyihao 已提交
1610
  if (rocksdb_iter_valid(pCur->iter)) {
dengyihao's avatar
dengyihao 已提交
1611 1612 1613 1614 1615 1616 1617 1618
    SWinKey curKey;
    size_t  kLen = 0;
    char*   keyStr = (char*)rocksdb_iter_key(pCur->iter, &kLen);
    winKeyDecode((void*)&curKey, keyStr);
    if (winKeyCmpr(key, sizeof(*key), &curKey, sizeof(curKey)) > 0) {
      return pCur;
    }
    rocksdb_iter_next(pCur->iter);
dengyihao's avatar
dengyihao 已提交
1619
    return pCur;
dengyihao's avatar
dengyihao 已提交
1620 1621 1622 1623 1624
  }
  streamStateFreeCur(pCur);
  return NULL;
}
SStreamStateCur* streamStateFillSeekKeyPrev_rocksdb(SStreamState* pState, const SWinKey* key) {
dengyihao's avatar
dengyihao 已提交
1625
  qDebug("streamStateFillSeekKeyPrev_rocksdb");
dengyihao's avatar
dengyihao 已提交
1626 1627 1628 1629
  SStreamStateCur* pCur = taosMemoryCalloc(1, sizeof(SStreamStateCur));
  if (pCur == NULL) {
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
1630

dengyihao's avatar
dengyihao 已提交
1631 1632
  pCur->db = pState->pTdbState->rocksdb;
  pCur->iter = streamStateIterCreate(pState, "fill", &pCur->snapshot, &pCur->readOpt);
dengyihao's avatar
dengyihao 已提交
1633

dengyihao's avatar
dengyihao 已提交
1634
  char buf[128] = {0};
dengyihao's avatar
dengyihao 已提交
1635
  int  len = winKeyEncode((void*)key, buf);
dengyihao's avatar
dengyihao 已提交
1636 1637 1638
  if (!streamStateIterSeekAndValid(pCur->iter, buf, len)) {
    streamStateFreeCur(pCur);
    return NULL;
dengyihao's avatar
dengyihao 已提交
1639
  }
dengyihao's avatar
dengyihao 已提交
1640 1641 1642
  while (rocksdb_iter_valid(pCur->iter) && iterValueIsStale(pCur->iter)) {
    rocksdb_iter_prev(pCur->iter);
  }
dengyihao's avatar
dengyihao 已提交
1643

dengyihao's avatar
dengyihao 已提交
1644
  if (rocksdb_iter_valid(pCur->iter)) {
dengyihao's avatar
dengyihao 已提交
1645 1646 1647 1648 1649 1650 1651 1652
    SWinKey curKey;
    size_t  kLen = 0;
    char*   keyStr = (char*)rocksdb_iter_key(pCur->iter, &kLen);
    winKeyDecode((void*)&curKey, keyStr);
    if (winKeyCmpr(key, sizeof(*key), &curKey, sizeof(curKey)) < 0) {
      return pCur;
    }
    rocksdb_iter_prev(pCur->iter);
dengyihao's avatar
dengyihao 已提交
1653
    return pCur;
dengyihao's avatar
dengyihao 已提交
1654
  }
dengyihao's avatar
dengyihao 已提交
1655

dengyihao's avatar
dengyihao 已提交
1656 1657 1658
  streamStateFreeCur(pCur);
  return NULL;
}
dengyihao's avatar
dengyihao 已提交
1659
int32_t streamStateSessionGetKeyByRange_rocksdb(SStreamState* pState, const SSessionKey* key, SSessionKey* curKey) {
dengyihao's avatar
dengyihao 已提交
1660
  qDebug("streamStateSessionGetKeyByRange_rocksdb");
dengyihao's avatar
dengyihao 已提交
1661 1662 1663 1664 1665
  SStreamStateCur* pCur = taosMemoryCalloc(1, sizeof(SStreamStateCur));
  if (pCur == NULL) {
    return -1;
  }
  pCur->number = pState->number;
dengyihao's avatar
dengyihao 已提交
1666 1667
  pCur->db = pState->pTdbState->rocksdb;
  pCur->iter = streamStateIterCreate(pState, "sess", &pCur->snapshot, &pCur->readOpt);
dengyihao's avatar
dengyihao 已提交
1668 1669 1670

  SStateSessionKey sKey = {.key = *key, .opNum = pState->number};
  int32_t          c = 0;
dengyihao's avatar
dengyihao 已提交
1671
  char             buf[128] = {0};
dengyihao's avatar
dengyihao 已提交
1672
  int              len = stateSessionKeyEncode(&sKey, buf);
dengyihao's avatar
dengyihao 已提交
1673 1674 1675
  if (!streamStateIterSeekAndValid(pCur->iter, buf, len)) {
    streamStateFreeCur(pCur);
    return -1;
dengyihao's avatar
dengyihao 已提交
1676 1677
  }

dengyihao's avatar
dengyihao 已提交
1678
  size_t           kLen;
dengyihao's avatar
dengyihao 已提交
1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716
  const char*      iKeyStr = rocksdb_iter_key(pCur->iter, (size_t*)&kLen);
  SStateSessionKey iKey = {0};
  stateSessionKeyDecode(&iKey, (char*)iKeyStr);

  c = stateSessionKeyCmpr(&sKey, sizeof(sKey), &iKey, sizeof(iKey));

  SSessionKey resKey = *key;
  int32_t     code = streamStateSessionGetKVByCur_rocksdb(pCur, &resKey, NULL, 0);
  if (code == 0 && sessionRangeKeyCmpr(key, &resKey) == 0) {
    *curKey = resKey;
    streamStateFreeCur(pCur);
    return code;
  }

  if (c > 0) {
    streamStateCurNext_rocksdb(pState, pCur);
    code = streamStateSessionGetKVByCur_rocksdb(pCur, &resKey, NULL, 0);
    if (code == 0 && sessionRangeKeyCmpr(key, &resKey) == 0) {
      *curKey = resKey;
      streamStateFreeCur(pCur);
      return code;
    }
  } else if (c < 0) {
    streamStateCurPrev(pState, pCur);
    code = streamStateSessionGetKVByCur_rocksdb(pCur, &resKey, NULL, 0);
    if (code == 0 && sessionRangeKeyCmpr(key, &resKey) == 0) {
      *curKey = resKey;
      streamStateFreeCur(pCur);
      return code;
    }
  }

  streamStateFreeCur(pCur);
  return -1;
}

int32_t streamStateSessionAddIfNotExist_rocksdb(SStreamState* pState, SSessionKey* key, TSKEY gap, void** pVal,
                                                int32_t* pVLen) {
dengyihao's avatar
dengyihao 已提交
1717
  qDebug("streamStateSessionAddIfNotExist_rocksdb");
dengyihao's avatar
dengyihao 已提交
1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728
  // todo refactor
  int32_t     res = 0;
  SSessionKey originKey = *key;
  SSessionKey searchKey = *key;
  searchKey.win.skey = key->win.skey - gap;
  searchKey.win.ekey = key->win.ekey + gap;
  int32_t valSize = *pVLen;

  void* tmp = taosMemoryMalloc(valSize);

  SStreamStateCur* pCur = streamStateSessionSeekKeyCurrentPrev_rocksdb(pState, key);
dengyihao's avatar
dengyihao 已提交
1729 1730 1731 1732
  if (pCur == NULL) {
  }
  int32_t code = streamStateSessionGetKVByCur_rocksdb(pCur, key, pVal, pVLen);

dengyihao's avatar
dengyihao 已提交
1733 1734 1735
  if (code == 0) {
    if (sessionRangeKeyCmpr(&searchKey, key) == 0) {
      memcpy(tmp, *pVal, valSize);
dengyihao's avatar
dengyihao 已提交
1736
      taosMemoryFreeClear(*pVal);
dengyihao's avatar
dengyihao 已提交
1737 1738 1739
      streamStateSessionDel_rocksdb(pState, key);
      goto _end;
    }
dengyihao's avatar
dengyihao 已提交
1740
    taosMemoryFreeClear(*pVal);
dengyihao's avatar
dengyihao 已提交
1741 1742 1743 1744
    streamStateCurNext_rocksdb(pState, pCur);
  } else {
    *key = originKey;
    streamStateFreeCur(pCur);
dengyihao's avatar
dengyihao 已提交
1745
    taosMemoryFreeClear(*pVal);
dengyihao's avatar
dengyihao 已提交
1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762
    pCur = streamStateSessionSeekKeyNext_rocksdb(pState, key);
  }

  code = streamStateSessionGetKVByCur_rocksdb(pCur, key, pVal, pVLen);
  if (code == 0) {
    if (sessionRangeKeyCmpr(&searchKey, key) == 0) {
      memcpy(tmp, *pVal, valSize);
      streamStateSessionDel_rocksdb(pState, key);
      goto _end;
    }
  }

  *key = originKey;
  res = 1;
  memset(tmp, 0, valSize);

_end:
dengyihao's avatar
dengyihao 已提交
1763
  taosMemoryFree(*pVal);
dengyihao's avatar
dengyihao 已提交
1764 1765 1766 1767
  *pVal = tmp;
  streamStateFreeCur(pCur);
  return res;
}
dengyihao's avatar
dengyihao 已提交
1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779
int32_t streamStateSessionClear_rocksdb(SStreamState* pState) {
  qDebug("streamStateSessionClear_rocksdb");
  SSessionKey      key = {.win.skey = 0, .win.ekey = 0, .groupId = 0};
  SStreamStateCur* pCur = streamStateSessionSeekKeyCurrentNext_rocksdb(pState, &key);

  while (1) {
    SSessionKey delKey = {0};
    void*       buf = NULL;
    int32_t     size = 0;
    int32_t     code = streamStateSessionGetKVByCur_rocksdb(pCur, &delKey, &buf, &size);
    if (code == 0 && size > 0) {
      memset(buf, 0, size);
dengyihao's avatar
dengyihao 已提交
1780
      // refactor later
dengyihao's avatar
dengyihao 已提交
1781 1782
      streamStateSessionPut_rocksdb(pState, &delKey, buf, size);
    } else {
dengyihao's avatar
dengyihao 已提交
1783
      taosMemoryFreeClear(buf);
dengyihao's avatar
dengyihao 已提交
1784 1785
      break;
    }
dengyihao's avatar
dengyihao 已提交
1786 1787
    taosMemoryFreeClear(buf);

dengyihao's avatar
dengyihao 已提交
1788 1789 1790 1791 1792
    streamStateCurNext_rocksdb(pState, pCur);
  }
  streamStateFreeCur(pCur);
  return -1;
}
dengyihao's avatar
dengyihao 已提交
1793 1794
int32_t streamStateStateAddIfNotExist_rocksdb(SStreamState* pState, SSessionKey* key, char* pKeyData,
                                              int32_t keyDataLen, state_key_cmpr_fn fn, void** pVal, int32_t* pVLen) {
dengyihao's avatar
dengyihao 已提交
1795
  qDebug("streamStateStateAddIfNotExist_rocksdb");
dengyihao's avatar
dengyihao 已提交
1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810
  // todo refactor
  int32_t     res = 0;
  SSessionKey tmpKey = *key;
  int32_t     valSize = *pVLen;
  void*       tmp = taosMemoryMalloc(valSize);
  // tdbRealloc(NULL, valSize);
  if (!tmp) {
    return -1;
  }

  SStreamStateCur* pCur = streamStateSessionSeekKeyCurrentPrev_rocksdb(pState, key);
  int32_t          code = streamStateSessionGetKVByCur_rocksdb(pCur, key, pVal, pVLen);
  if (code == 0) {
    if (key->win.skey <= tmpKey.win.skey && tmpKey.win.ekey <= key->win.ekey) {
      memcpy(tmp, *pVal, valSize);
dengyihao's avatar
dengyihao 已提交
1811
      streamStateSessionDel_rocksdb(pState, key);
dengyihao's avatar
dengyihao 已提交
1812 1813 1814 1815 1816 1817
      goto _end;
    }

    void* stateKey = (char*)(*pVal) + (valSize - keyDataLen);
    if (fn(pKeyData, stateKey) == true) {
      memcpy(tmp, *pVal, valSize);
dengyihao's avatar
dengyihao 已提交
1818
      streamStateSessionDel_rocksdb(pState, key);
dengyihao's avatar
dengyihao 已提交
1819 1820 1821 1822 1823 1824 1825 1826 1827
      goto _end;
    }

    streamStateCurNext_rocksdb(pState, pCur);
  } else {
    *key = tmpKey;
    streamStateFreeCur(pCur);
    pCur = streamStateSessionSeekKeyNext_rocksdb(pState, key);
  }
dengyihao's avatar
dengyihao 已提交
1828
  taosMemoryFreeClear(*pVal);
dengyihao's avatar
dengyihao 已提交
1829 1830 1831 1832 1833 1834 1835 1836 1837
  code = streamStateSessionGetKVByCur_rocksdb(pCur, key, pVal, pVLen);
  if (code == 0) {
    void* stateKey = (char*)(*pVal) + (valSize - keyDataLen);
    if (fn(pKeyData, stateKey) == true) {
      memcpy(tmp, *pVal, valSize);
      streamStateSessionDel_rocksdb(pState, key);
      goto _end;
    }
  }
dengyihao's avatar
dengyihao 已提交
1838
  taosMemoryFreeClear(*pVal);
dengyihao's avatar
dengyihao 已提交
1839 1840 1841 1842 1843 1844

  *key = tmpKey;
  res = 1;
  memset(tmp, 0, valSize);

_end:
dengyihao's avatar
dengyihao 已提交
1845
  taosMemoryFreeClear(*pVal);
dengyihao's avatar
dengyihao 已提交
1846 1847 1848 1849
  *pVal = tmp;
  streamStateFreeCur(pCur);
  return res;
}
dengyihao's avatar
dengyihao 已提交
1850

dengyihao's avatar
dengyihao 已提交
1851
//  partag cf
dengyihao's avatar
dengyihao 已提交
1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862
int32_t streamStatePutParTag_rocksdb(SStreamState* pState, int64_t groupId, const void* tag, int32_t tagLen) {
  int code = 0;
  STREAM_STATE_PUT_ROCKSDB(pState, "partag", &groupId, tag, tagLen);
  return code;
}

int32_t streamStateGetParTag_rocksdb(SStreamState* pState, int64_t groupId, void** tagVal, int32_t* tagLen) {
  int code = 0;
  STREAM_STATE_GET_ROCKSDB(pState, "partag", &groupId, tagVal, tagLen);
  return code;
}
dengyihao's avatar
dengyihao 已提交
1863
// parname cfg
dengyihao's avatar
dengyihao 已提交
1864 1865
int32_t streamStatePutParName_rocksdb(SStreamState* pState, int64_t groupId, const char tbname[TSDB_TABLE_NAME_LEN]) {
  int code = 0;
dengyihao's avatar
dengyihao 已提交
1866
  STREAM_STATE_PUT_ROCKSDB(pState, "parname", &groupId, (char*)tbname, TSDB_TABLE_NAME_LEN);
dengyihao's avatar
dengyihao 已提交
1867 1868 1869 1870 1871 1872 1873 1874 1875
  return code;
}
int32_t streamStateGetParName_rocksdb(SStreamState* pState, int64_t groupId, void** pVal) {
  int    code = 0;
  size_t tagLen;
  STREAM_STATE_GET_ROCKSDB(pState, "parname", &groupId, pVal, &tagLen);
  return code;
}

dengyihao's avatar
dengyihao 已提交
1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943
int32_t streamDefaultPut_rocksdb(SStreamState* pState, const void* key, void* pVal, int32_t pVLen) {
  int code = 0;
  STREAM_STATE_PUT_ROCKSDB(pState, "default", &key, pVal, pVLen);
  return code;
}
int32_t streamDefaultGet_rocksdb(SStreamState* pState, const void* key, void** pVal, int32_t* pVLen) {
  int code = 0;
  STREAM_STATE_GET_ROCKSDB(pState, "default", &key, pVal, pVLen);
  return code;
}
int32_t streamDefaultDel_rocksdb(SStreamState* pState, const void* key) {
  int code = 0;
  STREAM_STATE_DEL_ROCKSDB(pState, "default", &key);
  return code;
}

int32_t streamDefaultIterGet_rocksdb(SStreamState* pState, const void* start, const void* end, SArray* result) {
  int   code = 0;
  char* err = NULL;

  rocksdb_snapshot_t*    snapshot = NULL;
  rocksdb_readoptions_t* readopts = NULL;
  rocksdb_iterator_t*    pIter = streamStateIterCreate(pState, "default", &snapshot, &readopts);
  if (pIter == NULL) {
    return -1;
  }

  rocksdb_iter_seek(pIter, start, strlen(start));
  while (rocksdb_iter_valid(pIter)) {
    const char* key = rocksdb_iter_key(pIter, NULL);
    int32_t     vlen = 0;
    const char* vval = rocksdb_iter_value(pIter, (size_t*)&vlen);
    char*       val = NULL;
    int32_t     len = decodeValueFunc((void*)vval, vlen, NULL, NULL);
    if (len < 0) {
      rocksdb_iter_next(pIter);
      continue;
    }

    if (end != NULL && strcmp(key, end) > 0) {
      break;
    }
    if (strncmp(key, start, strlen(start)) == 0 && strlen(key) >= strlen(start) + 1) {
      int64_t checkPoint = 0;
      if (sscanf(key + strlen(key), ":%" PRId64 "", &checkPoint) == 1) {
        taosArrayPush(result, &checkPoint);
      }
    } else {
      break;
    }
    rocksdb_iter_next(pIter);
  }
  rocksdb_release_snapshot(pState->pTdbState->rocksdb, snapshot);
  rocksdb_readoptions_destroy(readopts);
  rocksdb_iter_destroy(pIter);
  return code;
}
void* streamDefaultIterCreate_rocksdb(SStreamState* pState) {
  SStreamStateCur* pCur = taosMemoryCalloc(1, sizeof(SStreamStateCur));

  pCur->db = pState->pTdbState->rocksdb;
  pCur->iter = streamStateIterCreate(pState, "default", &pCur->snapshot, &pCur->readOpt);
  return pCur;
}
int32_t streamDefaultIterValid_rocksdb(void* iter) {
  SStreamStateCur* pCur = iter;
  bool             val = rocksdb_iter_valid(pCur->iter);

dengyihao's avatar
dengyihao 已提交
1944
  return val ? 1 : 0;
dengyihao's avatar
dengyihao 已提交
1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006
}
void streamDefaultIterSeek_rocksdb(void* iter, const char* key) {
  SStreamStateCur* pCur = iter;
  rocksdb_iter_seek(pCur->iter, key, strlen(key));
}
void streamDefaultIterNext_rocksdb(void* iter) {
  SStreamStateCur* pCur = iter;
  rocksdb_iter_next(pCur->iter);
}
char* streamDefaultIterKey_rocksdb(void* iter, int32_t* len) {
  SStreamStateCur* pCur = iter;
  return (char*)rocksdb_iter_key(pCur->iter, (size_t*)len);
}
char* streamDefaultIterVal_rocksdb(void* iter, int32_t* len) {
  SStreamStateCur* pCur = iter;
  int32_t          vlen = 0;
  char*            dst = NULL;
  const char*      vval = rocksdb_iter_value(pCur->iter, (size_t*)&vlen);
  if (decodeValueFunc((void*)vval, vlen, NULL, &dst) < 0) {
    return NULL;
  }
  return dst;
}
// batch func
void* streamStateCreateBatch() {
  rocksdb_writebatch_t* pBatch = rocksdb_writebatch_create();
  return pBatch;
}
int32_t streamStateGetBatchSize(void* pBatch) {
  if (pBatch == NULL) return 0;
  return rocksdb_writebatch_count(pBatch);
}

void    streamStateClearBatch(void* pBatch) { rocksdb_writebatch_clear((rocksdb_writebatch_t*)pBatch); }
void    streamStateDestroyBatch(void* pBatch) { rocksdb_writebatch_destroy((rocksdb_writebatch_t*)pBatch); }
int32_t streamStatePutBatch(SStreamState* pState, const char* cfName, rocksdb_writebatch_t* pBatch, void* key,
                            void* val, int32_t vlen) {
  int i = streamGetInit(cfName);

  if (i < 0) {
    qError("streamState failed to put to cf name:%s", cfName);
    return -1;
  }
  char    buf[128] = {0};
  int32_t klen = ginitDict[i].enFunc((void*)key, buf);

  char*                           ttlV = NULL;
  int32_t                         ttlVLen = ginitDict[i].enValueFunc(val, vlen, 0, &ttlV);
  rocksdb_column_family_handle_t* pCf = pState->pTdbState->pHandle[ginitDict[i].idx];
  rocksdb_writebatch_put_cf((rocksdb_writebatch_t*)pBatch, pCf, buf, (size_t)klen, ttlV, (size_t)ttlVLen);
  taosMemoryFree(ttlV);
  return 0;
}
int32_t streamStatePutBatch_rocksdb(SStreamState* pState, void* pBatch) {
  char* err = NULL;
  rocksdb_write(pState->pTdbState->rocksdb, pState->pTdbState->writeOpts, (rocksdb_writebatch_t*)pBatch, &err);
  if (err != NULL) {
    qError("streamState failed to write batch, err:%s", err);
    taosMemoryFree(err);
    return -1;
  }
  return 0;
L
liuyao 已提交
2007
}