tdbPgFile.c 4.4 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
  pgno_t    fsize;
H
Hongze Cheng 已提交
34 35 36

  *ppPgFile = NULL;

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

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

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

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

H
Hongze Cheng 已提交
58
  tdbGnrtFileID(fname, pPgFile->fileid, false);
H
Hongze Cheng 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
  tdbGetFileSize(fname, tdbEnvGetPageSize(pEnv), &fsize);

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

  if (pPgFile->fsize == 0) {
    // A created file
    pgno_t pgno;
    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 已提交
81

H
Hongze Cheng 已提交
82 83 84 85 86
  /* TODO: other open operations */

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

  *ppPgFile = pPgFile;
H
Hongze Cheng 已提交
89 90 91 92
  return 0;
}

int pgFileClose(SPgFile *pPgFile) {
H
Hongze Cheng 已提交
93 94 95 96 97 98 99 100 101
  if (pPgFile) {
    if (pPgFile->fd >= 0) {
      close(pPgFile->fd);
    }

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

H
Hongze Cheng 已提交
102 103 104 105
  return 0;
}

SPage *pgFileFetch(SPgFile *pPgFile, pgno_t pgno) {
H
more  
Hongze Cheng 已提交
106 107 108 109
  SPgCache *pPgCache;
  SPage *   pPage;
  pgid_t    pgid;

H
Hongze Cheng 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
  // 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 已提交
125
#if 0
H
more  
Hongze Cheng 已提交
126 127 128 129 130
  pPgCache = pPgFile->pPgCache;
  pPage = NULL;
  memcpy(pgid.fileid, pPgFile->fileid, TDB_FILE_ID_LEN);
  pgid.pgno = pgno;

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

  return pPage;
H
Hongze Cheng 已提交
148 149 150
}

int pgFileRelease(SPage *pPage) {
H
more  
Hongze Cheng 已提交
151
  pgCacheRelease(pPage);
H
Hongze Cheng 已提交
152 153 154 155 156 157
  return 0;
}

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

H
Hongze Cheng 已提交
160 161 162
int pgFileAllocatePage(SPgFile *pPgFile, pgno_t *pPgno) {
  pgno_t pgno;

H
Hongze Cheng 已提交
163
  if (pPgFile->lsize == 0) {
H
Hongze Cheng 已提交
164
    pgno = ++(pPgFile->lsize);
H
Hongze Cheng 已提交
165 166 167 168 169 170
  } else {
    if (0) {
      // TODO: allocate from the free list
    } else {
      pgno = ++(pPgFile->lsize);
    }
H
Hongze Cheng 已提交
171 172 173 174 175 176
  }

  *pPgno = pgno;
  return 0;
}

H
Hongze Cheng 已提交
177
static int pgFileRead(SPgFile *pPgFile, pgno_t pgno, uint8_t *pData) {
H
Hongze Cheng 已提交
178
  pgsz_t   pgSize;
H
Hongze Cheng 已提交
179 180 181 182
  ssize_t  rsize;
  uint8_t *pTData;
  size_t   szToRead;

H
Hongze Cheng 已提交
183 184
#if 0

H
Hongze Cheng 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
  // 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 已提交
203
#endif
H
Hongze Cheng 已提交
204

H
Hongze Cheng 已提交
205
  return 0;
H
Hongze Cheng 已提交
206
}