tdbPgFile.c 3.5 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 19 20 21 22 23 24 25 26
typedef struct SPage1 {
  char     magic[64];
  pgno_t   mdbRootPgno;  // master DB root page number
  pgno_t   freePgno;     // free list page number
  uint32_t nFree;        // number of free pages
} SPage1;

TDB_STATIC_ASSERT(sizeof(SPage1) <= TDB_MIN_PGSIZE, "TDB Page1 definition too large");

H
Hongze Cheng 已提交
27 28
static int pgFileRead(SPgFile *pPgFile, pgno_t pgno, uint8_t *pData);

H
Hongze Cheng 已提交
29 30 31
int pgFileOpen(SPgFile **ppPgFile, const char *fname, TENV *pEnv) {
  SPgFile * pPgFile;
  SPgCache *pPgCache;
H
Hongze Cheng 已提交
32
  size_t    fnameLen;
H
Hongze Cheng 已提交
33 34 35

  *ppPgFile = NULL;

H
Hongze Cheng 已提交
36 37 38
  // create the handle
  fnameLen = strlen(fname);
  pPgFile = (SPgFile *)calloc(1, sizeof(*pPgFile) + fnameLen + 1);
H
Hongze Cheng 已提交
39 40 41 42
  if (pPgFile == NULL) {
    return -1;
  }

H
Hongze Cheng 已提交
43
  ASSERT(pEnv != NULL);
H
Hongze Cheng 已提交
44

H
Hongze Cheng 已提交
45 46 47 48 49
  // init the handle
  pPgFile->fname = (char *)(&(pPgFile[1]));
  memcpy(pPgFile->fname, fname, fnameLen);
  pPgFile->fname[fnameLen] = '\0';
  pPgFile->fd = -1;
H
Hongze Cheng 已提交
50

H
Hongze Cheng 已提交
51
  pPgFile->fd = open(fname, O_CREAT | O_RDWR, 0755);
H
Hongze Cheng 已提交
52
  if (pPgFile->fd < 0) {
H
Hongze Cheng 已提交
53
    // TODO: handle error
H
Hongze Cheng 已提交
54 55 56
    return -1;
  }

H
Hongze Cheng 已提交
57
  tdbGnrtFileID(fname, pPgFile->fileid, false);
H
Hongze Cheng 已提交
58

H
Hongze Cheng 已提交
59 60 61 62 63
  /* TODO: other open operations */

  // add the page file to the environment
  tdbEnvRgstPageFile(pEnv, pPgFile);
  pPgFile->pEnv = pEnv;
H
Hongze Cheng 已提交
64 65

  *ppPgFile = pPgFile;
H
Hongze Cheng 已提交
66 67 68 69
  return 0;
}

int pgFileClose(SPgFile *pPgFile) {
H
Hongze Cheng 已提交
70 71 72 73 74 75 76 77 78
  if (pPgFile) {
    if (pPgFile->fd >= 0) {
      close(pPgFile->fd);
    }

    tfree(pPgFile->fname);
    free(pPgFile);
  }

H
Hongze Cheng 已提交
79 80 81 82
  return 0;
}

SPage *pgFileFetch(SPgFile *pPgFile, pgno_t pgno) {
H
more  
Hongze Cheng 已提交
83 84 85 86
  SPgCache *pPgCache;
  SPage *   pPage;
  pgid_t    pgid;

H
Hongze Cheng 已提交
87
#if 0
H
more  
Hongze Cheng 已提交
88 89 90 91 92
  pPgCache = pPgFile->pPgCache;
  pPage = NULL;
  memcpy(pgid.fileid, pPgFile->fileid, TDB_FILE_ID_LEN);
  pgid.pgno = pgno;

H
Hongze Cheng 已提交
93 94 95 96
  if (pgno > pPgFile->pgFileSize) {
    // TODO
  } else {
    pPage = pgCacheFetch(pPgCache, pgid);
H
Hongze Cheng 已提交
97 98 99
    if (1 /*Page is cached, no need to load from file*/) {
      return pPage;
    } else {
H
Hongze Cheng 已提交
100 101
      // TODO: handle error
      if (pgFileRead(pPgFile, pgno, (void *)pPage) < 0) {
H
Hongze Cheng 已提交
102 103 104 105
        // todoerr
      }
      return pPage;
    }
H
Hongze Cheng 已提交
106
  }
H
Hongze Cheng 已提交
107
#endif
H
more  
Hongze Cheng 已提交
108 109

  return pPage;
H
Hongze Cheng 已提交
110 111 112
}

int pgFileRelease(SPage *pPage) {
H
more  
Hongze Cheng 已提交
113
  pgCacheRelease(pPage);
H
Hongze Cheng 已提交
114 115 116 117 118 119
  return 0;
}

int pgFileWrite(SPage *pPage) {
  // TODO
  return 0;
H
Hongze Cheng 已提交
120 121
}

H
Hongze Cheng 已提交
122 123 124 125 126 127
int pgFileAllocatePage(SPgFile *pPgFile, pgno_t *pPgno) {
  pgno_t pgno;

  if (0) {
    // TODO: allocate from the free list
  } else {
H
Hongze Cheng 已提交
128
    pgno = ++(pPgFile->lsize);
H
Hongze Cheng 已提交
129 130 131 132 133 134
  }

  *pPgno = pgno;
  return 0;
}

H
Hongze Cheng 已提交
135
static int pgFileRead(SPgFile *pPgFile, pgno_t pgno, uint8_t *pData) {
H
Hongze Cheng 已提交
136
  pgsz_t   pgSize;
H
Hongze Cheng 已提交
137 138 139 140
  ssize_t  rsize;
  uint8_t *pTData;
  size_t   szToRead;

H
Hongze Cheng 已提交
141 142
#if 0

H
Hongze Cheng 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
  // pgSize = ; (TODO)
  pTData = pData;
  szToRead = pgSize;
  for (; szToRead > 0;) {
    rsize = pread(pPgFile->fd, pTData, szToRead, pgno * pgSize);
    if (rsize < 0) {
      if (errno == EINTR) {
        continue;
      } else {
        return -1;
      }
    } else if (rsize == 0) {
      return -1;
    }

    szToRead -= rsize;
    pTData += rsize;
  }
H
Hongze Cheng 已提交
161
#endif
H
Hongze Cheng 已提交
162

H
Hongze Cheng 已提交
163
  return 0;
H
Hongze Cheng 已提交
164
}