walRead.c 5.2 KB
Newer Older
H
refact  
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/>.
S
Shengliang Guan 已提交
14 15
 */

L
Liu Jicong 已提交
16 17
#include "walInt.h"
#include "tfile.h"
S
Shengliang Guan 已提交
18

L
Liu Jicong 已提交
19 20 21 22 23 24 25 26
SWalReadHandle* walOpenReadHandle(SWal* pWal) {
  SWalReadHandle *pRead = malloc(sizeof(SWalReadHandle));
  if(pRead == NULL) {
    return NULL;
  }
  pRead->pWal = pWal;
  pRead->readIdxTfd = -1;
  pRead->readLogTfd = -1;
L
Liu Jicong 已提交
27 28 29 30 31 32 33 34 35 36
  pRead->curVersion = -1;
  pRead->curFileFirstVer = -1;
  pRead->capacity = 0;
  pRead->status = 0;
  pRead->pHead = malloc(sizeof(SWalHead));
  if(pRead->pHead == NULL) {
    free(pRead);
    return NULL;
  }
  return pRead;
L
Liu Jicong 已提交
37 38 39 40 41
}

void walCloseReadHandle(SWalReadHandle *pRead) {
  tfClose(pRead->readIdxTfd);
  tfClose(pRead->readLogTfd);
L
Liu Jicong 已提交
42
  tfree(pRead->pHead);
L
Liu Jicong 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56
  free(pRead);
}

int32_t walRegisterRead(SWalReadHandle *pRead, int64_t ver) {
  return 0;
}

static int32_t walReadSeekFilePos(SWalReadHandle *pRead, int64_t fileFirstVer, int64_t ver) {
  int code = 0;

  int64_t idxTfd = pRead->readIdxTfd;
  int64_t logTfd = pRead->readLogTfd;
  
  //seek position
L
Liu Jicong 已提交
57
  int64_t offset = (ver - fileFirstVer) * sizeof(WalIdxEntry);
L
Liu Jicong 已提交
58
  code = tfLseek(idxTfd, offset, SEEK_SET);
L
Liu Jicong 已提交
59
  if(code < 0) {
L
Liu Jicong 已提交
60 61 62
    return -1;
  }
  WalIdxEntry entry;
L
Liu Jicong 已提交
63
  if(tfRead(idxTfd, &entry, sizeof(WalIdxEntry)) != sizeof(WalIdxEntry)) {
L
Liu Jicong 已提交
64 65 66 67 68
    return -1;
  }
  //TODO:deserialize
  ASSERT(entry.ver == ver);
  code = tfLseek(logTfd, entry.offset, SEEK_SET);
L
Liu Jicong 已提交
69
  if (code < 0) {
L
Liu Jicong 已提交
70 71 72 73 74 75 76 77 78 79 80 81
    return -1;
  }
  return code;
}

static int32_t walReadChangeFile(SWalReadHandle *pRead, int64_t fileFirstVer) {
  char fnameStr[WAL_FILE_LEN];

  tfClose(pRead->readIdxTfd);
  tfClose(pRead->readLogTfd);

  walBuildLogName(pRead->pWal, fileFirstVer, fnameStr);
L
Liu Jicong 已提交
82
  int64_t logTfd = tfOpenRead(fnameStr);
L
Liu Jicong 已提交
83 84 85 86 87
  if(logTfd < 0) {
    return -1;
  }

  walBuildIdxName(pRead->pWal, fileFirstVer, fnameStr);
L
Liu Jicong 已提交
88
  int64_t idxTfd = tfOpenRead(fnameStr);
L
Liu Jicong 已提交
89 90 91 92 93 94 95 96 97 98 99 100
  if(idxTfd < 0) {
    return -1;
  }

  pRead->readLogTfd = logTfd;
  pRead->readIdxTfd = idxTfd;
  return 0;
}

static int32_t walReadSeekVer(SWalReadHandle *pRead, int64_t ver) {
  int code;
  SWal *pWal = pRead->pWal;
L
Liu Jicong 已提交
101
  if(ver == pRead->curVersion) {
L
Liu Jicong 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
    return 0;
  }
  if(ver > pWal->vers.lastVer || ver < pWal->vers.firstVer) {
    return -1;
  }
  if(ver < pWal->vers.snapshotVer) {

  }

  WalFileInfo tmpInfo;
  tmpInfo.firstVer = ver;
  //bsearch in fileSet
  WalFileInfo* pRet = taosArraySearch(pWal->fileInfoSet, &tmpInfo, compareWalFileInfo, TD_LE);
  ASSERT(pRet != NULL);
  if(pRead->curFileFirstVer != pRet->firstVer) {
    code = walReadChangeFile(pRead, pRet->firstVer);
    if(code < 0) {
      //TODO: set error flag
      return -1;
    }
  }

  code = walReadSeekFilePos(pRead, pRet->firstVer, ver);
  if(code < 0) {
    return -1;
  }
  pRead->curVersion = ver;
   
  return 0;
}

int32_t walReadWithHandle(SWalReadHandle *pRead, int64_t ver) {
  int code;
  //TODO: check wal life
  if(pRead->curVersion != ver) {
L
Liu Jicong 已提交
137 138 139 140
    code = walReadSeekVer(pRead, ver);   
    if(code != 0) {
      return -1;
    }
L
Liu Jicong 已提交
141 142 143 144
  }

  if(!tfValid(pRead->readLogTfd)) return -1;

L
Liu Jicong 已提交
145 146
  code = tfRead(pRead->readLogTfd, pRead->pHead, sizeof(SWalHead));
  if(code != sizeof(SWalHead)) {
L
Liu Jicong 已提交
147 148
    return -1;
  }
L
Liu Jicong 已提交
149
  code = walValidHeadCksum(pRead->pHead);
L
Liu Jicong 已提交
150 151 152
  if(code != 0) {
    return -1;
  }
L
Liu Jicong 已提交
153 154
  if(pRead->capacity < pRead->pHead->head.len) {
    void* ptr = realloc(pRead->pHead, sizeof(SWalHead) + pRead->pHead->head.len);
L
Liu Jicong 已提交
155 156 157
    if(ptr == NULL) {
      return -1;
    }
L
Liu Jicong 已提交
158 159
    pRead->pHead = ptr;
    pRead->capacity = pRead->pHead->head.len;
L
Liu Jicong 已提交
160
  }
L
Liu Jicong 已提交
161
  if(pRead->pHead->head.len != tfRead(pRead->readLogTfd, pRead->pHead->head.body, pRead->pHead->head.len)) {
L
Liu Jicong 已提交
162 163
    return -1;
  }
L
Liu Jicong 已提交
164 165 166 167

  /*code = walValidBodyCksum(pRead->pHead);*/
  ASSERT(pRead->pHead->head.version == ver);

L
Liu Jicong 已提交
168 169 170
  if(code != 0) {
    return -1;
  }
L
Liu Jicong 已提交
171
  pRead->curVersion++;
L
Liu Jicong 已提交
172 173 174 175

  return 0;
}

L
Liu Jicong 已提交
176
int32_t walRead(SWal *pWal, SWalHead **ppHead, int64_t ver) {
L
Liu Jicong 已提交
177 178 179 180 181 182 183 184 185 186 187 188
  int code;
  code = walSeekVer(pWal, ver);
  if(code != 0) {
    return code;
  }
  if(*ppHead == NULL) {
    void* ptr = realloc(*ppHead, sizeof(SWalHead));
    if(ptr == NULL) {
      return -1;
    }
    *ppHead = ptr;
  }
L
Liu Jicong 已提交
189
  if(tfRead(pWal->writeLogTfd, *ppHead, sizeof(SWalHead)) != sizeof(SWalHead)) {
L
Liu Jicong 已提交
190 191 192 193 194 195
    return -1;
  }
  //TODO: endian compatibility processing after read
  if(walValidHeadCksum(*ppHead) != 0) {
    return -1;
  }
L
Liu Jicong 已提交
196
  void* ptr = realloc(*ppHead, sizeof(SWalHead) + (*ppHead)->head.len);
L
Liu Jicong 已提交
197 198 199 200 201
  if(ptr == NULL) {
    free(*ppHead);
    *ppHead = NULL;
    return -1;
  }
L
Liu Jicong 已提交
202
  if(tfRead(pWal->writeLogTfd, (*ppHead)->head.body, (*ppHead)->head.len) != (*ppHead)->head.len) {
L
Liu Jicong 已提交
203 204 205 206 207 208 209
    return -1;
  }
  //TODO: endian compatibility processing after read
  if(walValidBodyCksum(*ppHead) != 0) {
    return -1;
  }
  
L
Liu Jicong 已提交
210 211
  return 0;
}
S
Shengliang Guan 已提交
212

L
Liu Jicong 已提交
213 214 215
/*int32_t walReadWithFp(SWal *pWal, FWalWrite writeFp, int64_t verStart, int32_t readNum) {*/
  /*return 0;*/
/*}*/