index_fst_counting_writer.c 5.0 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/>.
 */
15
#include "index_fst_counting_writer.h"
dengyihao's avatar
dengyihao 已提交
16
#include "indexInt.h"
dengyihao's avatar
dengyihao 已提交
17
#include "index_fst_util.h"
18
#include "tutil.h"
dengyihao's avatar
dengyihao 已提交
19

dengyihao's avatar
dengyihao 已提交
20
static int writeCtxDoWrite(WriterCtx* ctx, uint8_t* buf, int len) {
dengyihao's avatar
dengyihao 已提交
21
  if (ctx->offset + len > ctx->limit) { return -1; }
dengyihao's avatar
dengyihao 已提交
22 23

  if (ctx->type == TFile) {
dengyihao's avatar
dengyihao 已提交
24
    assert(len == tfWrite(ctx->file.fd, 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 writeCtxDoRead(WriterCtx* ctx, uint8_t* buf, int len) {
32
  int nRead = 0;
dengyihao's avatar
dengyihao 已提交
33
  if (ctx->type == TFile) {
dengyihao's avatar
dengyihao 已提交
34
    nRead = tfRead(ctx->file.fd, buf, len);
dengyihao's avatar
dengyihao 已提交
35
  } else {
dengyihao's avatar
dengyihao 已提交
36
    memcpy(buf, ctx->mem.buf + ctx->offset, len);
dengyihao's avatar
dengyihao 已提交
37
  }
dengyihao's avatar
dengyihao 已提交
38
  ctx->offset += nRead;
dengyihao's avatar
dengyihao 已提交
39

dengyihao's avatar
dengyihao 已提交
40
  return nRead;
41
}
dengyihao's avatar
dengyihao 已提交
42 43 44
static int writeCtxDoReadFrom(WriterCtx* ctx, uint8_t* buf, int len, int32_t offset) {
  int nRead = 0;
  if (ctx->type == TFile) {
dengyihao's avatar
dengyihao 已提交
45 46
    // tfLseek(ctx->file.fd, offset, 0);
    nRead = tfPread(ctx->file.fd, buf, len, offset);
dengyihao's avatar
dengyihao 已提交
47 48 49 50 51 52
  } else {
    // refactor later
    assert(0);
  }
  return nRead;
}
dengyihao's avatar
dengyihao 已提交
53
static int writeCtxDoFlush(WriterCtx* ctx) {
dengyihao's avatar
dengyihao 已提交
54
  if (ctx->type == TFile) {
dengyihao's avatar
dengyihao 已提交
55
    // taosFsyncFile(ctx->file.fd);
dengyihao's avatar
dengyihao 已提交
56
    tfFsync(ctx->file.fd);
57
    // tfFlush(ctx->file.fd);
dengyihao's avatar
dengyihao 已提交
58 59 60 61 62 63
  } else {
    // do nothing
  }
  return 1;
}

dengyihao's avatar
dengyihao 已提交
64 65
WriterCtx* writerCtxCreate(WriterType type, const char* path, bool readOnly, int32_t capacity) {
  WriterCtx* ctx = calloc(1, sizeof(WriterCtx));
dengyihao's avatar
dengyihao 已提交
66
  if (ctx == NULL) { return NULL; }
dengyihao's avatar
dengyihao 已提交
67

dengyihao's avatar
dengyihao 已提交
68
  ctx->type = type;
dengyihao's avatar
dengyihao 已提交
69
  if (ctx->type == TFile) {
dengyihao's avatar
dengyihao 已提交
70
    // ugly code, refactor later
dengyihao's avatar
dengyihao 已提交
71
    ctx->file.readOnly = readOnly;
dengyihao's avatar
dengyihao 已提交
72
    if (readOnly == false) {
dengyihao's avatar
dengyihao 已提交
73
      // ctx->file.fd = open(path, O_WRONLY | O_CREAT | O_APPEND, S_IRWXU | S_IRWXG | S_IRWXO);
dengyihao's avatar
dengyihao 已提交
74
      ctx->file.fd = tfOpenCreateWriteAppend(path);
dengyihao's avatar
dengyihao 已提交
75

dengyihao's avatar
dengyihao 已提交
76 77 78
      struct stat fstat;
      stat(path, &fstat);
      ctx->file.size = fstat.st_size;
dengyihao's avatar
dengyihao 已提交
79
    } else {
dengyihao's avatar
dengyihao 已提交
80 81
      // ctx->file.fd = open(path, O_RDONLY, S_IRWXU | S_IRWXG | S_IRWXO);
      ctx->file.fd = tfOpenRead(path);
dengyihao's avatar
dengyihao 已提交
82

dengyihao's avatar
dengyihao 已提交
83 84 85
      struct stat fstat;
      stat(path, &fstat);
      ctx->file.size = fstat.st_size;
86
    }
dengyihao's avatar
dengyihao 已提交
87
    memcpy(ctx->file.buf, path, strlen(path));
dengyihao's avatar
dengyihao 已提交
88
    if (ctx->file.fd < 0) {
dengyihao's avatar
dengyihao 已提交
89
      indexError("failed to open file, error %d", errno);
dengyihao's avatar
dengyihao 已提交
90
      goto END;
dengyihao's avatar
dengyihao 已提交
91
    }
dengyihao's avatar
dengyihao 已提交
92
  } else if (ctx->type == TMemory) {
93 94 95
    ctx->mem.buf = calloc(1, sizeof(char) * capacity);
    ctx->mem.capa = capacity;
  }
dengyihao's avatar
dengyihao 已提交
96
  ctx->write = writeCtxDoWrite;
97
  ctx->read = writeCtxDoRead;
dengyihao's avatar
dengyihao 已提交
98
  ctx->flush = writeCtxDoFlush;
dengyihao's avatar
dengyihao 已提交
99
  ctx->readFrom = writeCtxDoReadFrom;
dengyihao's avatar
dengyihao 已提交
100 101

  ctx->offset = 0;
102
  ctx->limit = capacity;
dengyihao's avatar
dengyihao 已提交
103 104

  return ctx;
dengyihao's avatar
dengyihao 已提交
105
END:
dengyihao's avatar
dengyihao 已提交
106
  if (ctx->type == TMemory) { free(ctx->mem.buf); }
dengyihao's avatar
dengyihao 已提交
107
  free(ctx);
dengyihao's avatar
dengyihao 已提交
108
  return NULL;
dengyihao's avatar
dengyihao 已提交
109
}
dengyihao's avatar
dengyihao 已提交
110
void writerCtxDestroy(WriterCtx* ctx, bool remove) {
dengyihao's avatar
dengyihao 已提交
111
  if (ctx->type == TMemory) {
dengyihao's avatar
dengyihao 已提交
112
    free(ctx->mem.buf);
dengyihao's avatar
dengyihao 已提交
113
  } else {
114
    tfClose(ctx->file.fd);
dengyihao's avatar
dengyihao 已提交
115
    ctx->flush(ctx);
dengyihao's avatar
dengyihao 已提交
116
    if (remove) { unlink(ctx->file.buf); }
dengyihao's avatar
dengyihao 已提交
117 118 119 120
  }
  free(ctx);
}

dengyihao's avatar
dengyihao 已提交
121 122
FstCountingWriter* fstCountingWriterCreate(void* wrt) {
  FstCountingWriter* cw = calloc(1, sizeof(FstCountingWriter));
dengyihao's avatar
dengyihao 已提交
123
  if (cw == NULL) { return NULL; }
124

dengyihao's avatar
dengyihao 已提交
125
  cw->wrt = wrt;
126 127
  //(void *)(writerCtxCreate(TFile, readOnly));
  return cw;
dengyihao's avatar
dengyihao 已提交
128
}
dengyihao's avatar
dengyihao 已提交
129
void fstCountingWriterDestroy(FstCountingWriter* cw) {
130
  // free wrt object: close fd or free mem
dengyihao's avatar
dengyihao 已提交
131
  fstCountingWriterFlush(cw);
132
  // writerCtxDestroy((WriterCtx *)(cw->wrt));
dengyihao's avatar
dengyihao 已提交
133 134 135
  free(cw);
}

dengyihao's avatar
dengyihao 已提交
136
int fstCountingWriterWrite(FstCountingWriter* write, uint8_t* buf, uint32_t len) {
dengyihao's avatar
dengyihao 已提交
137
  if (write == NULL) { return 0; }
138
  // update checksum
dengyihao's avatar
dengyihao 已提交
139
  // write data to file/socket or mem
dengyihao's avatar
dengyihao 已提交
140
  WriterCtx* ctx = write->wrt;
dengyihao's avatar
dengyihao 已提交
141

142
  int nWrite = ctx->write(ctx, buf, len);
dengyihao's avatar
dengyihao 已提交
143 144
  assert(nWrite == len);
  write->count += len;
145 146
  return len;
}
dengyihao's avatar
dengyihao 已提交
147
int fstCountingWriterRead(FstCountingWriter* write, uint8_t* buf, uint32_t len) {
dengyihao's avatar
dengyihao 已提交
148
  if (write == NULL) { return 0; }
dengyihao's avatar
dengyihao 已提交
149
  WriterCtx* ctx = write->wrt;
150 151 152
  int        nRead = ctx->read(ctx, buf, len);
  // assert(nRead == len);
  return nRead;
dengyihao's avatar
dengyihao 已提交
153
}
154

dengyihao's avatar
dengyihao 已提交
155
uint32_t fstCountingWriterMaskedCheckSum(FstCountingWriter* write) { return 0; }
dengyihao's avatar
dengyihao 已提交
156 157

int fstCountingWriterFlush(FstCountingWriter* write) {
dengyihao's avatar
dengyihao 已提交
158
  WriterCtx* ctx = write->wrt;
dengyihao's avatar
dengyihao 已提交
159
  ctx->flush(ctx);
160
  // write->wtr->flush
dengyihao's avatar
dengyihao 已提交
161 162 163
  return 1;
}

dengyihao's avatar
dengyihao 已提交
164
void fstCountingWriterPackUintIn(FstCountingWriter* writer, uint64_t n, uint8_t nBytes) {
dengyihao's avatar
dengyihao 已提交
165
  assert(1 <= nBytes && nBytes <= 8);
dengyihao's avatar
dengyihao 已提交
166
  uint8_t* buf = calloc(8, sizeof(uint8_t));
dengyihao's avatar
dengyihao 已提交
167
  for (uint8_t i = 0; i < nBytes; i++) {
168
    buf[i] = (uint8_t)n;
dengyihao's avatar
dengyihao 已提交
169 170 171 172
    n = n >> 8;
  }
  fstCountingWriterWrite(writer, buf, nBytes);
  free(buf);
dengyihao's avatar
dengyihao 已提交
173 174
  return;
}
dengyihao's avatar
dengyihao 已提交
175

dengyihao's avatar
dengyihao 已提交
176
uint8_t fstCountingWriterPackUint(FstCountingWriter* writer, uint64_t n) {
dengyihao's avatar
dengyihao 已提交
177 178
  uint8_t nBytes = packSize(n);
  fstCountingWriterPackUintIn(writer, n, nBytes);
179 180
  return nBytes;
}