tpagedbuf.c 19.0 KB
Newer Older
S
Shengliang Guan 已提交
1
#define _DEFAULT_SOURCE
H
Haojun Liao 已提交
2
#include "tpagedbuf.h"
3 4
#include "taoserror.h"
#include "tcompression.h"
H
Haojun Liao 已提交
5
#include "thash.h"
S
Shengliang Guan 已提交
6
#include "tlog.h"
7

S
Shengliang Guan 已提交
8
#define GET_DATA_PAYLOAD(_p)          ((char*)(_p)->pData + POINTER_BYTES)
9 10
#define NO_IN_MEM_AVAILABLE_PAGES(_b) (listNEles((_b)->lruList) >= (_b)->inMemPages)

11
typedef struct SPageDiskInfo {
S
Shengliang Guan 已提交
12 13
  int64_t offset;
  int32_t length;
H
Haojun Liao 已提交
14
} SPageDiskInfo, SFreeListItem;
15

H
Haojun Liao 已提交
16
struct SPageInfo {
17
  SListNode* pn;  // point to list node struct
S
Shengliang Guan 已提交
18 19 20
  void*      pData;
  int64_t    offset;
  int32_t    pageId;
21
  int32_t    length : 29;
S
Shengliang Guan 已提交
22 23
  bool       used : 1;   // set current page is in used
  bool       dirty : 1;  // set current buffer page is dirty or not
H
Haojun Liao 已提交
24
};
25

H
Haojun Liao 已提交
26
struct SDiskbasedBuf {
27 28
  int32_t   numOfPages;
  int64_t   totalBufSize;
S
Shengliang Guan 已提交
29
  uint64_t  fileSize;  // disk file size
30
  TdFilePtr pFile;
S
Shengliang Guan 已提交
31 32 33 34
  int32_t   allocateId;  // allocated page id
  char*     path;        // file path
  int32_t   pageSize;    // current used page size
  int32_t   inMemPages;  // numOfPages that are allocated in memory
35 36
  SList*    freePgList;  // free page list
  SHashObj* groupSet;    // id hash table, todo remove it
37 38
  SHashObj* all;
  SList*    lruList;
S
Shengliang Guan 已提交
39 40 41 42 43 44
  void*     emptyDummyIdList;  // dummy id list
  void*     assistBuf;         // assistant buffer for compress/decompress data
  SArray*   pFree;             // free area in file
  bool      comp;              // compressed before flushed to disk
  uint64_t  nextPos;           // next page flush position

H
Haojun Liao 已提交
45 46
  char*     id;          // for debug purpose
  bool      printStatis;  // Print statistics info when closing this buffer.
H
Haojun Liao 已提交
47
  SDiskbasedBufStatis statis;
H
Haojun Liao 已提交
48
};
49

H
Haojun Liao 已提交
50
static int32_t createDiskFile(SDiskbasedBuf* pBuf) {
H
Haojun Liao 已提交
51
  pBuf->pFile = taosOpenFile(pBuf->path, TD_FILE_CTEATE | TD_FILE_WRITE | TD_FILE_READ | TD_FILE_TRUNC | TD_FILE_AUTO_DEL);
52
  if (pBuf->pFile == NULL) {
53 54 55 56 57 58
    return TAOS_SYSTEM_ERROR(errno);
  }

  return TSDB_CODE_SUCCESS;
}

S
Shengliang Guan 已提交
59
static char* doCompressData(void* data, int32_t srcSize, int32_t* dst, SDiskbasedBuf* pBuf) {  // do nothing
H
Haojun Liao 已提交
60
  if (!pBuf->comp) {
61 62 63 64
    *dst = srcSize;
    return data;
  }

H
Haojun Liao 已提交
65
  *dst = tsCompressString(data, srcSize, 1, pBuf->assistBuf, srcSize, ONE_STAGE_COMP, NULL, 0);
66

H
Haojun Liao 已提交
67
  memcpy(data, pBuf->assistBuf, *dst);
68 69 70
  return data;
}

S
Shengliang Guan 已提交
71
static char* doDecompressData(void* data, int32_t srcSize, int32_t* dst, SDiskbasedBuf* pBuf) {  // do nothing
H
Haojun Liao 已提交
72
  if (!pBuf->comp) {
73 74 75 76
    *dst = srcSize;
    return data;
  }

H
Haojun Liao 已提交
77
  *dst = tsDecompressString(data, srcSize, 1, pBuf->assistBuf, pBuf->pageSize, ONE_STAGE_COMP, NULL, 0);
78
  if (*dst > 0) {
H
Haojun Liao 已提交
79
    memcpy(data, pBuf->assistBuf, *dst);
80 81 82 83
  }
  return data;
}

H
Haojun Liao 已提交
84 85 86
static uint64_t allocatePositionInFile(SDiskbasedBuf* pBuf, size_t size) {
  if (pBuf->pFree == NULL) {
    return pBuf->nextPos;
87 88 89
  } else {
    int32_t offset = -1;

H
Haojun Liao 已提交
90
    size_t num = taosArrayGetSize(pBuf->pFree);
S
Shengliang Guan 已提交
91
    for (int32_t i = 0; i < num; ++i) {
H
Haojun Liao 已提交
92
      SFreeListItem* pi = taosArrayGet(pBuf->pFree, i);
H
Haojun Liao 已提交
93
      if (pi->length >= size) {
94 95
        offset = pi->offset;
        pi->offset += (int32_t)size;
H
Haojun Liao 已提交
96
        pi->length -= (int32_t)size;
97 98 99 100 101 102

        return offset;
      }
    }

    // no available recycle space, allocate new area in file
H
Haojun Liao 已提交
103
    return pBuf->nextPos;
104 105 106
  }
}

107
static void setPageNotInBuf(SPageInfo* pPageInfo) { pPageInfo->pData = NULL; }
H
Haojun Liao 已提交
108

109
static FORCE_INLINE size_t getAllocPageSize(int32_t pageSize) { return pageSize + POINTER_BYTES + 2; }
H
Haojun Liao 已提交
110

H
Haojun Liao 已提交
111 112 113 114 115 116 117 118
/**
 *   +--------------------------+-------------------+--------------+
 *   | PTR to SPageInfo (8bytes)| Payload (PageSize)| 2 Extra Bytes|
 *   +--------------------------+-------------------+--------------+
 * @param pBuf
 * @param pg
 * @return
 */
H
Haojun Liao 已提交
119
static char* doFlushPageToDisk(SDiskbasedBuf* pBuf, SPageInfo* pg) {
120 121
  assert(!pg->used && pg->pData != NULL);

H
Haojun Liao 已提交
122
  int32_t size = pBuf->pageSize;
H
Haojun Liao 已提交
123 124
  char*   t = NULL;
  if (pg->offset == -1 || pg->dirty) {
H
Haojun Liao 已提交
125 126 127
    void* payload = GET_DATA_PAYLOAD(pg);
    t = doCompressData(payload, pBuf->pageSize, &size, pBuf);
    assert(size >= 0);
H
Haojun Liao 已提交
128
  }
129 130

  // this page is flushed to disk for the first time
H
Haojun Liao 已提交
131 132 133
  if (pg->dirty) {
    if (pg->offset == -1) {
      assert(pg->dirty == true);
H
Haojun Liao 已提交
134

H
Haojun Liao 已提交
135 136
      pg->offset = allocatePositionInFile(pBuf, size);
      pBuf->nextPos += size;
137

138 139 140 141 142
      int32_t ret = taosLSeekFile(pBuf->pFile, pg->offset, SEEK_SET);
      if (ret != 0) {
        terrno = TAOS_SYSTEM_ERROR(errno);
        return NULL;
      }
143

144 145 146 147 148
      ret = (int32_t)taosWriteFile(pBuf->pFile, t, size);
      if (ret != size) {
        terrno = TAOS_SYSTEM_ERROR(errno);
        return NULL;
      }
149

H
Haojun Liao 已提交
150 151 152
      if (pBuf->fileSize < pg->offset + size) {
        pBuf->fileSize = pg->offset + size;
      }
H
Haojun Liao 已提交
153

H
Haojun Liao 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166
      pBuf->statis.flushBytes += size;
      pBuf->statis.flushPages += 1;
    } else {
      // length becomes greater, current space is not enough, allocate new place, otherwise, do nothing
      if (pg->length < size) {
        // 1. add current space to free list
        SPageDiskInfo dinfo = {.length = pg->length, .offset = pg->offset};
        taosArrayPush(pBuf->pFree, &dinfo);

        // 2. allocate new position, and update the info
        pg->offset = allocatePositionInFile(pBuf, size);
        pBuf->nextPos += size;
      }
167

168 169 170 171 172 173
      // 3. write to disk.
      int32_t ret = taosLSeekFile(pBuf->pFile, pg->offset, SEEK_SET);
      if (ret != 0) {
        terrno = TAOS_SYSTEM_ERROR(errno);
        return NULL;
      }
174

175 176 177 178 179
      ret = (int32_t)taosWriteFile(pBuf->pFile, t, size);
      if (ret != size) {
        terrno = TAOS_SYSTEM_ERROR(errno);
        return NULL;
      }
180

H
Haojun Liao 已提交
181 182 183
      if (pBuf->fileSize < pg->offset + size) {
        pBuf->fileSize = pg->offset + size;
      }
184

H
Haojun Liao 已提交
185 186
      pBuf->statis.flushBytes += size;
      pBuf->statis.flushPages += 1;
187
    }
188
  } else {  // NOTE: the size may be -1, the this recycle page has not been flushed to disk yet.
H
Haojun Liao 已提交
189
    size = pg->length;
H
Haojun Liao 已提交
190 191
    if (size == -1) {
      printf("----\n");
192 193 194
    }
  }

H
Haojun Liao 已提交
195
  ASSERT(size > 0 || (pg->offset == -1 && pg->length == -1));
196

H
Haojun Liao 已提交
197
  char* pDataBuf = pg->pData;
H
Haojun Liao 已提交
198
  memset(pDataBuf, 0, getAllocPageSize(pBuf->pageSize));
199

H
Haojun Liao 已提交
200
  pg->length = size;  // on disk size
H
Haojun Liao 已提交
201
  return pDataBuf;
202 203
}

H
Haojun Liao 已提交
204
static char* flushPageToDisk(SDiskbasedBuf* pBuf, SPageInfo* pg) {
205
  int32_t ret = TSDB_CODE_SUCCESS;
S
Shengliang Guan 已提交
206
  assert(((int64_t)pBuf->numOfPages * pBuf->pageSize) == pBuf->totalBufSize && pBuf->numOfPages >= pBuf->inMemPages);
207

208
  if (pBuf->pFile == NULL) {
H
Haojun Liao 已提交
209
    if ((ret = createDiskFile(pBuf)) != TSDB_CODE_SUCCESS) {
210 211 212 213 214
      terrno = ret;
      return NULL;
    }
  }

H
Haojun Liao 已提交
215 216 217 218 219
  char* p = doFlushPageToDisk(pBuf, pg);
  setPageNotInBuf(pg);
  pg->dirty = false;

  return p;
220 221 222
}

// load file block data in disk
H
Haojun Liao 已提交
223
static int32_t loadPageFromDisk(SDiskbasedBuf* pBuf, SPageInfo* pg) {
224
  int32_t ret = taosLSeekFile(pBuf->pFile, pg->offset, SEEK_SET);
H
Haojun Liao 已提交
225 226 227 228 229
  if (ret != 0) {
    ret = TAOS_SYSTEM_ERROR(errno);
    return ret;
  }

230
  void* pPage = (void*)GET_DATA_PAYLOAD(pg);
H
Haojun Liao 已提交
231
  ret = (int32_t)taosReadFile(pBuf->pFile, pPage, pg->length);
232
  if (ret != pg->length) {
H
Haojun Liao 已提交
233 234
    ret = TAOS_SYSTEM_ERROR(errno);
    return ret;
235 236
  }

H
Haojun Liao 已提交
237 238
  pBuf->statis.loadBytes += pg->length;
  pBuf->statis.loadPages += 1;
239 240

  int32_t fullSize = 0;
H
Haojun Liao 已提交
241
  doDecompressData(pPage, pg->length, &fullSize, pBuf);
H
Haojun Liao 已提交
242
  return 0;
243 244
}

H
Haojun Liao 已提交
245
static SIDList addNewGroup(SDiskbasedBuf* pBuf, int32_t groupId) {
S
Shengliang Guan 已提交
246
  assert(taosHashGet(pBuf->groupSet, (const char*)&groupId, sizeof(int32_t)) == NULL);
247 248

  SArray* pa = taosArrayInit(1, POINTER_BYTES);
H
Haojun Liao 已提交
249
  int32_t ret = taosHashPut(pBuf->groupSet, (const char*)&groupId, sizeof(int32_t), &pa, POINTER_BYTES);
250 251 252 253 254
  assert(ret == 0);

  return pa;
}

H
Haojun Liao 已提交
255
static SPageInfo* registerPage(SDiskbasedBuf* pBuf, int32_t groupId, int32_t pageId) {
256 257
  SIDList list = NULL;

H
Haojun Liao 已提交
258
  char** p = taosHashGet(pBuf->groupSet, (const char*)&groupId, sizeof(int32_t));
259
  if (p == NULL) {  // it is a new group id
H
Haojun Liao 已提交
260
    list = addNewGroup(pBuf, groupId);
261
  } else {
S
Shengliang Guan 已提交
262
    list = (SIDList)(*p);
263 264
  }

H
Haojun Liao 已提交
265
  pBuf->numOfPages += 1;
266

wafwerar's avatar
wafwerar 已提交
267
  SPageInfo* ppi = taosMemoryMalloc(sizeof(SPageInfo));
268 269

  ppi->pageId = pageId;
270
  ppi->pData  = NULL;
271 272
  ppi->offset = -1;
  ppi->length = -1;
273 274 275
  ppi->used   = true;
  ppi->pn     = NULL;
  ppi->dirty  = false;
276

S
Shengliang Guan 已提交
277
  return *(SPageInfo**)taosArrayPush(list, &ppi);
278 279
}

H
Haojun Liao 已提交
280
static SListNode* getEldestUnrefedPage(SDiskbasedBuf* pBuf) {
281
  SListIter iter = {0};
H
Haojun Liao 已提交
282
  tdListInitIter(pBuf->lruList, &iter, TD_LIST_BACKWARD);
283 284

  SListNode* pn = NULL;
S
Shengliang Guan 已提交
285 286
  while ((pn = tdListNext(&iter)) != NULL) {
    SPageInfo* pageInfo = *(SPageInfo**)pn->data;
287 288 289
    assert(pageInfo->pageId >= 0 && pageInfo->pn == pn);

    if (!pageInfo->used) {
290
      //      printf("%d is chosen\n", pageInfo->pageId);
291
      break;
H
Haojun Liao 已提交
292
    } else {
293
      //      printf("page %d is used, dirty:%d\n", pageInfo->pageId, pageInfo->dirty);
294 295 296
    }
  }

297 298 299 300 301 302 303 304 305
  //  int32_t pos = listNEles(pBuf->lruList);
  //  SListIter iter1 = {0};
  //  tdListInitIter(pBuf->lruList, &iter1, TD_LIST_BACKWARD);
  //  SListNode* pn1 = NULL;
  //  while((pn1 = tdListNext(&iter1)) != NULL) {
  //    SPageInfo* pageInfo = *(SPageInfo**) pn1->data;
  //    printf("page %d is used, dirty:%d, pos:%d\n", pageInfo->pageId, pageInfo->dirty, pos - 1);
  //    pos -= 1;
  //  }
H
Haojun Liao 已提交
306

307 308 309
  return pn;
}

H
Haojun Liao 已提交
310
static char* evacOneDataPage(SDiskbasedBuf* pBuf) {
S
Shengliang Guan 已提交
311
  char*      bufPage = NULL;
H
Haojun Liao 已提交
312
  SListNode* pn = getEldestUnrefedPage(pBuf);
313 314 315

  // all pages are referenced by user, try to allocate new space
  if (pn == NULL) {
316
    assert(0);
H
Haojun Liao 已提交
317
    int32_t prev = pBuf->inMemPages;
318 319

    // increase by 50% of previous mem pages
H
Haojun Liao 已提交
320
    pBuf->inMemPages = (int32_t)(pBuf->inMemPages * 1.5f);
321

S
Shengliang Guan 已提交
322 323
    //    qWarn("%p in memory buf page not sufficient, expand from %d to %d, page size:%d", pBuf, prev,
    //          pBuf->inMemPages, pBuf->pageSize);
324
  } else {
H
Haojun Liao 已提交
325
    tdListPopNode(pBuf->lruList, pn);
326

S
Shengliang Guan 已提交
327
    SPageInfo* d = *(SPageInfo**)pn->data;
328 329 330
    assert(d->pn == pn);

    d->pn = NULL;
wafwerar's avatar
wafwerar 已提交
331
    taosMemoryFreeClear(pn);
332

H
Haojun Liao 已提交
333
    bufPage = flushPageToDisk(pBuf, d);
334 335 336 337 338
  }

  return bufPage;
}

S
Shengliang Guan 已提交
339
static void lruListPushFront(SList* pList, SPageInfo* pi) {
340 341 342 343 344
  tdListPrepend(pList, &pi);
  SListNode* front = tdListGetHead(pList);
  pi->pn = front;
}

S
Shengliang Guan 已提交
345
static void lruListMoveToFront(SList* pList, SPageInfo* pi) {
346 347 348 349
  tdListPopNode(pList, pi->pn);
  tdListPrependNode(pList, pi->pn);
}

H
Haojun Liao 已提交
350 351
static SPageInfo* getPageInfoFromPayload(void* page) {
  int32_t offset = offsetof(SPageInfo, pData);
352
  char*   p = (char *)page - offset;
H
Haojun Liao 已提交
353

354
  SPageInfo* ppi = ((SPageInfo**)p)[0];
H
Haojun Liao 已提交
355
  return ppi;
356 357
}

H
Haojun Liao 已提交
358
int32_t createDiskbasedBuf(SDiskbasedBuf** pBuf, int32_t pagesize, int32_t inMemBufSize, const char* id,
359
                           const char* dir) {
wafwerar's avatar
wafwerar 已提交
360
  *pBuf = taosMemoryCalloc(1, sizeof(SDiskbasedBuf));
H
Haojun Liao 已提交
361

H
Haojun Liao 已提交
362 363
  SDiskbasedBuf* pPBuf = *pBuf;
  if (pPBuf == NULL) {
H
Haojun Liao 已提交
364 365 366
    return TSDB_CODE_OUT_OF_MEMORY;
  }

367
  pPBuf->pageSize = pagesize;
H
Haojun Liao 已提交
368
  pPBuf->numOfPages   = 0;  // all pages are in buffer in the first place
H
Haojun Liao 已提交
369
  pPBuf->totalBufSize = 0;
370 371
  pPBuf->inMemPages = inMemBufSize / pagesize;  // maximum allowed pages, it is a soft limit.
  pPBuf->allocateId = -1;
H
Haojun Liao 已提交
372 373 374
  pPBuf->comp     = true;
  pPBuf->pFile    = NULL;
  pPBuf->id       = strdup(id);
375 376 377
  pPBuf->fileSize = 0;
  pPBuf->pFree = taosArrayInit(4, sizeof(SFreeListItem));
  pPBuf->freePgList = tdListNew(POINTER_BYTES);
H
Haojun Liao 已提交
378 379 380 381

  // at least more than 2 pages must be in memory
  assert(inMemBufSize >= pagesize * 2);

H
Haojun Liao 已提交
382
  pPBuf->lruList = tdListNew(POINTER_BYTES);
H
Haojun Liao 已提交
383 384 385

  // init id hash table
  _hash_fn_t fn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT);
386
  pPBuf->groupSet = taosHashInit(10, fn, true, false);
wafwerar's avatar
wafwerar 已提交
387
  pPBuf->assistBuf = taosMemoryMalloc(pPBuf->pageSize + 2);  // EXTRA BYTES
H
Haojun Liao 已提交
388
  pPBuf->all = taosHashInit(10, fn, true, false);
H
Haojun Liao 已提交
389 390 391

  char path[PATH_MAX] = {0};
  taosGetTmpfilePath(dir, "paged-buf", path);
H
Haojun Liao 已提交
392
  pPBuf->path = strdup(path);
H
Haojun Liao 已提交
393

H
Haojun Liao 已提交
394
  pPBuf->emptyDummyIdList = taosArrayInit(1, sizeof(int32_t));
H
Haojun Liao 已提交
395

396 397 398
  //  qDebug("QInfo:0x%"PRIx64" create resBuf for output, page size:%d, inmem buf pages:%d, file:%s", qId,
  //  pPBuf->pageSize,
  //         pPBuf->inMemPages, pPBuf->path);
H
Haojun Liao 已提交
399 400

  return TSDB_CODE_SUCCESS;
401 402
}

H
Haojun Liao 已提交
403
void* getNewBufPage(SDiskbasedBuf* pBuf, int32_t groupId, int32_t* pageId) {
H
Haojun Liao 已提交
404
  pBuf->statis.getPages += 1;
405 406

  char* availablePage = NULL;
H
Haojun Liao 已提交
407 408
  if (NO_IN_MEM_AVAILABLE_PAGES(pBuf)) {
    availablePage = evacOneDataPage(pBuf);
409

410 411
    // Failed to allocate a new buffer page, and there is an error occurs.
    if (availablePage == NULL) {
H
Haojun Liao 已提交
412
      assert(0);
413 414
      return NULL;
    }
H
Haojun Liao 已提交
415 416
  }

H
Haojun Liao 已提交
417 418 419
  SPageInfo* pi = NULL;
  if (listNEles(pBuf->freePgList) != 0) {
    SListNode* pItem = tdListPopHead(pBuf->freePgList);
420
    pi = *(SPageInfo**)pItem->data;
H
Haojun Liao 已提交
421
    pi->used = true;
422
    *pageId = pi->pageId;
wafwerar's avatar
wafwerar 已提交
423
    taosMemoryFreeClear(pItem);
424
  } else {  // create a new pageinfo
H
Haojun Liao 已提交
425 426 427 428 429 430 431 432 433 434
    // register new id in this group
    *pageId = (++pBuf->allocateId);

    // register page id info
    pi = registerPage(pBuf, groupId, *pageId);

    // add to hash map
    taosHashPut(pBuf->all, pageId, sizeof(int32_t), &pi, POINTER_BYTES);
    pBuf->totalBufSize += pBuf->pageSize;
  }
435 436

  // add to LRU list
H
Haojun Liao 已提交
437 438
  assert(listNEles(pBuf->lruList) < pBuf->inMemPages && pBuf->inMemPages > 0);
  lruListPushFront(pBuf->lruList, pi);
439 440 441

  // allocate buf
  if (availablePage == NULL) {
wafwerar's avatar
wafwerar 已提交
442
    pi->pData = taosMemoryCalloc(1, getAllocPageSize(pBuf->pageSize));  // add extract bytes in case of zipped buffer increased.
443 444 445 446 447
  } else {
    pi->pData = availablePage;
  }

  ((void**)pi->pData)[0] = pi;
S
Shengliang Guan 已提交
448
  return (void*)(GET_DATA_PAYLOAD(pi));
449 450
}

H
Haojun Liao 已提交
451
void* getBufPage(SDiskbasedBuf* pBuf, int32_t id) {
H
Haojun Liao 已提交
452 453
  assert(pBuf != NULL && id >= 0);
  pBuf->statis.getPages += 1;
454

H
Haojun Liao 已提交
455
  SPageInfo** pi = taosHashGet(pBuf->all, &id, sizeof(int32_t));
456 457
  assert(pi != NULL && *pi != NULL);

S
Shengliang Guan 已提交
458
  if ((*pi)->pData != NULL) {  // it is in memory
459
    // no need to update the LRU list if only one page exists
H
Haojun Liao 已提交
460
    if (pBuf->numOfPages == 1) {
461
      (*pi)->used = true;
S
Shengliang Guan 已提交
462
      return (void*)(GET_DATA_PAYLOAD(*pi));
463 464
    }

S
Shengliang Guan 已提交
465
    SPageInfo** pInfo = (SPageInfo**)((*pi)->pn->data);
466 467
    assert(*pInfo == *pi);

H
Haojun Liao 已提交
468
    lruListMoveToFront(pBuf->lruList, (*pi));
469 470
    (*pi)->used = true;

S
Shengliang Guan 已提交
471 472
    return (void*)(GET_DATA_PAYLOAD(*pi));
  } else {  // not in memory
473
    assert((*pi)->pData == NULL && (*pi)->pn == NULL && (((*pi)->length >= 0 && (*pi)->offset >= 0) || ((*pi)->length == -1 && (*pi)->offset == -1)));
474 475

    char* availablePage = NULL;
H
Haojun Liao 已提交
476 477
    if (NO_IN_MEM_AVAILABLE_PAGES(pBuf)) {
      availablePage = evacOneDataPage(pBuf);
478 479 480
      if (availablePage == NULL) {
        return NULL;
      }
481 482 483
    }

    if (availablePage == NULL) {
wafwerar's avatar
wafwerar 已提交
484
      (*pi)->pData = taosMemoryCalloc(1, getAllocPageSize(pBuf->pageSize));
485 486 487 488
    } else {
      (*pi)->pData = availablePage;
    }

H
Haojun Liao 已提交
489
    // set the ptr to the new SPageInfo
490 491
    ((void**)((*pi)->pData))[0] = (*pi);

H
Haojun Liao 已提交
492
    lruListPushFront(pBuf->lruList, *pi);
493 494
    (*pi)->used = true;

495 496 497 498 499 500
    // some data has been flushed to disk, and needs to be loaded into buffer again.
    if ((*pi)->length > 0 && (*pi)->offset >= 0) {
      int32_t code = loadPageFromDisk(pBuf, *pi);
      if (code != 0) {
        return NULL;
      }
H
Haojun Liao 已提交
501 502
    }

S
Shengliang Guan 已提交
503
    return (void*)(GET_DATA_PAYLOAD(*pi));
504 505 506
  }
}

H
Haojun Liao 已提交
507 508
void releaseBufPage(SDiskbasedBuf* pBuf, void* page) {
  assert(pBuf != NULL && page != NULL);
H
Haojun Liao 已提交
509
  SPageInfo* ppi = getPageInfoFromPayload(page);
H
Haojun Liao 已提交
510
  releaseBufPageInfo(pBuf, ppi);
511 512
}

H
Haojun Liao 已提交
513
void releaseBufPageInfo(SDiskbasedBuf* pBuf, SPageInfo* pi) {
H
Haojun Liao 已提交
514
  assert(pi->pData != NULL && pi->used == true);
515 516

  pi->used = false;
H
Haojun Liao 已提交
517
  pBuf->statis.releasePages += 1;
518 519
}

H
Haojun Liao 已提交
520
size_t getNumOfBufGroupId(const SDiskbasedBuf* pBuf) { return taosHashGetSize(pBuf->groupSet); }
521

H
Haojun Liao 已提交
522
size_t getTotalBufSize(const SDiskbasedBuf* pBuf) { return (size_t)pBuf->totalBufSize; }
523

H
Haojun Liao 已提交
524 525
SIDList getDataBufPagesIdList(SDiskbasedBuf* pBuf, int32_t groupId) {
  assert(pBuf != NULL);
526

H
Haojun Liao 已提交
527
  char** p = taosHashGet(pBuf->groupSet, (const char*)&groupId, sizeof(int32_t));
528
  if (p == NULL) {  // it is a new group id
H
Haojun Liao 已提交
529
    return pBuf->emptyDummyIdList;
530
  } else {
S
Shengliang Guan 已提交
531
    return (SArray*)(*p);
532 533 534
  }
}

H
Haojun Liao 已提交
535
void destroyDiskbasedBuf(SDiskbasedBuf* pBuf) {
H
Haojun Liao 已提交
536
  if (pBuf == NULL) {
537 538 539
    return;
  }

H
Haojun Liao 已提交
540
  dBufPrintStatis(pBuf);
H
Haojun Liao 已提交
541

542
  if (pBuf->pFile != NULL) {
S
Shengliang Guan 已提交
543 544
    uDebug(
        "Paged buffer closed, total:%.2f Kb (%d Pages), inmem size:%.2f Kb (%d Pages), file size:%.2f Kb, page "
H
Haojun Liao 已提交
545
        "size:%.2f Kb, %s\n",
S
Shengliang Guan 已提交
546
        pBuf->totalBufSize / 1024.0, pBuf->numOfPages, listNEles(pBuf->lruList) * pBuf->pageSize / 1024.0,
H
Haojun Liao 已提交
547
        listNEles(pBuf->lruList), pBuf->fileSize / 1024.0, pBuf->pageSize / 1024.0f, pBuf->id);
548

S
Shengliang Guan 已提交
549
    taosCloseFile(&pBuf->pFile);
550
  } else {
H
Haojun Liao 已提交
551
    uDebug("Paged buffer closed, total:%.2f Kb, no file created, %s", pBuf->totalBufSize / 1024.0, pBuf->id);
552 553
  }

H
Haojun Liao 已提交
554 555
  // print the statistics information
  {
S
Shengliang Guan 已提交
556 557 558 559 560 561
    SDiskbasedBufStatis* ps = &pBuf->statis;
    uDebug(
        "Get/Release pages:%d/%d, flushToDisk:%.2f Kb (%d Pages), loadFromDisk:%.2f Kb (%d Pages), avgPageSize:%.2f "
        "Kb\n",
        ps->getPages, ps->releasePages, ps->flushBytes / 1024.0f, ps->flushPages, ps->loadBytes / 1024.0f,
        ps->loadPages, ps->loadBytes / (1024.0 * ps->loadPages));
H
Haojun Liao 已提交
562
  }
563

564
  taosRemoveFile(pBuf->path);
wafwerar's avatar
wafwerar 已提交
565
  taosMemoryFreeClear(pBuf->path);
H
Haojun Liao 已提交
566 567

  SArray** p = taosHashIterate(pBuf->groupSet, NULL);
S
Shengliang Guan 已提交
568
  while (p) {
569
    size_t n = taosArrayGetSize(*p);
S
Shengliang Guan 已提交
570
    for (int32_t i = 0; i < n; ++i) {
571
      SPageInfo* pi = taosArrayGetP(*p, i);
wafwerar's avatar
wafwerar 已提交
572 573
      taosMemoryFreeClear(pi->pData);
      taosMemoryFreeClear(pi);
574 575 576
    }

    taosArrayDestroy(*p);
H
Haojun Liao 已提交
577
    p = taosHashIterate(pBuf->groupSet, p);
578 579
  }

H
Haojun Liao 已提交
580
  tdListFree(pBuf->lruList);
H
Haojun Liao 已提交
581 582
  tdListFree(pBuf->freePgList);

H
Haojun Liao 已提交
583
  taosArrayDestroy(pBuf->emptyDummyIdList);
H
Haojun Liao 已提交
584 585
  taosArrayDestroy(pBuf->pFree);

H
Haojun Liao 已提交
586 587
  taosHashCleanup(pBuf->groupSet);
  taosHashCleanup(pBuf->all);
588

wafwerar's avatar
wafwerar 已提交
589 590 591
  taosMemoryFreeClear(pBuf->id);
  taosMemoryFreeClear(pBuf->assistBuf);
  taosMemoryFreeClear(pBuf);
592 593
}

594
SPageInfo* getLastPageInfo(SIDList pList) {
S
Shengliang Guan 已提交
595
  size_t     size = taosArrayGetSize(pList);
596 597 598 599
  SPageInfo* pPgInfo = taosArrayGetP(pList, size - 1);
  return pPgInfo;
}

600
int32_t getPageId(const SPageInfo* pPgInfo) {
601 602 603 604
  ASSERT(pPgInfo != NULL);
  return pPgInfo->pageId;
}

S
Shengliang Guan 已提交
605
int32_t getBufPageSize(const SDiskbasedBuf* pBuf) { return pBuf->pageSize; }
H
Haojun Liao 已提交
606

S
Shengliang Guan 已提交
607
int32_t getNumOfInMemBufPages(const SDiskbasedBuf* pBuf) { return pBuf->inMemPages; }
H
Haojun Liao 已提交
608

S
Shengliang Guan 已提交
609
bool isAllDataInMemBuf(const SDiskbasedBuf* pBuf) { return pBuf->fileSize == 0; }
H
Haojun Liao 已提交
610

H
Haojun Liao 已提交
611
void setBufPageDirty(void* pPage, bool dirty) {
H
Haojun Liao 已提交
612
  SPageInfo* ppi = getPageInfoFromPayload(pPage);
H
Haojun Liao 已提交
613
  ppi->dirty = dirty;
614 615
}

616
void setBufPageCompressOnDisk(SDiskbasedBuf* pBuf, bool comp) { pBuf->comp = comp; }
H
Haojun Liao 已提交
617

618
void dBufSetBufPageRecycled(SDiskbasedBuf* pBuf, void* pPage) {
H
Haojun Liao 已提交
619 620
  SPageInfo* ppi = getPageInfoFromPayload(pPage);

621
  ppi->used = false;
H
Haojun Liao 已提交
622 623
  ppi->dirty = false;

H
Haojun Liao 已提交
624 625
  // add this pageinfo into the free page info list
  SListNode* pNode = tdListPopNode(pBuf->lruList, ppi->pn);
wafwerar's avatar
wafwerar 已提交
626 627
  taosMemoryFreeClear(ppi->pData);
  taosMemoryFreeClear(pNode);
H
Haojun Liao 已提交
628 629

  tdListAppend(pBuf->freePgList, &ppi);
H
Haojun Liao 已提交
630
}
H
Haojun Liao 已提交
631

632
void dBufSetPrintInfo(SDiskbasedBuf* pBuf) { pBuf->printStatis = true; }
633

S
Shengliang Guan 已提交
634
SDiskbasedBufStatis getDBufStatis(const SDiskbasedBuf* pBuf) { return pBuf->statis; }
H
Haojun Liao 已提交
635

H
Haojun Liao 已提交
636
void dBufPrintStatis(const SDiskbasedBuf* pBuf) {
H
Haojun Liao 已提交
637 638 639 640 641 642 643 644
  if (!pBuf->printStatis) {
    return;
  }

  const SDiskbasedBufStatis* ps = &pBuf->statis;

  printf(
      "Paged buffer closed, total:%.2f Kb (%d Pages), inmem size:%.2f Kb (%d Pages), file size:%.2f Kb, page size:%.2f "
H
Haojun Liao 已提交
645
      "Kb, %s\n",
H
Haojun Liao 已提交
646
      pBuf->totalBufSize / 1024.0, pBuf->numOfPages, listNEles(pBuf->lruList) * pBuf->pageSize / 1024.0,
H
Haojun Liao 已提交
647
      listNEles(pBuf->lruList), pBuf->fileSize / 1024.0, pBuf->pageSize / 1024.0f, pBuf->id);
H
Haojun Liao 已提交
648 649 650 651 652

  printf(
      "Get/Release pages:%d/%d, flushToDisk:%.2f Kb (%d Pages), loadFromDisk:%.2f Kb (%d Pages), avgPageSize:%.2f Kb\n",
      ps->getPages, ps->releasePages, ps->flushBytes / 1024.0f, ps->flushPages, ps->loadBytes / 1024.0f, ps->loadPages,
      ps->loadBytes / (1024.0 * ps->loadPages));
653
}