tsdbFS.h 3.2 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 19 20 21 22
/*
 * 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_

#ifdef __cplusplus
extern "C" {
#endif

H
Hongze Cheng 已提交
23 24 25 26 27
#define TSDB_FS_VERSION 0

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

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

H
Hongze Cheng 已提交
38
// ==================
H
refact  
Hongze Cheng 已提交
39
typedef struct {
H
Hongze Cheng 已提交
40
  STsdbFSMeta meta;  // FS meta
H
Hongze Cheng 已提交
41
  SMFile*     pmf;   // meta file pointer
H
Hongze Cheng 已提交
42 43 44
  SMFile      mf;    // meta file
  SArray*     df;    // data file array
} SFSStatus;
H
refact  
Hongze Cheng 已提交
45 46 47 48

typedef struct {
  pthread_rwlock_t lock;

H
Hongze Cheng 已提交
49 50
  SFSStatus* cstatus;    // current status
  SHashObj*  metaCache;  // meta cache
H
Hongze Cheng 已提交
51
  bool       intxn;
H
Hongze Cheng 已提交
52
  SFSStatus* nstatus;  // new status
H
refact  
Hongze Cheng 已提交
53 54
} STsdbFS;

H
Hongze Cheng 已提交
55 56 57 58
#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 已提交
59
typedef struct {
H
Hongze Cheng 已提交
60
  int        direction;
H
Hongze Cheng 已提交
61
  uint64_t   version;  // current FS version
H
Hongze Cheng 已提交
62 63 64
  STsdbFS*   pfs;
  int        index;  // used to position next fset when version the same
  int        fid;    // used to seek when version is changed
H
refact  
Hongze Cheng 已提交
65 66 67
  SDFileSet* pSet;
} SFSIter;

H
Hongze Cheng 已提交
68 69 70
#define TSDB_FS_ITER_FORWARD TSDB_ORDER_ASC
#define TSDB_FS_ITER_BACKWARD TSDB_ORDER_DESC

H
Hongze Cheng 已提交
71 72
STsdbFS *tsdbNewFS(int keep, int days);
void *   tsdbFreeFS(STsdbFS *pfs);
H
Hongze Cheng 已提交
73
int      tsdbOpenFS(STsdbFS *pFs, int keep, int days);
H
Hongze Cheng 已提交
74 75 76 77 78 79
void     tsdbCloseFS(STsdbFS *pFs);
int      tsdbStartTxn(STsdbFS *pfs);
int      tsdbEndTxn(STsdbFS *pfs);
int      tsdbEndTxnWithError(STsdbFS *pfs);
void     tsdbUpdateMFile(STsdbFS *pfs, const SMFile *pMFile);
int      tsdbUpdateDFileSet(STsdbFS *pfs, const SDFileSet *pSet);
H
refact  
Hongze Cheng 已提交
80

H
Hongze Cheng 已提交
81 82 83 84
void       tsdbFSIterInit(SFSIter *pIter, STsdbFS *pfs, int direction);
void       tsdbFSIterSeek(SFSIter *pIter, int fid);
SDFileSet *tsdbFSIterNext(SFSIter *pIter);

H
Hongze Cheng 已提交
85
static FORCE_INLINE int tsdbRLockFS(STsdbFS* pFs) {
H
refact  
Hongze Cheng 已提交
86 87 88 89 90 91 92 93
  int code = pthread_rwlock_rdlock(&(pFs->lock));
  if (code != 0) {
    terrno = TAOS_SYSTEM_ERROR(code);
    return -1;
  }
  return 0;
}

H
Hongze Cheng 已提交
94
static FORCE_INLINE int tsdbWLockFS(STsdbFS* pFs) {
H
refact  
Hongze Cheng 已提交
95 96 97 98 99 100 101 102
  int code = pthread_rwlock_wrlock(&(pFs->lock));
  if (code != 0) {
    terrno = TAOS_SYSTEM_ERROR(code);
    return -1;
  }
  return 0;
}

H
Hongze Cheng 已提交
103
static FORCE_INLINE int tsdbUnLockFS(STsdbFS* pFs) {
H
refact  
Hongze Cheng 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116
  int code = pthread_rwlock_unlock(&(pFs->lock));
  if (code != 0) {
    terrno = TAOS_SYSTEM_ERROR(code);
    return -1;
  }
  return 0;
}

#ifdef __cplusplus
}
#endif

#endif /* _TD_TSDB_FS_H_ */