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 25 26 27
#include "tfile.h"


#define DefaultMem  1024*1024

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

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);
  WriterType type; 
  union {
dengyihao's avatar
dengyihao 已提交
37 38 39 40 41 42 43 44
    struct {
      int fd; 
      bool readOnly;
    } file; 
    struct {
      int32_t capa;
      char *buf;
    } mem;
dengyihao's avatar
dengyihao 已提交
45 46 47 48 49 50 51 52 53
  };
  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);

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

dengyihao's avatar
dengyihao 已提交
57 58 59 60 61 62 63 64 65
typedef uint32_t CheckSummer;


typedef struct FstCountingWriter {
  void*    wrt;  // wrap any writer that counts and checksum bytes written 
  uint64_t count; 
  CheckSummer summer;  
} FstCountingWriter;

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

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

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


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

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


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


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

dengyihao's avatar
dengyihao 已提交
91 92 93
#endif