tdbPgFile.c 2.8 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
int pgFileOpen(SPgFile **ppPgFile, const char *fname, SPgCache *pPgCache) {
H
Hongze Cheng 已提交
21
  SPgFile *pPgFile;
H
Hongze Cheng 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

  *ppPgFile = NULL;

  pPgFile = (SPgFile *)calloc(1, sizeof(*pPgFile));
  if (pPgFile == NULL) {
    return -1;
  }

  pPgFile->fd = -1;

  pPgFile->fname = strdup(fname);
  if (pPgFile->fname == NULL) {
    pgFileClose(pPgFile);
    return -1;
  }

  pPgFile->pPgCache = pPgCache;
H
Hongze Cheng 已提交
39
  // pPgFile->pgSize = ; (TODO)
H
Hongze Cheng 已提交
40 41 42 43 44 45 46

  pPgFile->fd = open(fname, O_RDWR, 0755);
  if (pPgFile->fd < 0) {
    pgFileClose(pPgFile);
    return -1;
  }

H
Hongze Cheng 已提交
47
  if (tdbGnrtFileID(fname, pPgFile->fileid, false) < 0) {
H
Hongze Cheng 已提交
48 49 50 51 52 53 54 55
    pgFileClose(pPgFile);
    return -1;
  }

  // TODO: get file size
  pPgFile->pgFileSize = 0;

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

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

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

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

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

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

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

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

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

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

static int pgFileRead(SPgFile *pPgFile, pgno_t pgno, uint8_t *pData) {
H
Hongze Cheng 已提交
110
  pgsz_t   pgSize;
H
Hongze Cheng 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
  ssize_t  rsize;
  uint8_t *pTData;
  size_t   szToRead;

  // 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 已提交
134
  return 0;
H
Hongze Cheng 已提交
135
}