qResultbuf.h 4.2 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 60
  int32_t   numOfRowsPerPage;
  int32_t   numOfPages;
  int64_t   totalBufSize;
H
Haojun Liao 已提交
61
  int64_t   diskFileSize;        // disk file size
H
Haojun Liao 已提交
62
  FILE*     file;
H
Haojun Liao 已提交
63 64
  int32_t   allocateId;          // allocated page id
  char*     path;                // file path
65 66
  int32_t   pageSize;            // current used page size
  int32_t   inMemPages;          // numOfPages that are allocated in memory
H
Haojun Liao 已提交
67
  SHashObj* groupSet;            // id hash table
H
Haojun Liao 已提交
68
  SHashObj* all;
H
Haojun Liao 已提交
69
  SList*    lruList;
H
Haojun Liao 已提交
70
  void*     emptyDummyIdList;    // dummy id list
H
Haojun Liao 已提交
71
  void*     assistBuf;           // assistant buffer for compress/decompress data
H
Haojun Liao 已提交
72
  SArray*   pFree;               // free area in file
H
Haojun Liao 已提交
73
  bool      comp;                // compressed before flushed to disk
H
Haojun Liao 已提交
74
  int32_t   nextPos;             // next page flush position
H
Haojun Liao 已提交
75

H
Haojun Liao 已提交
76
  const void*      handle;        // for debug purpose
H
Haojun Liao 已提交
77
  SResultBufStatis statis;
78
} SDiskbasedResultBuf;
79

H
Haojun Liao 已提交
80
#define DEFAULT_INTERN_BUF_PAGE_SIZE  (4096L)
81
#define DEFAULT_INMEM_BUF_PAGES       10
H
Haojun Liao 已提交
82
#define PAGE_INFO_INITIALIZER         (SPageDiskInfo){-1, -1}
83

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

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

/**
 *
 * @param pResultBuf
 * @return
 */
H
Haojun Liao 已提交
110
size_t getNumOfRowsPerPage(const SDiskbasedResultBuf* pResultBuf);
111 112 113 114 115 116 117

/**
 *
 * @param pResultBuf
 * @param groupId
 * @return
 */
118
SIDList getDataBufPagesIdList(SDiskbasedResultBuf* pResultBuf, int32_t groupId);
119 120 121 122 123 124 125

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

H
Haojun Liao 已提交
128 129 130 131 132
/**
 * release the referenced buf pages
 * @param pResultBuf
 * @param page
 */
H
Haojun Liao 已提交
133 134
void releaseResBufPage(SDiskbasedResultBuf* pResultBuf, void* page);

H
Haojun Liao 已提交
135 136 137
/**
 *
 * @param pResultBuf
H
Haojun Liao 已提交
138
 * @param pi
H
Haojun Liao 已提交
139
 */
H
Haojun Liao 已提交
140 141
void releaseResBufPageInfo(SDiskbasedResultBuf* pResultBuf, SPageInfo* pi);

H
Haojun Liao 已提交
142

143 144 145 146 147
/**
 * get the total buffer size in the format of disk file
 * @param pResultBuf
 * @return
 */
H
Haojun Liao 已提交
148
size_t getResBufSize(const SDiskbasedResultBuf* pResultBuf);
149 150 151 152 153 154

/**
 * get the number of groups in the result buffer
 * @param pResultBuf
 * @return
 */
H
Haojun Liao 已提交
155
size_t getNumOfResultBufGroupId(const SDiskbasedResultBuf* pResultBuf);
156 157 158 159 160

/**
 * destroy result buffer
 * @param pResultBuf
 */
H
Haojun Liao 已提交
161
void destroyResultBuf(SDiskbasedResultBuf* pResultBuf);
162 163 164 165 166 167

/**
 *
 * @param pList
 * @return
 */
H
Haojun Liao 已提交
168
SPageInfo* getLastPageInfo(SIDList pList);
169 170 171 172 173

#ifdef __cplusplus
}
#endif

174
#endif  // TDENGINE_QRESULTBUF_H