tqMetaStore.c 19.4 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/>.
 */
L
Liu Jicong 已提交
15
#include "tqMetaStore.h"
L
Liu Jicong 已提交
16
// TODO:replace by an abstract file layer
L
Liu Jicong 已提交
17
#include <fcntl.h>
L
Liu Jicong 已提交
18
#include <string.h>
L
Liu Jicong 已提交
19
#include <unistd.h>
L
Liu Jicong 已提交
20
#include "osDir.h"
L
Liu Jicong 已提交
21

L
Liu Jicong 已提交
22
#define TQ_META_NAME "tq.meta"
L
Liu Jicong 已提交
23
#define TQ_IDX_NAME "tq.idx"
L
Liu Jicong 已提交
24

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

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

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

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

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

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

  return lseek(fd, offset, SEEK_SET);
}
L
Liu Jicong 已提交
70

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

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

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

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

L
Liu Jicong 已提交
106
  pMeta->idxFd = idxFd;
L
Liu Jicong 已提交
107
  pMeta->unpersistHead = calloc(1, sizeof(STqMetaList));
L
Liu Jicong 已提交
108
  if (pMeta->unpersistHead == NULL) {
L
Liu Jicong 已提交
109
    terrno = TSDB_CODE_TQ_OUT_OF_MEMORY;
L
Liu Jicong 已提交
110 111
    return NULL;
  }
L
Liu Jicong 已提交
112
  pMeta->unpersistHead->unpersistNext = pMeta->unpersistHead->unpersistPrev = pMeta->unpersistHead;
L
Liu Jicong 已提交
113 114 115

  strcpy(name, path);
  strcat(name, "/" TQ_META_NAME);
L
Liu Jicong 已提交
116
  int fileFd = open(name, O_RDWR | O_CREAT, 0755);
L
Liu Jicong 已提交
117
  if (fileFd < 0) {
L
Liu Jicong 已提交
118 119
    terrno = TAOS_SYSTEM_ERROR(errno);
    tqError("failed to open file:%s since %s", name, terrstr());
L
Liu Jicong 已提交
120 121
    return NULL;
  }
L
Liu Jicong 已提交
122 123

  pMeta->fileFd = fileFd;
L
Liu Jicong 已提交
124

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

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

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

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

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

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

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

  tqReadLastPage(pMeta->idxFd, &idxBuf);

L
Liu Jicong 已提交
309
  if (idxBuf.head.writeOffset == TQ_PAGE_SIZE) {
L
Liu Jicong 已提交
310 311 312 313 314 315 316
    lseek(pMeta->idxFd, 0, SEEK_END);
    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 已提交
317
  while (pHead != pNode) {
L
Liu Jicong 已提交
318 319
    int nBytes = 0;

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

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

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

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

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

L
Liu Jicong 已提交
371 372 373
    // 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 已提交
374
      STqMetaList* pBucketHead = pMeta->bucket[bucketKey];
L
Liu Jicong 已提交
375
      if (pBucketHead == pNode) {
L
Liu Jicong 已提交
376
        pMeta->bucket[bucketKey] = pNode->next;
L
Liu Jicong 已提交
377
      } else {
L
Liu Jicong 已提交
378
        STqMetaList* pBucketNode = pBucketHead;
L
Liu Jicong 已提交
379 380
        while (pBucketNode->next != NULL && pBucketNode->next != pNode) {
          pBucketNode = pBucketNode->next;
L
Liu Jicong 已提交
381
        }
L
Liu Jicong 已提交
382
        // impossible for pBucket->next == NULL
L
Liu Jicong 已提交
383 384
        ASSERT(pBucketNode->next == pNode);
        pBucketNode->next = pNode->next;
L
Liu Jicong 已提交
385
      }
L
Liu Jicong 已提交
386
      free(pNode);
L
Liu Jicong 已提交
387
    }
L
Liu Jicong 已提交
388
  }
L
Liu Jicong 已提交
389

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

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

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

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

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

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

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

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

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

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

L
Liu Jicong 已提交
580
int32_t tqHandleDel(STqMetaStore* pMeta, int64_t key) {
L
Liu Jicong 已提交
581
  int64_t      bucketKey = key & TQ_BUCKET_MASK;
L
Liu Jicong 已提交
582
  STqMetaList* pNode = pMeta->bucket[bucketKey];
L
Liu Jicong 已提交
583
  while (pNode) {
L
Liu Jicong 已提交
584 585 586 587 588 589 590 591 592
    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 已提交
593
      }
L
Liu Jicong 已提交
594 595 596 597 598 599 600
    } else {
      pNode = pNode->next;
    }
  }
  terrno = TSDB_CODE_TQ_META_NO_SUCH_KEY;
  return -1;
}
L
Liu Jicong 已提交
601

L
Liu Jicong 已提交
602 603 604 605 606 607
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 已提交
608
      tqLinkUnpersist(pMeta, pNode);
L
Liu Jicong 已提交
609
      return 0;
L
Liu Jicong 已提交
610 611 612 613
    } else {
      pNode = pNode->next;
    }
  }
L
Liu Jicong 已提交
614
  terrno = TSDB_CODE_TQ_META_NO_SUCH_KEY;
L
Liu Jicong 已提交
615
  return -1;
L
Liu Jicong 已提交
616 617
}

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