streamState.h 6.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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/>.
 */

#include "tdatablock.h"
dengyihao's avatar
dengyihao 已提交
17 18

#include "rocksdb/c.h"
19
#include "tdbInt.h"
5
54liuyao 已提交
20
#include "tstreamFileState.h"
21 22 23 24 25 26 27 28 29 30

#ifdef __cplusplus
extern "C" {
#endif

#ifndef _STREAM_STATE_H_
#define _STREAM_STATE_H_

typedef struct SStreamTask SStreamTask;

5
54liuyao 已提交
31 32
typedef bool (*state_key_cmpr_fn)(void* pKey1, void* pKey2);

5
54liuyao 已提交
33
typedef struct STdbState {
34
  SStreamTask* pOwner;
dengyihao's avatar
dengyihao 已提交
35

dengyihao's avatar
dengyihao 已提交
36 37
  rocksdb_t*                       rocksdb;
  rocksdb_column_family_handle_t** pHandle;
dengyihao's avatar
dengyihao 已提交
38 39 40 41 42
  rocksdb_writeoptions_t*          writeOpts;
  rocksdb_readoptions_t*           readOpts;
  rocksdb_options_t**              cfOpts;
  rocksdb_comparator_t**           pCompare;
  rocksdb_options_t*               dbOpt;
dengyihao's avatar
dengyihao 已提交
43 44 45 46 47 48 49 50 51 52

  TDB* db;
  TTB* pStateDb;
  TTB* pFuncStateDb;
  TTB* pFillStateDb;  // todo refactor
  TTB* pSessionStateDb;
  TTB* pParNameDb;
  TTB* pParTagDb;
  TXN* txn;

5
54liuyao 已提交
53 54 55 56 57
} STdbState;

// incremental state storage
typedef struct {
  STdbState* pTdbState;
5
54liuyao 已提交
58
  SStreamFileState* pFileState;
5
54liuyao 已提交
59
  int32_t    number;
60 61
} SStreamState;

62
SStreamState* streamStateOpen(char* path, SStreamTask* pTask, bool specPath, int32_t szPage, int32_t pages);
63 64 65
void          streamStateClose(SStreamState* pState);
int32_t       streamStateBegin(SStreamState* pState);
int32_t       streamStateCommit(SStreamState* pState);
5
54liuyao 已提交
66
void          streamStateDestroy(SStreamState* pState);
67 68

typedef struct {
dengyihao's avatar
dengyihao 已提交
69 70 71 72
  rocksdb_iterator_t*    iter;
  rocksdb_snapshot_t*    snapshot;
  rocksdb_readoptions_t* readOpt;
  rocksdb_t*             db;
dengyihao's avatar
dengyihao 已提交
73

74 75
  TBC*    pCur;
  int64_t number;
76 77 78 79 80 81 82 83
} SStreamStateCur;

int32_t streamStateFuncPut(SStreamState* pState, const STupleKey* key, const void* value, int32_t vLen);
int32_t streamStateFuncGet(SStreamState* pState, const STupleKey* key, void** pVal, int32_t* pVLen);
int32_t streamStateFuncDel(SStreamState* pState, const STupleKey* key);

int32_t streamStatePut(SStreamState* pState, const SWinKey* key, const void* value, int32_t vLen);
int32_t streamStateGet(SStreamState* pState, const SWinKey* key, void** pVal, int32_t* pVLen);
5
54liuyao 已提交
84 85
bool streamStateCheck(SStreamState* pState, const SWinKey* key);
int32_t streamStateGetByPos(SStreamState* pState, void* pos, void** pVal);
86
int32_t streamStateDel(SStreamState* pState, const SWinKey* key);
87 88
int32_t streamStateClear(SStreamState* pState);
void    streamStateSetNumber(SStreamState* pState, int32_t number);
5
54liuyao 已提交
89

90
int32_t streamStateSessionAddIfNotExist(SStreamState* pState, SSessionKey* key, TSKEY gap, void** pVal, int32_t* pVLen);
5
54liuyao 已提交
91 92 93 94
int32_t streamStateSessionPut(SStreamState* pState, const SSessionKey* key, const void* value, int32_t vLen);
int32_t streamStateSessionGet(SStreamState* pState, SSessionKey* key, void** pVal, int32_t* pVLen);
int32_t streamStateSessionDel(SStreamState* pState, const SSessionKey* key);
int32_t streamStateSessionClear(SStreamState* pState);
95
int32_t streamStateSessionGetKVByCur(SStreamStateCur* pCur, SSessionKey* pKey, void** pVal, int32_t* pVLen);
5
54liuyao 已提交
96 97
int32_t streamStateStateAddIfNotExist(SStreamState* pState, SSessionKey* key, char* pKeyData, int32_t keyDataLen,
                                      state_key_cmpr_fn fn, void** pVal, int32_t* pVLen);
98
int32_t streamStateSessionGetKeyByRange(SStreamState* pState, const SSessionKey* range, SSessionKey* curKey);
5
54liuyao 已提交
99 100

SStreamStateCur* streamStateSessionSeekKeyNext(SStreamState* pState, const SSessionKey* key);
101
SStreamStateCur* streamStateSessionSeekKeyCurrentPrev(SStreamState* pState, const SSessionKey* key);
102
SStreamStateCur* streamStateSessionSeekKeyCurrentNext(SStreamState* pState, const SSessionKey* key);
5
54liuyao 已提交
103

5
54liuyao 已提交
104 105 106 107
int32_t streamStateFillPut(SStreamState* pState, const SWinKey* key, const void* value, int32_t vLen);
int32_t streamStateFillGet(SStreamState* pState, const SWinKey* key, void** pVal, int32_t* pVLen);
int32_t streamStateFillDel(SStreamState* pState, const SWinKey* key);

108 109 110 111
int32_t streamStateAddIfNotExist(SStreamState* pState, const SWinKey* key, void** pVal, int32_t* pVLen);
int32_t streamStateReleaseBuf(SStreamState* pState, const SWinKey* key, void* pVal);
void    streamFreeVal(void* val);

5
54liuyao 已提交
112
SStreamStateCur* streamStateGetAndCheckCur(SStreamState* pState, SWinKey* key);
113
SStreamStateCur* streamStateSeekKeyNext(SStreamState* pState, const SWinKey* key);
5
54liuyao 已提交
114 115
SStreamStateCur* streamStateFillSeekKeyNext(SStreamState* pState, const SWinKey* key);
SStreamStateCur* streamStateFillSeekKeyPrev(SStreamState* pState, const SWinKey* key);
116 117
void             streamStateFreeCur(SStreamStateCur* pCur);

5
54liuyao 已提交
118
int32_t streamStateGetGroupKVByCur(SStreamStateCur* pCur, SWinKey* pKey, const void** pVal, int32_t* pVLen);
119 120
int32_t streamStateGetKVByCur(SStreamStateCur* pCur, SWinKey* pKey, const void** pVal, int32_t* pVLen);

121
int32_t streamStateGetFirst(SStreamState* pState, SWinKey* key);
122 123 124 125 126 127
int32_t streamStateSeekFirst(SStreamState* pState, SStreamStateCur* pCur);
int32_t streamStateSeekLast(SStreamState* pState, SStreamStateCur* pCur);

int32_t streamStateCurNext(SStreamState* pState, SStreamStateCur* pCur);
int32_t streamStateCurPrev(SStreamState* pState, SStreamStateCur* pCur);

128 129 130
int32_t streamStatePutParName(SStreamState* pState, int64_t groupId, const char* tbname);
int32_t streamStateGetParName(SStreamState* pState, int64_t groupId, void** pVal);

dengyihao's avatar
dengyihao 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
/***compare func **/

// todo refactor
typedef struct SStateKey {
  SWinKey key;
  int64_t opNum;
} SStateKey;

typedef struct SStateSessionKey {
  SSessionKey key;
  int64_t     opNum;
} SStateSessionKey;

int sessionRangeKeyCmpr(const SSessionKey* pWin1, const SSessionKey* pWin2);
int sessionWinKeyCmpr(const SSessionKey* pWin1, const SSessionKey* pWin2);
int stateSessionKeyCmpr(const void* pKey1, int kLen1, const void* pKey2, int kLen2);
int stateKeyCmpr(const void* pKey1, int kLen1, const void* pKey2, int kLen2);
148 149
#if 0
char* streamStateSessionDump(SStreamState* pState);
5
54liuyao 已提交
150
char* streamStateIntervalDump(SStreamState* pState);
151
#endif
5
54liuyao 已提交
152

153 154 155 156 157
#ifdef __cplusplus
}
#endif

#endif /* ifndef _STREAM_STATE_H_ */