streamState.h 7.4 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"
L
liuyao 已提交
20
#include "tsimplehash.h"
5
54liuyao 已提交
21
#include "tstreamFileState.h"
22 23 24 25 26 27 28 29

#ifdef __cplusplus
extern "C" {
#endif

#ifndef _STREAM_STATE_H_
#define _STREAM_STATE_H_

30 31 32 33
// void*      streamBackendInit(const char* path);
// void       streamBackendCleanup(void* arg);
// SListNode* streamBackendAddCompare(void* backend, void* arg);
// void       streamBackendDelCompare(void* backend, void* arg);
5
54liuyao 已提交
34 35
typedef bool (*state_key_cmpr_fn)(void* pKey1, void* pKey2);

5
54liuyao 已提交
36
typedef struct STdbState {
dengyihao's avatar
dengyihao 已提交
37 38
  rocksdb_t*                       rocksdb;
  rocksdb_column_family_handle_t** pHandle;
dengyihao's avatar
dengyihao 已提交
39 40 41 42
  rocksdb_writeoptions_t*          writeOpts;
  rocksdb_readoptions_t*           readOpts;
  rocksdb_options_t**              cfOpts;
  rocksdb_options_t*               dbOpt;
43
  struct SStreamTask*              pOwner;
dengyihao's avatar
dengyihao 已提交
44 45
  void*                            param;
  void*                            env;
dengyihao's avatar
dengyihao 已提交
46
  SListNode*                       pComparNode;
47
  void*                            pBackendHandle;
dengyihao's avatar
dengyihao 已提交
48
  char                             idstr[64];
dengyihao's avatar
dengyihao 已提交
49
  void*                            compactFactory;
dengyihao's avatar
dengyihao 已提交
50 51 52 53 54 55 56 57 58

  TDB* db;
  TTB* pStateDb;
  TTB* pFuncStateDb;
  TTB* pFillStateDb;  // todo refactor
  TTB* pSessionStateDb;
  TTB* pParNameDb;
  TTB* pParTagDb;
  TXN* txn;
5
54liuyao 已提交
59 60 61 62
} STdbState;

// incremental state storage
typedef struct {
L
liuyao 已提交
63
  STdbState*        pTdbState;
5
54liuyao 已提交
64
  SStreamFileState* pFileState;
L
liuyao 已提交
65 66
  int32_t           number;
  SSHashObj*        parNameMap;
L
liuyao 已提交
67
  int64_t           checkPointId;
dengyihao's avatar
dengyihao 已提交
68
  int32_t           taskId;
dengyihao's avatar
dengyihao 已提交
69
  int64_t           streamId;
70 71
} SStreamState;

72
SStreamState* streamStateOpen(char* path, struct SStreamTask* pTask, bool specPath, int32_t szPage, int32_t pages);
dengyihao's avatar
dengyihao 已提交
73
void          streamStateClose(SStreamState* pState, bool remove);
74 75
int32_t       streamStateBegin(SStreamState* pState);
int32_t       streamStateCommit(SStreamState* pState);
dengyihao's avatar
dengyihao 已提交
76
void          streamStateDestroy(SStreamState* pState, bool remove);
L
liuyao 已提交
77
int32_t       streamStateDeleteCheckPoint(SStreamState* pState, TSKEY mark);
78 79

typedef struct {
dengyihao's avatar
dengyihao 已提交
80 81 82 83
  rocksdb_iterator_t*    iter;
  rocksdb_snapshot_t*    snapshot;
  rocksdb_readoptions_t* readOpt;
  rocksdb_t*             db;
dengyihao's avatar
dengyihao 已提交
84

85 86
  TBC*    pCur;
  int64_t number;
87 88 89 90 91 92 93 94
} 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);
dengyihao's avatar
dengyihao 已提交
95
bool    streamStateCheck(SStreamState* pState, const SWinKey* key);
5
54liuyao 已提交
96
int32_t streamStateGetByPos(SStreamState* pState, void* pos, void** pVal);
97
int32_t streamStateDel(SStreamState* pState, const SWinKey* key);
98 99
int32_t streamStateClear(SStreamState* pState);
void    streamStateSetNumber(SStreamState* pState, int32_t number);
L
fix bug  
liuyao 已提交
100 101
int32_t streamStateSaveInfo(SStreamState* pState, void* pKey, int32_t keyLen, void* pVal, int32_t vLen);
int32_t streamStateGetInfo(SStreamState* pState, void* pKey, int32_t keyLen, void** pVal, int32_t* pLen);
5
54liuyao 已提交
102

103
int32_t streamStateSessionAddIfNotExist(SStreamState* pState, SSessionKey* key, TSKEY gap, void** pVal, int32_t* pVLen);
5
54liuyao 已提交
104 105 106 107
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);
108
int32_t streamStateSessionGetKVByCur(SStreamStateCur* pCur, SSessionKey* pKey, void** pVal, int32_t* pVLen);
5
54liuyao 已提交
109 110
int32_t streamStateStateAddIfNotExist(SStreamState* pState, SSessionKey* key, char* pKeyData, int32_t keyDataLen,
                                      state_key_cmpr_fn fn, void** pVal, int32_t* pVLen);
111
int32_t streamStateSessionGetKeyByRange(SStreamState* pState, const SSessionKey* range, SSessionKey* curKey);
5
54liuyao 已提交
112 113

SStreamStateCur* streamStateSessionSeekKeyNext(SStreamState* pState, const SSessionKey* key);
114
SStreamStateCur* streamStateSessionSeekKeyCurrentPrev(SStreamState* pState, const SSessionKey* key);
115
SStreamStateCur* streamStateSessionSeekKeyCurrentNext(SStreamState* pState, const SSessionKey* key);
5
54liuyao 已提交
116

5
54liuyao 已提交
117 118 119 120
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);

121 122 123 124
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 已提交
125
SStreamStateCur* streamStateGetAndCheckCur(SStreamState* pState, SWinKey* key);
126
SStreamStateCur* streamStateSeekKeyNext(SStreamState* pState, const SWinKey* key);
5
54liuyao 已提交
127 128
SStreamStateCur* streamStateFillSeekKeyNext(SStreamState* pState, const SWinKey* key);
SStreamStateCur* streamStateFillSeekKeyPrev(SStreamState* pState, const SWinKey* key);
129 130
void             streamStateFreeCur(SStreamStateCur* pCur);

5
54liuyao 已提交
131
int32_t streamStateGetGroupKVByCur(SStreamStateCur* pCur, SWinKey* pKey, const void** pVal, int32_t* pVLen);
132 133
int32_t streamStateGetKVByCur(SStreamStateCur* pCur, SWinKey* pKey, const void** pVal, int32_t* pVLen);

134
int32_t streamStateGetFirst(SStreamState* pState, SWinKey* key);
135 136 137 138 139 140
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);

141 142 143
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 已提交
144 145
/***compare func **/

dengyihao's avatar
dengyihao 已提交
146 147 148 149
typedef struct SStateChekpoint {
  char*   taskName;
  int64_t checkpointId;
} SStateChekpoint;
dengyihao's avatar
dengyihao 已提交
150 151 152 153 154 155 156 157 158 159 160
// todo refactor
typedef struct SStateKey {
  SWinKey key;
  int64_t opNum;
} SStateKey;

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

dengyihao's avatar
dengyihao 已提交
161
typedef struct SStreamValue {
dengyihao's avatar
dengyihao 已提交
162 163
  int64_t unixTimestamp;
  int32_t len;
dengyihao's avatar
dengyihao 已提交
164 165
  char*   data;
} SStreamValue;
dengyihao's avatar
dengyihao 已提交
166

dengyihao's avatar
dengyihao 已提交
167 168 169 170
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);
171 172
#if 0
char* streamStateSessionDump(SStreamState* pState);
5
54liuyao 已提交
173
char* streamStateIntervalDump(SStreamState* pState);
174
#endif
5
54liuyao 已提交
175

176 177 178 179 180
#ifdef __cplusplus
}
#endif

#endif /* ifndef _STREAM_STATE_H_ */