tqMetaStore.h 3.2 KB
Newer Older
L
Liu Jicong 已提交
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 _TQ_META_STORE_H_
#define _TQ_META_STORE_H_

#include "os.h"
L
Liu Jicong 已提交
20
#include "tq.h"
L
Liu Jicong 已提交
21

L
Liu Jicong 已提交
22 23 24 25 26

#ifdef __cplusplus
extern "C" {
#endif

L
Liu Jicong 已提交
27 28 29
#define TQ_BUCKET_MASK 0xFF
#define TQ_BUCKET_SIZE 256

L
Liu Jicong 已提交
30 31
#define TQ_PAGE_SIZE 4096
//key + offset + size
L
Liu Jicong 已提交
32 33 34 35 36 37 38 39
#define TQ_IDX_SIZE 24
//4096 / 24
#define TQ_MAX_IDX_ONE_PAGE 170
//24 * 170
#define TQ_IDX_PAGE_BODY_SIZE 4080
//4096 - 4080
#define TQ_IDX_PAGE_HEAD_SIZE 16

L
Liu Jicong 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#define TQ_ACTION_CONST      0
#define TQ_ACTION_INUSE      1
#define TQ_ACTION_INUSE_CONT 2
#define TQ_ACTION_INTXN      3

#define TQ_SVER              0

static const int8_t TQ_CONST_DELETE = TQ_ACTION_CONST;
#define TQ_DELETE_TOKEN  (void*)&TQ_CONST_DELETE

typedef struct TqSerializedHead {
  int16_t ver;
  int16_t action;
  int32_t checksum;
  int64_t ssize;
  char    content[];
} TqSerializedHead;

L
Liu Jicong 已提交
58
typedef struct TqMetaHandle {
L
Liu Jicong 已提交
59 60
  int64_t key;
  int64_t offset;
L
Liu Jicong 已提交
61 62 63
  int64_t serializedSize;
  void*   valueInUse;
  void*   valueInTxn;
L
Liu Jicong 已提交
64 65 66 67 68
} TqMetaHandle;

typedef struct TqMetaList {
  TqMetaHandle handle;
  struct TqMetaList* next;
L
Liu Jicong 已提交
69 70
  //struct TqMetaList* inTxnPrev;
  //struct TqMetaList* inTxnNext;
L
Liu Jicong 已提交
71 72 73 74 75
  struct TqMetaList* unpersistPrev;
  struct TqMetaList* unpersistNext;
} TqMetaList;

typedef struct TqMetaStore {
L
Liu Jicong 已提交
76
  TqMetaList* bucket[TQ_BUCKET_SIZE];
L
Liu Jicong 已提交
77
  //a table head
L
Liu Jicong 已提交
78
  TqMetaList* unpersistHead;
L
Liu Jicong 已提交
79 80
  int fileFd; //TODO:temporaral use, to be replaced by unified tfile
  int idxFd;  //TODO:temporaral use, to be replaced by unified tfile
L
Liu Jicong 已提交
81
  char* dirPath;
L
Liu Jicong 已提交
82 83
  int (*serializer)(const void* pObj, TqSerializedHead** ppHead);
  const void* (*deserializer)(const TqSerializedHead* pHead, void** ppObj);
L
Liu Jicong 已提交
84
  void  (*deleter)(void*);
L
Liu Jicong 已提交
85 86
} TqMetaStore;

L
Liu Jicong 已提交
87
TqMetaStore*  tqStoreOpen(const char* path,
L
Liu Jicong 已提交
88 89
    int serializer(const void* pObj, TqSerializedHead** ppHead),
    const void* deserializer(const TqSerializedHead* pHead, void** ppObj),
L
Liu Jicong 已提交
90
    void deleter(void* pObj));
L
Liu Jicong 已提交
91
int32_t       tqStoreClose(TqMetaStore*);
L
Liu Jicong 已提交
92
//int32_t       tqStoreDelete(TqMetaStore*);
L
Liu Jicong 已提交
93 94
//int32_t       TqStoreCommitAll(TqMetaStore*);
int32_t       tqStorePersist(TqMetaStore*);
L
Liu Jicong 已提交
95 96
//clean deleted idx and data from persistent file
int32_t       tqStoreCompact(TqMetaStore*);
L
Liu Jicong 已提交
97

L
Liu Jicong 已提交
98
void*   tqHandleGet(TqMetaStore*, int64_t key);
L
Liu Jicong 已提交
99 100
int32_t tqHandleMovePut(TqMetaStore*, int64_t key, void* value);
int32_t tqHandleCopyPut(TqMetaStore*, int64_t key, void* value, size_t vsize);
L
Liu Jicong 已提交
101 102
//delete committed kv pair
//notice that a delete action still needs to be committed
L
Liu Jicong 已提交
103
int32_t tqHandleDel(TqMetaStore*, int64_t key);
L
Liu Jicong 已提交
104 105
int32_t tqHandleCommit(TqMetaStore*, int64_t key);
int32_t tqHandleAbort(TqMetaStore*, int64_t key);
L
Liu Jicong 已提交
106 107 108 109 110 111

#ifdef __cplusplus
}
#endif

#endif /* ifndef _TQ_META_STORE_H_ */