walSeek.c 3.3 KB
Newer Older
L
Liu Jicong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * 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/>.
 */

#define _DEFAULT_SOURCE
#include "os.h"
#include "taoserror.h"
#include "tref.h"
#include "tfile.h"
#include "walInt.h"

L
Liu Jicong 已提交
23
static int walSeekFilePos(SWal* pWal, int64_t ver) {
L
Liu Jicong 已提交
24
  int code = 0;
L
Liu Jicong 已提交
25

L
Liu Jicong 已提交
26 27
  int64_t idxTfd = pWal->writeIdxTfd;
  int64_t logTfd = pWal->writeLogTfd;
L
Liu Jicong 已提交
28 29
  
  //seek position
L
Liu Jicong 已提交
30 31
  int64_t idxOff = walGetVerIdxOffset(pWal, ver);
  code = tfLseek(idxTfd, idxOff, SEEK_SET);
L
Liu Jicong 已提交
32
  if(code != 0) {
L
Liu Jicong 已提交
33
    return -1;
L
Liu Jicong 已提交
34
  }
L
Liu Jicong 已提交
35 36 37
  WalIdxEntry entry;
  //TODO:deserialize
  code = tfRead(idxTfd, &entry, sizeof(WalIdxEntry));
L
Liu Jicong 已提交
38
  if(code != 0) {
L
Liu Jicong 已提交
39
    return -1;
L
Liu Jicong 已提交
40
  }
L
Liu Jicong 已提交
41 42 43
  ASSERT(entry.ver == ver);
  code = tfLseek(logTfd, entry.offset, SEEK_CUR);
  if (code < 0) {
L
Liu Jicong 已提交
44
    return -1;
L
Liu Jicong 已提交
45 46 47 48
  }
  return code;
}

L
Liu Jicong 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
int walChangeFileToLast(SWal *pWal) {
  int64_t idxTfd, logTfd;
  WalFileInfo* pRet = taosArrayGetLast(pWal->fileInfoSet);
  ASSERT(pRet != NULL);
  int64_t fileFirstVer = pRet->firstVer;

  char fnameStr[WAL_FILE_LEN];
  walBuildIdxName(pWal, fileFirstVer, fnameStr);
  idxTfd = tfOpenReadWrite(fnameStr);
  if(idxTfd < 0) {
    return -1;
  }
  walBuildLogName(pWal, fileFirstVer, fnameStr);
  logTfd = tfOpenReadWrite(fnameStr);
  if(logTfd < 0) {
    return -1;
  }
  //switch file
  pWal->writeIdxTfd = idxTfd;
  pWal->writeLogTfd = logTfd;
  return 0;
}

int walChangeFile(SWal *pWal, int64_t ver) {
L
Liu Jicong 已提交
73 74 75
  int code = 0;
  int64_t idxTfd, logTfd;
  char fnameStr[WAL_FILE_LEN];
L
Liu Jicong 已提交
76
  code = tfClose(pWal->writeLogTfd);
L
Liu Jicong 已提交
77 78
  if(code != 0) {
   //TODO 
L
Liu Jicong 已提交
79
    return -1;
L
Liu Jicong 已提交
80
  }
L
Liu Jicong 已提交
81
  code = tfClose(pWal->writeIdxTfd);
L
Liu Jicong 已提交
82 83
  if(code != 0) {
   //TODO 
L
Liu Jicong 已提交
84
    return -1;
L
Liu Jicong 已提交
85
  }
L
Liu Jicong 已提交
86 87
  WalFileInfo tmpInfo;
  tmpInfo.firstVer = ver;
L
Liu Jicong 已提交
88
  //bsearch in fileSet
L
Liu Jicong 已提交
89
  WalFileInfo* pRet = taosArraySearch(pWal->fileInfoSet, &tmpInfo, compareWalFileInfo, TD_LE);
L
Liu Jicong 已提交
90
  ASSERT(pRet != NULL);
L
Liu Jicong 已提交
91 92 93 94
  int64_t fileFirstVer = pRet->firstVer;
  //closed
  if(taosArrayGetLast(pWal->fileInfoSet) != pRet) {
    walBuildIdxName(pWal, fileFirstVer, fnameStr);
L
Liu Jicong 已提交
95
    idxTfd = tfOpenRead(fnameStr);
L
Liu Jicong 已提交
96
    walBuildLogName(pWal, fileFirstVer, fnameStr);
L
Liu Jicong 已提交
97 98
    logTfd = tfOpenRead(fnameStr);
  } else {
L
Liu Jicong 已提交
99
    walBuildIdxName(pWal, fileFirstVer, fnameStr);
L
Liu Jicong 已提交
100
    idxTfd = tfOpenReadWrite(fnameStr);
L
Liu Jicong 已提交
101
    walBuildLogName(pWal, fileFirstVer, fnameStr);
L
Liu Jicong 已提交
102 103 104
    logTfd = tfOpenReadWrite(fnameStr);
  }

L
Liu Jicong 已提交
105 106
  pWal->writeLogTfd = logTfd;
  pWal->writeIdxTfd = idxTfd;
L
Liu Jicong 已提交
107
  return fileFirstVer;
L
Liu Jicong 已提交
108 109
}

L
Liu Jicong 已提交
110
int walSeekVer(SWal *pWal, int64_t ver) {
L
Liu Jicong 已提交
111
  int code;
L
Liu Jicong 已提交
112
  if(ver == pWal->vers.lastVer) {
L
Liu Jicong 已提交
113 114
    return 0;
  }
L
Liu Jicong 已提交
115
  if(ver > pWal->vers.lastVer|| ver < pWal->vers.firstVer) {
L
Liu Jicong 已提交
116 117
    return -1;
  }
L
Liu Jicong 已提交
118 119
  if(ver < pWal->vers.snapshotVer) {

L
Liu Jicong 已提交
120
  }
L
Liu Jicong 已提交
121
  if(ver < walGetCurFileFirstVer(pWal) || (ver > walGetCurFileLastVer(pWal))) {
L
Liu Jicong 已提交
122 123 124 125
    code = walChangeFile(pWal, ver);
    if(code != 0) {
      return -1;
    }
L
Liu Jicong 已提交
126
  }
L
Liu Jicong 已提交
127 128 129 130 131
  code = walSeekFilePos(pWal, ver);
  if(code != 0) {
    return -1;
  }
   
L
Liu Jicong 已提交
132 133
  return 0;
}