tpagedbuf.c 19.1 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) {
51 52 53
  // pBuf->file = fopen(pBuf->path, "wb+");
  pBuf->pFile = taosOpenFile(pBuf->path, TD_FILE_CTEATE | TD_FILE_WRITE | TD_FILE_READ | TD_FILE_TRUNC);
  if (pBuf->pFile == NULL) {
S
Shengliang Guan 已提交
54
    //    qError("failed to create tmp file: %s on disk. %s", pBuf->path, strerror(errno));
55 56 57 58 59 60
    return TAOS_SYSTEM_ERROR(errno);
  }

  return TSDB_CODE_SUCCESS;
}

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

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

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

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

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

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

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

        return offset;
      }
    }

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

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

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

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

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

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

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

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

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

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

H
Haojun Liao 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168
      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;
      }
169

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

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

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

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

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

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

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

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

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

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

  return p;
222 223 224
}

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

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

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

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

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

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

  return pa;
}

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

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

H
Haojun Liao 已提交
267
  pBuf->numOfPages += 1;
268

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

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

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

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

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

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

299 300 301 302 303 304 305 306 307
  //  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 已提交
308

309 310 311
  return pn;
}

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

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

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

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

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

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

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

  return bufPage;
}

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

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

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

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

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

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

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

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

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

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

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

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

398 399 400
  //  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 已提交
401 402

  return TSDB_CODE_SUCCESS;
403 404
}

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

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

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

H
Haojun Liao 已提交
419 420 421
  SPageInfo* pi = NULL;
  if (listNEles(pBuf->freePgList) != 0) {
    SListNode* pItem = tdListPopHead(pBuf->freePgList);
422
    pi = *(SPageInfo**)pItem->data;
H
Haojun Liao 已提交
423
    pi->used = true;
424
    *pageId = pi->pageId;
wafwerar's avatar
wafwerar 已提交
425
    taosMemoryFreeClear(pItem);
426
  } else {  // create a new pageinfo
H
Haojun Liao 已提交
427 428 429 430 431 432 433 434 435 436
    // 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;
  }
437 438

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

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

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

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

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

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

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

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

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

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

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

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

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

497 498 499 500 501 502
    // 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 已提交
503 504
    }

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

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

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

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

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

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

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

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

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

H
Haojun Liao 已提交
542
  dBufPrintStatis(pBuf);
H
Haojun Liao 已提交
543

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

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

H
Haojun Liao 已提交
556 557
  // print the statistics information
  {
S
Shengliang Guan 已提交
558 559 560 561 562 563
    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 已提交
564
  }
565

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

623
  ppi->used = false;
H
Haojun Liao 已提交
624 625
  ppi->dirty = false;

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

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

634
void dBufSetPrintInfo(SDiskbasedBuf* pBuf) { pBuf->printStatis = true; }
635

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

H
Haojun Liao 已提交
638
void dBufPrintStatis(const SDiskbasedBuf* pBuf) {
H
Haojun Liao 已提交
639 640 641 642 643 644 645 646
  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 已提交
647
      "Kb, %s\n",
H
Haojun Liao 已提交
648
      pBuf->totalBufSize / 1024.0, pBuf->numOfPages, listNEles(pBuf->lruList) * pBuf->pageSize / 1024.0,
H
Haojun Liao 已提交
649
      listNEles(pBuf->lruList), pBuf->fileSize / 1024.0, pBuf->pageSize / 1024.0f, pBuf->id);
H
Haojun Liao 已提交
650 651 652 653 654

  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));
655
}