tsdbFS.h 4.0 KB
Newer Older
H
refact  
Hongze Cheng 已提交
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 _TD_TSDB_FS_H_
#define _TD_TSDB_FS_H_

H
Hongze Cheng 已提交
19
#include "tsdbFile.h"
H
Hongze Cheng 已提交
20

C
Cary Xu 已提交
21 22 23 24
#ifdef __cplusplus
extern "C" {
#endif

L
Liu Jicong 已提交
25 26 27
// ================== TSDB global config
extern bool tsdbForceKeepFile;

H
Hongze Cheng 已提交
28 29
// ================== CURRENT file header info
typedef struct {
H
Hongze Cheng 已提交
30
  uint32_t version;  // Current file system version (relating to code)
H
Hongze Cheng 已提交
31
  uint32_t len;      // Encode content length (including checksum)
H
Hongze Cheng 已提交
32 33 34
} SFSHeader;

// ================== TSDB File System Meta
H
refact  
Hongze Cheng 已提交
35
typedef struct {
H
Hongze Cheng 已提交
36
  uint32_t version;       // Commit version from 0 to increase
H
Hongze Cheng 已提交
37 38
  int64_t  totalPoints;   // total points
  int64_t  totalStorage;  // Uncompressed total storage
H
refact  
Hongze Cheng 已提交
39 40
} STsdbFSMeta;

H
Hongze Cheng 已提交
41
// ==================
H
refact  
Hongze Cheng 已提交
42
typedef struct {
H
Hongze Cheng 已提交
43
  STsdbFSMeta meta;  // FS meta
H
Hongze Cheng 已提交
44
  SArray *    df;    // data file array
45
  SArray *    sf;    // sma data file array    v2f1900.index_name_1
H
Hongze Cheng 已提交
46
} SFSStatus;
H
refact  
Hongze Cheng 已提交
47

C
Cary Xu 已提交
48 49
/**
 * @brief Directory structure of .tsma data files.
C
Cary Xu 已提交
50
 *
51 52 53 54 55 56 57 58 59 60 61
 *  /vnode2/tsdb $ tree tsma/
 *   tsma/
 *   ├── v2f100.index_name_1
 *   ├── v2f101.index_name_1
 *   ├── v2f102.index_name_1
 *   ├── v2f1900.index_name_3
 *   ├── v2f1901.index_name_3
 *   ├── v2f1902.index_name_3
 *   ├── v2f200.index_name_2
 *   ├── v2f201.index_name_2
 *   └── v2f202.index_name_2
C
Cary Xu 已提交
62 63 64 65
 *
 *   0 directories, 9 files
 */

C
Cary Xu 已提交
66
typedef struct {
wafwerar's avatar
wafwerar 已提交
67
  TdThreadRwlock lock;
H
refact  
Hongze Cheng 已提交
68

H
Hongze Cheng 已提交
69 70 71
  SFSStatus *cstatus;        // current status
  SHashObj * metaCache;      // meta cache
  SHashObj * metaCacheComp;  // meta cache for compact
H
Hongze Cheng 已提交
72
  bool       intxn;
H
Hongze Cheng 已提交
73
  SFSStatus *nstatus;  // new status
H
refact  
Hongze Cheng 已提交
74 75
} STsdbFS;

H
Hongze Cheng 已提交
76 77 78
#define FS_CURRENT_STATUS(pfs) ((pfs)->cstatus)
#define FS_NEW_STATUS(pfs) ((pfs)->nstatus)
#define FS_IN_TXN(pfs) (pfs)->intxn
H
refact  
Hongze Cheng 已提交
79
#define FS_VERSION(pfs) ((pfs)->cstatus->meta.version)
H
Hongze Cheng 已提交
80
#define FS_TXN_VERSION(pfs) ((pfs)->nstatus->meta.version)
H
Hongze Cheng 已提交
81

H
refact  
Hongze Cheng 已提交
82
typedef struct {
H
Hongze Cheng 已提交
83
  int        direction;
H
Hongze Cheng 已提交
84
  uint64_t   version;  // current FS version
H
Hongze Cheng 已提交
85
  STsdbFS *  pfs;
H
Hongze Cheng 已提交
86 87
  int        index;  // used to position next fset when version the same
  int        fid;    // used to seek when version is changed
H
Hongze Cheng 已提交
88
  SDFileSet *pSet;
H
refact  
Hongze Cheng 已提交
89 90
} SFSIter;

H
Hongze Cheng 已提交
91 92 93
#define TSDB_FS_ITER_FORWARD TSDB_ORDER_ASC
#define TSDB_FS_ITER_BACKWARD TSDB_ORDER_DESC

H
more  
Hongze Cheng 已提交
94
STsdbFS *tsdbNewFS(const STsdbCfg *pCfg);
H
Hongze Cheng 已提交
95
void *   tsdbFreeFS(STsdbFS *pfs);
H
Hongze Cheng 已提交
96 97 98 99
int      tsdbOpenFS(STsdb *pRepo);
void     tsdbCloseFS(STsdb *pRepo);
void     tsdbStartFSTxn(STsdb *pRepo, int64_t pointsAdd, int64_t storageAdd);
int      tsdbEndFSTxn(STsdb *pRepo);
H
Hongze Cheng 已提交
100 101
int      tsdbEndFSTxnWithError(STsdbFS *pfs);
void     tsdbUpdateFSTxnMeta(STsdbFS *pfs, STsdbFSMeta *pMeta);
H
Hongze Cheng 已提交
102
// void     tsdbUpdateMFile(STsdbFS *pfs, const SMFile *pMFile);
H
more  
Hongze Cheng 已提交
103
int tsdbUpdateDFileSet(STsdbFS *pfs, const SDFileSet *pSet);
H
refact  
Hongze Cheng 已提交
104

H
Hongze Cheng 已提交
105 106 107
void       tsdbFSIterInit(SFSIter *pIter, STsdbFS *pfs, int direction);
void       tsdbFSIterSeek(SFSIter *pIter, int fid);
SDFileSet *tsdbFSIterNext(SFSIter *pIter);
H
Hongze Cheng 已提交
108
int        tsdbLoadMetaCache(STsdb *pRepo, bool recoverMeta);
H
Hongze Cheng 已提交
109

H
Hongze Cheng 已提交
110
static FORCE_INLINE int tsdbRLockFS(STsdbFS *pFs) {
wafwerar's avatar
wafwerar 已提交
111
  int code = taosThreadRwlockRdlock(&(pFs->lock));
H
refact  
Hongze Cheng 已提交
112 113 114 115 116 117 118
  if (code != 0) {
    terrno = TAOS_SYSTEM_ERROR(code);
    return -1;
  }
  return 0;
}

H
Hongze Cheng 已提交
119
static FORCE_INLINE int tsdbWLockFS(STsdbFS *pFs) {
wafwerar's avatar
wafwerar 已提交
120
  int code = taosThreadRwlockWrlock(&(pFs->lock));
H
refact  
Hongze Cheng 已提交
121 122 123 124 125 126 127
  if (code != 0) {
    terrno = TAOS_SYSTEM_ERROR(code);
    return -1;
  }
  return 0;
}

H
Hongze Cheng 已提交
128
static FORCE_INLINE int tsdbUnLockFS(STsdbFS *pFs) {
wafwerar's avatar
wafwerar 已提交
129
  int code = taosThreadRwlockUnlock(&(pFs->lock));
H
refact  
Hongze Cheng 已提交
130 131 132 133 134 135 136
  if (code != 0) {
    terrno = TAOS_SYSTEM_ERROR(code);
    return -1;
  }
  return 0;
}

C
Cary Xu 已提交
137 138 139 140
#ifdef __cplusplus
}
#endif

L
Liu Jicong 已提交
141
#endif /* _TD_TSDB_FS_H_ */