index_fst_counting_writer.h 2.5 KB
Newer Older
dengyihao's avatar
dengyihao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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/>.
 */

#ifndef __INDEX_FST_COUNTING_WRITER_H__
#define __INDEX_FST_COUNTING_WRITER_H__

dengyihao's avatar
dengyihao 已提交
19 20 21 22
#ifdef __cplusplus
extern "C" {
#endif

dengyihao's avatar
dengyihao 已提交
23 24
#include "tfile.h"

25
#define DefaultMem 1024 * 1024
dengyihao's avatar
dengyihao 已提交
26

dengyihao's avatar
dengyihao 已提交
27
static char tmpFile[] = "./index";
28
typedef enum WriterType { TMemory, TFile } WriterType;
dengyihao's avatar
dengyihao 已提交
29 30 31 32 33

typedef struct WriterCtx {
  int (*write)(struct WriterCtx *ctx, uint8_t *buf, int len);
  int (*read)(struct WriterCtx *ctx, uint8_t *buf, int len);
  int (*flush)(struct WriterCtx *ctx);
34
  WriterType type;
dengyihao's avatar
dengyihao 已提交
35
  union {
dengyihao's avatar
dengyihao 已提交
36
    struct {
37
      int  fd;
dengyihao's avatar
dengyihao 已提交
38
      bool readOnly;
39
    } file;
dengyihao's avatar
dengyihao 已提交
40 41
    struct {
      int32_t capa;
42
      char *  buf;
dengyihao's avatar
dengyihao 已提交
43
    } mem;
dengyihao's avatar
dengyihao 已提交
44 45 46 47 48 49 50 51 52
  };
  int32_t offset;
  int32_t limit;
} WriterCtx;

static int writeCtxDoWrite(WriterCtx *ctx, uint8_t *buf, int len);
static int writeCtxDoRead(WriterCtx *ctx, uint8_t *buf, int len);
static int writeCtxDoFlush(WriterCtx *ctx);

53 54
WriterCtx *writerCtxCreate(WriterType type, const char *path, bool readOnly, int32_t capacity);
void       writerCtxDestroy(WriterCtx *w);
dengyihao's avatar
dengyihao 已提交
55

dengyihao's avatar
dengyihao 已提交
56 57 58
typedef uint32_t CheckSummer;

typedef struct FstCountingWriter {
59 60 61
  void *      wrt;  // wrap any writer that counts and checksum bytes written
  uint64_t    count;
  CheckSummer summer;
dengyihao's avatar
dengyihao 已提交
62 63
} FstCountingWriter;

64
int fstCountingWriterWrite(FstCountingWriter *write, uint8_t *buf, uint32_t len);
dengyihao's avatar
dengyihao 已提交
65 66

int fstCountingWriterRead(FstCountingWriter *write, uint8_t *buf, uint32_t len);
dengyihao's avatar
dengyihao 已提交
67

dengyihao's avatar
dengyihao 已提交
68
int fstCountingWriterFlush(FstCountingWriter *write);
dengyihao's avatar
dengyihao 已提交
69

dengyihao's avatar
dengyihao 已提交
70 71
uint32_t fstCountingWriterMaskedCheckSum(FstCountingWriter *write);

dengyihao's avatar
dengyihao 已提交
72
FstCountingWriter *fstCountingWriterCreate(void *wtr);
73
void               fstCountingWriterDestroy(FstCountingWriter *w);
dengyihao's avatar
dengyihao 已提交
74

75
void    fstCountingWriterPackUintIn(FstCountingWriter *writer, uint64_t n, uint8_t nBytes);
dengyihao's avatar
dengyihao 已提交
76
uint8_t fstCountingWriterPackUint(FstCountingWriter *writer, uint64_t n);
dengyihao's avatar
dengyihao 已提交
77

dengyihao's avatar
dengyihao 已提交
78 79 80 81
#define FST_WRITER_COUNT(writer) (writer->count)
#define FST_WRITER_INTER_WRITER(writer) (writer->wtr)
#define FST_WRITE_CHECK_SUMMER(writer) (writer->summer)

dengyihao's avatar
dengyihao 已提交
82 83 84 85
#ifdef __cplusplus
}
#endif

dengyihao's avatar
dengyihao 已提交
86
#endif