index_tfile.h 3.9 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
#define TFILE_HEADER_SIZE (sizeof(TFileHeader))
#define TFILE_HEADER_NO_FST (TFILE_HEADER_SIZE - sizeof(int32_t))
dengyihao's avatar
dengyihao 已提交
45 46 47 48 49 50

typedef struct TFileValue {
  char*   colVal;  // null terminated
  SArray* tableId;
  int32_t offset;
} TFileValue;
dengyihao's avatar
dengyihao 已提交
51 52

typedef struct TFileCacheKey {
dengyihao's avatar
dengyihao 已提交
53 54
  uint64_t suid;
  uint8_t  colType;
dengyihao's avatar
dengyihao 已提交
55
  char*    colName;
dengyihao's avatar
dengyihao 已提交
56
  int32_t  nColName;
57
} TFileCacheKey;
dengyihao's avatar
dengyihao 已提交
58 59 60 61

// table cache
// refactor to LRU cache later
typedef struct TFileCache {
dengyihao's avatar
dengyihao 已提交
62
  SHashObj* tableCache;
63 64
  int16_t   capacity;
  // add more param
dengyihao's avatar
dengyihao 已提交
65 66
} TFileCache;

dengyihao's avatar
dengyihao 已提交
67
typedef struct TFileWriter {
dengyihao's avatar
dengyihao 已提交
68 69
  FstBuilder* fb;
  WriterCtx*  ctx;
dengyihao's avatar
dengyihao 已提交
70
  TFileHeader header;
dengyihao's avatar
dengyihao 已提交
71
  uint32_t    offset;
dengyihao's avatar
dengyihao 已提交
72 73
} TFileWriter;

dengyihao's avatar
dengyihao 已提交
74
// multi reader and single write
dengyihao's avatar
dengyihao 已提交
75
typedef struct TFileReader {
76
  T_REF_DECLARE()
dengyihao's avatar
dengyihao 已提交
77 78
  Fst*        fst;
  WriterCtx*  ctx;
dengyihao's avatar
dengyihao 已提交
79
  TFileHeader header;
80
} TFileReader;
dengyihao's avatar
dengyihao 已提交
81

dengyihao's avatar
dengyihao 已提交
82
typedef struct IndexTFile {
dengyihao's avatar
dengyihao 已提交
83 84 85
  char*        path;
  TFileCache*  cache;
  TFileWriter* tw;
dengyihao's avatar
dengyihao 已提交
86 87
} IndexTFile;

dengyihao's avatar
dengyihao 已提交
88 89 90
typedef struct TFileWriterOpt {
  uint64_t suid;
  int8_t   colType;
dengyihao's avatar
dengyihao 已提交
91
  char*    colName;
92
  int32_t  nColName;
dengyihao's avatar
dengyihao 已提交
93
  int32_t  version;
94
} TFileWriterOpt;
dengyihao's avatar
dengyihao 已提交
95 96

typedef struct TFileReaderOpt {
97
  uint64_t suid;
dengyihao's avatar
dengyihao 已提交
98
  char*    colName;
99
  int32_t  nColName;
dengyihao's avatar
dengyihao 已提交
100 101
} TFileReaderOpt;

102
// tfile cache, manage tindex reader
dengyihao's avatar
dengyihao 已提交
103 104 105 106
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 已提交
107

dengyihao's avatar
dengyihao 已提交
108 109
TFileReader* tfileGetReaderByCol(IndexTFile* tf, char* colName);

dengyihao's avatar
dengyihao 已提交
110 111 112
TFileReader* tfileReaderCreate(WriterCtx* ctx);
void         tfileReaderDestroy(TFileReader* reader);
int          tfileReaderSearch(TFileReader* reader, SIndexTermQuery* query, SArray* result);
dengyihao's avatar
dengyihao 已提交
113 114
void         tfileReaderRef(TFileReader* reader);
void         tfileReaderUnRef(TFileReader* reader);
dengyihao's avatar
dengyihao 已提交
115

dengyihao's avatar
dengyihao 已提交
116 117
TFileWriter* tfileWriterOpen(char* path, uint64_t suid, int32_t version, const char* colName, uint8_t type);
void         tfileWriteClose(TFileWriter* tw);
dengyihao's avatar
dengyihao 已提交
118 119
TFileWriter* tfileWriterCreate(WriterCtx* ctx, TFileHeader* header);
void         tfileWriterDestroy(TFileWriter* tw);
dengyihao's avatar
dengyihao 已提交
120
int          tfileWriterPut(TFileWriter* tw, void* data);
dengyihao's avatar
dengyihao 已提交
121
int          tfileWriterFinish(TFileWriter* tw);
dengyihao's avatar
dengyihao 已提交
122

123
//
dengyihao's avatar
dengyihao 已提交
124 125 126
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 已提交
127

dengyihao's avatar
dengyihao 已提交
128 129 130 131 132 133 134 135
Iterate* tfileIteratorCreate(TFileReader* reader);
void     tfileIteratorDestroy(Iterate* iterator);

TFileValue* tfileValueCreate(char* val);

int  tfileValuePush(TFileValue* tf, uint64_t val);
void tfileValueDestroy(TFileValue* tf);

dengyihao's avatar
dengyihao 已提交
136 137 138 139 140 141
#ifdef __cplusplus
}

#endif

#endif