qResultbuf.h 3.5 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 "hash.h"
H
Haojun Liao 已提交
24 25
#include "os.h"
#include "qExtbuffer.h"
26

H
Haojun Liao 已提交
27
typedef struct SArray* SIDList;
28

29 30 31 32 33 34
typedef struct SPageInfo {
  int32_t pageId;
  int32_t offset;
  int32_t lengthOnDisk;
} SPageInfo;

35
typedef struct SDiskbasedResultBuf {
H
Haojun Liao 已提交
36 37 38
  int32_t   numOfRowsPerPage;
  int32_t   numOfPages;
  int64_t   totalBufSize;
39 40
  int32_t   fd;
//  FILE*     file;
H
Haojun Liao 已提交
41 42
  int32_t   allocateId;          // allocated page id
  int32_t   incStep;             // minimum allocated pages
43
  void*     pBuf;                // mmap buffer pointer
H
Haojun Liao 已提交
44
  char*     path;                // file path
45 46
  int32_t   pageSize;            // current used page size
  int32_t   inMemPages;          // numOfPages that are allocated in memory
H
Haojun Liao 已提交
47 48
  SHashObj* idsTable;            // id hash table
  SIDList   list;                // for each id, there is a page id list
49 50 51

  void*     iBuf;                // inmemory buf
  void*     handle;              // for debug purpose
H
Haojun Liao 已提交
52
  void*     emptyDummyIdList;    // dummy id list
53 54
  bool      comp;

55
} SDiskbasedResultBuf;
56

57 58
#define DEFAULT_INTERN_BUF_PAGE_SIZE (1024L)
#define DEFAULT_INMEM_BUF_PAGES       10
59

60 61 62 63 64 65 66
/**
 * create disk-based result buffer
 * @param pResultBuf
 * @param size
 * @param rowSize
 * @return
 */
67
int32_t createDiskbasedResultBuffer(SDiskbasedResultBuf** pResultBuf, int32_t numOfPages, int32_t rowSize, int32_t pagesize,
68
    int32_t inMemPages, const void* handle);
69 70 71 72 73 74 75 76

/**
 *
 * @param pResultBuf
 * @param groupId
 * @param pageId
 * @return
 */
77
tFilePage* getNewDataBuf(SDiskbasedResultBuf* pResultBuf, int32_t groupId, int32_t* pageId);
78 79 80 81 82 83

/**
 *
 * @param pResultBuf
 * @return
 */
84
int32_t getNumOfRowsPerPage(SDiskbasedResultBuf* pResultBuf);
85 86 87 88 89 90 91

/**
 *
 * @param pResultBuf
 * @param groupId
 * @return
 */
92
SIDList getDataBufPagesIdList(SDiskbasedResultBuf* pResultBuf, int32_t groupId);
93 94 95 96 97 98 99

/**
 * get the specified buffer page by id
 * @param pResultBuf
 * @param id
 * @return
 */
100 101
static FORCE_INLINE tFilePage* getResBufPage(SDiskbasedResultBuf* pResultBuf, int32_t id) {
  if (id < pResultBuf->inMemPages) {
H
Haojun Liao 已提交
102
    return (tFilePage*) ((char*) pResultBuf->iBuf + id * pResultBuf->pageSize);
103
  } else {
H
Haojun Liao 已提交
104
    return (tFilePage*) ((char*) pResultBuf->pBuf + (id - pResultBuf->inMemPages) * pResultBuf->pageSize);
105 106
  }
}
107 108 109 110 111
/**
 * get the total buffer size in the format of disk file
 * @param pResultBuf
 * @return
 */
112
int32_t getResBufSize(SDiskbasedResultBuf* pResultBuf);
113 114 115 116 117 118

/**
 * get the number of groups in the result buffer
 * @param pResultBuf
 * @return
 */
119
int32_t getNumOfResultBufGroupId(SDiskbasedResultBuf* pResultBuf);
120 121 122 123 124

/**
 * destroy result buffer
 * @param pResultBuf
 */
H
hjxilinx 已提交
125
void destroyResultBuf(SDiskbasedResultBuf* pResultBuf, void* handle);
126 127 128 129 130 131

/**
 *
 * @param pList
 * @return
 */
H
Haojun Liao 已提交
132
int32_t getLastPageId(SIDList pList);
133 134 135 136 137

#ifdef __cplusplus
}
#endif

138
#endif  // TDENGINE_QRESULTBUF_H