index_fst_counting_writer.h 2.4 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 37 38 39 40 41 42 43 44 45 46 47
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 {
    int fd;
    void *mem;
  };
  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 已提交
48
WriterCtx* writerCtxCreate(WriterType type, bool readOnly);
dengyihao's avatar
dengyihao 已提交
49 50
void writerCtxDestroy(WriterCtx *w);

dengyihao's avatar
dengyihao 已提交
51 52 53 54 55 56 57 58 59
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 已提交
60 61 62
int fstCountingWriterWrite(FstCountingWriter *write, uint8_t *buf, uint32_t len); 

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

dengyihao's avatar
dengyihao 已提交
64
int fstCountingWriterFlush(FstCountingWriter *write);
dengyihao's avatar
dengyihao 已提交
65 66


dengyihao's avatar
dengyihao 已提交
67 68
uint32_t fstCountingWriterMaskedCheckSum(FstCountingWriter *write);

dengyihao's avatar
dengyihao 已提交
69
FstCountingWriter *fstCountingWriterCreate(void *wtr, bool readOnly);
dengyihao's avatar
dengyihao 已提交
70
void fstCountingWriterDestroy(FstCountingWriter *w); 
dengyihao's avatar
dengyihao 已提交
71 72


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


dengyihao's avatar
dengyihao 已提交
77 78 79 80
#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 已提交
81 82 83 84
#ifdef __cplusplus
}
#endif

dengyihao's avatar
dengyihao 已提交
85 86 87
#endif