tdbPgFile.c 3.2 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
static int pgFileRead(SPgFile *pPgFile, pgno_t pgno, uint8_t *pData);

H
Hongze Cheng 已提交
20 21 22
int pgFileOpen(SPgFile **ppPgFile, const char *fname, TENV *pEnv) {
  SPgFile * pPgFile;
  SPgCache *pPgCache;
H
Hongze Cheng 已提交
23
  size_t    fnameLen;
H
Hongze Cheng 已提交
24 25 26

  *ppPgFile = NULL;

H
Hongze Cheng 已提交
27 28 29
  // create the handle
  fnameLen = strlen(fname);
  pPgFile = (SPgFile *)calloc(1, sizeof(*pPgFile) + fnameLen + 1);
H
Hongze Cheng 已提交
30 31 32 33
  if (pPgFile == NULL) {
    return -1;
  }

H
Hongze Cheng 已提交
34
  ASSERT(pEnv != NULL);
H
Hongze Cheng 已提交
35

H
Hongze Cheng 已提交
36 37 38 39 40
  // init the handle
  pPgFile->fname = (char *)(&(pPgFile[1]));
  memcpy(pPgFile->fname, fname, fnameLen);
  pPgFile->fname[fnameLen] = '\0';
  pPgFile->fd = -1;
H
Hongze Cheng 已提交
41

H
Hongze Cheng 已提交
42
  pPgFile->fd = open(fname, O_CREAT | O_RDWR, 0755);
H
Hongze Cheng 已提交
43
  if (pPgFile->fd < 0) {
H
Hongze Cheng 已提交
44
    // TODO: handle error
H
Hongze Cheng 已提交
45 46 47
    return -1;
  }

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

H
Hongze Cheng 已提交
50 51 52 53 54
  /* TODO: other open operations */

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

  *ppPgFile = pPgFile;
H
Hongze Cheng 已提交
57 58 59 60
  return 0;
}

int pgFileClose(SPgFile *pPgFile) {
H
Hongze Cheng 已提交
61 62 63 64 65 66 67 68 69
  if (pPgFile) {
    if (pPgFile->fd >= 0) {
      close(pPgFile->fd);
    }

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

H
Hongze Cheng 已提交
70 71 72 73
  return 0;
}

SPage *pgFileFetch(SPgFile *pPgFile, pgno_t pgno) {
H
more  
Hongze Cheng 已提交
74 75 76 77
  SPgCache *pPgCache;
  SPage *   pPage;
  pgid_t    pgid;

H
Hongze Cheng 已提交
78
#if 0
H
more  
Hongze Cheng 已提交
79 80 81 82 83
  pPgCache = pPgFile->pPgCache;
  pPage = NULL;
  memcpy(pgid.fileid, pPgFile->fileid, TDB_FILE_ID_LEN);
  pgid.pgno = pgno;

H
Hongze Cheng 已提交
84 85 86 87
  if (pgno > pPgFile->pgFileSize) {
    // TODO
  } else {
    pPage = pgCacheFetch(pPgCache, pgid);
H
Hongze Cheng 已提交
88 89 90
    if (1 /*Page is cached, no need to load from file*/) {
      return pPage;
    } else {
H
Hongze Cheng 已提交
91 92
      // TODO: handle error
      if (pgFileRead(pPgFile, pgno, (void *)pPage) < 0) {
H
Hongze Cheng 已提交
93 94 95 96
        // todoerr
      }
      return pPage;
    }
H
Hongze Cheng 已提交
97
  }
H
Hongze Cheng 已提交
98
#endif
H
more  
Hongze Cheng 已提交
99 100

  return pPage;
H
Hongze Cheng 已提交
101 102 103
}

int pgFileRelease(SPage *pPage) {
H
more  
Hongze Cheng 已提交
104
  pgCacheRelease(pPage);
H
Hongze Cheng 已提交
105 106 107 108 109 110
  return 0;
}

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

H
Hongze Cheng 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125
int pgFileAllocatePage(SPgFile *pPgFile, pgno_t *pPgno) {
  pgno_t pgno;

  if (0) {
    // TODO: allocate from the free list
  } else {
    pgno = ++pPgFile->dbNewSize;
  }

  *pPgno = pgno;
  return 0;
}

H
Hongze Cheng 已提交
126
static int pgFileRead(SPgFile *pPgFile, pgno_t pgno, uint8_t *pData) {
H
Hongze Cheng 已提交
127
  pgsz_t   pgSize;
H
Hongze Cheng 已提交
128 129 130 131
  ssize_t  rsize;
  uint8_t *pTData;
  size_t   szToRead;

H
Hongze Cheng 已提交
132 133
#if 0

H
Hongze Cheng 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
  // 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 已提交
152
#endif
H
Hongze Cheng 已提交
153

H
Hongze Cheng 已提交
154
  return 0;
H
Hongze Cheng 已提交
155
}