index_fst_counting_writer.h 2.7 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

typedef struct WriterCtx {
dengyihao's avatar
dengyihao 已提交
31 32 33
  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);
dengyihao's avatar
dengyihao 已提交
34
  int (*readFrom)(struct WriterCtx* ctx, uint8_t* buf, int len, int32_t offset);
35
  WriterType type;
dengyihao's avatar
dengyihao 已提交
36
  union {
dengyihao's avatar
dengyihao 已提交
37
    struct {
38
      int  fd;
dengyihao's avatar
dengyihao 已提交
39
      bool readOnly;
dengyihao's avatar
dengyihao 已提交
40
      char buf[256];
dengyihao's avatar
dengyihao 已提交
41
      int  size;
42
    } file;
dengyihao's avatar
dengyihao 已提交
43 44
    struct {
      int32_t capa;
dengyihao's avatar
dengyihao 已提交
45
      char*   buf;
dengyihao's avatar
dengyihao 已提交
46
    } mem;
dengyihao's avatar
dengyihao 已提交
47 48 49 50 51
  };
  int32_t offset;
  int32_t limit;
} WriterCtx;

dengyihao's avatar
dengyihao 已提交
52 53
static int writeCtxDoWrite(WriterCtx* ctx, uint8_t* buf, int len);
static int writeCtxDoRead(WriterCtx* ctx, uint8_t* buf, int len);
dengyihao's avatar
dengyihao 已提交
54
static int writeCtxDoReadFrom(WriterCtx* ctx, uint8_t* buf, int len, int32_t offset);
dengyihao's avatar
dengyihao 已提交
55
static int writeCtxDoFlush(WriterCtx* ctx);
dengyihao's avatar
dengyihao 已提交
56

dengyihao's avatar
dengyihao 已提交
57
WriterCtx* writerCtxCreate(WriterType type, const char* path, bool readOnly, int32_t capacity);
dengyihao's avatar
dengyihao 已提交
58
void       writerCtxDestroy(WriterCtx* w, bool remove);
dengyihao's avatar
dengyihao 已提交
59

dengyihao's avatar
dengyihao 已提交
60 61 62
typedef uint32_t CheckSummer;

typedef struct FstCountingWriter {
dengyihao's avatar
dengyihao 已提交
63
  void*       wrt;  // wrap any writer that counts and checksum bytes written
64 65
  uint64_t    count;
  CheckSummer summer;
dengyihao's avatar
dengyihao 已提交
66 67
} FstCountingWriter;

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

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

dengyihao's avatar
dengyihao 已提交
72
int fstCountingWriterFlush(FstCountingWriter* write);
dengyihao's avatar
dengyihao 已提交
73

dengyihao's avatar
dengyihao 已提交
74
uint32_t fstCountingWriterMaskedCheckSum(FstCountingWriter* write);
dengyihao's avatar
dengyihao 已提交
75

dengyihao's avatar
dengyihao 已提交
76 77
FstCountingWriter* fstCountingWriterCreate(void* wtr);
void               fstCountingWriterDestroy(FstCountingWriter* w);
dengyihao's avatar
dengyihao 已提交
78

dengyihao's avatar
dengyihao 已提交
79 80
void    fstCountingWriterPackUintIn(FstCountingWriter* writer, uint64_t n, uint8_t nBytes);
uint8_t fstCountingWriterPackUint(FstCountingWriter* writer, uint64_t n);
dengyihao's avatar
dengyihao 已提交
81

dengyihao's avatar
dengyihao 已提交
82 83 84 85
#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 已提交
86 87 88 89
#ifdef __cplusplus
}
#endif

dengyihao's avatar
dengyihao 已提交
90
#endif