index_tfile.h 3.3 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 19
/*
 * 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_TFILE_H__
#define __INDEX_TFILE_H__

#include "index.h"
#include "indexInt.h"
dengyihao's avatar
dengyihao 已提交
20
#include "index_fst.h"
21 22 23
#include "index_fst_counting_writer.h"
#include "index_tfile.h"
#include "tlockfree.h"
dengyihao's avatar
dengyihao 已提交
24 25 26 27 28

#ifdef __cplusplus
extern "C" {
#endif

dengyihao's avatar
dengyihao 已提交
29
// tfile header content
dengyihao's avatar
dengyihao 已提交
30 31
// |<---suid--->|<---version--->|<-------colName------>|<---type-->|<--fstOffset->|
// |<-uint64_t->|<---int32_t--->|<--TSDB_COL_NAME_LEN-->|<-uint8_t->|<---int32_t-->|
32

dengyihao's avatar
dengyihao 已提交
33
#pragma pack(push, 1)
dengyihao's avatar
dengyihao 已提交
34
typedef struct TFileHeader {
dengyihao's avatar
dengyihao 已提交
35 36
  uint64_t suid;
  int32_t  version;
dengyihao's avatar
dengyihao 已提交
37
  char     colName[TSDB_COL_NAME_LEN];  //
38
  uint8_t  colType;
dengyihao's avatar
dengyihao 已提交
39
  int32_t  fstOffset;
dengyihao's avatar
dengyihao 已提交
40
} TFileHeader;
dengyihao's avatar
dengyihao 已提交
41
#pragma pack(pop)
dengyihao's avatar
dengyihao 已提交
42

dengyihao's avatar
dengyihao 已提交
43 44 45
#define TFILE_HEADER_SIZE (sizeof(TFileHeader))
#define TFILE_HEADER_NO_FST (TFILE_HEADER_SIZE - sizeof(int32_t))
//#define TFILE_HADER_PRE_SIZE (sizeof(uint64_t) + sizeof(int32_t) + sizeof(int32_t))
dengyihao's avatar
dengyihao 已提交
46 47

typedef struct TFileCacheKey {
dengyihao's avatar
dengyihao 已提交
48 49
  uint64_t suid;
  uint8_t  colType;
dengyihao's avatar
dengyihao 已提交
50
  char*    colName;
dengyihao's avatar
dengyihao 已提交
51
  int32_t  nColName;
52
} TFileCacheKey;
dengyihao's avatar
dengyihao 已提交
53 54 55 56

// table cache
// refactor to LRU cache later
typedef struct TFileCache {
dengyihao's avatar
dengyihao 已提交
57
  SHashObj* tableCache;
58 59
  int16_t   capacity;
  // add more param
dengyihao's avatar
dengyihao 已提交
60 61
} TFileCache;

dengyihao's avatar
dengyihao 已提交
62
typedef struct TFileWriter {
dengyihao's avatar
dengyihao 已提交
63 64
  FstBuilder* fb;
  WriterCtx*  ctx;
dengyihao's avatar
dengyihao 已提交
65
  TFileHeader header;
dengyihao's avatar
dengyihao 已提交
66
  uint32_t    offset;
dengyihao's avatar
dengyihao 已提交
67 68
} TFileWriter;

dengyihao's avatar
dengyihao 已提交
69
// multi reader and single write
dengyihao's avatar
dengyihao 已提交
70
typedef struct TFileReader {
71
  T_REF_DECLARE()
dengyihao's avatar
dengyihao 已提交
72 73
  Fst*        fst;
  WriterCtx*  ctx;
dengyihao's avatar
dengyihao 已提交
74
  TFileHeader header;
75
} TFileReader;
dengyihao's avatar
dengyihao 已提交
76

dengyihao's avatar
dengyihao 已提交
77
typedef struct IndexTFile {
dengyihao's avatar
dengyihao 已提交
78 79 80
  char*        path;
  TFileCache*  cache;
  TFileWriter* tw;
dengyihao's avatar
dengyihao 已提交
81 82
} IndexTFile;

dengyihao's avatar
dengyihao 已提交
83 84 85
typedef struct TFileWriterOpt {
  uint64_t suid;
  int8_t   colType;
dengyihao's avatar
dengyihao 已提交
86
  char*    colName;
87
  int32_t  nColName;
dengyihao's avatar
dengyihao 已提交
88
  int32_t  version;
89
} TFileWriterOpt;
dengyihao's avatar
dengyihao 已提交
90 91

typedef struct TFileReaderOpt {
92
  uint64_t suid;
dengyihao's avatar
dengyihao 已提交
93
  char*    colName;
94
  int32_t  nColName;
dengyihao's avatar
dengyihao 已提交
95 96
} TFileReaderOpt;

97
// tfile cache, manage tindex reader
dengyihao's avatar
dengyihao 已提交
98 99 100 101
TFileCache*  tfileCacheCreate(const char* path);
void         tfileCacheDestroy(TFileCache* tcache);
TFileReader* tfileCacheGet(TFileCache* tcache, TFileCacheKey* key);
void         tfileCachePut(TFileCache* tcache, TFileCacheKey* key, TFileReader* reader);
dengyihao's avatar
dengyihao 已提交
102

dengyihao's avatar
dengyihao 已提交
103 104 105
TFileReader* tfileReaderCreate(WriterCtx* ctx);
void         tfileReaderDestroy(TFileReader* reader);
int          tfileReaderSearch(TFileReader* reader, SIndexTermQuery* query, SArray* result);
dengyihao's avatar
dengyihao 已提交
106

dengyihao's avatar
dengyihao 已提交
107 108
TFileWriter* tfileWriterCreate(WriterCtx* ctx, TFileHeader* header);
void         tfileWriterDestroy(TFileWriter* tw);
dengyihao's avatar
dengyihao 已提交
109
int          tfileWriterPut(TFileWriter* tw, void* data);
dengyihao's avatar
dengyihao 已提交
110
int          tfileWriterFinish(TFileWriter* tw);
dengyihao's avatar
dengyihao 已提交
111

112
//
dengyihao's avatar
dengyihao 已提交
113 114 115
IndexTFile* indexTFileCreate(const char* path);
int         indexTFilePut(void* tfile, SIndexTerm* term, uint64_t uid);
int         indexTFileSearch(void* tfile, SIndexTermQuery* query, SArray* result);
dengyihao's avatar
dengyihao 已提交
116

dengyihao's avatar
dengyihao 已提交
117 118 119 120 121 122
#ifdef __cplusplus
}

#endif

#endif