tdbPgFile.c 4.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 19
typedef struct SPage1 {
  char     magic[64];
H
more  
Hongze Cheng 已提交
20 21
  SPgno   mdbRootPgno;  // master DB root page number
  SPgno   freePgno;     // free list page number
H
Hongze Cheng 已提交
22 23 24
  uint32_t nFree;        // number of free pages
} SPage1;

H
Hongze Cheng 已提交
25 26 27 28
typedef struct SFreePage {
  /* TODO */
} SFreePage;

H
Hongze Cheng 已提交
29 30
TDB_STATIC_ASSERT(sizeof(SPage1) <= TDB_MIN_PGSIZE, "TDB Page1 definition too large");

H
more  
Hongze Cheng 已提交
31
static int pgFileRead(SPgFile *pPgFile, SPgno pgno, uint8_t *pData);
H
Hongze Cheng 已提交
32

H
Hongze Cheng 已提交
33 34 35
int pgFileOpen(SPgFile **ppPgFile, const char *fname, TENV *pEnv) {
  SPgFile * pPgFile;
  SPgCache *pPgCache;
H
Hongze Cheng 已提交
36
  size_t    fnameLen;
H
more  
Hongze Cheng 已提交
37
  SPgno    fsize;
H
Hongze Cheng 已提交
38 39 40

  *ppPgFile = NULL;

H
Hongze Cheng 已提交
41 42 43
  // create the handle
  fnameLen = strlen(fname);
  pPgFile = (SPgFile *)calloc(1, sizeof(*pPgFile) + fnameLen + 1);
H
Hongze Cheng 已提交
44 45 46 47
  if (pPgFile == NULL) {
    return -1;
  }

H
Hongze Cheng 已提交
48
  ASSERT(pEnv != NULL);
H
Hongze Cheng 已提交
49

H
Hongze Cheng 已提交
50 51 52 53 54
  // init the handle
  pPgFile->fname = (char *)(&(pPgFile[1]));
  memcpy(pPgFile->fname, fname, fnameLen);
  pPgFile->fname[fnameLen] = '\0';
  pPgFile->fd = -1;
H
Hongze Cheng 已提交
55

H
Hongze Cheng 已提交
56
  pPgFile->fd = open(fname, O_CREAT | O_RDWR, 0755);
H
Hongze Cheng 已提交
57
  if (pPgFile->fd < 0) {
H
Hongze Cheng 已提交
58
    // TODO: handle error
H
Hongze Cheng 已提交
59 60 61
    return -1;
  }

H
Hongze Cheng 已提交
62
  tdbGnrtFileID(fname, pPgFile->fileid, false);
H
Hongze Cheng 已提交
63 64 65 66 67 68 69
  tdbGetFileSize(fname, tdbEnvGetPageSize(pEnv), &fsize);

  pPgFile->fsize = fsize;
  pPgFile->lsize = fsize;

  if (pPgFile->fsize == 0) {
    // A created file
H
more  
Hongze Cheng 已提交
70
    SPgno pgno;
H
Hongze Cheng 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84
    pgid_t pgid;

    pgFileAllocatePage(pPgFile, &pgno);

    ASSERT(pgno == 1);

    memcpy(pgid.fileid, pPgFile->fileid, TDB_FILE_ID_LEN);
    pgid.pgno = pgno;

    pgCacheFetch(pPgCache, pgid);
    // Need to allocate the first page as a description page
  } else {
    // An existing file
  }
H
Hongze Cheng 已提交
85

H
Hongze Cheng 已提交
86 87 88 89 90
  /* TODO: other open operations */

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

  *ppPgFile = pPgFile;
H
Hongze Cheng 已提交
93 94 95 96
  return 0;
}

int pgFileClose(SPgFile *pPgFile) {
H
Hongze Cheng 已提交
97 98 99 100 101 102 103 104 105
  if (pPgFile) {
    if (pPgFile->fd >= 0) {
      close(pPgFile->fd);
    }

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

H
Hongze Cheng 已提交
106 107 108
  return 0;
}

H
more  
Hongze Cheng 已提交
109
SPage *pgFileFetch(SPgFile *pPgFile, SPgno pgno) {
H
more  
Hongze Cheng 已提交
110 111 112 113
  SPgCache *pPgCache;
  SPage *   pPage;
  pgid_t    pgid;

H
Hongze Cheng 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
  // 1. Fetch from the page cache
  // pgCacheFetch(pPgCache, pgid);

  // 2. If only get a page frame, no content, maybe
  // need to load from the file
  if (1 /*page not initialized*/) {
    if (pgno < pPgFile->fsize) {
      // load the page content from the disk
      // ?? How about the freed pages ??
    } else {
      // zero the page, make the page as a empty
      // page with zero records.
    }
  }

H
Hongze Cheng 已提交
129
#if 0
H
more  
Hongze Cheng 已提交
130 131 132 133 134
  pPgCache = pPgFile->pPgCache;
  pPage = NULL;
  memcpy(pgid.fileid, pPgFile->fileid, TDB_FILE_ID_LEN);
  pgid.pgno = pgno;

H
Hongze Cheng 已提交
135 136 137 138
  if (pgno > pPgFile->pgFileSize) {
    // TODO
  } else {
    pPage = pgCacheFetch(pPgCache, pgid);
H
Hongze Cheng 已提交
139 140 141
    if (1 /*Page is cached, no need to load from file*/) {
      return pPage;
    } else {
H
Hongze Cheng 已提交
142 143
      // TODO: handle error
      if (pgFileRead(pPgFile, pgno, (void *)pPage) < 0) {
H
Hongze Cheng 已提交
144 145 146 147
        // todoerr
      }
      return pPage;
    }
H
Hongze Cheng 已提交
148
  }
H
Hongze Cheng 已提交
149
#endif
H
more  
Hongze Cheng 已提交
150 151

  return pPage;
H
Hongze Cheng 已提交
152 153 154
}

int pgFileRelease(SPage *pPage) {
H
more  
Hongze Cheng 已提交
155
  pgCacheRelease(pPage);
H
Hongze Cheng 已提交
156 157 158 159 160 161
  return 0;
}

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

H
more  
Hongze Cheng 已提交
164 165
int pgFileAllocatePage(SPgFile *pPgFile, SPgno *pPgno) {
  SPgno    pgno;
H
Hongze Cheng 已提交
166 167 168 169
  SPage1 *  pPage1;
  SPgCache *pPgCache;
  pgid_t    pgid;
  SPage *   pPage;
H
Hongze Cheng 已提交
170

H
Hongze Cheng 已提交
171
  if (pPgFile->lsize == 0) {
H
Hongze Cheng 已提交
172
    pgno = ++(pPgFile->lsize);
H
Hongze Cheng 已提交
173 174 175
  } else {
    if (0) {
      // TODO: allocate from the free list
H
Hongze Cheng 已提交
176 177 178 179 180 181 182
      pPage = pgCacheFetch(pPgCache, pgid);

      if (pPage1->nFree > 0) {
        // TODO
      } else {
        pgno = ++(pPgFile->lsize);
      }
H
Hongze Cheng 已提交
183 184 185
    } else {
      pgno = ++(pPgFile->lsize);
    }
H
Hongze Cheng 已提交
186 187 188 189 190 191
  }

  *pPgno = pgno;
  return 0;
}

H
more  
Hongze Cheng 已提交
192
static int pgFileRead(SPgFile *pPgFile, SPgno pgno, uint8_t *pData) {
H
Hongze Cheng 已提交
193
  pgsz_t   pgSize;
H
Hongze Cheng 已提交
194 195 196 197
  ssize_t  rsize;
  uint8_t *pTData;
  size_t   szToRead;

H
Hongze Cheng 已提交
198 199
#if 0

H
Hongze Cheng 已提交
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
  // 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 已提交
218
#endif
H
Hongze Cheng 已提交
219

H
Hongze Cheng 已提交
220
  return 0;
H
Hongze Cheng 已提交
221
}