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
/*
 * 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 "indexInt.h"
dengyihao's avatar
dengyihao 已提交
19
#include "index_fst.h"
20 21 22
#include "index_fst_counting_writer.h"
#include "index_tfile.h"
#include "tlockfree.h"
dengyihao's avatar
dengyihao 已提交
23 24 25 26 27

#ifdef __cplusplus
extern "C" {
#endif

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

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

dengyihao's avatar
dengyihao 已提交
42 43
#define TFILE_HEADER_SIZE (sizeof(TFileHeader))
#define TFILE_HEADER_NO_FST (TFILE_HEADER_SIZE - sizeof(int32_t))
dengyihao's avatar
dengyihao 已提交
44 45 46 47 48 49

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

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

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

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

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

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

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

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

dengyihao's avatar
dengyihao 已提交
101
TFileReader* tfileGetReaderByCol(IndexTFile* tf, uint64_t suid, char* colName);
dengyihao's avatar
dengyihao 已提交
102

dengyihao's avatar
dengyihao 已提交
103
TFileReader* tfileReaderOpen(char* path, uint64_t suid, int32_t version, const char* colName);
dengyihao's avatar
dengyihao 已提交
104 105 106
TFileReader* tfileReaderCreate(WriterCtx* ctx);
void         tfileReaderDestroy(TFileReader* reader);
int          tfileReaderSearch(TFileReader* reader, SIndexTermQuery* query, SArray* result);
dengyihao's avatar
dengyihao 已提交
107 108
void         tfileReaderRef(TFileReader* reader);
void         tfileReaderUnRef(TFileReader* reader);
dengyihao's avatar
dengyihao 已提交
109

dengyihao's avatar
dengyihao 已提交
110
TFileWriter* tfileWriterOpen(char* path, uint64_t suid, int32_t version, const char* colName, uint8_t type);
dengyihao's avatar
dengyihao 已提交
111
void         tfileWriterClose(TFileWriter* tw);
dengyihao's avatar
dengyihao 已提交
112 113
TFileWriter* tfileWriterCreate(WriterCtx* ctx, TFileHeader* header);
void         tfileWriterDestroy(TFileWriter* tw);
dengyihao's avatar
dengyihao 已提交
114
int          tfileWriterPut(TFileWriter* tw, void* data, bool order);
dengyihao's avatar
dengyihao 已提交
115
int          tfileWriterFinish(TFileWriter* tw);
dengyihao's avatar
dengyihao 已提交
116

117
//
dengyihao's avatar
dengyihao 已提交
118
IndexTFile* indexTFileCreate(const char* path);
119
void        indexTFileDestroy(IndexTFile* tfile);
dengyihao's avatar
dengyihao 已提交
120 121
int         indexTFilePut(void* tfile, SIndexTerm* term, uint64_t uid);
int         indexTFileSearch(void* tfile, SIndexTermQuery* query, SArray* result);
dengyihao's avatar
dengyihao 已提交
122

dengyihao's avatar
dengyihao 已提交
123 124 125 126 127 128 129 130
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 已提交
131 132 133 134 135 136
#ifdef __cplusplus
}

#endif

#endif