tdbPgFile.c 2.9 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 41
  // init the handle
  pPgFile->pEnv = pEnv;
  pPgFile->fname = (char *)(&(pPgFile[1]));
  memcpy(pPgFile->fname, fname, fnameLen);
  pPgFile->fname[fnameLen] = '\0';
  pPgFile->fd = -1;
H
Hongze Cheng 已提交
42

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

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

H
Hongze Cheng 已提交
51
  /* TODO */
H
Hongze Cheng 已提交
52 53

  *ppPgFile = pPgFile;
H
Hongze Cheng 已提交
54 55 56 57
  return 0;
}

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

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

H
Hongze Cheng 已提交
67 68 69 70
  return 0;
}

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

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

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

  return pPage;
H
Hongze Cheng 已提交
98 99 100
}

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

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

static int pgFileRead(SPgFile *pPgFile, pgno_t pgno, uint8_t *pData) {
H
Hongze Cheng 已提交
111
  pgsz_t   pgSize;
H
Hongze Cheng 已提交
112 113 114 115
  ssize_t  rsize;
  uint8_t *pTData;
  size_t   szToRead;

H
Hongze Cheng 已提交
116 117
#if 0

H
Hongze Cheng 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
  // 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 已提交
136
#endif
H
Hongze Cheng 已提交
137

H
Hongze Cheng 已提交
138
  return 0;
H
Hongze Cheng 已提交
139
}