tqMetaStore.c 19.1 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 74
STqMetaStore* tqStoreOpen(const char* path, FTqSerialize serializer, FTqDeserialize deserializer, FTqDelete deleter,
                          int32_t tqConfigFlag) {
  STqMetaStore* pMeta = malloc(sizeof(STqMetaStore));
  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
  memset(pMeta, 0, sizeof(STqMetaStore));
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 85
  if (pMeta->dirPath == NULL) {
    terrno = TSDB_CODE_TQ_OUT_OF_MEMORY;
    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) != 0 && 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);
L
Liu Jicong 已提交
97
  int idxFd = open(name, O_RDWR | O_CREAT, 0755);
L
Liu Jicong 已提交
98
  if (idxFd < 0) {
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

L
Liu Jicong 已提交
105
  pMeta->idxFd = idxFd;
L
Liu Jicong 已提交
106
  pMeta->unpersistHead = malloc(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
  memset(pMeta->unpersistHead, 0, sizeof(STqMetaList));
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 148 149
    // loop read every entry
    for (int i = 0; i < idxBuf.head.writeOffset - TQ_IDX_PAGE_HEAD_SIZE; i += TQ_IDX_SIZE) {
      STqMetaList* pNode = malloc(sizeof(STqMetaList));
      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
      memset(pNode, 0, sizeof(STqMetaList));
L
Liu Jicong 已提交
154 155 156
      memcpy(&pNode->handle, &idxBuf.buffer[i], TQ_IDX_SIZE);

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

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

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

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

L
Liu Jicong 已提交
292
// TODO: wrap in tfile
L
Liu Jicong 已提交
293
int32_t tqStorePersist(STqMetaStore* pMeta) {
L
Liu Jicong 已提交
294 295 296 297 298 299
  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 已提交
300
    terrno = TSDB_CODE_TQ_OUT_OF_MEMORY;
L
Liu Jicong 已提交
301 302 303 304
    return -1;
  }
  pSHead->ver = TQ_SVER;
  pSHead->checksum = 0;
L
Liu Jicong 已提交
305 306
  pSHead->ssize = sizeof(STqSerializedHead);
  int allocatedSize = sizeof(STqSerializedHead);
L
Liu Jicong 已提交
307
  int offset = lseek(pMeta->fileFd, 0, SEEK_CUR);
L
Liu Jicong 已提交
308 309 310

  tqReadLastPage(pMeta->idxFd, &idxBuf);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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