walSeek.c 3.4 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
/*
 * 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 "tfile.h"
L
Liu Jicong 已提交
20
#include "tref.h"
L
Liu Jicong 已提交
21 22
#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
  SWalIdxEntry entry;
L
Liu Jicong 已提交
36
  // TODO:deserialize
L
Liu Jicong 已提交
37
  code = tfRead(idxTfd, &entry, sizeof(SWalIdxEntry));
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
int walChangeFileToLast(SWal* pWal) {
L
Liu Jicong 已提交
50 51
  int64_t       idxTfd, logTfd;
  SWalFileInfo* pRet = taosArrayGetLast(pWal->fileInfoSet);
L
Liu Jicong 已提交
52 53 54 55 56 57
  ASSERT(pRet != NULL);
  int64_t fileFirstVer = pRet->firstVer;

  char fnameStr[WAL_FILE_LEN];
  walBuildIdxName(pWal, fileFirstVer, fnameStr);
  idxTfd = tfOpenReadWrite(fnameStr);
L
Liu Jicong 已提交
58
  if (idxTfd < 0) {
L
Liu Jicong 已提交
59 60 61 62
    return -1;
  }
  walBuildLogName(pWal, fileFirstVer, fnameStr);
  logTfd = tfOpenReadWrite(fnameStr);
L
Liu Jicong 已提交
63
  if (logTfd < 0) {
L
Liu Jicong 已提交
64 65
    return -1;
  }
L
Liu Jicong 已提交
66
  // switch file
L
Liu Jicong 已提交
67 68 69 70 71
  pWal->writeIdxTfd = idxTfd;
  pWal->writeLogTfd = logTfd;
  return 0;
}

L
Liu Jicong 已提交
72 73
int walChangeFile(SWal* pWal, int64_t ver) {
  int     code = 0;
L
Liu Jicong 已提交
74
  int64_t idxTfd, logTfd;
L
Liu Jicong 已提交
75
  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
  SWalFileInfo tmpInfo;
L
Liu Jicong 已提交
87
  tmpInfo.firstVer = ver;
L
Liu Jicong 已提交
88
  // bsearch in fileSet
L
Liu Jicong 已提交
89
  SWalFileInfo* pRet = taosArraySearch(pWal->fileInfoSet, &tmpInfo, compareWalFileInfo, TD_LE);
L
Liu Jicong 已提交
90
  ASSERT(pRet != NULL);
L
Liu Jicong 已提交
91
  int64_t fileFirstVer = pRet->firstVer;
L
Liu Jicong 已提交
92 93
  // closed
  if (taosArrayGetLast(pWal->fileInfoSet) != pRet) {
L
Liu Jicong 已提交
94
    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
  if (ver < pWal->vers.snapshotVer) {
L
Liu Jicong 已提交
119
  }
L
Liu Jicong 已提交
120
  if (ver < walGetCurFileFirstVer(pWal) || (ver > walGetCurFileLastVer(pWal))) {
L
Liu Jicong 已提交
121
    code = walChangeFile(pWal, ver);
L
Liu Jicong 已提交
122
    if (code != 0) {
L
Liu Jicong 已提交
123 124
      return -1;
    }
L
Liu Jicong 已提交
125
  }
L
Liu Jicong 已提交
126
  code = walSeekFilePos(pWal, ver);
L
Liu Jicong 已提交
127
  if (code != 0) {
L
Liu Jicong 已提交
128 129
    return -1;
  }
L
Liu Jicong 已提交
130

L
Liu Jicong 已提交
131 132
  return 0;
}