tdbPager.c 11.7 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * 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/>.
H
refact  
Hongze Cheng 已提交
14 15
 */

H
Hongze Cheng 已提交
16 17
#include "tdbInt.h"

H
Hongze Cheng 已提交
18
#pragma pack(push, 1)
wafwerar's avatar
wafwerar 已提交
19
typedef struct {
H
Hongze Cheng 已提交
20 21 22 23 24
  u8    hdrString[16];
  u16   pageSize;
  SPgno freePage;
  u32   nFreePages;
  u8    reserved[102];
H
Hongze Cheng 已提交
25
} SFileHdr;
wafwerar's avatar
wafwerar 已提交
26
#pragma pack(pop)
H
Hongze Cheng 已提交
27

H
Hongze Cheng 已提交
28 29
TDB_STATIC_ASSERT(sizeof(SFileHdr) == 128, "Size of file header is not correct");

H
Hongze Cheng 已提交
30 31
#define TDB_PAGE_INITIALIZED(pPage) ((pPage)->pPager != NULL)

H
Hongze Cheng 已提交
32 33
static int tdbPagerInitPage(SPager *pPager, SPage *pPage, int (*initPage)(SPage *, void *, int), void *arg,
                            u8 loadPage);
H
Hongze Cheng 已提交
34 35
static int tdbPagerWritePageToJournal(SPager *pPager, SPage *pPage);
static int tdbPagerWritePageToDB(SPager *pPager, SPage *pPage);
H
Hongze Cheng 已提交
36

H
refact  
Hongze Cheng 已提交
37
int tdbPagerOpen(SPCache *pCache, const char *fileName, SPager **ppPager) {
H
more  
Hongze Cheng 已提交
38
  uint8_t *pPtr;
H
Hongze Cheng 已提交
39
  SPager  *pPager;
H
more  
Hongze Cheng 已提交
40 41
  int      fsize;
  int      zsize;
H
Hongze Cheng 已提交
42
  int      ret;
H
more  
Hongze Cheng 已提交
43

H
refact  
Hongze Cheng 已提交
44
  *ppPager = NULL;
H
more  
Hongze Cheng 已提交
45 46

  fsize = strlen(fileName);
H
refact  
Hongze Cheng 已提交
47
  zsize = sizeof(*pPager)  /* SPager */
H
more  
Hongze Cheng 已提交
48 49
          + fsize + 1      /* dbFileName */
          + fsize + 8 + 1; /* jFileName */
H
Hongze Cheng 已提交
50
  pPtr = (uint8_t *)tdbOsCalloc(1, zsize);
H
more  
Hongze Cheng 已提交
51 52 53 54
  if (pPtr == NULL) {
    return -1;
  }

H
refact  
Hongze Cheng 已提交
55 56 57 58 59 60
  pPager = (SPager *)pPtr;
  pPtr += sizeof(*pPager);
  // pPager->dbFileName
  pPager->dbFileName = (char *)pPtr;
  memcpy(pPager->dbFileName, fileName, fsize);
  pPager->dbFileName[fsize] = '\0';
H
more  
Hongze Cheng 已提交
61
  pPtr += fsize + 1;
H
refact  
Hongze Cheng 已提交
62 63 64 65 66 67 68 69
  // pPager->jFileName
  pPager->jFileName = (char *)pPtr;
  memcpy(pPager->jFileName, fileName, fsize);
  memcpy(pPager->jFileName + fsize, "-journal", 8);
  pPager->jFileName[fsize + 8] = '\0';
  // pPager->pCache
  pPager->pCache = pCache;

H
Hongze Cheng 已提交
70
  pPager->fd = tdbOsOpen(pPager->dbFileName, TDB_O_CREAT | TDB_O_RDWR, 0755);
H
refact  
Hongze Cheng 已提交
71
  if (pPager->fd < 0) {
H
more  
Hongze Cheng 已提交
72 73 74
    return -1;
  }

wafwerar's avatar
wafwerar 已提交
75
  ret = tdbGnrtFileID(pPager->fd, pPager->fid, false);
H
Hongze Cheng 已提交
76 77 78 79
  if (ret < 0) {
    return -1;
  }

H
Hongze Cheng 已提交
80
  // pPager->jfd = -1;
H
Hongze Cheng 已提交
81
  pPager->pageSize = tdbPCacheGetPageSize(pCache);
H
Hongze Cheng 已提交
82 83
  // pPager->dbOrigSize
  ret = tdbGetFileSize(pPager->fd, pPager->pageSize, &(pPager->dbOrigSize));
H
Hongze Cheng 已提交
84
  pPager->dbFileSize = pPager->dbOrigSize;
H
more  
Hongze Cheng 已提交
85

H
refact  
Hongze Cheng 已提交
86
  *ppPager = pPager;
H
more  
Hongze Cheng 已提交
87 88 89
  return 0;
}

H
refact  
Hongze Cheng 已提交
90
int tdbPagerClose(SPager *pPager) {
H
Hongze Cheng 已提交
91 92 93 94 95 96 97
  if (pPager) {
    if (pPager->inTran) {
      tdbOsClose(pPager->jfd);
    }
    tdbOsClose(pPager->fd);
    tdbOsFree(pPager);
  }
H
more  
Hongze Cheng 已提交
98 99 100
  return 0;
}

101
int tdbPagerOpenDB(SPager *pPager, SPgno *ppgno, bool toCreate, SBTree *pBt) {
H
Hongze Cheng 已提交
102 103 104 105
  SPgno  pgno;
  SPage *pPage;
  int    ret;

H
Hongze Cheng 已提交
106 107 108
  if (pPager->dbOrigSize > 0) {
    pgno = 1;
  } else {
H
Hongze Cheng 已提交
109 110 111
    pgno = 0;
  }

H
Hongze Cheng 已提交
112
  {
113 114
    // TODO: try to search the main DB to get the page number
    // pgno = 0;
H
Hongze Cheng 已提交
115 116
  }

117 118 119 120
  if (pgno == 0 && toCreate) {
    // allocate a new child page
    TXN txn;
    tdbTxnOpen(&txn, 0, tdbDefaultMalloc, tdbDefaultFree, NULL, 0);
H
Hongze Cheng 已提交
121

122
    pPager->inTran = 1;
H
refact  
Hongze Cheng 已提交
123

124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
    SBtreeInitPageArg zArg;
    zArg.flags = 0x1 | 0x2;  // root leaf node;
    zArg.pBt = pBt;
    ret = tdbPagerFetchPage(pPager, &pgno, &pPage, tdbBtreeInitPage, &zArg, &txn);
    if (ret < 0) {
      return -1;
    }

    //    ret = tdbPagerAllocPage(pPager, &pPage, &pgno);
    // if (ret < 0) {
    //  return -1;
    //}

    // TODO: Need to zero the page

    ret = tdbPagerWrite(pPager, pPage);
    if (ret < 0) {
      return -1;
    }
H
Hongze Cheng 已提交
143

144 145 146 147
    tdbTxnClose(&txn);
  }

  *ppgno = pgno;
H
Hongze Cheng 已提交
148 149 150
  return 0;
}

H
refact  
Hongze Cheng 已提交
151
int tdbPagerWrite(SPager *pPager, SPage *pPage) {
H
Hongze Cheng 已提交
152 153
  int     ret;
  SPage **ppPage;
H
more  
Hongze Cheng 已提交
154

H
Hongze Cheng 已提交
155 156
  ASSERT(pPager->inTran);
#if 0
H
refact  
Hongze Cheng 已提交
157 158
  if (pPager->inTran == 0) {
    ret = tdbPagerBegin(pPager);
H
more  
Hongze Cheng 已提交
159 160 161 162
    if (ret < 0) {
      return -1;
    }
  }
H
Hongze Cheng 已提交
163
#endif
H
more  
Hongze Cheng 已提交
164

H
Hongze Cheng 已提交
165
  if (pPage->isDirty) return 0;
H
more  
Hongze Cheng 已提交
166

H
Hongze Cheng 已提交
167
  // ref page one more time so the page will not be release
H
Hongze Cheng 已提交
168
  tdbRefPage(pPage);
H
Hongze Cheng 已提交
169

H
Hongze Cheng 已提交
170 171 172
  // Set page as dirty
  pPage->isDirty = 1;

H
Hongze Cheng 已提交
173
  // Add page to dirty list(TODO: NOT use O(n^2) algorithm)
H
Hongze Cheng 已提交
174 175 176
  for (ppPage = &pPager->pDirty; (*ppPage) && TDB_PAGE_PGNO(*ppPage) < TDB_PAGE_PGNO(pPage);
       ppPage = &((*ppPage)->pDirtyNext)) {
  }
177 178 179 180 181 182 183

  if (*ppPage && TDB_PAGE_PGNO(*ppPage) == TDB_PAGE_PGNO(pPage)) {
    tdbUnrefPage(pPage);

    return 0;
  }

H
Hongze Cheng 已提交
184 185 186
  ASSERT(*ppPage == NULL || TDB_PAGE_PGNO(*ppPage) > TDB_PAGE_PGNO(pPage));
  pPage->pDirtyNext = *ppPage;
  *ppPage = pPage;
H
Hongze Cheng 已提交
187

H
Hongze Cheng 已提交
188
  // Write page to journal if neccessary
H
Hongze Cheng 已提交
189 190 191 192 193
  if (TDB_PAGE_PGNO(pPage) <= pPager->dbOrigSize) {
    ret = tdbPagerWritePageToJournal(pPager, pPage);
    if (ret < 0) {
      ASSERT(0);
      return -1;
H
Hongze Cheng 已提交
194
    }
H
more  
Hongze Cheng 已提交
195
  }
H
Hongze Cheng 已提交
196

H
Hongze Cheng 已提交
197 198 199
  return 0;
}

H
Hongze Cheng 已提交
200
int tdbPagerBegin(SPager *pPager, TXN *pTxn) {
H
refact  
Hongze Cheng 已提交
201
  if (pPager->inTran) {
H
more  
Hongze Cheng 已提交
202 203
    return 0;
  }
H
Hongze Cheng 已提交
204 205

  // Open the journal
H
Hongze Cheng 已提交
206
  pPager->jfd = tdbOsOpen(pPager->jFileName, TDB_O_CREAT | TDB_O_RDWR, 0755);
H
refact  
Hongze Cheng 已提交
207
  if (pPager->jfd < 0) {
H
Hongze Cheng 已提交
208 209 210 211 212
    return -1;
  }

  // TODO: write the size of the file

H
refact  
Hongze Cheng 已提交
213
  pPager->inTran = 1;
H
Hongze Cheng 已提交
214

H
more  
Hongze Cheng 已提交
215 216 217
  return 0;
}

H
Hongze Cheng 已提交
218
int tdbPagerCommit(SPager *pPager, TXN *pTxn) {
H
Hongze Cheng 已提交
219 220 221
  SPage *pPage;
  int    ret;

H
Hongze Cheng 已提交
222 223 224 225 226 227
  // sync the journal file
  ret = tdbOsFSync(pPager->jfd);
  if (ret < 0) {
    // TODO
    ASSERT(0);
    return 0;
H
Hongze Cheng 已提交
228 229
  }

H
Hongze Cheng 已提交
230 231
  // loop to write the dirty pages to file
  for (pPage = pPager->pDirty; pPage; pPage = pPage->pDirtyNext) {
H
Hongze Cheng 已提交
232
    // TODO: update the page footer
H
Hongze Cheng 已提交
233 234 235 236 237 238 239
    ret = tdbPagerWritePageToDB(pPager, pPage);
    if (ret < 0) {
      ASSERT(0);
      return -1;
    }
  }

240
  tdbTrace("tdbttl commit:%p, %d", pPager, pPager->dbOrigSize);
H
Hongze Cheng 已提交
241 242
  pPager->dbOrigSize = pPager->dbFileSize;

H
Hongze Cheng 已提交
243
  // release the page
H
Hongze Cheng 已提交
244
  for (pPage = pPager->pDirty; pPage; pPage = pPager->pDirty) {
H
Hongze Cheng 已提交
245 246 247 248 249
    pPager->pDirty = pPage->pDirtyNext;
    pPage->pDirtyNext = NULL;

    pPage->isDirty = 0;

H
Hongze Cheng 已提交
250
    tdbPCacheRelease(pPager->pCache, pPage, pTxn);
H
Hongze Cheng 已提交
251
  }
H
Hongze Cheng 已提交
252 253

  // sync the db file
H
Hongze Cheng 已提交
254
  tdbOsFSync(pPager->fd);
H
Hongze Cheng 已提交
255

H
Hongze Cheng 已提交
256
  // remote the journal file
H
Hongze Cheng 已提交
257
  tdbOsClose(pPager->jfd);
H
Hongze Cheng 已提交
258
  tdbOsRemove(pPager->jFileName);
H
Hongze Cheng 已提交
259
  pPager->inTran = 0;
H
Hongze Cheng 已提交
260

H
more  
Hongze Cheng 已提交
261 262 263
  return 0;
}

H
Hongze Cheng 已提交
264
int tdbPagerFetchPage(SPager *pPager, SPgno *ppgno, SPage **ppPage, int (*initPage)(SPage *, void *, int), void *arg,
H
Hongze Cheng 已提交
265
                      TXN *pTxn) {
H
more  
Hongze Cheng 已提交
266 267 268
  SPage *pPage;
  SPgid  pgid;
  int    ret;
H
Hongze Cheng 已提交
269 270
  SPgno  pgno;
  u8     loadPage;
H
more  
Hongze Cheng 已提交
271

H
Hongze Cheng 已提交
272 273
  pgno = *ppgno;
  loadPage = 1;
H
more  
Hongze Cheng 已提交
274

H
Hongze Cheng 已提交
275 276 277 278
  // alloc new page
  if (pgno == 0) {
    loadPage = 0;
    ret = tdbPagerAllocPage(pPager, &pgno);
H
Hongze Cheng 已提交
279
    if (ret < 0) {
H
Hongze Cheng 已提交
280
      ASSERT(0);
H
Hongze Cheng 已提交
281
      return -1;
H
more  
Hongze Cheng 已提交
282
    }
H
more  
Hongze Cheng 已提交
283 284
  }

H
Hongze Cheng 已提交
285
  ASSERT(pgno > 0);
H
Hongze Cheng 已提交
286

H
Hongze Cheng 已提交
287
  // fetch a page container
H
more  
Hongze Cheng 已提交
288
  memcpy(&pgid, pPager->fid, TDB_FILE_ID_LEN);
H
Hongze Cheng 已提交
289
  pgid.pgno = pgno;
H
Hongze Cheng 已提交
290
  pPage = tdbPCacheFetch(pPager->pCache, &pgid, pTxn);
H
more  
Hongze Cheng 已提交
291
  if (pPage == NULL) {
H
Hongze Cheng 已提交
292
    ASSERT(0);
H
more  
Hongze Cheng 已提交
293 294 295
    return -1;
  }

296
  tdbTrace("tdbttl fetch pager:%p", pPage->pPager);
H
Hongze Cheng 已提交
297 298 299 300
  // init page if need
  if (!TDB_PAGE_INITIALIZED(pPage)) {
    ret = tdbPagerInitPage(pPager, pPage, initPage, arg, loadPage);
    if (ret < 0) {
H
Hongze Cheng 已提交
301
      ASSERT(0);
H
Hongze Cheng 已提交
302 303
      return -1;
    }
H
Hongze Cheng 已提交
304
  }
H
more  
Hongze Cheng 已提交
305

H
Hongze Cheng 已提交
306 307 308
  // printf("thread %" PRId64 " pager fetch page %d pgno %d ppage %p\n", taosGetSelfPthreadId(), pPage->id,
  //        TDB_PAGE_PGNO(pPage), pPage);

H
Hongze Cheng 已提交
309 310
  ASSERT(TDB_PAGE_INITIALIZED(pPage));
  ASSERT(pPage->pPager == pPager);
H
more  
Hongze Cheng 已提交
311

H
Hongze Cheng 已提交
312
  *ppgno = pgno;
H
more  
Hongze Cheng 已提交
313 314 315 316
  *ppPage = pPage;
  return 0;
}

H
Hongze Cheng 已提交
317 318 319 320 321
void tdbPagerReturnPage(SPager *pPager, SPage *pPage, TXN *pTxn) {
  tdbPCacheRelease(pPager->pCache, pPage, pTxn);
  // printf("thread %" PRId64 " pager retun page %d pgno %d ppage %p\n", taosGetSelfPthreadId(), pPage->id,
  //        TDB_PAGE_PGNO(pPage), pPage);
}
H
Hongze Cheng 已提交
322

H
more  
Hongze Cheng 已提交
323 324 325 326 327 328 329 330 331 332
static int tdbPagerAllocFreePage(SPager *pPager, SPgno *ppgno) {
  // TODO: Allocate a page from the free list
  return 0;
}

static int tdbPagerAllocNewPage(SPager *pPager, SPgno *ppgno) {
  *ppgno = ++pPager->dbFileSize;
  return 0;
}

H
Hongze Cheng 已提交
333
int tdbPagerAllocPage(SPager *pPager, SPgno *ppgno) {
H
more  
Hongze Cheng 已提交
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
  int ret;

  *ppgno = 0;

  // Try to allocate from the free list of the pager
  ret = tdbPagerAllocFreePage(pPager, ppgno);
  if (ret < 0) {
    return -1;
  }

  if (*ppgno != 0) return 0;

  // Allocate the page by extending the pager
  ret = tdbPagerAllocNewPage(pPager, ppgno);
  if (ret < 0) {
    return -1;
  }

  ASSERT(*ppgno != 0);

H
Hongze Cheng 已提交
354 355 356
  return 0;
}

H
Hongze Cheng 已提交
357 358 359 360 361 362 363 364
static int tdbPagerInitPage(SPager *pPager, SPage *pPage, int (*initPage)(SPage *, void *, int), void *arg,
                            u8 loadPage) {
  int   ret;
  int   lcode;
  int   nLoops;
  i64   nRead;
  SPgno pgno;
  int   init = 0;
H
Hongze Cheng 已提交
365

H
Hongze Cheng 已提交
366 367
  lcode = TDB_TRY_LOCK_PAGE(pPage);
  if (lcode == P_LOCK_SUCC) {
H
Hongze Cheng 已提交
368 369 370 371 372
    if (TDB_PAGE_INITIALIZED(pPage)) {
      TDB_UNLOCK_PAGE(pPage);
      return 0;
    }

H
Hongze Cheng 已提交
373 374
    pgno = TDB_PAGE_PGNO(pPage);

375
    tdbTrace("tdbttl init pager:%p, pgno:%d, loadPage:%d, size:%d", pPager, pgno, loadPage, pPager->dbOrigSize);
H
Hongze Cheng 已提交
376 377 378
    if (loadPage && pgno <= pPager->dbOrigSize) {
      init = 1;

H
Hongze Cheng 已提交
379
      nRead = tdbOsPRead(pPager->fd, pPage->pData, pPage->pageSize, ((i64)pPage->pageSize) * (pgno - 1));
S
Shengliang Guan 已提交
380
      tdbTrace("tdbttl pager:%p, pgno:%d, nRead:%" PRId64, pPager, pgno, nRead);
H
Hongze Cheng 已提交
381
      if (nRead < pPage->pageSize) {
H
Hongze Cheng 已提交
382 383 384
        ASSERT(0);
        return -1;
      }
H
Hongze Cheng 已提交
385 386
    } else {
      init = 0;
H
Hongze Cheng 已提交
387 388
    }

H
Hongze Cheng 已提交
389
    ret = (*initPage)(pPage, arg, init);
H
Hongze Cheng 已提交
390
    if (ret < 0) {
H
Hongze Cheng 已提交
391
      ASSERT(0);
H
Hongze Cheng 已提交
392 393 394 395 396 397 398
      TDB_UNLOCK_PAGE(pPage);
      return -1;
    }

    pPage->pPager = pPager;

    TDB_UNLOCK_PAGE(pPage);
H
Hongze Cheng 已提交
399
  } else if (lcode == P_LOCK_BUSY) {
H
Hongze Cheng 已提交
400 401 402 403 404 405 406 407 408
    nLoops = 0;
    for (;;) {
      if (TDB_PAGE_INITIALIZED(pPage)) break;
      nLoops++;
      if (nLoops > 1000) {
        sched_yield();
        nLoops = 0;
      }
    }
H
Hongze Cheng 已提交
409
  } else {
H
Hongze Cheng 已提交
410
    ASSERT(0);
H
Hongze Cheng 已提交
411
    return -1;
H
Hongze Cheng 已提交
412 413
  }

H
Hongze Cheng 已提交
414 415 416 417 418 419 420 421 422 423
  return 0;
}

// ---------------------------- Journal manipulation
static int tdbPagerWritePageToJournal(SPager *pPager, SPage *pPage) {
  int   ret;
  SPgno pgno;

  pgno = TDB_PAGE_PGNO(pPage);

H
Hongze Cheng 已提交
424
  ret = tdbOsWrite(pPager->jfd, &pgno, sizeof(pgno));
H
Hongze Cheng 已提交
425 426 427 428
  if (ret < 0) {
    return -1;
  }

H
Hongze Cheng 已提交
429
  ret = tdbOsWrite(pPager->jfd, pPage->pData, pPage->pageSize);
H
Hongze Cheng 已提交
430 431 432 433 434 435 436 437 438 439 440
  if (ret < 0) {
    return -1;
  }

  return 0;
}

static int tdbPagerWritePageToDB(SPager *pPager, SPage *pPage) {
  i64 offset;
  int ret;

H
Hongze Cheng 已提交
441
  offset = pPage->pageSize * (TDB_PAGE_PGNO(pPage) - 1);
H
Hongze Cheng 已提交
442
  if (tdbOsLSeek(pPager->fd, offset, SEEK_SET) < 0) {
H
Hongze Cheng 已提交
443 444 445 446
    ASSERT(0);
    return -1;
  }

H
Hongze Cheng 已提交
447
  ret = tdbOsWrite(pPager->fd, pPage->pData, pPage->pageSize);
H
Hongze Cheng 已提交
448 449 450 451 452
  if (ret < 0) {
    ASSERT(0);
    return -1;
  }

H
refact  
Hongze Cheng 已提交
453
  return 0;
454 455 456
}

int tdbPagerRestore(SPager *pPager, SBTree *pBt) {
457
  int   ret = 0;
458
  SPgno journalSize = 0;
459
  u8   *pageBuf = NULL;
460 461

  tdb_fd_t jfd = tdbOsOpen(pPager->jFileName, TDB_O_RDWR, 0755);
462
  if (jfd == NULL) {
463 464 465 466 467 468 469 470 471 472 473 474 475 476
    return 0;
  }

  ret = tdbGetFileSize(jfd, pPager->pageSize, &journalSize);
  if (ret < 0) {
    return -1;
  }

  pageBuf = tdbOsCalloc(1, pPager->pageSize);
  if (pageBuf == NULL) {
    return -1;
  }

  TXN txn;
M
Minglei Jin 已提交
477
  tdbTxnOpen(&txn, 0, tdbDefaultMalloc, tdbDefaultFree, NULL, TDB_TXN_WRITE | TDB_TXN_READ_UNCOMMITTED);
478 479 480 481 482 483
  SBtreeInitPageArg iArg;
  iArg.pBt = pBt;
  iArg.flags = 0;

  for (int pgIndex = 0; pgIndex < journalSize; ++pgIndex) {
    // read pgno & the page from journal
484
    SPgno  pgno;
485 486 487 488 489 490 491 492 493 494 495 496
    SPage *pPage;

    int ret = tdbOsRead(jfd, &pgno, sizeof(pgno));
    if (ret < 0) {
      return -1;
    }

    ret = tdbOsRead(jfd, pageBuf, pPager->pageSize);
    if (ret < 0) {
      return -1;
    }

M
Minglei Jin 已提交
497
    /*
498 499 500 501 502 503 504 505 506 507 508 509
    ret = tdbPagerFetchPage(pPager, &pgno, &pPage, tdbBtreeInitPage, &iArg, &txn);
    if (ret < 0) {
      return -1;
    }

    // write the page to db
    ret = tdbPagerWritePageToDB(pPager, pPage);
    if (ret < 0) {
      return -1;
    }

    tdbPCacheRelease(pPager->pCache, pPage, &txn);
M
Minglei Jin 已提交
510 511 512 513 514 515 516 517 518 519 520 521
    */
    i64 offset = pPager->pageSize * (pgno - 1);
    if (tdbOsLSeek(pPager->fd, offset, SEEK_SET) < 0) {
      ASSERT(0);
      return -1;
    }

    ret = tdbOsWrite(pPager->fd, pageBuf, pPager->pageSize);
    if (ret < 0) {
      ASSERT(0);
      return -1;
    }
522 523 524 525 526 527 528 529 530 531 532 533 534
  }

  tdbOsFSync(pPager->fd);

  tdbTxnClose(&txn);

  tdbOsFree(pageBuf);

  tdbOsClose(jfd);
  tdbOsRemove(pPager->jFileName);

  return 0;
}