indexFstFile.c 5.6 KB
Newer Older
dengyihao's avatar
dengyihao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * 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 已提交
15

dengyihao's avatar
dengyihao 已提交
16
#include "indexFstFile.h"
dengyihao's avatar
dengyihao 已提交
17
#include "indexFstUtil.h"
dengyihao's avatar
dengyihao 已提交
18
#include "indexInt.h"
dengyihao's avatar
dengyihao 已提交
19
#include "os.h"
20
#include "tutil.h"
dengyihao's avatar
dengyihao 已提交
21

dengyihao's avatar
dengyihao 已提交
22
static int idxFileCtxDoWrite(IFileCtx* ctx, uint8_t* buf, int len) {
dengyihao's avatar
dengyihao 已提交
23
  if (ctx->type == TFile) {
24
    assert(len == taosWriteFile(ctx->file.pFile, buf, len));
dengyihao's avatar
dengyihao 已提交
25
  } else {
26 27
    memcpy(ctx->mem.buf + ctx->offset, buf, len);
  }
dengyihao's avatar
dengyihao 已提交
28 29 30
  ctx->offset += len;
  return len;
}
dengyihao's avatar
dengyihao 已提交
31
static int idxFileCtxDoRead(IFileCtx* ctx, uint8_t* buf, int len) {
32
  int nRead = 0;
dengyihao's avatar
dengyihao 已提交
33
  if (ctx->type == TFile) {
dengyihao's avatar
dengyihao 已提交
34 35 36 37
#ifdef USE_MMAP
    nRead = len < ctx->file.size ? len : ctx->file.size;
    memcpy(buf, ctx->file.ptr, nRead);
#else
38
    nRead = taosReadFile(ctx->file.pFile, buf, len);
dengyihao's avatar
dengyihao 已提交
39
#endif
dengyihao's avatar
dengyihao 已提交
40
  } else {
dengyihao's avatar
dengyihao 已提交
41
    memcpy(buf, ctx->mem.buf + ctx->offset, len);
dengyihao's avatar
dengyihao 已提交
42
  }
dengyihao's avatar
dengyihao 已提交
43
  ctx->offset += nRead;
dengyihao's avatar
dengyihao 已提交
44

dengyihao's avatar
dengyihao 已提交
45
  return nRead;
46
}
dengyihao's avatar
dengyihao 已提交
47
static int idxFileCtxDoReadFrom(IFileCtx* ctx, uint8_t* buf, int len, int32_t offset) {
dengyihao's avatar
dengyihao 已提交
48 49
  int nRead = 0;
  if (ctx->type == TFile) {
50
    // tfLseek(ctx->file.pFile, offset, 0);
dengyihao's avatar
dengyihao 已提交
51 52 53 54 55
#ifdef USE_MMAP
    int32_t last = ctx->file.size - offset;
    nRead = last >= len ? len : last;
    memcpy(buf, ctx->file.ptr + offset, nRead);
#else
56
    nRead = taosPReadFile(ctx->file.pFile, buf, len, offset);
dengyihao's avatar
dengyihao 已提交
57
#endif
dengyihao's avatar
dengyihao 已提交
58 59 60 61 62 63
  } else {
    // refactor later
    assert(0);
  }
  return nRead;
}
dengyihao's avatar
dengyihao 已提交
64
static int idxFileCtxGetSize(IFileCtx* ctx) {
dengyihao's avatar
dengyihao 已提交
65
  if (ctx->type == TFile) {
66 67 68
    int64_t file_size = 0;
    taosStatFile(ctx->file.buf, &file_size, NULL);
    return (int)file_size;
dengyihao's avatar
dengyihao 已提交
69 70 71
  }
  return 0;
}
dengyihao's avatar
dengyihao 已提交
72
static int idxFileCtxDoFlush(IFileCtx* ctx) {
dengyihao's avatar
dengyihao 已提交
73
  if (ctx->type == TFile) {
74
    taosFsyncFile(ctx->file.pFile);
dengyihao's avatar
dengyihao 已提交
75 76 77 78 79 80
  } else {
    // do nothing
  }
  return 1;
}

dengyihao's avatar
dengyihao 已提交
81 82
IFileCtx* idxFileCtxCreate(WriterType type, const char* path, bool readOnly, int32_t capacity) {
  IFileCtx* ctx = taosMemoryCalloc(1, sizeof(IFileCtx));
dengyihao's avatar
dengyihao 已提交
83 84 85
  if (ctx == NULL) {
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
86

dengyihao's avatar
dengyihao 已提交
87
  ctx->type = type;
dengyihao's avatar
dengyihao 已提交
88
  if (ctx->type == TFile) {
dengyihao's avatar
dengyihao 已提交
89
    // ugly code, refactor later
dengyihao's avatar
dengyihao 已提交
90
    ctx->file.readOnly = readOnly;
dengyihao's avatar
dengyihao 已提交
91
    memcpy(ctx->file.buf, path, strlen(path));
dengyihao's avatar
dengyihao 已提交
92
    if (readOnly == false) {
93
      ctx->file.pFile = taosOpenFile(path, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_APPEND);
94
      taosFtruncateFile(ctx->file.pFile, 0);
dengyihao's avatar
dengyihao 已提交
95 96
      taosStatFile(path, &ctx->file.size, NULL);
      // ctx->file.size = (int)size;
97

dengyihao's avatar
dengyihao 已提交
98
    } else {
99
      ctx->file.pFile = taosOpenFile(path, TD_FILE_READ);
dengyihao's avatar
dengyihao 已提交
100

dengyihao's avatar
dengyihao 已提交
101 102 103
      int64_t size = 0;
      taosFStatFile(ctx->file.pFile, &ctx->file.size, NULL);
      ctx->file.size = (int)size;
dengyihao's avatar
dengyihao 已提交
104
#ifdef USE_MMAP
105
      ctx->file.ptr = (char*)tfMmapReadOnly(ctx->file.pFile, ctx->file.size);
dengyihao's avatar
dengyihao 已提交
106
#endif
107
    }
108
    if (ctx->file.pFile == NULL) {
dengyihao's avatar
dengyihao 已提交
109
      indexError("failed to open file, error %d", errno);
dengyihao's avatar
dengyihao 已提交
110
      goto END;
dengyihao's avatar
dengyihao 已提交
111
    }
dengyihao's avatar
dengyihao 已提交
112
  } else if (ctx->type == TMemory) {
wafwerar's avatar
wafwerar 已提交
113
    ctx->mem.buf = taosMemoryCalloc(1, sizeof(char) * capacity);
dengyihao's avatar
dengyihao 已提交
114
    ctx->mem.cap = capacity;
115
  }
dengyihao's avatar
dengyihao 已提交
116 117 118 119
  ctx->write = idxFileCtxDoWrite;
  ctx->read = idxFileCtxDoRead;
  ctx->flush = idxFileCtxDoFlush;
  ctx->readFrom = idxFileCtxDoReadFrom;
dengyihao's avatar
dengyihao 已提交
120
  ctx->size = idxFileCtxGetSize;
dengyihao's avatar
dengyihao 已提交
121 122

  ctx->offset = 0;
123
  ctx->limit = capacity;
dengyihao's avatar
dengyihao 已提交
124 125

  return ctx;
dengyihao's avatar
dengyihao 已提交
126
END:
dengyihao's avatar
dengyihao 已提交
127 128 129
  if (ctx->type == TMemory) {
    taosMemoryFree(ctx->mem.buf);
  }
wafwerar's avatar
wafwerar 已提交
130
  taosMemoryFree(ctx);
dengyihao's avatar
dengyihao 已提交
131
  return NULL;
dengyihao's avatar
dengyihao 已提交
132
}
dengyihao's avatar
dengyihao 已提交
133
void idxFileCtxDestroy(IFileCtx* ctx, bool remove) {
dengyihao's avatar
dengyihao 已提交
134
  if (ctx->type == TMemory) {
wafwerar's avatar
wafwerar 已提交
135
    taosMemoryFree(ctx->mem.buf);
dengyihao's avatar
dengyihao 已提交
136
  } else {
dengyihao's avatar
dengyihao 已提交
137
    ctx->flush(ctx);
138
    taosCloseFile(&ctx->file.pFile);
dengyihao's avatar
dengyihao 已提交
139 140 141 142 143
    if (ctx->file.readOnly) {
#ifdef USE_MMAP
      munmap(ctx->file.ptr, ctx->file.size);
#endif
    }
dengyihao's avatar
dengyihao 已提交
144
    if (ctx->file.readOnly == false) {
145 146
      int64_t file_size = 0;
      taosStatFile(ctx->file.buf, &file_size, NULL);
dengyihao's avatar
dengyihao 已提交
147
    }
dengyihao's avatar
dengyihao 已提交
148 149 150
    if (remove) {
      unlink(ctx->file.buf);
    }
dengyihao's avatar
dengyihao 已提交
151
  }
wafwerar's avatar
wafwerar 已提交
152
  taosMemoryFree(ctx);
dengyihao's avatar
dengyihao 已提交
153 154
}

dengyihao's avatar
dengyihao 已提交
155 156
IdxFstFile* idxFileCreate(void* wrt) {
  IdxFstFile* cw = taosMemoryCalloc(1, sizeof(IdxFstFile));
dengyihao's avatar
dengyihao 已提交
157 158 159
  if (cw == NULL) {
    return NULL;
  }
160

dengyihao's avatar
dengyihao 已提交
161
  cw->wrt = wrt;
162
  return cw;
dengyihao's avatar
dengyihao 已提交
163
}
dengyihao's avatar
dengyihao 已提交
164
void idxFileDestroy(IdxFstFile* cw) {
165
  // free wrt object: close fd or free mem
dengyihao's avatar
dengyihao 已提交
166 167
  idxFileFlush(cw);
  // idxFileCtxDestroy((IFileCtx *)(cw->wrt));
wafwerar's avatar
wafwerar 已提交
168
  taosMemoryFree(cw);
dengyihao's avatar
dengyihao 已提交
169 170
}

dengyihao's avatar
dengyihao 已提交
171
int idxFileWrite(IdxFstFile* write, uint8_t* buf, uint32_t len) {
dengyihao's avatar
dengyihao 已提交
172 173 174
  if (write == NULL) {
    return 0;
  }
175
  // update checksum
dengyihao's avatar
dengyihao 已提交
176
  // write data to file/socket or mem
dengyihao's avatar
dengyihao 已提交
177
  IFileCtx* ctx = write->wrt;
dengyihao's avatar
dengyihao 已提交
178

179
  int nWrite = ctx->write(ctx, buf, len);
dengyihao's avatar
dengyihao 已提交
180 181
  assert(nWrite == len);
  write->count += len;
dengyihao's avatar
dengyihao 已提交
182 183

  write->summer = taosCalcChecksum(write->summer, buf, len);
184 185
  return len;
}
dengyihao's avatar
dengyihao 已提交
186
int idxFileRead(IdxFstFile* write, uint8_t* buf, uint32_t len) {
dengyihao's avatar
dengyihao 已提交
187 188 189
  if (write == NULL) {
    return 0;
  }
dengyihao's avatar
dengyihao 已提交
190
  IFileCtx* ctx = write->wrt;
dengyihao's avatar
dengyihao 已提交
191
  return ctx->read(ctx, buf, len);
dengyihao's avatar
dengyihao 已提交
192
}
193

dengyihao's avatar
dengyihao 已提交
194
uint32_t idxFileMaskedCheckSum(IdxFstFile* write) {
dengyihao's avatar
dengyihao 已提交
195
  //////
dengyihao's avatar
dengyihao 已提交
196 197
  return write->summer;
}
dengyihao's avatar
dengyihao 已提交
198

dengyihao's avatar
dengyihao 已提交
199 200
int idxFileFlush(IdxFstFile* write) {
  IFileCtx* ctx = write->wrt;
dengyihao's avatar
dengyihao 已提交
201
  ctx->flush(ctx);
dengyihao's avatar
dengyihao 已提交
202 203 204
  return 1;
}

dengyihao's avatar
dengyihao 已提交
205
void idxFilePackUintIn(IdxFstFile* writer, uint64_t n, uint8_t nBytes) {
dengyihao's avatar
dengyihao 已提交
206
  assert(1 <= nBytes && nBytes <= 8);
wafwerar's avatar
wafwerar 已提交
207
  uint8_t* buf = taosMemoryCalloc(8, sizeof(uint8_t));
dengyihao's avatar
dengyihao 已提交
208
  for (uint8_t i = 0; i < nBytes; i++) {
209
    buf[i] = (uint8_t)n;
dengyihao's avatar
dengyihao 已提交
210 211
    n = n >> 8;
  }
dengyihao's avatar
dengyihao 已提交
212
  idxFileWrite(writer, buf, nBytes);
wafwerar's avatar
wafwerar 已提交
213
  taosMemoryFree(buf);
dengyihao's avatar
dengyihao 已提交
214 215
  return;
}
dengyihao's avatar
dengyihao 已提交
216

dengyihao's avatar
dengyihao 已提交
217
uint8_t idxFilePackUint(IdxFstFile* writer, uint64_t n) {
dengyihao's avatar
dengyihao 已提交
218
  uint8_t nBytes = packSize(n);
dengyihao's avatar
dengyihao 已提交
219
  idxFilePackUintIn(writer, n, nBytes);
220 221
  return nBytes;
}