tqMetaStore.c 19.9 KB
Newer Older
L
Liu Jicong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * 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/>.
 */
H
Hongze Cheng 已提交
15
#include "vnodeInt.h"
H
Hongze Cheng 已提交
16 17 18 19
// #include <fcntl.h>
// #include <string.h>
// #include <unistd.h>
// #include "osDir.h"
L
Liu Jicong 已提交
20

L
Liu Jicong 已提交
21
#define TQ_META_NAME "tq.meta"
H
Hongze Cheng 已提交
22
#define TQ_IDX_NAME  "tq.idx"
L
Liu Jicong 已提交
23

L
Liu Jicong 已提交
24 25
static int32_t tqHandlePutCommitted(STqMetaStore*, int64_t key, void* value);
static void*   tqHandleGetUncommitted(STqMetaStore*, int64_t key);
L
Liu Jicong 已提交
26

L
Liu Jicong 已提交
27 28
static inline void tqLinkUnpersist(STqMetaStore* pMeta, STqMetaList* pNode) {
  if (pNode->unpersistNext == NULL) {
L
Liu Jicong 已提交
29 30 31 32 33
    pNode->unpersistNext = pMeta->unpersistHead->unpersistNext;
    pNode->unpersistPrev = pMeta->unpersistHead;
    pMeta->unpersistHead->unpersistNext->unpersistPrev = pNode;
    pMeta->unpersistHead->unpersistNext = pNode;
  }
L
Liu Jicong 已提交
34 35
}

36 37
static inline int64_t tqSeekLastPage(TdFilePtr pFile) {
  int offset = taosLSeekFile(pFile, 0, SEEK_END);
L
Liu Jicong 已提交
38 39
  int pageNo = offset / TQ_PAGE_SIZE;
  int curPageOffset = pageNo * TQ_PAGE_SIZE;
40
  return taosLSeekFile(pFile, curPageOffset, SEEK_SET);
L
Liu Jicong 已提交
41 42
}

L
Liu Jicong 已提交
43 44
// TODO: the struct is tightly coupled with index entry
typedef struct STqIdxPageHead {
L
Liu Jicong 已提交
45 46
  int16_t writeOffset;
  int8_t  unused[14];
L
Liu Jicong 已提交
47
} STqIdxPageHead;
L
Liu Jicong 已提交
48

L
Liu Jicong 已提交
49 50 51 52
typedef struct STqIdxPageBuf {
  STqIdxPageHead head;
  char           buffer[TQ_IDX_PAGE_BODY_SIZE];
} STqIdxPageBuf;
L
Liu Jicong 已提交
53

54 55
static inline int tqReadLastPage(TdFilePtr pFile, STqIdxPageBuf* pBuf) {
  int offset = tqSeekLastPage(pFile);
L
Liu Jicong 已提交
56
  int nBytes;
57
  if ((nBytes = taosReadFile(pFile, pBuf, TQ_PAGE_SIZE)) == -1) {
L
Liu Jicong 已提交
58
    terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
59 60
    return -1;
  }
L
Liu Jicong 已提交
61
  if (nBytes == 0) {
L
Liu Jicong 已提交
62 63 64 65 66
    memset(pBuf, 0, TQ_PAGE_SIZE);
    pBuf->head.writeOffset = TQ_IDX_PAGE_HEAD_SIZE;
  }
  ASSERT(nBytes == 0 || nBytes == pBuf->head.writeOffset);

67
  return taosLSeekFile(pFile, offset, SEEK_SET);
L
Liu Jicong 已提交
68
}
L
Liu Jicong 已提交
69

L
Liu Jicong 已提交
70 71
STqMetaStore* tqStoreOpen(STQ* pTq, const char* path, FTqSerialize serializer, FTqDeserialize deserializer,
                          FTqDelete deleter, int32_t tqConfigFlag) {
wafwerar's avatar
wafwerar 已提交
72
  STqMetaStore* pMeta = taosMemoryCalloc(1, sizeof(STqMetaStore));
L
Liu Jicong 已提交
73
  if (pMeta == NULL) {
L
Liu Jicong 已提交
74
    terrno = TSDB_CODE_TQ_OUT_OF_MEMORY;
L
Liu Jicong 已提交
75 76
    return NULL;
  }
L
Liu Jicong 已提交
77
  pMeta->pTq = pTq;
L
Liu Jicong 已提交
78

L
Liu Jicong 已提交
79
  // concat data file name and index file name
L
Liu Jicong 已提交
80
  size_t pathLen = strlen(path);
wafwerar's avatar
wafwerar 已提交
81
  pMeta->dirPath = taosMemoryMalloc(pathLen + 1);
L
Liu Jicong 已提交
82 83
  if (pMeta->dirPath == NULL) {
    terrno = TSDB_CODE_TQ_OUT_OF_MEMORY;
wafwerar's avatar
wafwerar 已提交
84
    taosMemoryFree(pMeta);
L
Liu Jicong 已提交
85
    return NULL;
L
Liu Jicong 已提交
86 87
  }
  strcpy(pMeta->dirPath, path);
L
Liu Jicong 已提交
88 89

  char name[pathLen + 10];
L
Liu Jicong 已提交
90

L
Liu Jicong 已提交
91
  strcpy(name, path);
92
  if (!taosDirExist(name) && taosMkDir(name) != 0) {
L
Liu Jicong 已提交
93 94
    terrno = TSDB_CODE_TQ_FAILED_TO_CREATE_DIR;
    tqError("failed to create dir:%s since %s ", name, terrstr());
L
Liu Jicong 已提交
95
  }
L
Liu Jicong 已提交
96
  strcat(name, "/" TQ_IDX_NAME);
97
  TdFilePtr pIdxFile = taosOpenFile(name, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_READ);
98
  if (pIdxFile == NULL) {
L
Liu Jicong 已提交
99 100
    terrno = TAOS_SYSTEM_ERROR(errno);
    tqError("failed to open file:%s since %s ", name, terrstr());
L
Liu Jicong 已提交
101
    // free memory
L
Liu Jicong 已提交
102 103
    return NULL;
  }
L
Liu Jicong 已提交
104

105
  pMeta->pIdxFile = pIdxFile;
wafwerar's avatar
wafwerar 已提交
106
  pMeta->unpersistHead = taosMemoryCalloc(1, sizeof(STqMetaList));
L
Liu Jicong 已提交
107
  if (pMeta->unpersistHead == NULL) {
L
Liu Jicong 已提交
108
    terrno = TSDB_CODE_TQ_OUT_OF_MEMORY;
L
Liu Jicong 已提交
109 110
    return NULL;
  }
L
Liu Jicong 已提交
111
  pMeta->unpersistHead->unpersistNext = pMeta->unpersistHead->unpersistPrev = pMeta->unpersistHead;
L
Liu Jicong 已提交
112 113 114

  strcpy(name, path);
  strcat(name, "/" TQ_META_NAME);
115
  TdFilePtr pFile = taosOpenFile(name, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_READ);
116
  if (pFile == NULL) {
L
Liu Jicong 已提交
117 118
    terrno = TAOS_SYSTEM_ERROR(errno);
    tqError("failed to open file:%s since %s", name, terrstr());
L
Liu Jicong 已提交
119 120
    return NULL;
  }
L
Liu Jicong 已提交
121

122
  pMeta->pFile = pFile;
L
Liu Jicong 已提交
123

L
Liu Jicong 已提交
124 125 126
  pMeta->pSerializer = serializer;
  pMeta->pDeserializer = deserializer;
  pMeta->pDeleter = deleter;
L
Liu Jicong 已提交
127
  pMeta->tqConfigFlag = tqConfigFlag;
L
Liu Jicong 已提交
128

L
Liu Jicong 已提交
129 130
  // read idx file and load into memory
  STqIdxPageBuf      idxBuf;
wafwerar's avatar
wafwerar 已提交
131
  STqSerializedHead* serializedObj = taosMemoryMalloc(TQ_PAGE_SIZE);
L
Liu Jicong 已提交
132
  if (serializedObj == NULL) {
L
Liu Jicong 已提交
133
    terrno = TSDB_CODE_TQ_OUT_OF_MEMORY;
L
Liu Jicong 已提交
134
  }
L
Liu Jicong 已提交
135 136
  int  idxRead;
  int  allocated = TQ_PAGE_SIZE;
L
Liu Jicong 已提交
137
  bool readEnd = false;
138
  while ((idxRead = taosReadFile(pIdxFile, &idxBuf, TQ_PAGE_SIZE))) {
L
Liu Jicong 已提交
139 140
    if (idxRead == -1) {
      // TODO: handle error
L
Liu Jicong 已提交
141 142
      terrno = TAOS_SYSTEM_ERROR(errno);
      tqError("failed to read tq index file since %s", terrstr());
L
Liu Jicong 已提交
143
    }
L
Liu Jicong 已提交
144
    ASSERT(idxBuf.head.writeOffset == idxRead);
L
Liu Jicong 已提交
145 146
    // loop read every entry
    for (int i = 0; i < idxBuf.head.writeOffset - TQ_IDX_PAGE_HEAD_SIZE; i += TQ_IDX_SIZE) {
wafwerar's avatar
wafwerar 已提交
147
      STqMetaList* pNode = taosMemoryCalloc(1, sizeof(STqMetaList));
L
Liu Jicong 已提交
148
      if (pNode == NULL) {
L
Liu Jicong 已提交
149 150
        terrno = TSDB_CODE_TQ_OUT_OF_MEMORY;
        // TODO: free memory
L
Liu Jicong 已提交
151
      }
L
Liu Jicong 已提交
152 153
      memcpy(&pNode->handle, &idxBuf.buffer[i], TQ_IDX_SIZE);

154
      taosLSeekFile(pFile, pNode->handle.offset, SEEK_SET);
L
Liu Jicong 已提交
155
      if (allocated < pNode->handle.serializedSize) {
wafwerar's avatar
wafwerar 已提交
156
        void* ptr = taosMemoryRealloc(serializedObj, pNode->handle.serializedSize);
L
Liu Jicong 已提交
157
        if (ptr == NULL) {
L
Liu Jicong 已提交
158 159
          terrno = TSDB_CODE_TQ_OUT_OF_MEMORY;
          // TODO: free memory
L
Liu Jicong 已提交
160
        }
L
Liu Jicong 已提交
161 162
        serializedObj = ptr;
        allocated = pNode->handle.serializedSize;
L
Liu Jicong 已提交
163
      }
L
Liu Jicong 已提交
164
      serializedObj->ssize = pNode->handle.serializedSize;
165
      if (taosReadFile(pFile, serializedObj, pNode->handle.serializedSize) != pNode->handle.serializedSize) {
L
Liu Jicong 已提交
166
        // TODO: read error
L
Liu Jicong 已提交
167
      }
L
Liu Jicong 已提交
168 169
      if (serializedObj->action == TQ_ACTION_INUSE) {
        if (serializedObj->ssize != sizeof(STqSerializedHead)) {
L
Liu Jicong 已提交
170
          pMeta->pDeserializer(pTq, serializedObj, &pNode->handle.valueInUse);
L
Liu Jicong 已提交
171 172 173
        } else {
          pNode->handle.valueInUse = TQ_DELETE_TOKEN;
        }
L
Liu Jicong 已提交
174 175
      } else if (serializedObj->action == TQ_ACTION_INTXN) {
        if (serializedObj->ssize != sizeof(STqSerializedHead)) {
L
Liu Jicong 已提交
176
          pMeta->pDeserializer(pTq, serializedObj, &pNode->handle.valueInTxn);
L
Liu Jicong 已提交
177 178 179
        } else {
          pNode->handle.valueInTxn = TQ_DELETE_TOKEN;
        }
L
Liu Jicong 已提交
180 181
      } else if (serializedObj->action == TQ_ACTION_INUSE_CONT) {
        if (serializedObj->ssize != sizeof(STqSerializedHead)) {
L
Liu Jicong 已提交
182
          pMeta->pDeserializer(pTq, serializedObj, &pNode->handle.valueInUse);
L
Liu Jicong 已提交
183 184 185
        } else {
          pNode->handle.valueInUse = TQ_DELETE_TOKEN;
        }
L
Liu Jicong 已提交
186 187
        STqSerializedHead* ptr = POINTER_SHIFT(serializedObj, serializedObj->ssize);
        if (ptr->ssize != sizeof(STqSerializedHead)) {
L
Liu Jicong 已提交
188
          pMeta->pDeserializer(pTq, ptr, &pNode->handle.valueInTxn);
L
Liu Jicong 已提交
189 190 191 192 193 194
        } else {
          pNode->handle.valueInTxn = TQ_DELETE_TOKEN;
        }
      } else {
        ASSERT(0);
      }
L
Liu Jicong 已提交
195

L
Liu Jicong 已提交
196 197
      // put into list
      int          bucketKey = pNode->handle.key & TQ_BUCKET_MASK;
L
Liu Jicong 已提交
198
      STqMetaList* pBucketNode = pMeta->bucket[bucketKey];
L
Liu Jicong 已提交
199
      if (pBucketNode == NULL) {
L
Liu Jicong 已提交
200
        pMeta->bucket[bucketKey] = pNode;
L
Liu Jicong 已提交
201
      } else if (pBucketNode->handle.key == pNode->handle.key) {
L
Liu Jicong 已提交
202 203 204
        pNode->next = pBucketNode->next;
        pMeta->bucket[bucketKey] = pNode;
      } else {
L
Liu Jicong 已提交
205 206
        while (pBucketNode->next && pBucketNode->next->handle.key != pNode->handle.key) {
          pBucketNode = pBucketNode->next;
L
Liu Jicong 已提交
207
        }
L
Liu Jicong 已提交
208
        if (pBucketNode->next) {
L
Liu Jicong 已提交
209
          ASSERT(pBucketNode->next->handle.key == pNode->handle.key);
L
Liu Jicong 已提交
210
          STqMetaList* pNodeFound = pBucketNode->next;
L
Liu Jicong 已提交
211 212 213
          pNode->next = pNodeFound->next;
          pBucketNode->next = pNode;
          pBucketNode = pNodeFound;
L
Liu Jicong 已提交
214
        } else {
L
Liu Jicong 已提交
215 216
          pNode->next = pMeta->bucket[bucketKey];
          pMeta->bucket[bucketKey] = pNode;
L
Liu Jicong 已提交
217 218 219
          pBucketNode = NULL;
        }
      }
L
Liu Jicong 已提交
220 221
      if (pBucketNode) {
        if (pBucketNode->handle.valueInUse && pBucketNode->handle.valueInUse != TQ_DELETE_TOKEN) {
L
Liu Jicong 已提交
222
          pMeta->pDeleter(pBucketNode->handle.valueInUse);
L
Liu Jicong 已提交
223
        }
L
Liu Jicong 已提交
224
        if (pBucketNode->handle.valueInTxn && pBucketNode->handle.valueInTxn != TQ_DELETE_TOKEN) {
L
Liu Jicong 已提交
225
          pMeta->pDeleter(pBucketNode->handle.valueInTxn);
L
Liu Jicong 已提交
226
        }
wafwerar's avatar
wafwerar 已提交
227
        taosMemoryFree(pBucketNode);
L
Liu Jicong 已提交
228
      }
L
Liu Jicong 已提交
229 230
    }
  }
wafwerar's avatar
wafwerar 已提交
231
  taosMemoryFree(serializedObj);
L
Liu Jicong 已提交
232 233 234
  return pMeta;
}

L
Liu Jicong 已提交
235
int32_t tqStoreClose(STqMetaStore* pMeta) {
L
Liu Jicong 已提交
236
  // commit data and idx
L
Liu Jicong 已提交
237
  tqStorePersist(pMeta);
L
Liu Jicong 已提交
238
  ASSERT(pMeta->unpersistHead && pMeta->unpersistHead->next == NULL);
239 240
  taosCloseFile(&pMeta->pFile);
  taosCloseFile(&pMeta->pIdxFile);
L
Liu Jicong 已提交
241 242
  // free memory
  for (int i = 0; i < TQ_BUCKET_SIZE; i++) {
L
Liu Jicong 已提交
243
    STqMetaList* pNode = pMeta->bucket[i];
L
Liu Jicong 已提交
244
    while (pNode) {
L
Liu Jicong 已提交
245 246
      ASSERT(pNode->unpersistNext == NULL);
      ASSERT(pNode->unpersistPrev == NULL);
L
Liu Jicong 已提交
247
      if (pNode->handle.valueInTxn && pNode->handle.valueInTxn != TQ_DELETE_TOKEN) {
L
Liu Jicong 已提交
248
        pMeta->pDeleter(pNode->handle.valueInTxn);
L
Liu Jicong 已提交
249
      }
L
Liu Jicong 已提交
250
      if (pNode->handle.valueInUse && pNode->handle.valueInUse != TQ_DELETE_TOKEN) {
L
Liu Jicong 已提交
251
        pMeta->pDeleter(pNode->handle.valueInUse);
L
Liu Jicong 已提交
252
      }
L
Liu Jicong 已提交
253
      STqMetaList* next = pNode->next;
wafwerar's avatar
wafwerar 已提交
254
      taosMemoryFree(pNode);
L
Liu Jicong 已提交
255
      pNode = next;
L
Liu Jicong 已提交
256 257
    }
  }
wafwerar's avatar
wafwerar 已提交
258 259 260
  taosMemoryFree(pMeta->dirPath);
  taosMemoryFree(pMeta->unpersistHead);
  taosMemoryFree(pMeta);
L
Liu Jicong 已提交
261 262 263
  return 0;
}

L
Liu Jicong 已提交
264
int32_t tqStoreDelete(STqMetaStore* pMeta) {
265 266
  taosCloseFile(&pMeta->pFile);
  taosCloseFile(&pMeta->pIdxFile);
L
Liu Jicong 已提交
267 268
  // free memory
  for (int i = 0; i < TQ_BUCKET_SIZE; i++) {
L
Liu Jicong 已提交
269
    STqMetaList* pNode = pMeta->bucket[i];
L
Liu Jicong 已提交
270
    pMeta->bucket[i] = NULL;
L
Liu Jicong 已提交
271 272
    while (pNode) {
      if (pNode->handle.valueInTxn && pNode->handle.valueInTxn != TQ_DELETE_TOKEN) {
L
Liu Jicong 已提交
273
        pMeta->pDeleter(pNode->handle.valueInTxn);
L
Liu Jicong 已提交
274
      }
L
Liu Jicong 已提交
275
      if (pNode->handle.valueInUse && pNode->handle.valueInUse != TQ_DELETE_TOKEN) {
L
Liu Jicong 已提交
276
        pMeta->pDeleter(pNode->handle.valueInUse);
L
Liu Jicong 已提交
277
      }
L
Liu Jicong 已提交
278
      STqMetaList* next = pNode->next;
wafwerar's avatar
wafwerar 已提交
279
      taosMemoryFree(pNode);
L
Liu Jicong 已提交
280 281 282
      pNode = next;
    }
  }
wafwerar's avatar
wafwerar 已提交
283
  taosMemoryFree(pMeta->unpersistHead);
L
Liu Jicong 已提交
284
  taosRemoveDir(pMeta->dirPath);
wafwerar's avatar
wafwerar 已提交
285 286
  taosMemoryFree(pMeta->dirPath);
  taosMemoryFree(pMeta);
L
Liu Jicong 已提交
287 288 289
  return 0;
}

L
Liu Jicong 已提交
290
int32_t tqStorePersist(STqMetaStore* pMeta) {
L
Liu Jicong 已提交
291 292 293 294
  STqIdxPageBuf      idxBuf;
  int64_t*           bufPtr = (int64_t*)idxBuf.buffer;
  STqMetaList*       pHead = pMeta->unpersistHead;
  STqMetaList*       pNode = pHead->unpersistNext;
wafwerar's avatar
wafwerar 已提交
295
  STqSerializedHead* pSHead = taosMemoryMalloc(sizeof(STqSerializedHead));
L
Liu Jicong 已提交
296
  if (pSHead == NULL) {
L
Liu Jicong 已提交
297
    terrno = TSDB_CODE_TQ_OUT_OF_MEMORY;
L
Liu Jicong 已提交
298 299 300 301
    return -1;
  }
  pSHead->ver = TQ_SVER;
  pSHead->checksum = 0;
L
Liu Jicong 已提交
302
  pSHead->ssize = sizeof(STqSerializedHead);
L
Liu Jicong 已提交
303
  /*int allocatedSize = sizeof(STqSerializedHead);*/
304
  int offset = taosLSeekFile(pMeta->pFile, 0, SEEK_CUR);
L
Liu Jicong 已提交
305

306
  tqReadLastPage(pMeta->pIdxFile, &idxBuf);
L
Liu Jicong 已提交
307

L
Liu Jicong 已提交
308
  if (idxBuf.head.writeOffset == TQ_PAGE_SIZE) {
309
    taosLSeekFile(pMeta->pIdxFile, 0, SEEK_END);
L
Liu Jicong 已提交
310 311 312 313 314 315
    memset(&idxBuf, 0, TQ_PAGE_SIZE);
    idxBuf.head.writeOffset = TQ_IDX_PAGE_HEAD_SIZE;
  } else {
    bufPtr = POINTER_SHIFT(&idxBuf, idxBuf.head.writeOffset);
  }

L
Liu Jicong 已提交
316
  while (pHead != pNode) {
L
Liu Jicong 已提交
317 318
    int nBytes = 0;

L
Liu Jicong 已提交
319 320
    if (pNode->handle.valueInUse) {
      if (pNode->handle.valueInTxn) {
L
Liu Jicong 已提交
321 322 323 324 325
        pSHead->action = TQ_ACTION_INUSE_CONT;
      } else {
        pSHead->action = TQ_ACTION_INUSE;
      }

L
Liu Jicong 已提交
326 327
      if (pNode->handle.valueInUse == TQ_DELETE_TOKEN) {
        pSHead->ssize = sizeof(STqSerializedHead);
L
Liu Jicong 已提交
328
      } else {
L
Liu Jicong 已提交
329
        pMeta->pSerializer(pNode->handle.valueInUse, &pSHead);
L
Liu Jicong 已提交
330
      }
331
      nBytes = taosWriteFile(pMeta->pFile, pSHead, pSHead->ssize);
L
Liu Jicong 已提交
332 333 334
      ASSERT(nBytes == pSHead->ssize);
    }

L
Liu Jicong 已提交
335
    if (pNode->handle.valueInTxn) {
L
Liu Jicong 已提交
336
      pSHead->action = TQ_ACTION_INTXN;
L
Liu Jicong 已提交
337 338
      if (pNode->handle.valueInTxn == TQ_DELETE_TOKEN) {
        pSHead->ssize = sizeof(STqSerializedHead);
L
Liu Jicong 已提交
339
      } else {
L
Liu Jicong 已提交
340
        pMeta->pSerializer(pNode->handle.valueInTxn, &pSHead);
L
Liu Jicong 已提交
341
      }
342
      int nBytesTxn = taosWriteFile(pMeta->pFile, pSHead, pSHead->ssize);
L
Liu Jicong 已提交
343 344 345
      ASSERT(nBytesTxn == pSHead->ssize);
      nBytes += nBytesTxn;
    }
L
Liu Jicong 已提交
346 347
    pNode->handle.offset = offset;
    offset += nBytes;
L
Liu Jicong 已提交
348

L
Liu Jicong 已提交
349 350
    // write idx file
    // TODO: endian check and convert
L
Liu Jicong 已提交
351 352 353
    *(bufPtr++) = pNode->handle.key;
    *(bufPtr++) = pNode->handle.offset;
    *(bufPtr++) = (int64_t)nBytes;
L
Liu Jicong 已提交
354
    idxBuf.head.writeOffset += TQ_IDX_SIZE;
L
Liu Jicong 已提交
355

L
Liu Jicong 已提交
356
    if (idxBuf.head.writeOffset >= TQ_PAGE_SIZE) {
357
      nBytes = taosWriteFile(pMeta->pIdxFile, &idxBuf, TQ_PAGE_SIZE);
L
Liu Jicong 已提交
358
      // TODO: handle error with tfile
L
Liu Jicong 已提交
359 360
      ASSERT(nBytes == TQ_PAGE_SIZE);
      memset(&idxBuf, 0, TQ_PAGE_SIZE);
L
Liu Jicong 已提交
361
      idxBuf.head.writeOffset = TQ_IDX_PAGE_HEAD_SIZE;
L
Liu Jicong 已提交
362
      bufPtr = (int64_t*)&idxBuf.buffer;
L
Liu Jicong 已提交
363
    }
L
Liu Jicong 已提交
364
    // remove from unpersist list
L
Liu Jicong 已提交
365 366 367 368 369
    pHead->unpersistNext = pNode->unpersistNext;
    pHead->unpersistNext->unpersistPrev = pHead;
    pNode->unpersistPrev = pNode->unpersistNext = NULL;
    pNode = pHead->unpersistNext;

L
Liu Jicong 已提交
370 371 372
    // remove from bucket
    if (pNode->handle.valueInUse == TQ_DELETE_TOKEN && pNode->handle.valueInTxn == NULL) {
      int          bucketKey = pNode->handle.key & TQ_BUCKET_MASK;
L
Liu Jicong 已提交
373
      STqMetaList* pBucketHead = pMeta->bucket[bucketKey];
L
Liu Jicong 已提交
374
      if (pBucketHead == pNode) {
L
Liu Jicong 已提交
375
        pMeta->bucket[bucketKey] = pNode->next;
L
Liu Jicong 已提交
376
      } else {
L
Liu Jicong 已提交
377
        STqMetaList* pBucketNode = pBucketHead;
L
Liu Jicong 已提交
378 379
        while (pBucketNode->next != NULL && pBucketNode->next != pNode) {
          pBucketNode = pBucketNode->next;
L
Liu Jicong 已提交
380
        }
L
Liu Jicong 已提交
381
        // impossible for pBucket->next == NULL
L
Liu Jicong 已提交
382 383
        ASSERT(pBucketNode->next == pNode);
        pBucketNode->next = pNode->next;
L
Liu Jicong 已提交
384
      }
wafwerar's avatar
wafwerar 已提交
385
      taosMemoryFree(pNode);
L
Liu Jicong 已提交
386
    }
L
Liu Jicong 已提交
387
  }
L
Liu Jicong 已提交
388

L
Liu Jicong 已提交
389
  // write left bytes
wafwerar's avatar
wafwerar 已提交
390
  taosMemoryFree(pSHead);
L
Liu Jicong 已提交
391 392
  // TODO: write new version in tfile
  if ((char*)bufPtr != idxBuf.buffer) {
393
    int nBytes = taosWriteFile(pMeta->pIdxFile, &idxBuf, idxBuf.head.writeOffset);
L
Liu Jicong 已提交
394
    // TODO: handle error in tfile
L
Liu Jicong 已提交
395
    ASSERT(nBytes == idxBuf.head.writeOffset);
L
Liu Jicong 已提交
396
  }
L
Liu Jicong 已提交
397
  // TODO: using fsync in tfile
398 399
  taosFsyncFile(pMeta->pIdxFile);
  taosFsyncFile(pMeta->pFile);
L
Liu Jicong 已提交
400 401 402
  return 0;
}

L
Liu Jicong 已提交
403
static int32_t tqHandlePutCommitted(STqMetaStore* pMeta, int64_t key, void* value) {
L
Liu Jicong 已提交
404
  int64_t      bucketKey = key & TQ_BUCKET_MASK;
L
Liu Jicong 已提交
405
  STqMetaList* pNode = pMeta->bucket[bucketKey];
L
Liu Jicong 已提交
406 407 408
  while (pNode) {
    if (pNode->handle.key == key) {
      if (pNode->handle.valueInUse && pNode->handle.valueInUse != TQ_DELETE_TOKEN) {
L
Liu Jicong 已提交
409
        pMeta->pDeleter(pNode->handle.valueInUse);
L
Liu Jicong 已提交
410
      }
L
Liu Jicong 已提交
411
      // change pointer ownership
L
Liu Jicong 已提交
412
      pNode->handle.valueInUse = value;
L
Liu Jicong 已提交
413
      return 0;
L
Liu Jicong 已提交
414 415 416 417
    } else {
      pNode = pNode->next;
    }
  }
wafwerar's avatar
wafwerar 已提交
418
  STqMetaList* pNewNode = taosMemoryCalloc(1, sizeof(STqMetaList));
L
Liu Jicong 已提交
419
  if (pNewNode == NULL) {
L
Liu Jicong 已提交
420
    terrno = TSDB_CODE_TQ_OUT_OF_MEMORY;
L
Liu Jicong 已提交
421 422 423 424
    return -1;
  }
  pNewNode->handle.key = key;
  pNewNode->handle.valueInUse = value;
L
Liu Jicong 已提交
425
  pNewNode->next = pMeta->bucket[bucketKey];
L
Liu Jicong 已提交
426
  // put into unpersist list
L
Liu Jicong 已提交
427 428 429 430
  pNewNode->unpersistPrev = pMeta->unpersistHead;
  pNewNode->unpersistNext = pMeta->unpersistHead->unpersistNext;
  pMeta->unpersistHead->unpersistNext->unpersistPrev = pNewNode;
  pMeta->unpersistHead->unpersistNext = pNewNode;
L
Liu Jicong 已提交
431 432 433
  return 0;
}

L
Liu Jicong 已提交
434
void* tqHandleGet(STqMetaStore* pMeta, int64_t key) {
L
Liu Jicong 已提交
435
  int64_t      bucketKey = key & TQ_BUCKET_MASK;
L
Liu Jicong 已提交
436
  STqMetaList* pNode = pMeta->bucket[bucketKey];
L
Liu Jicong 已提交
437 438 439
  while (pNode) {
    if (pNode->handle.key == key) {
      if (pNode->handle.valueInUse != NULL && pNode->handle.valueInUse != TQ_DELETE_TOKEN) {
L
Liu Jicong 已提交
440
        return pNode->handle.valueInUse;
L
Liu Jicong 已提交
441 442 443 444 445 446 447
      } else {
        return NULL;
      }
    } else {
      pNode = pNode->next;
    }
  }
L
Liu Jicong 已提交
448 449 450
  return NULL;
}

L
Liu Jicong 已提交
451
void* tqHandleTouchGet(STqMetaStore* pMeta, int64_t key) {
L
Liu Jicong 已提交
452
  int64_t      bucketKey = key & TQ_BUCKET_MASK;
L
Liu Jicong 已提交
453
  STqMetaList* pNode = pMeta->bucket[bucketKey];
L
Liu Jicong 已提交
454 455 456
  while (pNode) {
    if (pNode->handle.key == key) {
      if (pNode->handle.valueInUse != NULL && pNode->handle.valueInUse != TQ_DELETE_TOKEN) {
L
Liu Jicong 已提交
457 458 459 460
        tqLinkUnpersist(pMeta, pNode);
        return pNode->handle.valueInUse;
      } else {
        return NULL;
L
Liu Jicong 已提交
461
      }
L
Liu Jicong 已提交
462 463 464 465
    } else {
      pNode = pNode->next;
    }
  }
L
Liu Jicong 已提交
466
  return NULL;
L
Liu Jicong 已提交
467 468
}

L
Liu Jicong 已提交
469
static inline int32_t tqHandlePutImpl(STqMetaStore* pMeta, int64_t key, void* value) {
L
Liu Jicong 已提交
470
  int64_t      bucketKey = key & TQ_BUCKET_MASK;
L
Liu Jicong 已提交
471
  STqMetaList* pNode = pMeta->bucket[bucketKey];
L
Liu Jicong 已提交
472 473 474
  while (pNode) {
    if (pNode->handle.key == key) {
      if (pNode->handle.valueInTxn) {
L
Liu Jicong 已提交
475
        if (tqDupIntxnReject(pMeta->tqConfigFlag)) {
L
Liu Jicong 已提交
476 477
          terrno = TSDB_CODE_TQ_META_KEY_DUP_IN_TXN;
          return -1;
L
Liu Jicong 已提交
478
        }
L
Liu Jicong 已提交
479
        if (pNode->handle.valueInTxn != TQ_DELETE_TOKEN) {
L
Liu Jicong 已提交
480
          pMeta->pDeleter(pNode->handle.valueInTxn);
L
Liu Jicong 已提交
481
        }
L
Liu Jicong 已提交
482
      }
L
Liu Jicong 已提交
483
      pNode->handle.valueInTxn = value;
L
Liu Jicong 已提交
484
      tqLinkUnpersist(pMeta, pNode);
L
Liu Jicong 已提交
485 486 487 488 489
      return 0;
    } else {
      pNode = pNode->next;
    }
  }
wafwerar's avatar
wafwerar 已提交
490
  STqMetaList* pNewNode = taosMemoryCalloc(1, sizeof(STqMetaList));
L
Liu Jicong 已提交
491
  if (pNewNode == NULL) {
L
Liu Jicong 已提交
492
    terrno = TSDB_CODE_TQ_OUT_OF_MEMORY;
L
Liu Jicong 已提交
493 494 495
    return -1;
  }
  pNewNode->handle.key = key;
L
Liu Jicong 已提交
496
  pNewNode->handle.valueInTxn = value;
L
Liu Jicong 已提交
497
  pNewNode->next = pMeta->bucket[bucketKey];
L
Liu Jicong 已提交
498
  pMeta->bucket[bucketKey] = pNewNode;
L
Liu Jicong 已提交
499
  tqLinkUnpersist(pMeta, pNewNode);
L
Liu Jicong 已提交
500 501 502
  return 0;
}

L
Liu Jicong 已提交
503
int32_t tqHandleMovePut(STqMetaStore* pMeta, int64_t key, void* value) { return tqHandlePutImpl(pMeta, key, value); }
L
Liu Jicong 已提交
504

L
Liu Jicong 已提交
505
int32_t tqHandleCopyPut(STqMetaStore* pMeta, int64_t key, void* value, size_t vsize) {
wafwerar's avatar
wafwerar 已提交
506
  void* vmem = taosMemoryMalloc(vsize);
L
Liu Jicong 已提交
507
  if (vmem == NULL) {
L
Liu Jicong 已提交
508
    terrno = TSDB_CODE_TQ_OUT_OF_MEMORY;
L
Liu Jicong 已提交
509 510 511 512 513 514
    return -1;
  }
  memcpy(vmem, value, vsize);
  return tqHandlePutImpl(pMeta, key, vmem);
}

L
Liu Jicong 已提交
515
static void* tqHandleGetUncommitted(STqMetaStore* pMeta, int64_t key) {
L
Liu Jicong 已提交
516
  int64_t      bucketKey = key & TQ_BUCKET_MASK;
L
Liu Jicong 已提交
517
  STqMetaList* pNode = pMeta->bucket[bucketKey];
L
Liu Jicong 已提交
518 519 520
  while (pNode) {
    if (pNode->handle.key == key) {
      if (pNode->handle.valueInTxn != NULL && pNode->handle.valueInTxn != TQ_DELETE_TOKEN) {
L
Liu Jicong 已提交
521
        return pNode->handle.valueInTxn;
L
Liu Jicong 已提交
522 523 524 525 526 527 528
      } else {
        return NULL;
      }
    } else {
      pNode = pNode->next;
    }
  }
L
Liu Jicong 已提交
529 530 531
  return NULL;
}

L
Liu Jicong 已提交
532
int32_t tqHandleCommit(STqMetaStore* pMeta, int64_t key) {
L
Liu Jicong 已提交
533
  int64_t      bucketKey = key & TQ_BUCKET_MASK;
L
Liu Jicong 已提交
534
  STqMetaList* pNode = pMeta->bucket[bucketKey];
L
Liu Jicong 已提交
535 536 537
  while (pNode) {
    if (pNode->handle.key == key) {
      if (pNode->handle.valueInTxn == NULL) {
L
Liu Jicong 已提交
538
        terrno = TSDB_CODE_TQ_META_KEY_NOT_IN_TXN;
L
Liu Jicong 已提交
539 540
        return -1;
      }
L
Liu Jicong 已提交
541
      if (pNode->handle.valueInUse && pNode->handle.valueInUse != TQ_DELETE_TOKEN) {
L
Liu Jicong 已提交
542
        pMeta->pDeleter(pNode->handle.valueInUse);
L
Liu Jicong 已提交
543 544
      }
      pNode->handle.valueInUse = pNode->handle.valueInTxn;
L
Liu Jicong 已提交
545
      pNode->handle.valueInTxn = NULL;
L
Liu Jicong 已提交
546
      tqLinkUnpersist(pMeta, pNode);
L
Liu Jicong 已提交
547 548 549 550 551
      return 0;
    } else {
      pNode = pNode->next;
    }
  }
L
Liu Jicong 已提交
552 553
  terrno = TSDB_CODE_TQ_META_NO_SUCH_KEY;
  return -1;
L
Liu Jicong 已提交
554 555
}

L
Liu Jicong 已提交
556
int32_t tqHandleAbort(STqMetaStore* pMeta, int64_t key) {
L
Liu Jicong 已提交
557
  int64_t      bucketKey = key & TQ_BUCKET_MASK;
L
Liu Jicong 已提交
558
  STqMetaList* pNode = pMeta->bucket[bucketKey];
L
Liu Jicong 已提交
559 560 561 562
  while (pNode) {
    if (pNode->handle.key == key) {
      if (pNode->handle.valueInTxn) {
        if (pNode->handle.valueInTxn != TQ_DELETE_TOKEN) {
L
Liu Jicong 已提交
563
          pMeta->pDeleter(pNode->handle.valueInTxn);
L
Liu Jicong 已提交
564
        }
L
Liu Jicong 已提交
565
        pNode->handle.valueInTxn = NULL;
L
Liu Jicong 已提交
566
        tqLinkUnpersist(pMeta, pNode);
L
Liu Jicong 已提交
567 568
        return 0;
      }
L
Liu Jicong 已提交
569
      terrno = TSDB_CODE_TQ_META_KEY_NOT_IN_TXN;
L
Liu Jicong 已提交
570 571 572 573 574
      return -1;
    } else {
      pNode = pNode->next;
    }
  }
L
Liu Jicong 已提交
575 576
  terrno = TSDB_CODE_TQ_META_NO_SUCH_KEY;
  return -1;
L
Liu Jicong 已提交
577 578
}

L
Liu Jicong 已提交
579
int32_t tqHandleDel(STqMetaStore* pMeta, int64_t key) {
L
Liu Jicong 已提交
580
  int64_t      bucketKey = key & TQ_BUCKET_MASK;
L
Liu Jicong 已提交
581
  STqMetaList* pNode = pMeta->bucket[bucketKey];
L
Liu Jicong 已提交
582
  while (pNode) {
L
Liu Jicong 已提交
583 584 585 586 587 588 589 590 591
    if (pNode->handle.key == key) {
      if (pNode->handle.valueInTxn != TQ_DELETE_TOKEN) {
        if (pNode->handle.valueInTxn) {
          pMeta->pDeleter(pNode->handle.valueInTxn);
        }

        pNode->handle.valueInTxn = TQ_DELETE_TOKEN;
        tqLinkUnpersist(pMeta, pNode);
        return 0;
L
Liu Jicong 已提交
592
      }
L
Liu Jicong 已提交
593 594 595 596 597 598 599
    } else {
      pNode = pNode->next;
    }
  }
  terrno = TSDB_CODE_TQ_META_NO_SUCH_KEY;
  return -1;
}
L
Liu Jicong 已提交
600

L
Liu Jicong 已提交
601 602 603 604 605 606
int32_t tqHandlePurge(STqMetaStore* pMeta, int64_t key) {
  int64_t      bucketKey = key & TQ_BUCKET_MASK;
  STqMetaList* pNode = pMeta->bucket[bucketKey];
  while (pNode) {
    if (pNode->handle.key == key) {
      pNode->handle.valueInUse = TQ_DELETE_TOKEN;
L
Liu Jicong 已提交
607
      tqLinkUnpersist(pMeta, pNode);
L
Liu Jicong 已提交
608
      return 0;
L
Liu Jicong 已提交
609 610 611 612
    } else {
      pNode = pNode->next;
    }
  }
L
Liu Jicong 已提交
613
  terrno = TSDB_CODE_TQ_META_NO_SUCH_KEY;
L
Liu Jicong 已提交
614
  return -1;
L
Liu Jicong 已提交
615 616
}

L
Liu Jicong 已提交
617 618
// TODO: clean deleted idx and data from persistent file
int32_t tqStoreCompact(STqMetaStore* pMeta) { return 0; }