index_fst_counting_writer.c 1.9 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/>.
 */
dengyihao's avatar
dengyihao 已提交
15
#include "tutil.h"
dengyihao's avatar
dengyihao 已提交
16
#include "index_fst_util.h"
dengyihao's avatar
dengyihao 已提交
17
#include "index_fst_counting_writer.h"
dengyihao's avatar
dengyihao 已提交
18 19 20 21

FstCountingWriter *fstCountingWriterCreate(void *wrt) {
  FstCountingWriter *cw = calloc(1, sizeof(FstCountingWriter)); 
  if (cw == NULL) { return NULL; }
dengyihao's avatar
dengyihao 已提交
22

dengyihao's avatar
dengyihao 已提交
23 24 25
  cw->wrt = wrt; 
  return cw; 
}
dengyihao's avatar
dengyihao 已提交
26
void fstCountingWriterDestroy(FstCountingWriter *cw) {
dengyihao's avatar
dengyihao 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39
  // free wrt object: close fd or free mem 
  free(cw);
}

uint64_t fstCountingWriterWrite(FstCountingWriter *write, uint8_t *buf, uint32_t bufLen) {
  if (write == NULL) { return 0; } 
  // update checksum 
  // write data to file/socket or mem
  
  write->count += bufLen;
  return bufLen; 
} 

dengyihao's avatar
dengyihao 已提交
40 41 42
uint32_t fstCountingWriterMaskedCheckSum(FstCountingWriter *write) {
  return 0;
}
dengyihao's avatar
dengyihao 已提交
43
int fstCountingWriterFlush(FstCountingWriter *write) {
dengyihao's avatar
dengyihao 已提交
44 45 46 47
  //write->wtr->flush
  return 1;
}

dengyihao's avatar
dengyihao 已提交
48
void fstCountingWriterPackUintIn(FstCountingWriter *writer, uint64_t n,  uint8_t nBytes) {
dengyihao's avatar
dengyihao 已提交
49 50 51 52 53 54 55 56
  assert(1 <= nBytes && nBytes <= 8);
  uint8_t *buf = calloc(8, sizeof(uint8_t));  
  for (uint8_t i = 0; i < nBytes; i++) {
    buf[i] = (uint8_t)n; 
    n = n >> 8;
  }
  fstCountingWriterWrite(writer, buf, nBytes);
  free(buf);
dengyihao's avatar
dengyihao 已提交
57 58
  return;
}
dengyihao's avatar
dengyihao 已提交
59

dengyihao's avatar
dengyihao 已提交
60 61 62 63 64 65
uint8_t fstCountingWriterPackUint(FstCountingWriter *writer, uint64_t n) {
  uint8_t nBytes = packSize(n);
  fstCountingWriterPackUintIn(writer, n, nBytes);
  return nBytes; 
} 

dengyihao's avatar
dengyihao 已提交
66