index_fst_counting_writer.c 4.2 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 21 22 23 24 25
static int writeCtxDoWrite(WriterCtx *ctx, uint8_t *buf, int len) {
  if (ctx->offset + len > ctx->limit) {
    return -1;
  }

  if (ctx->type == TFile) {
dengyihao's avatar
dengyihao 已提交
26
    assert(len == tfWrite(ctx->file.fd, buf, len));
dengyihao's avatar
dengyihao 已提交
27
  } else {
28 29
    memcpy(ctx->mem.buf + ctx->offset, buf, len);
  }
dengyihao's avatar
dengyihao 已提交
30 31 32 33
  ctx->offset += len;
  return len;
}
static int writeCtxDoRead(WriterCtx *ctx, uint8_t *buf, int len) {
34
  int nRead = 0;
dengyihao's avatar
dengyihao 已提交
35
  if (ctx->type == TFile) {
dengyihao's avatar
dengyihao 已提交
36
    nRead = tfRead(ctx->file.fd, buf, len);
dengyihao's avatar
dengyihao 已提交
37
  } else {
dengyihao's avatar
dengyihao 已提交
38
    memcpy(buf, ctx->mem.buf + ctx->offset, len);
dengyihao's avatar
dengyihao 已提交
39
  }
dengyihao's avatar
dengyihao 已提交
40
  ctx->offset += nRead;
dengyihao's avatar
dengyihao 已提交
41

dengyihao's avatar
dengyihao 已提交
42
  return nRead;
43
}
dengyihao's avatar
dengyihao 已提交
44 45
static int writeCtxDoFlush(WriterCtx *ctx) {
  if (ctx->type == TFile) {
46 47
    // tfFsync(ctx->fd);
    // tfFlush(ctx->file.fd);
dengyihao's avatar
dengyihao 已提交
48 49 50 51 52 53
  } else {
    // do nothing
  }
  return 1;
}

54
WriterCtx *writerCtxCreate(WriterType type, const char *path, bool readOnly, int32_t capacity) {
dengyihao's avatar
dengyihao 已提交
55
  WriterCtx *ctx = calloc(1, sizeof(WriterCtx));
56 57 58
  if (ctx == NULL) {
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
59

dengyihao's avatar
dengyihao 已提交
60
  ctx->type = type;
dengyihao's avatar
dengyihao 已提交
61
  if (ctx->type == TFile) {
dengyihao's avatar
dengyihao 已提交
62
    // ugly code, refactor later
dengyihao's avatar
dengyihao 已提交
63
    ctx->file.readOnly = readOnly;
dengyihao's avatar
dengyihao 已提交
64
    if (readOnly == false) {
65
      ctx->file.fd = tfOpenCreateWriteAppend(tmpFile);
dengyihao's avatar
dengyihao 已提交
66
    } else {
dengyihao's avatar
dengyihao 已提交
67
      ctx->file.fd = tfOpenReadWrite(tmpFile);
68
    }
dengyihao's avatar
dengyihao 已提交
69
    if (ctx->file.fd < 0) {
dengyihao's avatar
dengyihao 已提交
70
      goto END;
71
      indexError("open file error %d", errno);
dengyihao's avatar
dengyihao 已提交
72
    }
dengyihao's avatar
dengyihao 已提交
73
  } else if (ctx->type == TMemory) {
74 75 76
    ctx->mem.buf = calloc(1, sizeof(char) * capacity);
    ctx->mem.capa = capacity;
  }
dengyihao's avatar
dengyihao 已提交
77
  ctx->write = writeCtxDoWrite;
78
  ctx->read = writeCtxDoRead;
dengyihao's avatar
dengyihao 已提交
79 80 81
  ctx->flush = writeCtxDoFlush;

  ctx->offset = 0;
82
  ctx->limit = capacity;
dengyihao's avatar
dengyihao 已提交
83 84

  return ctx;
dengyihao's avatar
dengyihao 已提交
85
END:
86 87 88
  if (ctx->type == TMemory) {
    free(ctx->mem.buf);
  }
dengyihao's avatar
dengyihao 已提交
89
  free(ctx);
dengyihao's avatar
dengyihao 已提交
90 91 92
}
void writerCtxDestroy(WriterCtx *ctx) {
  if (ctx->type == TMemory) {
dengyihao's avatar
dengyihao 已提交
93
    free(ctx->mem.buf);
dengyihao's avatar
dengyihao 已提交
94
  } else {
95
    tfClose(ctx->file.fd);
dengyihao's avatar
dengyihao 已提交
96 97 98 99
  }
  free(ctx);
}

dengyihao's avatar
dengyihao 已提交
100
FstCountingWriter *fstCountingWriterCreate(void *wrt) {
101 102 103 104 105
  FstCountingWriter *cw = calloc(1, sizeof(FstCountingWriter));
  if (cw == NULL) {
    return NULL;
  }

dengyihao's avatar
dengyihao 已提交
106
  cw->wrt = wrt;
107 108
  //(void *)(writerCtxCreate(TFile, readOnly));
  return cw;
dengyihao's avatar
dengyihao 已提交
109
}
dengyihao's avatar
dengyihao 已提交
110
void fstCountingWriterDestroy(FstCountingWriter *cw) {
111
  // free wrt object: close fd or free mem
dengyihao's avatar
dengyihao 已提交
112
  fstCountingWriterFlush(cw);
113
  // writerCtxDestroy((WriterCtx *)(cw->wrt));
dengyihao's avatar
dengyihao 已提交
114 115 116
  free(cw);
}

dengyihao's avatar
dengyihao 已提交
117
int fstCountingWriterWrite(FstCountingWriter *write, uint8_t *buf, uint32_t len) {
118 119 120 121
  if (write == NULL) {
    return 0;
  }
  // update checksum
dengyihao's avatar
dengyihao 已提交
122
  // write data to file/socket or mem
dengyihao's avatar
dengyihao 已提交
123 124
  WriterCtx *ctx = write->wrt;

125
  int nWrite = ctx->write(ctx, buf, len);
dengyihao's avatar
dengyihao 已提交
126 127
  assert(nWrite == len);
  write->count += len;
128 129
  return len;
}
dengyihao's avatar
dengyihao 已提交
130
int fstCountingWriterRead(FstCountingWriter *write, uint8_t *buf, uint32_t len) {
131 132 133
  if (write == NULL) {
    return 0;
  }
dengyihao's avatar
dengyihao 已提交
134
  WriterCtx *ctx = write->wrt;
135 136 137
  int        nRead = ctx->read(ctx, buf, len);
  // assert(nRead == len);
  return nRead;
dengyihao's avatar
dengyihao 已提交
138
}
139 140 141

uint32_t fstCountingWriterMaskedCheckSum(FstCountingWriter *write) { return 0; }
int      fstCountingWriterFlush(FstCountingWriter *write) {
dengyihao's avatar
dengyihao 已提交
142 143
  WriterCtx *ctx = write->wrt;
  ctx->flush(ctx);
144
  // write->wtr->flush
dengyihao's avatar
dengyihao 已提交
145 146 147
  return 1;
}

148
void fstCountingWriterPackUintIn(FstCountingWriter *writer, uint64_t n, uint8_t nBytes) {
dengyihao's avatar
dengyihao 已提交
149
  assert(1 <= nBytes && nBytes <= 8);
150
  uint8_t *buf = calloc(8, sizeof(uint8_t));
dengyihao's avatar
dengyihao 已提交
151
  for (uint8_t i = 0; i < nBytes; i++) {
152
    buf[i] = (uint8_t)n;
dengyihao's avatar
dengyihao 已提交
153 154 155 156
    n = n >> 8;
  }
  fstCountingWriterWrite(writer, buf, nBytes);
  free(buf);
dengyihao's avatar
dengyihao 已提交
157 158
  return;
}
dengyihao's avatar
dengyihao 已提交
159

dengyihao's avatar
dengyihao 已提交
160 161 162
uint8_t fstCountingWriterPackUint(FstCountingWriter *writer, uint64_t n) {
  uint8_t nBytes = packSize(n);
  fstCountingWriterPackUintIn(writer, n, nBytes);
163 164
  return nBytes;
}