streamStateRocksdb.c 55.0 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/>.
 */

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

int defaultKeyComp(void* state, const char* aBuf, size_t aLen, const char* bBuf, size_t bLen) {
dengyihao's avatar
dengyihao 已提交
21 22 23 24 25 26 27 28 29 30 31
  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 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
}
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 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
//
//  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 已提交
79
  len += taosEncodeFixedI64((void**)&buf, key->opNum);
dengyihao's avatar
dengyihao 已提交
80 81 82 83 84 85 86 87 88 89 90 91
  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 已提交
92 93 94
int stateKeyToString(void* k, char* buf) {
  SStateKey* key = k;
  int        n = 0;
dengyihao's avatar
dengyihao 已提交
95 96 97
  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 已提交
98 99 100
  return n;
}

dengyihao's avatar
dengyihao 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
//
// 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 已提交
151 152 153
int stateSessionKeyToString(void* k, char* buf) {
  SStateSessionKey* key = k;
  int               n = 0;
dengyihao's avatar
dengyihao 已提交
154 155 156 157
  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 已提交
158 159
  return n;
}
dengyihao's avatar
dengyihao 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198

/**
 *  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 已提交
199 200 201 202

int winKeyToString(void* k, char* buf) {
  SWinKey* key = k;
  int      n = 0;
dengyihao's avatar
dengyihao 已提交
203 204
  n += sprintf(buf + n, "[groupId:%" PRIu64 ",", key->groupId);
  n += sprintf(buf + n, "ts:%" PRIi64 "]", key->ts);
dengyihao's avatar
dengyihao 已提交
205 206
  return n;
}
dengyihao's avatar
dengyihao 已提交
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
/*
 * 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 已提交
249 250 251
int tupleKeyToString(void* k, char* buf) {
  int        n = 0;
  STupleKey* key = k;
dengyihao's avatar
dengyihao 已提交
252 253 254
  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 已提交
255 256
  return n;
}
dengyihao's avatar
dengyihao 已提交
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

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 已提交
285 286 287
int parKeyToString(void* k, char* buf) {
  int64_t* key = k;
  int      n = 0;
dengyihao's avatar
dengyihao 已提交
288
  n = sprintf(buf + n, "[groupId:%" PRIi64 "]", *key);
dengyihao's avatar
dengyihao 已提交
289 290
  return n;
}
dengyihao's avatar
dengyihao 已提交
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
int stremaValueEncode(void* k, char* buf) {
  int          len = 0;
  streamValue* key = k;
  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) {
  streamValue* key = k;
  char*        p = buf;
  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) {
  streamValue* key = k;
  int          n = 0;
  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;
}
/*1: stale, 0: no stale*/

int32_t streaValueIsStale(void* k, int64_t ts) {
  streamValue* key = k;
  if (key->unixTimestamp < ts) {
    return 1;
  }
  return 0;
}
dengyihao's avatar
dengyihao 已提交
324

dengyihao's avatar
dengyihao 已提交
325 326 327 328
typedef struct {
  void* tableOpt;
  void* lru;  // global or not
} rocksdbCfParam;
dengyihao's avatar
dengyihao 已提交
329 330
const char* cfName[] = {"default", "state", "fill", "sess", "func", "parname", "partag"};

dengyihao's avatar
dengyihao 已提交
331 332
typedef int (*EncodeFunc)(void* key, char* buf);
typedef int (*DecodeFunc)(void* key, char* buf);
dengyihao's avatar
dengyihao 已提交
333
typedef int (*ToStringFunc)(void* key, char* buf);
dengyihao's avatar
dengyihao 已提交
334 335 336 337 338 339 340 341 342 343 344
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);

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);
dengyihao's avatar
dengyihao 已提交
345

dengyihao's avatar
dengyihao 已提交
346 347
void destroyFunc(void* stata) { return; }

dengyihao's avatar
dengyihao 已提交
348
typedef struct {
dengyihao's avatar
dengyihao 已提交
349 350 351 352 353 354 355 356 357 358
  const char*    key;
  int32_t        len;
  int            idx;
  BackendCmpFunc cmpFunc;
  EncodeFunc     enFunc;
  DecodeFunc     deFunc;
  ToStringFunc   toStrFunc;
  CompareName    cmpName;
  DestroyFunc    detroyFunc;

dengyihao's avatar
dengyihao 已提交
359 360 361
} SCfInit;

SCfInit ginitDict[] = {
dengyihao's avatar
dengyihao 已提交
362
    {"default", 7, 0, defaultKeyComp, defaultKeyEncode, defaultKeyDecode, defaultKeyToString, compareDefaultName,
dengyihao's avatar
dengyihao 已提交
363
     destroyFunc},
dengyihao's avatar
dengyihao 已提交
364 365 366 367 368 369 370
    {"state", 5, 1, stateKeyDBComp, stateKeyEncode, stateKeyDecode, stateKeyToString, compareStateName, destroyFunc},
    {"fill", 4, 2, winKeyDBComp, winKeyEncode, winKeyDecode, winKeyToString, compareWinKeyName, destroyFunc},
    {"sess", 4, 3, stateSessionKeyDBComp, stateSessionKeyEncode, stateSessionKeyDecode, stateSessionKeyToString,
     compareSessionKeyName, destroyFunc},
    {"func", 4, 4, tupleKeyDBComp, tupleKeyEncode, tupleKeyDecode, tupleKeyToString, compareFuncKeyName, destroyFunc},
    {"parname", 7, 5, parKeyDBComp, parKeyEncode, parKeyDecode, parKeyToString, compareParKeyName, destroyFunc},
    {"partag", 6, 6, parKeyDBComp, parKeyEncode, parKeyDecode, parKeyToString, comparePartagKeyName, destroyFunc},
dengyihao's avatar
dengyihao 已提交
371 372
};

dengyihao's avatar
dengyihao 已提交
373 374 375 376 377 378 379 380
const char* compareDefaultName(void* name) { return ginitDict[0].key; }
const char* compareStateName(void* name) { return ginitDict[1].key; }
const char* compareWinKeyName(void* name) { return ginitDict[2].key; }
const char* compareSessionKeyName(void* name) { return ginitDict[3].key; }
const char* compareFuncKeyName(void* name) { return ginitDict[4].key; }
const char* compareParKeyName(void* name) { return ginitDict[5].key; }
const char* comparePartagKeyName(void* name) { return ginitDict[6].key; }

dengyihao's avatar
dengyihao 已提交
381
int streamInitBackend(SStreamState* pState, char* path) {
dengyihao's avatar
dengyihao 已提交
382
  rocksdb_env_t* env = rocksdb_create_default_env();  // rocksdb_envoptions_create();
dengyihao's avatar
dengyihao 已提交
383 384
  rocksdb_env_set_low_priority_background_threads(env, 4);
  rocksdb_env_set_high_priority_background_threads(env, 2);
dengyihao's avatar
dengyihao 已提交
385

dengyihao's avatar
dengyihao 已提交
386
  rocksdb_options_t* opts = rocksdb_options_create();
dengyihao's avatar
dengyihao 已提交
387 388 389 390
  rocksdb_options_set_env(opts, env);
  // rocksdb_options_increase_parallelism(opts, 8);
  //  rocksdb_options_optimize_level_style_compaction(opts, 0);
  //   create the DB if it's not already present
dengyihao's avatar
dengyihao 已提交
391 392
  rocksdb_options_set_create_if_missing(opts, 1);
  rocksdb_options_set_create_missing_column_families(opts, 1);
dengyihao's avatar
dengyihao 已提交
393
  rocksdb_options_set_write_buffer_size(opts, 128 << 20);
dengyihao's avatar
dengyihao 已提交
394 395
  rocksdb_options_set_recycle_log_file_num(opts, 6);
  rocksdb_options_set_max_write_buffer_number(opts, 3);
dengyihao's avatar
dengyihao 已提交
396 397

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

dengyihao's avatar
dengyihao 已提交
400
  rocksdbCfParam*           param = taosMemoryCalloc(cfLen, sizeof(rocksdbCfParam));
dengyihao's avatar
dengyihao 已提交
401 402 403
  const rocksdb_options_t** cfOpt = taosMemoryCalloc(cfLen, sizeof(rocksdb_options_t*));
  for (int i = 0; i < cfLen; i++) {
    cfOpt[i] = rocksdb_options_create_copy(opts);
dengyihao's avatar
dengyihao 已提交
404
    // refactor later
dengyihao's avatar
dengyihao 已提交
405
    rocksdb_block_based_table_options_t* tableOpt = rocksdb_block_based_options_create();
dengyihao's avatar
dengyihao 已提交
406
    rocksdb_cache_t*                     cache = rocksdb_cache_create_lru(64 << 20);
dengyihao's avatar
dengyihao 已提交
407 408
    rocksdb_block_based_options_set_block_cache(tableOpt, cache);

dengyihao's avatar
dengyihao 已提交
409
    rocksdb_filterpolicy_t* filter = rocksdb_filterpolicy_create_bloom(15);
dengyihao's avatar
dengyihao 已提交
410
    rocksdb_block_based_options_set_filter_policy(tableOpt, filter);
dengyihao's avatar
dengyihao 已提交
411 412

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

dengyihao's avatar
dengyihao 已提交
414 415
    param[i].tableOpt = tableOpt;
    param[i].lru = cache;
dengyihao's avatar
dengyihao 已提交
416 417
    // rocksdb_slicetransform_t* trans = rocksdb_slicetransform_create_fixed_prefix(8);
    // rocksdb_options_set_prefix_extractor((rocksdb_options_t*)cfOpt[i], trans);
dengyihao's avatar
dengyihao 已提交
418
  };
dengyihao's avatar
dengyihao 已提交
419

dengyihao's avatar
dengyihao 已提交
420
  rocksdb_comparator_t** pCompare = taosMemoryCalloc(cfLen, sizeof(rocksdb_comparator_t**));
dengyihao's avatar
dengyihao 已提交
421 422 423 424 425 426
  for (int i = 0; i < cfLen; i++) {
    SCfInit*              cf = &ginitDict[i];
    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 已提交
427 428

  rocksdb_column_family_handle_t** cfHandle = taosMemoryMalloc(cfLen * sizeof(rocksdb_column_family_handle_t*));
dengyihao's avatar
dengyihao 已提交
429
  rocksdb_t*                       db = rocksdb_open_column_families(opts, path, cfLen, cfName, cfOpt, cfHandle, &err);
dengyihao's avatar
dengyihao 已提交
430 431 432

  pState->pTdbState->rocksdb = db;
  pState->pTdbState->pHandle = cfHandle;
dengyihao's avatar
dengyihao 已提交
433
  pState->pTdbState->writeOpts = rocksdb_writeoptions_create();
dengyihao's avatar
dengyihao 已提交
434
  // rocksdb_writeoptions_
dengyihao's avatar
dengyihao 已提交
435
  // rocksdb_writeoptions_set_no_slowdown(pState->pTdbState->writeOpts, 1);
dengyihao's avatar
dengyihao 已提交
436 437 438 439
  pState->pTdbState->readOpts = rocksdb_readoptions_create();
  pState->pTdbState->cfOpts = (rocksdb_options_t**)cfOpt;
  pState->pTdbState->pCompare = pCompare;
  pState->pTdbState->dbOpt = opts;
dengyihao's avatar
dengyihao 已提交
440 441
  pState->pTdbState->param = param;
  pState->pTdbState->env = env;
dengyihao's avatar
dengyihao 已提交
442 443 444
  return 0;
}
void streamCleanBackend(SStreamState* pState) {
dengyihao's avatar
dengyihao 已提交
445 446 447 448
  if (pState->pTdbState->rocksdb == NULL) {
    qInfo("rocksdb already free");
    return;
  }
dengyihao's avatar
dengyihao 已提交
449

dengyihao's avatar
dengyihao 已提交
450 451
  int             cfLen = sizeof(ginitDict) / sizeof(ginitDict[0]);
  rocksdbCfParam* param = pState->pTdbState->param;
dengyihao's avatar
dengyihao 已提交
452 453 454
  for (int i = 0; i < cfLen; i++) {
    rocksdb_column_family_handle_destroy(pState->pTdbState->pHandle[i]);
  }
dengyihao's avatar
dengyihao 已提交
455
  taosMemoryFreeClear(pState->pTdbState->pHandle);
dengyihao's avatar
dengyihao 已提交
456 457

  rocksdb_options_destroy(pState->pTdbState->dbOpt);
dengyihao's avatar
dengyihao 已提交
458 459 460 461 462 463 464

  rocksdb_writeoptions_destroy(pState->pTdbState->writeOpts);
  pState->pTdbState->writeOpts = NULL;

  rocksdb_readoptions_destroy(pState->pTdbState->readOpts);
  pState->pTdbState->readOpts = NULL;

dengyihao's avatar
dengyihao 已提交
465
  rocksdb_close(pState->pTdbState->rocksdb);
dengyihao's avatar
dengyihao 已提交
466
  // wait for all background work to finish
dengyihao's avatar
dengyihao 已提交
467 468 469 470 471 472 473 474 475 476
  for (int i = 0; i < cfLen; i++) {
    rocksdb_options_destroy(pState->pTdbState->cfOpts[i]);
    rocksdb_comparator_destroy(pState->pTdbState->pCompare[i]);

    rocksdb_cache_destroy(param[i].lru);
    rocksdb_block_based_options_destroy(param[i].tableOpt);
  }
  taosMemoryFreeClear(pState->pTdbState->cfOpts);
  taosMemoryFree(pState->pTdbState->pCompare);
  taosMemoryFree(pState->pTdbState->param);
dengyihao's avatar
dengyihao 已提交
477
  rocksdb_env_destroy(pState->pTdbState->env);
dengyihao's avatar
dengyihao 已提交
478

dengyihao's avatar
dengyihao 已提交
479
  pState->pTdbState->rocksdb = NULL;
dengyihao's avatar
dengyihao 已提交
480 481 482
}

int streamGetInit(const char* funcName) {
dengyihao's avatar
dengyihao 已提交
483
  size_t len = strlen(funcName);
dengyihao's avatar
dengyihao 已提交
484
  for (int i = 0; i < sizeof(ginitDict) / sizeof(ginitDict[0]); i++) {
dengyihao's avatar
dengyihao 已提交
485
    if (len == ginitDict[i].len && strncmp(funcName, ginitDict[i].key, strlen(funcName)) == 0) {
dengyihao's avatar
dengyihao 已提交
486 487 488 489 490
      return i;
    }
  }
  return -1;
}
dengyihao's avatar
dengyihao 已提交
491
bool streamStateIterSeekAndValid(rocksdb_iterator_t* iter, char* buf, size_t len) {
dengyihao's avatar
dengyihao 已提交
492
  rocksdb_iter_seek(iter, buf, len);
dengyihao's avatar
dengyihao 已提交
493
  if (!rocksdb_iter_valid(iter)) {
dengyihao's avatar
dengyihao 已提交
494 495 496 497
    rocksdb_iter_seek_for_prev(iter, buf, len);
    if (!rocksdb_iter_valid(iter)) {
      return false;
    }
dengyihao's avatar
dengyihao 已提交
498
  }
dengyihao's avatar
dengyihao 已提交
499
  return true;
dengyihao's avatar
dengyihao 已提交
500
}
dengyihao's avatar
dengyihao 已提交
501 502
rocksdb_iterator_t* streamStateIterCreate(SStreamState* pState, const char* cfName, rocksdb_snapshot_t** snapshot,
                                          rocksdb_readoptions_t** readOpt) {
dengyihao's avatar
dengyihao 已提交
503
  int idx = streamGetInit(cfName);
dengyihao's avatar
dengyihao 已提交
504

dengyihao's avatar
dengyihao 已提交
505
  if (snapshot != NULL) {
dengyihao's avatar
dengyihao 已提交
506
    *snapshot = (rocksdb_snapshot_t*)rocksdb_create_snapshot(pState->pTdbState->rocksdb);
dengyihao's avatar
dengyihao 已提交
507
  }
dengyihao's avatar
dengyihao 已提交
508

dengyihao's avatar
dengyihao 已提交
509 510
  rocksdb_readoptions_t* rOpt = rocksdb_readoptions_create();
  *readOpt = rOpt;
dengyihao's avatar
dengyihao 已提交
511

dengyihao's avatar
dengyihao 已提交
512 513
  // rocksdb_readoptions_set_snapshot(rOpt, *snapshot);

dengyihao's avatar
dengyihao 已提交
514 515 516
  rocksdb_readoptions_set_fill_cache(rOpt, 0);

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

dengyihao's avatar
dengyihao 已提交
519 520 521 522 523 524 525 526
#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);                                        \
dengyihao's avatar
dengyihao 已提交
527 528
      code = -1;                                                                                       \
      break;                                                                                           \
dengyihao's avatar
dengyihao 已提交
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
    }                                                                                                  \
    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_put_cf(db, opts, pHandle, (const char*)buf, klen, (const char*)value, (size_t)vLen, &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);           \
    }                                                                                                  \
dengyihao's avatar
dengyihao 已提交
544 545
  } while (0);

dengyihao's avatar
dengyihao 已提交
546 547 548 549 550 551 552 553
#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);                                 \
dengyihao's avatar
dengyihao 已提交
554 555
      code = -1;                                                                                \
      break;                                                                                    \
dengyihao's avatar
dengyihao 已提交
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
    }                                                                                           \
    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) {                                                                          \
      qDebug("streamState str: %s failed to read from %s, err: not exist", toString, funcname); \
      if (err != NULL) taosMemoryFree(err);                                                     \
      code = -1;                                                                                \
    } else {                                                                                    \
      if (pVal != NULL) *pVal = val;                                                            \
      if (vLen != NULL) *vLen = len;                                                            \
    }                                                                                           \
    if (err != NULL) {                                                                          \
      taosMemoryFree(err);                                                                      \
      qDebug("streamState str: %s failed to read from %s, err: %s", toString, funcname, err);   \
      code = -1;                                                                                \
    } else {                                                                                    \
      if (code == 0) qDebug("streamState str: %s succ to read from %s", toString, funcname);    \
    }                                                                                           \
dengyihao's avatar
dengyihao 已提交
580 581
  } while (0);

dengyihao's avatar
dengyihao 已提交
582 583 584
#define STREAM_STATE_DEL_ROCKSDB(pState, funcname, key)                                      \
  do {                                                                                       \
    code = 0;                                                                                \
dengyihao's avatar
dengyihao 已提交
585
    char  buf[128] = {0};                                                                    \
dengyihao's avatar
dengyihao 已提交
586 587 588 589
    char* err = NULL;                                                                        \
    int   i = streamGetInit(funcname);                                                       \
    if (i < 0) {                                                                             \
      qWarn("streamState failed to get cf name: %s", funcname);                              \
dengyihao's avatar
dengyihao 已提交
590 591
      code = -1;                                                                             \
      break;                                                                                 \
dengyihao's avatar
dengyihao 已提交
592
    }                                                                                        \
dengyihao's avatar
dengyihao 已提交
593
    char toString[128] = {0};                                                                \
dengyihao's avatar
dengyihao 已提交
594
    if (qDebugFlag & DEBUG_TRACE) ginitDict[i].toStrFunc((void*)key, toString);              \
dengyihao's avatar
dengyihao 已提交
595
    int32_t                         klen = ginitDict[i].enFunc((void*)key, buf);             \
dengyihao's avatar
dengyihao 已提交
596 597 598
    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;                     \
dengyihao's avatar
dengyihao 已提交
599
    rocksdb_delete_cf(db, opts, pHandle, (const char*)buf, klen, &err);                      \
dengyihao's avatar
dengyihao 已提交
600 601 602 603 604 605 606
    if (err != NULL) {                                                                       \
      qDebug("streamState str: %s failed to del from %s, err: %s", toString, funcname, err); \
      taosMemoryFree(err);                                                                   \
      code = -1;                                                                             \
    } else {                                                                                 \
      qDebug("streamState str: %s succ to del from %s", toString, funcname);                 \
    }                                                                                        \
dengyihao's avatar
dengyihao 已提交
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
  } while (0);

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, 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);
  return 0;
}

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 已提交
629
  STREAM_STATE_PUT_ROCKSDB(pState, "state", &sKey, value, vLen);
dengyihao's avatar
dengyihao 已提交
630 631
  return code;
}
dengyihao's avatar
dengyihao 已提交
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
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;
}

void* streamStateCreateBatch() {
  rocksdb_writebatch_t* pBatch = rocksdb_writebatch_create();
  return pBatch;
}
int32_t streamStateGetBatchSize(void* pBatch) {
  if (pBatch == NULL) return -1;

  return rocksdb_writebatch_count((rocksdb_writebatch_t*)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);

  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, val, (size_t)vlen);
  return 0;
}
dengyihao's avatar
dengyihao 已提交
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686

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;
}

dengyihao's avatar
dengyihao 已提交
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 712 713 714 715
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);

  return val ? 0 : -1;
}
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;
  return (char*)rocksdb_iter_value(pCur->iter, (size_t*)len);
}
dengyihao's avatar
dengyihao 已提交
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
// typedef struct {
//   char* start;
//   char* end;
//   void* result;
// } StreamFilterArg;

// typedef int (*streamfilterFunc)(StreamFilterArg* arg);

// int32_t streamDefaultIterFilter_rocksdb(SStreamState* pState, streamfilterFunc filterFunc, StreamFilterArg* arg) {
//   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;
//   }
//   char*   start = arg->start;
//   char*   end = arg->end;
//   SArray* result = arg->result;

//   rocksdb_iter_seek(pIter, start, strlen(start));
//   while (rocksdb_iter_valid(pIter)) {
//     const char* key = rocksdb_iter_key(pIter, NULL);
//     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;
// }
dengyihao's avatar
dengyihao 已提交
759 760 761
int32_t streamDefaultIterGet_rocksdb(SStreamState* pState, const void* start, const void* end, SArray* result) {
  int   code = 0;
  char* err = NULL;
dengyihao's avatar
dengyihao 已提交
762

dengyihao's avatar
dengyihao 已提交
763 764 765 766 767 768
  rocksdb_snapshot_t*    snapshot = NULL;
  rocksdb_readoptions_t* readopts = NULL;
  rocksdb_iterator_t*    pIter = streamStateIterCreate(pState, "default", &snapshot, &readopts);
  if (pIter == NULL) {
    return -1;
  }
dengyihao's avatar
dengyihao 已提交
769

dengyihao's avatar
dengyihao 已提交
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
  rocksdb_iter_seek(pIter, start, strlen(start));
  while (rocksdb_iter_valid(pIter)) {
    const char* key = rocksdb_iter_key(pIter, NULL);
    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;
dengyihao's avatar
dengyihao 已提交
790 791
}

dengyihao's avatar
dengyihao 已提交
792 793 794
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 已提交
795
  STREAM_STATE_GET_ROCKSDB(pState, "state", &sKey, pVal, pVLen);
dengyihao's avatar
dengyihao 已提交
796 797 798 799 800 801
  return code;
}
// todo refactor
int32_t streamStateDel_rocksdb(SStreamState* pState, const SWinKey* key) {
  int       code = 0;
  SStateKey sKey = {.key = *key, .opNum = pState->number};
dengyihao's avatar
dengyihao 已提交
802
  STREAM_STATE_DEL_ROCKSDB(pState, "state", &sKey);
dengyihao's avatar
dengyihao 已提交
803 804 805 806 807 808 809 810 811 812 813
  return code;
}

// todo refactor
int32_t streamStateFillPut_rocksdb(SStreamState* pState, const SWinKey* key, const void* value, int32_t vLen) {
  int code = 0;

  STREAM_STATE_PUT_ROCKSDB(pState, "fill", key, value, vLen);
  return code;
}

dengyihao's avatar
dengyihao 已提交
814 815 816 817 818 819 820 821 822 823 824
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 已提交
825
// todo refactor
dengyihao's avatar
dengyihao 已提交
826
int32_t streamStateClear_rocksdb(SStreamState* pState) {
dengyihao's avatar
dengyihao 已提交
827
  qDebug("streamStateClear_rocksdb");
dengyihao's avatar
dengyihao 已提交
828

dengyihao's avatar
dengyihao 已提交
829 830 831 832
  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 已提交
833

dengyihao's avatar
dengyihao 已提交
834 835
  int sLen = stateKeyEncode(&sKey, sKeyStr);
  int eLen = stateKeyEncode(&eKey, eKeyStr);
dengyihao's avatar
dengyihao 已提交
836

dengyihao's avatar
dengyihao 已提交
837 838 839 840 841 842
  char toStringStart[128] = {0};
  char toStringEnd[128] = {0};
  if (qDebugFlag & DEBUG_TRACE) {
    stateKeyToString(&sKey, toStringStart);
    stateKeyToString(&eKey, toStringEnd);
  }
dengyihao's avatar
dengyihao 已提交
843

dengyihao's avatar
dengyihao 已提交
844
  char* err = NULL;
dengyihao's avatar
dengyihao 已提交
845
  rocksdb_delete_range_cf(pState->pTdbState->rocksdb, pState->pTdbState->writeOpts, pState->pTdbState->pHandle[1],
dengyihao's avatar
dengyihao 已提交
846 847 848
                          sKeyStr, sLen, eKeyStr, eLen, &err);
  // rocksdb_compact_range_cf(pState->pTdbState->rocksdb, pState->pTdbState->pHandle[0], sKeyStr, sLen, eKeyStr, eLen);
  if (err != NULL) {
dengyihao's avatar
dengyihao 已提交
849
    qWarn("failed to delete range cf(state) err: %s, start: %s, end:%s", err, toStringStart, toStringEnd);
dengyihao's avatar
dengyihao 已提交
850 851
    taosMemoryFree(err);
  }
dengyihao's avatar
dengyihao 已提交
852 853 854

  // del one by one

dengyihao's avatar
dengyihao 已提交
855 856 857 858 859
  // char      buf[128] = {0};
  // char*     s = "null";
  // SWinKey   key = {.ts = 0, .groupId = 0};
  // SStateKey skey = {.key = key, .opNum = pState->number};
  // int       sLen = stateKeyEncode(&skey, buf);
dengyihao's avatar
dengyihao 已提交
860

dengyihao's avatar
dengyihao 已提交
861
  // streamStatePut_rocksdb(pState, &key, s, strlen(s));
dengyihao's avatar
dengyihao 已提交
862

dengyihao's avatar
dengyihao 已提交
863
  // rocksdb_readoptions_t* opt = NULL;
dengyihao's avatar
dengyihao 已提交
864
  // rocksdb_iterator_t*    iter = streamStateIterCreate(pState, "state", NULL, &opt);
dengyihao's avatar
dengyihao 已提交
865
  // rocksdb_iter_seek(iter, buf, sLen);
dengyihao's avatar
dengyihao 已提交
866

dengyihao's avatar
dengyihao 已提交
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
  // char* err = NULL;
  // while (rocksdb_iter_valid(iter)) {
  //   int32_t kLen = 0;
  //   char*   key = (char*)rocksdb_iter_key(iter, (size_t*)&kLen);

  //   SStateKey ckey = {0};
  //   stateKeyDecode((void*)&ckey, key);
  //   if (ckey.opNum != pState->number) {
  //     break;
  //   }
  //   if (stateKeyCmpr(&skey, sizeof(skey), &ckey, sizeof(ckey)) > 0) {
  //     break;
  //   }

  //   rocksdb_delete_cf(pState->pTdbState->rocksdb, pState->pTdbState->writeOpts, pState->pTdbState->pHandle[0], key,
  //                     kLen, &err);
  //   if (err != NULL) {
  //     taosMemoryFree(err);
  //   }
  //   if (rocksdb_iter_valid(iter)) rocksdb_iter_next(iter);
  // }
  // rocksdb_iter_destroy(iter);
dengyihao's avatar
dengyihao 已提交
889

dengyihao's avatar
dengyihao 已提交
890 891
  return 0;
}
dengyihao's avatar
dengyihao 已提交
892 893 894 895

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 已提交
896
  STREAM_STATE_PUT_ROCKSDB(pState, "sess", &sKey, value, vLen);
dengyihao's avatar
dengyihao 已提交
897 898
  if (code == -1) {
  }
dengyihao's avatar
dengyihao 已提交
899 900
  return code;
}
dengyihao's avatar
dengyihao 已提交
901
SStreamStateCur* streamStateSessionSeekKeyCurrentPrev_rocksdb(SStreamState* pState, const SSessionKey* key) {
dengyihao's avatar
dengyihao 已提交
902
  qDebug("streamStateSessionSeekKeyCurrentPrev_rocksdb");
dengyihao's avatar
dengyihao 已提交
903 904 905 906 907
  SStreamStateCur* pCur = taosMemoryCalloc(1, sizeof(SStreamStateCur));
  if (pCur == NULL) {
    return NULL;
  }
  pCur->number = pState->number;
dengyihao's avatar
dengyihao 已提交
908 909
  pCur->db = pState->pTdbState->rocksdb;
  pCur->iter = streamStateIterCreate(pState, "sess", &pCur->snapshot, &pCur->readOpt);
dengyihao's avatar
dengyihao 已提交
910

dengyihao's avatar
dengyihao 已提交
911
  char             buf[128] = {0};
dengyihao's avatar
dengyihao 已提交
912 913
  SStateSessionKey sKey = {.key = *key, .opNum = pState->number};
  int              len = stateSessionKeyEncode(&sKey, buf);
dengyihao's avatar
dengyihao 已提交
914
  if (!streamStateIterSeekAndValid(pCur->iter, buf, len)) {
dengyihao's avatar
dengyihao 已提交
915 916
    streamStateFreeCur(pCur);
    return NULL;
dengyihao's avatar
dengyihao 已提交
917 918 919 920 921 922 923 924 925 926
  }

  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 已提交
927
  if (!rocksdb_iter_valid(pCur->iter)) {
dengyihao's avatar
dengyihao 已提交
928
    // qWarn("streamState failed to seek key prev %s", toString);
dengyihao's avatar
dengyihao 已提交
929 930 931
    streamStateFreeCur(pCur);
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
932 933
  return pCur;
}
dengyihao's avatar
dengyihao 已提交
934
SStreamStateCur* streamStateSessionSeekKeyCurrentNext_rocksdb(SStreamState* pState, SSessionKey* key) {
dengyihao's avatar
dengyihao 已提交
935
  qDebug("streamStateSessionSeekKeyCurrentNext_rocksdb");
dengyihao's avatar
dengyihao 已提交
936 937 938 939
  SStreamStateCur* pCur = taosMemoryCalloc(1, sizeof(SStreamStateCur));
  if (pCur == NULL) {
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
940 941
  pCur->db = pState->pTdbState->rocksdb;
  pCur->iter = streamStateIterCreate(pState, "sess", &pCur->snapshot, &pCur->readOpt);
dengyihao's avatar
dengyihao 已提交
942
  pCur->number = pState->number;
dengyihao's avatar
dengyihao 已提交
943

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

dengyihao's avatar
dengyihao 已提交
946
  SStateSessionKey sKey = {.key = *key, .opNum = pState->number};
dengyihao's avatar
dengyihao 已提交
947
  int              len = stateSessionKeyEncode(&sKey, buf);
dengyihao's avatar
dengyihao 已提交
948
  if (!streamStateIterSeekAndValid(pCur->iter, buf, len)) {
dengyihao's avatar
dengyihao 已提交
949 950
    streamStateFreeCur(pCur);
    return NULL;
dengyihao's avatar
dengyihao 已提交
951 952 953 954 955 956 957 958
  }
  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 已提交
959 960 961 962
  if (!rocksdb_iter_valid(pCur->iter)) {
    streamStateFreeCur(pCur);
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
963 964
  return pCur;
}
dengyihao's avatar
dengyihao 已提交
965

dengyihao's avatar
dengyihao 已提交
966
SStreamStateCur* streamStateSessionSeekKeyNext_rocksdb(SStreamState* pState, const SSessionKey* key) {
dengyihao's avatar
dengyihao 已提交
967
  qDebug("streamStateSessionSeekKeyNext_rocksdb");
dengyihao's avatar
dengyihao 已提交
968 969 970 971
  SStreamStateCur* pCur = taosMemoryCalloc(1, sizeof(SStreamStateCur));
  if (pCur == NULL) {
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
972 973
  pCur->db = pState->pTdbState->rocksdb;
  pCur->iter = streamStateIterCreate(pState, "sess", &pCur->snapshot, &pCur->readOpt);
dengyihao's avatar
dengyihao 已提交
974
  pCur->number = pState->number;
dengyihao's avatar
dengyihao 已提交
975

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

dengyihao's avatar
dengyihao 已提交
978
  char buf[128] = {0};
dengyihao's avatar
dengyihao 已提交
979
  int  len = stateSessionKeyEncode(&sKey, buf);
dengyihao's avatar
dengyihao 已提交
980
  if (!streamStateIterSeekAndValid(pCur->iter, buf, len)) {
dengyihao's avatar
dengyihao 已提交
981 982
    streamStateFreeCur(pCur);
    return NULL;
dengyihao's avatar
dengyihao 已提交
983
  }
dengyihao's avatar
dengyihao 已提交
984

dengyihao's avatar
dengyihao 已提交
985 986 987 988 989 990 991
  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 已提交
992 993 994 995
  if (!rocksdb_iter_valid(pCur->iter)) {
    streamStateFreeCur(pCur);
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
996 997 998
  return pCur;
}

dengyihao's avatar
dengyihao 已提交
999
int32_t streamStateAddIfNotExist_rocksdb(SStreamState* pState, const SWinKey* key, void** pVal, int32_t* pVLen) {
dengyihao's avatar
dengyihao 已提交
1000
  qDebug("streamStateAddIfNotExist_rocksdb");
dengyihao's avatar
dengyihao 已提交
1001 1002 1003 1004 1005 1006 1007 1008
  int32_t size = *pVLen;
  if (streamStateGet_rocksdb(pState, key, pVal, pVLen) == 0) {
    return 0;
  }
  *pVal = taosMemoryMalloc(size);
  memset(*pVal, 0, size);
  return 0;
}
dengyihao's avatar
dengyihao 已提交
1009 1010
SStreamStateCur* streamStateSeekToLast_rocksdb(SStreamState* pState, const SWinKey* key) {
  qDebug("streamStateGetCur_rocksdb");
dengyihao's avatar
dengyihao 已提交
1011 1012 1013 1014 1015
  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 已提交
1016

dengyihao's avatar
dengyihao 已提交
1017
  SStreamStateCur* pCur = taosMemoryCalloc(1, sizeof(SStreamStateCur));
dengyihao's avatar
dengyihao 已提交
1018 1019 1020
  if (pCur == NULL) return NULL;
  pCur->db = pState->pTdbState->rocksdb;
  pCur->iter = streamStateIterCreate(pState, "state", &pCur->snapshot, &pCur->readOpt);
dengyihao's avatar
dengyihao 已提交
1021 1022
  rocksdb_iter_seek(pCur->iter, buf, (size_t)klen);
  rocksdb_iter_prev(pCur->iter);
dengyihao's avatar
dengyihao 已提交
1023

dengyihao's avatar
dengyihao 已提交
1024
  STREAM_STATE_DEL_ROCKSDB(pState, "state", &maxStateKey);
dengyihao's avatar
dengyihao 已提交
1025 1026
  return pCur;
}
dengyihao's avatar
dengyihao 已提交
1027
SStreamStateCur* streamStateGetCur_rocksdb(SStreamState* pState, const SWinKey* key) {
dengyihao's avatar
dengyihao 已提交
1028
  qDebug("streamStateGetCur_rocksdb");
dengyihao's avatar
dengyihao 已提交
1029
  SStreamStateCur* pCur = taosMemoryCalloc(1, sizeof(SStreamStateCur));
dengyihao's avatar
dengyihao 已提交
1030

dengyihao's avatar
dengyihao 已提交
1031
  if (pCur == NULL) return NULL;
dengyihao's avatar
dengyihao 已提交
1032
  pCur->db = pState->pTdbState->rocksdb;
dengyihao's avatar
dengyihao 已提交
1033
  pCur->iter = streamStateIterCreate(pState, "state", &pCur->snapshot, &pCur->readOpt);
dengyihao's avatar
dengyihao 已提交
1034 1035

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

dengyihao's avatar
dengyihao 已提交
1039
  rocksdb_iter_seek(pCur->iter, buf, len);
dengyihao's avatar
dengyihao 已提交
1040 1041 1042 1043 1044
  if (rocksdb_iter_valid(pCur->iter)) {
    SStateKey curKey;
    size_t    kLen = 0;
    char*     keyStr = (char*)rocksdb_iter_key(pCur->iter, &kLen);
    stateKeyDecode((void*)&curKey, keyStr);
dengyihao's avatar
dengyihao 已提交
1045

dengyihao's avatar
dengyihao 已提交
1046 1047 1048 1049 1050 1051 1052 1053
    if (stateKeyCmpr(&sKey, sizeof(sKey), &curKey, sizeof(curKey)) == 0) {
      pCur->number = pState->number;
      return pCur;
    }
  }
  streamStateFreeCur(pCur);
  return NULL;
}
dengyihao's avatar
dengyihao 已提交
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077

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;
    streamStateFreeCur(pCur);
  }
  return NULL;
}
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;

  if (rocksdb_iter_valid(pCur->iter)) {
    size_t tlen;
    char*  keyStr = (char*)rocksdb_iter_key(pCur->iter, &tlen);
    stateKeyDecode((void*)pKtmp, keyStr);
    if (pKtmp->opNum != pCur->number) {
      return -1;
    }
dengyihao's avatar
dengyihao 已提交
1078 1079 1080
    size_t vlen = 0;
    if (pVal != NULL) *pVal = (char*)rocksdb_iter_value(pCur->iter, &vlen);
    if (pVLen != NULL) *pVLen = vlen;
dengyihao's avatar
dengyihao 已提交
1081 1082 1083 1084 1085
    *pKey = pKtmp->key;
    return 0;
  }
  return -1;
}
dengyihao's avatar
dengyihao 已提交
1086
SStreamStateCur* streamStateFillGetCur_rocksdb(SStreamState* pState, const SWinKey* key) {
dengyihao's avatar
dengyihao 已提交
1087
  qDebug("streamStateFillGetCur_rocksdb");
dengyihao's avatar
dengyihao 已提交
1088
  SStreamStateCur* pCur = taosMemoryCalloc(1, sizeof(SStreamStateCur));
dengyihao's avatar
dengyihao 已提交
1089

dengyihao's avatar
dengyihao 已提交
1090 1091
  if (pCur == NULL) return NULL;

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

dengyihao's avatar
dengyihao 已提交
1095
  char buf[128] = {0};
dengyihao's avatar
dengyihao 已提交
1096
  int  len = winKeyEncode((void*)key, buf);
dengyihao's avatar
dengyihao 已提交
1097
  if (!streamStateIterSeekAndValid(pCur->iter, buf, len)) {
dengyihao's avatar
dengyihao 已提交
1098 1099
    streamStateFreeCur(pCur);
    return NULL;
dengyihao's avatar
dengyihao 已提交
1100
  }
dengyihao's avatar
dengyihao 已提交
1101

dengyihao's avatar
dengyihao 已提交
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
  if (rocksdb_iter_valid(pCur->iter)) {
    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) {
      return pCur;
    }
  }

  streamStateFreeCur(pCur);
  return NULL;
}
int32_t streamStateFillGetKVByCur_rocksdb(SStreamStateCur* pCur, SWinKey* pKey, const void** pVal, int32_t* pVLen) {
dengyihao's avatar
dengyihao 已提交
1116
  qDebug("streamStateFillGetKVByCur_rocksdb");
dengyihao's avatar
dengyihao 已提交
1117 1118 1119
  if (!pCur) {
    return -1;
  }
dengyihao's avatar
dengyihao 已提交
1120
  SWinKey winKey;
dengyihao's avatar
dengyihao 已提交
1121 1122 1123 1124
  if (rocksdb_iter_valid(pCur->iter)) {
    size_t tlen;
    char*  keyStr = (char*)rocksdb_iter_key(pCur->iter, &tlen);
    winKeyDecode(&winKey, keyStr);
dengyihao's avatar
dengyihao 已提交
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134

    size_t      vlen = 0;
    const char* valStr = rocksdb_iter_value(pCur->iter, &vlen);
    if (pVal != NULL) {
      char* dst = taosMemoryCalloc(1, vlen);
      memcpy(dst, valStr, vlen);
      *pVal = dst;
    }
    if (pVLen != NULL) *pVLen = vlen;

dengyihao's avatar
dengyihao 已提交
1135 1136 1137
  } else {
    return -1;
  }
dengyihao's avatar
dengyihao 已提交
1138
  *pKey = winKey;
dengyihao's avatar
dengyihao 已提交
1139 1140 1141
  return 0;
}
int32_t streamStateGetGroupKVByCur_rocksdb(SStreamStateCur* pCur, SWinKey* pKey, const void** pVal, int32_t* pVLen) {
dengyihao's avatar
dengyihao 已提交
1142
  qDebug("streamStateGetGroupKVByCur_rocksdb");
dengyihao's avatar
dengyihao 已提交
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
  if (!pCur) {
    return -1;
  }
  uint64_t groupId = pKey->groupId;

  int32_t code = streamStateFillGetKVByCur_rocksdb(pCur, pKey, pVal, pVLen);
  if (code == 0) {
    if (pKey->groupId == groupId) {
      return 0;
    }
  }
  return -1;
}
int32_t streamStateGetFirst_rocksdb(SStreamState* pState, SWinKey* key) {
dengyihao's avatar
dengyihao 已提交
1157
  qDebug("streamStateGetFirst_rocksdb");
dengyihao's avatar
dengyihao 已提交
1158 1159 1160 1161 1162 1163 1164 1165
  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);
  return code;
}
dengyihao's avatar
dengyihao 已提交
1166
int32_t streamStateSessionGetKVByCur_rocksdb(SStreamStateCur* pCur, SSessionKey* pKey, void** pVal, int32_t* pVLen) {
dengyihao's avatar
dengyihao 已提交
1167
  qDebug("streamStateSessionGetKVByCur_rocksdb");
dengyihao's avatar
dengyihao 已提交
1168 1169 1170
  if (!pCur) {
    return -1;
  }
dengyihao's avatar
dengyihao 已提交
1171
  SStateSessionKey ktmp = {0};
dengyihao's avatar
dengyihao 已提交
1172
  size_t           kLen = 0, vLen = 0;
dengyihao's avatar
dengyihao 已提交
1173 1174 1175 1176 1177 1178 1179

  if (!rocksdb_iter_valid(pCur->iter)) {
    return -1;
  }
  const char* curKey = rocksdb_iter_key(pCur->iter, (size_t*)&kLen);
  stateSessionKeyDecode((void*)&ktmp, (char*)curKey);

dengyihao's avatar
dengyihao 已提交
1180 1181
  SStateSessionKey* pKTmp = &ktmp;
  const char*       val = rocksdb_iter_value(pCur->iter, (size_t*)&vLen);
dengyihao's avatar
dengyihao 已提交
1182 1183 1184
  if (pVal != NULL) {
    *pVal = (char*)val;
  }
dengyihao's avatar
dengyihao 已提交
1185
  if (pVLen != NULL) *pVLen = vLen;
dengyihao's avatar
dengyihao 已提交
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196

  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 已提交
1197
SStreamStateCur* streamStateSeekKeyNext_rocksdb(SStreamState* pState, const SWinKey* key) {
dengyihao's avatar
dengyihao 已提交
1198
  qDebug("streamStateSeekKeyNext_rocksdb");
dengyihao's avatar
dengyihao 已提交
1199 1200 1201 1202 1203
  SStreamStateCur* pCur = taosMemoryCalloc(1, sizeof(SStreamStateCur));
  if (pCur == NULL) {
    return NULL;
  }
  pCur->number = pState->number;
dengyihao's avatar
dengyihao 已提交
1204
  pCur->db = pState->pTdbState->rocksdb;
dengyihao's avatar
dengyihao 已提交
1205
  pCur->iter = streamStateIterCreate(pState, "state", &pCur->snapshot, &pCur->readOpt);
dengyihao's avatar
dengyihao 已提交
1206

dengyihao's avatar
dengyihao 已提交
1207
  SStateKey sKey = {.key = *key, .opNum = pState->number};
dengyihao's avatar
dengyihao 已提交
1208
  char      buf[128] = {0};
dengyihao's avatar
dengyihao 已提交
1209
  int       len = stateKeyEncode((void*)&sKey, buf);
dengyihao's avatar
dengyihao 已提交
1210 1211 1212
  if (!streamStateIterSeekAndValid(pCur->iter, buf, len)) {
    streamStateFreeCur(pCur);
    return NULL;
dengyihao's avatar
dengyihao 已提交
1213 1214
  }

dengyihao's avatar
dengyihao 已提交
1215 1216 1217 1218 1219 1220 1221 1222 1223
  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;
    }
    rocksdb_iter_next(pCur->iter);
dengyihao's avatar
dengyihao 已提交
1224
    return pCur;
dengyihao's avatar
dengyihao 已提交
1225 1226 1227 1228 1229
  }
  streamStateFreeCur(pCur);
  return NULL;
}
SStreamStateCur* streamStateFillSeekKeyNext_rocksdb(SStreamState* pState, const SWinKey* key) {
dengyihao's avatar
dengyihao 已提交
1230
  qDebug("streamStateFillSeekKeyNext_rocksdb");
dengyihao's avatar
dengyihao 已提交
1231 1232 1233 1234
  SStreamStateCur* pCur = taosMemoryCalloc(1, sizeof(SStreamStateCur));
  if (!pCur) {
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
1235

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

dengyihao's avatar
dengyihao 已提交
1239
  char buf[128] = {0};
dengyihao's avatar
dengyihao 已提交
1240
  int  len = winKeyEncode((void*)key, buf);
dengyihao's avatar
dengyihao 已提交
1241 1242 1243
  if (!streamStateIterSeekAndValid(pCur->iter, buf, len)) {
    streamStateFreeCur(pCur);
    return NULL;
dengyihao's avatar
dengyihao 已提交
1244
  }
dengyihao's avatar
dengyihao 已提交
1245

dengyihao's avatar
dengyihao 已提交
1246 1247 1248 1249 1250 1251 1252 1253 1254
  {
    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 已提交
1255
    return pCur;
dengyihao's avatar
dengyihao 已提交
1256 1257 1258 1259 1260
  }
  streamStateFreeCur(pCur);
  return NULL;
}
SStreamStateCur* streamStateFillSeekKeyPrev_rocksdb(SStreamState* pState, const SWinKey* key) {
dengyihao's avatar
dengyihao 已提交
1261
  qDebug("streamStateFillSeekKeyPrev_rocksdb");
dengyihao's avatar
dengyihao 已提交
1262 1263 1264 1265
  SStreamStateCur* pCur = taosMemoryCalloc(1, sizeof(SStreamStateCur));
  if (pCur == NULL) {
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
1266

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

dengyihao's avatar
dengyihao 已提交
1270
  char buf[128] = {0};
dengyihao's avatar
dengyihao 已提交
1271
  int  len = winKeyEncode((void*)key, buf);
dengyihao's avatar
dengyihao 已提交
1272 1273 1274
  if (!streamStateIterSeekAndValid(pCur->iter, buf, len)) {
    streamStateFreeCur(pCur);
    return NULL;
dengyihao's avatar
dengyihao 已提交
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
  }

  {
    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 已提交
1286
    return pCur;
dengyihao's avatar
dengyihao 已提交
1287 1288 1289 1290 1291
  }
  streamStateFreeCur(pCur);
  return NULL;
}
int32_t streamStateCurPrev_rocksdb(SStreamState* pState, SStreamStateCur* pCur) {
dengyihao's avatar
dengyihao 已提交
1292
  qDebug("streamStateCurPrev_rocksdb");
dengyihao's avatar
dengyihao 已提交
1293
  if (!pCur) return -1;
dengyihao's avatar
dengyihao 已提交
1294

dengyihao's avatar
dengyihao 已提交
1295 1296 1297
  rocksdb_iter_prev(pCur->iter);
  return 0;
}
dengyihao's avatar
dengyihao 已提交
1298 1299 1300 1301 1302 1303 1304
int32_t streamStateCurNext_rocksdb(SStreamState* pState, SStreamStateCur* pCur) {
  if (!pCur) {
    return -1;
  }
  rocksdb_iter_next(pCur->iter);
  return 0;
}
dengyihao's avatar
dengyihao 已提交
1305
int32_t streamStateSessionGetKeyByRange_rocksdb(SStreamState* pState, const SSessionKey* key, SSessionKey* curKey) {
dengyihao's avatar
dengyihao 已提交
1306
  qDebug("streamStateSessionGetKeyByRange_rocksdb");
dengyihao's avatar
dengyihao 已提交
1307 1308 1309 1310 1311
  SStreamStateCur* pCur = taosMemoryCalloc(1, sizeof(SStreamStateCur));
  if (pCur == NULL) {
    return -1;
  }
  pCur->number = pState->number;
dengyihao's avatar
dengyihao 已提交
1312 1313
  pCur->db = pState->pTdbState->rocksdb;
  pCur->iter = streamStateIterCreate(pState, "sess", &pCur->snapshot, &pCur->readOpt);
dengyihao's avatar
dengyihao 已提交
1314 1315 1316

  SStateSessionKey sKey = {.key = *key, .opNum = pState->number};
  int32_t          c = 0;
dengyihao's avatar
dengyihao 已提交
1317
  char             buf[128] = {0};
dengyihao's avatar
dengyihao 已提交
1318
  int              len = stateSessionKeyEncode(&sKey, buf);
dengyihao's avatar
dengyihao 已提交
1319 1320 1321
  if (!streamStateIterSeekAndValid(pCur->iter, buf, len)) {
    streamStateFreeCur(pCur);
    return -1;
dengyihao's avatar
dengyihao 已提交
1322 1323
  }

dengyihao's avatar
dengyihao 已提交
1324
  size_t           kLen;
dengyihao's avatar
dengyihao 已提交
1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360
  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;
}

dengyihao's avatar
dengyihao 已提交
1361
int32_t streamStateSessionGet_rocksdb(SStreamState* pState, SSessionKey* key, void** pVal, int32_t* pVLen) {
dengyihao's avatar
dengyihao 已提交
1362
  qDebug("streamStateSessionGet_rocksdb");
dengyihao's avatar
dengyihao 已提交
1363
  int              code = 0;
dengyihao's avatar
dengyihao 已提交
1364
  SStreamStateCur* pCur = streamStateSessionSeekKeyCurrentNext_rocksdb(pState, key);
dengyihao's avatar
dengyihao 已提交
1365
  SSessionKey      resKey = *key;
dengyihao's avatar
dengyihao 已提交
1366
  void*            tmp = NULL;
dengyihao's avatar
dengyihao 已提交
1367 1368
  int32_t          vLen = 0;
  code = streamStateSessionGetKVByCur_rocksdb(pCur, &resKey, &tmp, &vLen);
dengyihao's avatar
dengyihao 已提交
1369
  if (code == 0) {
dengyihao's avatar
dengyihao 已提交
1370 1371
    if (pVLen != NULL) *pVLen = vLen;

dengyihao's avatar
dengyihao 已提交
1372 1373 1374 1375
    if (key->win.skey != resKey.win.skey) {
      code = -1;
    } else {
      *key = resKey;
dengyihao's avatar
dengyihao 已提交
1376
      *pVal = taosMemoryCalloc(1, *pVLen);
dengyihao's avatar
dengyihao 已提交
1377
      memcpy(*pVal, tmp, *pVLen);
dengyihao's avatar
dengyihao 已提交
1378 1379 1380
    }
  }
  streamStateFreeCur(pCur);
dengyihao's avatar
dengyihao 已提交
1381 1382 1383 1384
  // impl later
  return code;
}

dengyihao's avatar
dengyihao 已提交
1385
int32_t streamStateSessionDel_rocksdb(SStreamState* pState, const SSessionKey* key) {
dengyihao's avatar
dengyihao 已提交
1386 1387
  int              code = 0;
  SStateSessionKey sKey = {.key = *key, .opNum = pState->number};
dengyihao's avatar
dengyihao 已提交
1388
  STREAM_STATE_DEL_ROCKSDB(pState, "sess", &sKey);
dengyihao's avatar
dengyihao 已提交
1389 1390
  return code;
}
dengyihao's avatar
dengyihao 已提交
1391 1392
int32_t streamStateSessionAddIfNotExist_rocksdb(SStreamState* pState, SSessionKey* key, TSKEY gap, void** pVal,
                                                int32_t* pVLen) {
dengyihao's avatar
dengyihao 已提交
1393
  qDebug("streamStateSessionAddIfNotExist_rocksdb");
dengyihao's avatar
dengyihao 已提交
1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404
  // 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 已提交
1405 1406 1407 1408
  if (pCur == NULL) {
  }
  int32_t code = streamStateSessionGetKVByCur_rocksdb(pCur, key, pVal, pVLen);

dengyihao's avatar
dengyihao 已提交
1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442
  if (code == 0) {
    if (sessionRangeKeyCmpr(&searchKey, key) == 0) {
      memcpy(tmp, *pVal, valSize);
      streamStateSessionDel_rocksdb(pState, key);
      goto _end;
    }
    streamStateCurNext_rocksdb(pState, pCur);
  } else {
    *key = originKey;
    streamStateFreeCur(pCur);
    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:

  *pVal = tmp;
  streamStateFreeCur(pCur);
  return res;
}
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 已提交
1443
  qDebug("streamStateStateAddIfNotExist_rocksdb");
dengyihao's avatar
dengyihao 已提交
1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
  // 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 已提交
1459
      streamStateSessionDel_rocksdb(pState, key);
dengyihao's avatar
dengyihao 已提交
1460 1461 1462 1463 1464 1465
      goto _end;
    }

    void* stateKey = (char*)(*pVal) + (valSize - keyDataLen);
    if (fn(pKeyData, stateKey) == true) {
      memcpy(tmp, *pVal, valSize);
dengyihao's avatar
dengyihao 已提交
1466
      streamStateSessionDel_rocksdb(pState, key);
dengyihao's avatar
dengyihao 已提交
1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496
      goto _end;
    }

    streamStateCurNext_rocksdb(pState, pCur);
  } else {
    *key = tmpKey;
    streamStateFreeCur(pCur);
    pCur = streamStateSessionSeekKeyNext_rocksdb(pState, key);
  }

  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;
    }
  }

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

_end:

  *pVal = tmp;
  streamStateFreeCur(pCur);
  return res;
}
dengyihao's avatar
dengyihao 已提交
1497

dengyihao's avatar
dengyihao 已提交
1498
int32_t streamStateSessionClear_rocksdb(SStreamState* pState) {
dengyihao's avatar
dengyihao 已提交
1499
  qDebug("streamStateSessionClear_rocksdb");
dengyihao's avatar
dengyihao 已提交
1500 1501 1502 1503 1504 1505 1506 1507 1508
  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) {
dengyihao's avatar
dengyihao 已提交
1509
      memset(buf, 0, size);
dengyihao's avatar
dengyihao 已提交
1510 1511 1512 1513 1514 1515 1516 1517 1518
      streamStateSessionPut_rocksdb(pState, &delKey, buf, size);
    } else {
      break;
    }
    streamStateCurNext_rocksdb(pState, pCur);
  }
  streamStateFreeCur(pCur);
  return -1;
}
dengyihao's avatar
dengyihao 已提交
1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546
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;
}

int32_t streamStatePutParName_rocksdb(SStreamState* pState, int64_t groupId, const char tbname[TSDB_TABLE_NAME_LEN]) {
  int code = 0;
  STREAM_STATE_PUT_ROCKSDB(pState, "parname", &groupId, tbname, TSDB_TABLE_NAME_LEN);
  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;
}

void streamStateDestroy_rocksdb(SStreamState* pState) {
  // only close db
  streamCleanBackend(pState);
}