qResultbuf.h 4.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

16 17
#ifndef TDENGINE_QRESULTBUF_H
#define TDENGINE_QRESULTBUF_H
18 19 20 21 22

#ifdef __cplusplus
extern "C" {
#endif

H
Haojun Liao 已提交
23
#include <tlist.h>
H
Haojun Liao 已提交
24
#include "hash.h"
H
Haojun Liao 已提交
25 26
#include "os.h"
#include "qExtbuffer.h"
H
Haojun Liao 已提交
27
#include "tlockfree.h"
28

H
Haojun Liao 已提交
29
typedef struct SArray* SIDList;
30

H
Haojun Liao 已提交
31
typedef struct SPageDiskInfo {
32
  int32_t offset;
H
Haojun Liao 已提交
33 34 35 36
  int32_t length;
} SPageDiskInfo;

typedef struct SPageInfo {
H
Haojun Liao 已提交
37
  SListNode*    pn;       // point to list node
H
Haojun Liao 已提交
38 39 40
  int32_t       pageId;
  SPageDiskInfo info;
  void*         pData;
H
Haojun Liao 已提交
41
  bool          used;     // set current page is in used
42 43
} SPageInfo;

H
Haojun Liao 已提交
44 45 46 47 48
typedef struct SFreeListItem {
  int32_t offset;
  int32_t len;
} SFreeListItem;

H
Haojun Liao 已提交
49 50 51 52 53 54 55 56
typedef struct SResultBufStatis {
  int32_t flushBytes;
  int32_t loadBytes;
  int32_t getPages;
  int32_t releasePages;
  int32_t flushPages;
} SResultBufStatis;

57
typedef struct SDiskbasedResultBuf {
H
Haojun Liao 已提交
58 59
  int32_t   numOfPages;
  int64_t   totalBufSize;
H
Haojun Liao 已提交
60
  int64_t   fileSize;            // disk file size
H
Haojun Liao 已提交
61
  FILE*     file;
H
Haojun Liao 已提交
62 63
  int32_t   allocateId;          // allocated page id
  char*     path;                // file path
64 65
  int32_t   pageSize;            // current used page size
  int32_t   inMemPages;          // numOfPages that are allocated in memory
H
Haojun Liao 已提交
66
  SHashObj* groupSet;            // id hash table
H
Haojun Liao 已提交
67
  SHashObj* all;
H
Haojun Liao 已提交
68
  SList*    lruList;
H
Haojun Liao 已提交
69
  void*     emptyDummyIdList;    // dummy id list
H
Haojun Liao 已提交
70
  void*     assistBuf;           // assistant buffer for compress/decompress data
H
Haojun Liao 已提交
71
  SArray*   pFree;               // free area in file
H
Haojun Liao 已提交
72
  bool      comp;                // compressed before flushed to disk
H
Haojun Liao 已提交
73
  int32_t   nextPos;             // next page flush position
H
Haojun Liao 已提交
74

H
Haojun Liao 已提交
75
  uint64_t  qId;                 // for debug purpose
H
Haojun Liao 已提交
76
  SResultBufStatis statis;
77
} SDiskbasedResultBuf;
78

H
Haojun Liao 已提交
79
#define DEFAULT_INTERN_BUF_PAGE_SIZE  (1024L)                          // in bytes
H
Haojun Liao 已提交
80
#define PAGE_INFO_INITIALIZER         (SPageDiskInfo){-1, -1}
81

82 83 84 85
/**
 * create disk-based result buffer
 * @param pResultBuf
 * @param rowSize
H
Haojun Liao 已提交
86 87 88
 * @param pagesize
 * @param inMemPages
 * @param handle
89 90
 * @return
 */
H
Haojun Liao 已提交
91
int32_t createDiskbasedResultBuffer(SDiskbasedResultBuf** pResultBuf, int32_t pagesize, int32_t inMemBufSize, uint64_t qId);
92 93 94 95 96 97 98 99

/**
 *
 * @param pResultBuf
 * @param groupId
 * @param pageId
 * @return
 */
100
tFilePage* getNewDataBuf(SDiskbasedResultBuf* pResultBuf, int32_t groupId, int32_t* pageId);
101 102 103 104 105 106 107

/**
 *
 * @param pResultBuf
 * @param groupId
 * @return
 */
108
SIDList getDataBufPagesIdList(SDiskbasedResultBuf* pResultBuf, int32_t groupId);
109 110 111 112 113 114 115

/**
 * get the specified buffer page by id
 * @param pResultBuf
 * @param id
 * @return
 */
H
Haojun Liao 已提交
116 117
tFilePage* getResBufPage(SDiskbasedResultBuf* pResultBuf, int32_t id);

H
Haojun Liao 已提交
118 119 120 121 122
/**
 * release the referenced buf pages
 * @param pResultBuf
 * @param page
 */
H
Haojun Liao 已提交
123 124
void releaseResBufPage(SDiskbasedResultBuf* pResultBuf, void* page);

H
Haojun Liao 已提交
125 126 127
/**
 *
 * @param pResultBuf
H
Haojun Liao 已提交
128
 * @param pi
H
Haojun Liao 已提交
129
 */
H
Haojun Liao 已提交
130 131
void releaseResBufPageInfo(SDiskbasedResultBuf* pResultBuf, SPageInfo* pi);

H
Haojun Liao 已提交
132

133 134 135 136 137
/**
 * get the total buffer size in the format of disk file
 * @param pResultBuf
 * @return
 */
H
Haojun Liao 已提交
138
size_t getResBufSize(const SDiskbasedResultBuf* pResultBuf);
139 140 141 142 143 144

/**
 * get the number of groups in the result buffer
 * @param pResultBuf
 * @return
 */
H
Haojun Liao 已提交
145
size_t getNumOfResultBufGroupId(const SDiskbasedResultBuf* pResultBuf);
146 147 148 149 150

/**
 * destroy result buffer
 * @param pResultBuf
 */
H
Haojun Liao 已提交
151
void destroyResultBuf(SDiskbasedResultBuf* pResultBuf);
152 153 154 155 156 157

/**
 *
 * @param pList
 * @return
 */
H
Haojun Liao 已提交
158
SPageInfo* getLastPageInfo(SIDList pList);
159 160 161 162 163

#ifdef __cplusplus
}
#endif

164
#endif  // TDENGINE_QRESULTBUF_H