tqMetaStore.c 17.5 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 "osDir.h"
L
Liu Jicong 已提交
18
#include <fcntl.h>
L
Liu Jicong 已提交
19
#include <string.h>
L
Liu Jicong 已提交
20 21
#include <unistd.h>

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

L
Liu Jicong 已提交
25

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

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


L
Liu Jicong 已提交
39 40 41 42 43
typedef struct TqMetaPageBuf {
  int16_t offset;
  char buffer[TQ_PAGE_SIZE];
} TqMetaPageBuf;

L
Liu Jicong 已提交
44
TqMetaStore* tqStoreOpen(const char* path,
L
Liu Jicong 已提交
45 46
    int serializer(const void* pObj, TqSerializedHead** ppHead),
    const void* deserializer(const TqSerializedHead* pHead, void** ppObj),
L
Liu Jicong 已提交
47
    void deleter(void* pObj)) {
L
Liu Jicong 已提交
48 49 50 51 52
  TqMetaStore* pMeta = malloc(sizeof(TqMetaStore)); 
  if(pMeta == NULL) {
    //close
    return NULL;
  }
L
Liu Jicong 已提交
53
  memset(pMeta, 0, sizeof(TqMetaStore));
L
Liu Jicong 已提交
54 55 56

  //concat data file name and index file name
  size_t pathLen = strlen(path);
L
Liu Jicong 已提交
57 58 59 60 61 62
  pMeta->dirPath = malloc(pathLen+1);
  if(pMeta->dirPath != NULL) {
    //TODO: memory insufficient
  }
  strcpy(pMeta->dirPath, path);
  
L
Liu Jicong 已提交
63 64
  char name[pathLen+10];

L
Liu Jicong 已提交
65
  strcpy(name, path);
L
Liu Jicong 已提交
66 67 68
  if(!taosDirExist(name) && !taosMkDir(name)) {
    ASSERT(false);
  }
L
Liu Jicong 已提交
69
  strcat(name, "/" TQ_IDX_NAME);
L
Liu Jicong 已提交
70
  int idxFd = open(name, O_RDWR | O_CREAT, 0755);
L
Liu Jicong 已提交
71
  if(idxFd < 0) {
L
Liu Jicong 已提交
72
    ASSERT(false);
L
Liu Jicong 已提交
73 74 75 76
    //close file
    //free memory
    return NULL;
  }
L
Liu Jicong 已提交
77

L
Liu Jicong 已提交
78 79 80
  pMeta->idxFd = idxFd;
  pMeta->unpersistHead = malloc(sizeof(TqMetaList));
  if(pMeta->unpersistHead == NULL) {
L
Liu Jicong 已提交
81
    ASSERT(false);
L
Liu Jicong 已提交
82 83 84 85
    //close file
    //free memory
    return NULL;
  }
L
Liu Jicong 已提交
86 87 88 89
  memset(pMeta->unpersistHead, 0, sizeof(TqMetaList));
  pMeta->unpersistHead->unpersistNext
    = pMeta->unpersistHead->unpersistPrev
    = pMeta->unpersistHead;
L
Liu Jicong 已提交
90 91 92

  strcpy(name, path);
  strcat(name, "/" TQ_META_NAME);
L
Liu Jicong 已提交
93 94 95 96 97
  int fileFd = open(name, O_RDWR | O_CREAT, 0755);
  if(fileFd < 0){
    ASSERT(false);
    return NULL;
  }
L
Liu Jicong 已提交
98 99 100

  pMeta->fileFd = fileFd;
  
L
Liu Jicong 已提交
101 102 103
  pMeta->serializer = serializer;
  pMeta->deserializer = deserializer;
  pMeta->deleter = deleter;
L
Liu Jicong 已提交
104 105

  //read idx file and load into memory
L
Liu Jicong 已提交
106
  char idxBuf[TQ_PAGE_SIZE];
L
Liu Jicong 已提交
107 108
  TqSerializedHead* serializedObj = malloc(TQ_PAGE_SIZE);
  if(serializedObj == NULL) {
L
Liu Jicong 已提交
109 110
    //TODO:memory insufficient
  }
L
Liu Jicong 已提交
111 112
  int idxRead;
  int allocated = TQ_PAGE_SIZE;
L
Liu Jicong 已提交
113 114 115 116 117
  while((idxRead = read(idxFd, idxBuf, TQ_PAGE_SIZE))) {
    if(idxRead == -1) {
      //TODO: handle error
      ASSERT(false);
    }
L
Liu Jicong 已提交
118
    //loop read every entry
L
Liu Jicong 已提交
119 120
    for(int i = 0; i < idxRead; i += TQ_IDX_ENTRY_SIZE) {
      TqMetaList *pNode = malloc(sizeof(TqMetaList));
L
Liu Jicong 已提交
121 122 123
      if(pNode == NULL) {
        //TODO: free memory and return error
      }
L
Liu Jicong 已提交
124 125 126
      memset(pNode, 0, sizeof(TqMetaList));
      memcpy(&pNode->handle, &idxBuf[i], TQ_IDX_ENTRY_SIZE);
      lseek(fileFd, pNode->handle.offset, SEEK_CUR);
L
Liu Jicong 已提交
127 128
      if(allocated < pNode->handle.serializedSize) {
        void *ptr = realloc(serializedObj, pNode->handle.serializedSize);
L
Liu Jicong 已提交
129 130 131
        if(ptr == NULL) {
          //TODO: memory insufficient
        }
L
Liu Jicong 已提交
132 133
        serializedObj = ptr;
        allocated = pNode->handle.serializedSize;
L
Liu Jicong 已提交
134
      }
L
Liu Jicong 已提交
135 136
      serializedObj->ssize = pNode->handle.serializedSize;
      if(read(fileFd, serializedObj, pNode->handle.serializedSize) != pNode->handle.serializedSize) {
L
Liu Jicong 已提交
137 138
        //TODO: read error
      }
L
Liu Jicong 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
      if(serializedObj->action == TQ_ACTION_INUSE) {
        if(serializedObj->ssize != sizeof(TqSerializedHead)) {
          pMeta->deserializer(serializedObj, &pNode->handle.valueInUse);
        } else {
          pNode->handle.valueInUse = TQ_DELETE_TOKEN;
        }
      } else if(serializedObj->action == TQ_ACTION_INTXN) {
        if(serializedObj->ssize != sizeof(TqSerializedHead)) {
          pMeta->deserializer(serializedObj, &pNode->handle.valueInTxn);
        } else {
          pNode->handle.valueInTxn = TQ_DELETE_TOKEN;
        }
      } else if(serializedObj->action == TQ_ACTION_INUSE_CONT) {
        if(serializedObj->ssize != sizeof(TqSerializedHead)) {
          pMeta->deserializer(serializedObj, &pNode->handle.valueInUse);
        } else {
          pNode->handle.valueInUse = TQ_DELETE_TOKEN;
        }
        serializedObj = POINTER_SHIFT(serializedObj, serializedObj->ssize);
        if(serializedObj->ssize != sizeof(TqSerializedHead)) {
          pMeta->deserializer(serializedObj, &pNode->handle.valueInTxn);
        } else {
          pNode->handle.valueInTxn = TQ_DELETE_TOKEN;
        }
      } else {
        ASSERT(0);
      }
L
Liu Jicong 已提交
166 167

      //put into list
L
Liu Jicong 已提交
168
      int bucketKey = pNode->handle.key & TQ_BUCKET_SIZE;
L
Liu Jicong 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
      TqMetaList* pBucketNode = pMeta->bucket[bucketKey];
      if(pBucketNode == NULL) {
        pMeta->bucket[bucketKey] = pNode;
      } else if(pBucketNode->handle.key == pNode->handle.key) {
        pNode->next = pBucketNode->next;
        pMeta->bucket[bucketKey] = pNode;
      } else {
        while(pBucketNode->next &&
            pBucketNode->next->handle.key == pNode->handle.key) {
          pBucketNode = pBucketNode->next; 
        }
        if(pBucketNode->next) {
          ASSERT(pBucketNode->next->handle.key == pNode->handle.key);
          TqMetaList *pNodeTmp = pBucketNode->next;
          pBucketNode->next = pNodeTmp->next;
          pBucketNode = pNodeTmp;
        } else {
          pBucketNode = NULL;
        }
      }
      if(pBucketNode) {
        if(pBucketNode->handle.valueInUse
            && pBucketNode->handle.valueInUse != TQ_DELETE_TOKEN) {
          pMeta->deleter(pBucketNode->handle.valueInUse);
        }
        if(pBucketNode->handle.valueInTxn
            && pBucketNode->handle.valueInTxn != TQ_DELETE_TOKEN) {
          pMeta->deleter(pBucketNode->handle.valueInTxn);
        }
        free(pBucketNode);
      }
L
Liu Jicong 已提交
200 201
    }
  }
L
Liu Jicong 已提交
202
  free(serializedObj);
L
Liu Jicong 已提交
203 204 205 206 207
  return pMeta;
}

int32_t tqStoreClose(TqMetaStore* pMeta) {
  //commit data and idx
L
Liu Jicong 已提交
208 209 210 211
  tqStorePersist(pMeta);
  ASSERT(pMeta->unpersistHead && pMeta->unpersistHead->next==NULL);
  close(pMeta->fileFd);
  close(pMeta->idxFd);
L
Liu Jicong 已提交
212
  //free memory
L
Liu Jicong 已提交
213
  for(int i = 0; i < TQ_BUCKET_SIZE; i++) {
L
Liu Jicong 已提交
214 215 216 217
    TqMetaList* pNode = pMeta->bucket[i];
    while(pNode) {
      ASSERT(pNode->unpersistNext == NULL);
      ASSERT(pNode->unpersistPrev == NULL);
L
Liu Jicong 已提交
218 219
      if(pNode->handle.valueInTxn
          && pNode->handle.valueInTxn != TQ_DELETE_TOKEN) {
L
Liu Jicong 已提交
220
        pMeta->deleter(pNode->handle.valueInTxn);
L
Liu Jicong 已提交
221
      }
L
Liu Jicong 已提交
222 223
      if(pNode->handle.valueInUse
          && pNode->handle.valueInUse != TQ_DELETE_TOKEN) {
L
Liu Jicong 已提交
224
        pMeta->deleter(pNode->handle.valueInUse);
L
Liu Jicong 已提交
225
      }
L
Liu Jicong 已提交
226 227 228
      TqMetaList* next = pNode->next;
      free(pNode);
      pNode = next;
L
Liu Jicong 已提交
229 230
    }
  }
L
Liu Jicong 已提交
231
  free(pMeta->dirPath);
L
Liu Jicong 已提交
232
  free(pMeta->unpersistHead);
L
Liu Jicong 已提交
233
  free(pMeta);
L
Liu Jicong 已提交
234 235 236 237
  return 0;
}

int32_t tqStoreDelete(TqMetaStore* pMeta) {
L
Liu Jicong 已提交
238 239
  close(pMeta->fileFd);
  close(pMeta->idxFd);
L
Liu Jicong 已提交
240
  //free memory
L
Liu Jicong 已提交
241 242 243 244
  for(int i = 0; i < TQ_BUCKET_SIZE; i++) {
    TqMetaList* pNode = pMeta->bucket[i];
    pMeta->bucket[i] = NULL;
    while(pNode) {
L
Liu Jicong 已提交
245 246
      if(pNode->handle.valueInTxn
          && pNode->handle.valueInTxn != TQ_DELETE_TOKEN) {
L
Liu Jicong 已提交
247 248
        pMeta->deleter(pNode->handle.valueInTxn);
      }
L
Liu Jicong 已提交
249 250
      if(pNode->handle.valueInUse
          && pNode->handle.valueInUse != TQ_DELETE_TOKEN) {
L
Liu Jicong 已提交
251 252 253 254 255 256 257 258 259 260 261
        pMeta->deleter(pNode->handle.valueInUse);
      }
      TqMetaList* next = pNode->next;
      free(pNode);
      pNode = next;
    }
  }
  free(pMeta->unpersistHead);
  taosRemoveDir(pMeta->dirPath);
  free(pMeta->dirPath);
  free(pMeta);
L
Liu Jicong 已提交
262 263 264
  return 0;
}

L
Liu Jicong 已提交
265
//TODO: wrap in tfile
L
Liu Jicong 已提交
266
int32_t tqStorePersist(TqMetaStore* pMeta) {
L
Liu Jicong 已提交
267 268
  char writeBuf[TQ_PAGE_SIZE];
  int64_t* bufPtr = (int64_t*)writeBuf;
L
Liu Jicong 已提交
269 270
  TqMetaList *pHead = pMeta->unpersistHead;
  TqMetaList *pNode = pHead->unpersistNext;
L
Liu Jicong 已提交
271 272 273 274 275 276 277 278 279 280
  TqSerializedHead *pSHead = malloc(sizeof(TqSerializedHead));
  if(pSHead == NULL) {
    //TODO: memory error
    return -1;
  }
  pSHead->ver = TQ_SVER;
  pSHead->checksum = 0;
  pSHead->ssize = sizeof(TqSerializedHead);
  int allocatedSize = sizeof(TqSerializedHead);
  int offset = lseek(pMeta->fileFd, 0, SEEK_CUR);
L
Liu Jicong 已提交
281
  while(pHead != pNode) {
L
Liu Jicong 已提交
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
    int nBytes = 0;

    if(pNode->handle.valueInUse) {
      if(pNode->handle.valueInTxn) {
        pSHead->action = TQ_ACTION_INUSE_CONT;
      } else {
        pSHead->action = TQ_ACTION_INUSE;
      }

      if(pNode->handle.valueInUse == TQ_DELETE_TOKEN) {
        pSHead->ssize = sizeof(TqSerializedHead);
      } else {
        pMeta->serializer(pNode->handle.valueInUse, &pSHead);
      }
      nBytes = write(pMeta->fileFd, pSHead, pSHead->ssize);
      ASSERT(nBytes == pSHead->ssize);
    }

    if(pNode->handle.valueInTxn) {
      pSHead->action = TQ_ACTION_INTXN;
      if(pNode->handle.valueInTxn == TQ_DELETE_TOKEN) {
        pSHead->ssize = sizeof(TqSerializedHead);
      } else {
        pMeta->serializer(pNode->handle.valueInTxn, &pSHead);
      }
      int nBytesTxn = write(pMeta->fileFd, pSHead, pSHead->ssize);
      ASSERT(nBytesTxn == pSHead->ssize);
      nBytes += nBytesTxn;
    }
L
Liu Jicong 已提交
311

L
Liu Jicong 已提交
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
    //write idx file
    //TODO: endian check and convert
    *(bufPtr++) = pNode->handle.key;
    *(bufPtr++) = pNode->handle.offset;
    *(bufPtr++) = (int64_t)nBytes;
    if((char*)(bufPtr + 3) > writeBuf + TQ_PAGE_SIZE) {
      nBytes = write(pMeta->idxFd, writeBuf, sizeof(writeBuf));
      //TODO: handle error with tfile
      ASSERT(nBytes == sizeof(writeBuf));
      memset(writeBuf, 0, TQ_PAGE_SIZE);
      bufPtr = (int64_t*)writeBuf;
    }
    //remove from unpersist list
    pHead->unpersistNext = pNode->unpersistNext;
    pHead->unpersistNext->unpersistPrev = pHead;
    pNode->unpersistPrev = pNode->unpersistNext = NULL;
    pNode = pHead->unpersistNext;

    //remove from bucket
    if(pNode->handle.valueInUse == TQ_DELETE_TOKEN &&
        pNode->handle.valueInTxn == NULL
        ) {
L
Liu Jicong 已提交
334 335 336
      int bucketKey = pNode->handle.key & TQ_BUCKET_SIZE;
      TqMetaList* pBucketHead = pMeta->bucket[bucketKey];
      if(pBucketHead == pNode) {
L
Liu Jicong 已提交
337
        pMeta->bucket[bucketKey] = pNode->next;
L
Liu Jicong 已提交
338 339 340 341 342 343
      } else {
        TqMetaList* pBucketNode = pBucketHead;
        while(pBucketNode->next != NULL
            && pBucketNode->next != pNode) {
          pBucketNode = pBucketNode->next; 
        }
L
Liu Jicong 已提交
344 345 346
        //impossible for pBucket->next == NULL
        ASSERT(pBucketNode->next == pNode);
        pBucketNode->next = pNode->next;
L
Liu Jicong 已提交
347
      }
L
Liu Jicong 已提交
348
      free(pNode);
L
Liu Jicong 已提交
349
    }
L
Liu Jicong 已提交
350
  }
L
Liu Jicong 已提交
351

L
Liu Jicong 已提交
352
  //write left bytes
L
Liu Jicong 已提交
353
  free(pSHead);
L
Liu Jicong 已提交
354 355 356 357 358 359 360 361 362
  if((char*)bufPtr != writeBuf) {
    int used = (char*)bufPtr - writeBuf;
    int nBytes = write(pMeta->idxFd, writeBuf, used);
    //TODO: handle error in tfile
    ASSERT(nBytes == used);
  }
  //TODO: using fsync in tfile
  fsync(pMeta->idxFd);
  fsync(pMeta->fileFd);
L
Liu Jicong 已提交
363 364 365
  return 0;
}

L
Liu Jicong 已提交
366
static int32_t tqHandlePutCommitted(TqMetaStore* pMeta, int64_t key, void* value) {
L
Liu Jicong 已提交
367 368 369 370 371
  int64_t bucketKey = key & TQ_BUCKET_SIZE;
  TqMetaList* pNode = pMeta->bucket[bucketKey];
  while(pNode) {
    if(pNode->handle.key == key) {
      //TODO: think about thread safety
L
Liu Jicong 已提交
372 373
      if(pNode->handle.valueInUse
          && pNode->handle.valueInUse != TQ_DELETE_TOKEN) {
L
Liu Jicong 已提交
374 375
        pMeta->deleter(pNode->handle.valueInUse);
      }
L
Liu Jicong 已提交
376 377
      //change pointer ownership
      pNode->handle.valueInUse = value;
L
Liu Jicong 已提交
378
      return 0;
L
Liu Jicong 已提交
379 380 381 382
    } else {
      pNode = pNode->next;
    }
  }
L
Liu Jicong 已提交
383 384 385 386 387 388 389 390 391 392 393 394 395
  TqMetaList *pNewNode = malloc(sizeof(TqMetaList));
  if(pNewNode == NULL) {
    //TODO: memory error
    return -1;
  }
  memset(pNewNode, 0, sizeof(TqMetaList));
  pNewNode->handle.key = key;
  pNewNode->handle.valueInUse = value;
  //put into unpersist list
  pNewNode->unpersistPrev = pMeta->unpersistHead;
  pNewNode->unpersistNext = pMeta->unpersistHead->unpersistNext;
  pMeta->unpersistHead->unpersistNext->unpersistPrev = pNewNode;
  pMeta->unpersistHead->unpersistNext = pNewNode;
L
Liu Jicong 已提交
396 397 398
  return 0;
}

L
Liu Jicong 已提交
399
void* tqHandleGet(TqMetaStore* pMeta, int64_t key) {
L
Liu Jicong 已提交
400 401 402 403 404
  int64_t bucketKey = key & TQ_BUCKET_SIZE;
  TqMetaList* pNode = pMeta->bucket[bucketKey];
  while(pNode) {
    if(pNode->handle.key == key) {
      if(pNode->handle.valueInUse != NULL) {
L
Liu Jicong 已提交
405
        return pNode->handle.valueInUse;
L
Liu Jicong 已提交
406 407 408 409 410 411 412
      } else {
        return NULL;
      }
    } else {
      pNode = pNode->next;
    }
  }
L
Liu Jicong 已提交
413 414 415
  return NULL;
}

L
Liu Jicong 已提交
416
int32_t tqHandleMovePut(TqMetaStore* pMeta, int64_t key, void* value) {
L
Liu Jicong 已提交
417 418 419 420 421
  int64_t bucketKey = key & TQ_BUCKET_SIZE;
  TqMetaList* pNode = pMeta->bucket[bucketKey];
  while(pNode) {
    if(pNode->handle.key == key) {
      //TODO: think about thread safety
L
Liu Jicong 已提交
422 423
      if(pNode->handle.valueInTxn
          && pNode->handle.valueInTxn != TQ_DELETE_TOKEN) {
L
Liu Jicong 已提交
424 425
        pMeta->deleter(pNode->handle.valueInTxn);
      }
L
Liu Jicong 已提交
426 427
      //change pointer ownership
      pNode->handle.valueInTxn = value;
L
Liu Jicong 已提交
428
      tqLinkUnpersist(pMeta, pNode);
L
Liu Jicong 已提交
429
      return 0;
L
Liu Jicong 已提交
430 431 432 433
    } else {
      pNode = pNode->next;
    }
  }
L
Liu Jicong 已提交
434 435 436 437 438 439 440 441
  TqMetaList *pNewNode = malloc(sizeof(TqMetaList));
  if(pNewNode == NULL) {
    //TODO: memory error
    return -1;
  }
  memset(pNewNode, 0, sizeof(TqMetaList));
  pNewNode->handle.key = key;
  pNewNode->handle.valueInTxn = value;
L
Liu Jicong 已提交
442
  pNewNode->next = pMeta->bucket[bucketKey];
L
Liu Jicong 已提交
443
  pMeta->bucket[bucketKey] = pNewNode;
L
Liu Jicong 已提交
444
  tqLinkUnpersist(pMeta, pNewNode);
L
Liu Jicong 已提交
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
  return 0;
}

int32_t tqHandleCopyPut(TqMetaStore* pMeta, int64_t key, void* value, size_t vsize) {
  void *vmem = malloc(vsize);
  if(vmem == NULL) {
    //TODO: memory error
    return -1;
  }
  memcpy(vmem, value, vsize);
  int64_t bucketKey = key & TQ_BUCKET_SIZE;
  TqMetaList* pNode = pMeta->bucket[bucketKey];
  while(pNode) {
    if(pNode->handle.key == key) {
      //TODO: think about thread safety
L
Liu Jicong 已提交
460 461
      if(pNode->handle.valueInTxn
          && pNode->handle.valueInTxn != TQ_DELETE_TOKEN) {
L
Liu Jicong 已提交
462 463 464 465
        pMeta->deleter(pNode->handle.valueInTxn);
      }
      //change pointer ownership
      pNode->handle.valueInTxn = vmem;
L
Liu Jicong 已提交
466
      tqLinkUnpersist(pMeta, pNode);
L
Liu Jicong 已提交
467 468 469 470 471 472 473 474 475 476 477 478 479 480
      return 0;
    } else {
      pNode = pNode->next;
    }
  }
  TqMetaList *pNewNode = malloc(sizeof(TqMetaList));
  if(pNewNode == NULL) {
    //TODO: memory error
    return -1;
  }
  memset(pNewNode, 0, sizeof(TqMetaList));
  pNewNode->handle.key = key;
  pNewNode->handle.valueInTxn = vmem;
  pNewNode->next = pMeta->bucket[bucketKey];
L
Liu Jicong 已提交
481
  pMeta->bucket[bucketKey] = pNewNode;
L
Liu Jicong 已提交
482
  tqLinkUnpersist(pMeta, pNewNode);
L
Liu Jicong 已提交
483 484 485
  return 0;
}

L
Liu Jicong 已提交
486
static void* tqHandleGetUncommitted(TqMetaStore* pMeta, int64_t key) {
L
Liu Jicong 已提交
487 488 489 490
  int64_t bucketKey = key & TQ_BUCKET_SIZE;
  TqMetaList* pNode = pMeta->bucket[bucketKey];
  while(pNode) {
    if(pNode->handle.key == key) {
L
Liu Jicong 已提交
491 492
      if(pNode->handle.valueInTxn != NULL
          && pNode->handle.valueInTxn != TQ_DELETE_TOKEN) {
L
Liu Jicong 已提交
493
        return pNode->handle.valueInTxn;
L
Liu Jicong 已提交
494 495 496 497 498 499 500
      } else {
        return NULL;
      }
    } else {
      pNode = pNode->next;
    }
  }
L
Liu Jicong 已提交
501 502 503 504
  return NULL;
}

int32_t tqHandleCommit(TqMetaStore* pMeta, int64_t key) {
L
Liu Jicong 已提交
505 506 507 508
  int64_t bucketKey = key & TQ_BUCKET_SIZE;
  TqMetaList* pNode = pMeta->bucket[bucketKey];
  while(pNode) {
    if(pNode->handle.key == key) {
L
Liu Jicong 已提交
509 510
      if(pNode->handle.valueInUse
          && pNode->handle.valueInUse != TQ_DELETE_TOKEN) {
L
Liu Jicong 已提交
511 512 513
        pMeta->deleter(pNode->handle.valueInUse);
      }
      pNode->handle.valueInUse = pNode->handle.valueInTxn;
L
Liu Jicong 已提交
514
      pNode->handle.valueInTxn = NULL;
L
Liu Jicong 已提交
515
      tqLinkUnpersist(pMeta, pNode);
L
Liu Jicong 已提交
516 517 518 519 520 521
      return 0;
    } else {
      pNode = pNode->next;
    }
  }
  return -1;
L
Liu Jicong 已提交
522 523 524
}

int32_t tqHandleAbort(TqMetaStore* pMeta, int64_t key) {
L
Liu Jicong 已提交
525 526 527 528
  int64_t bucketKey = key & TQ_BUCKET_SIZE;
  TqMetaList* pNode = pMeta->bucket[bucketKey];
  while(pNode) {
    if(pNode->handle.key == key) {
L
Liu Jicong 已提交
529 530 531 532
      if(pNode->handle.valueInTxn) {
        if(pNode->handle.valueInTxn != TQ_DELETE_TOKEN) {
          pMeta->deleter(pNode->handle.valueInTxn);
        }
L
Liu Jicong 已提交
533
        pNode->handle.valueInTxn = NULL;
L
Liu Jicong 已提交
534
        tqLinkUnpersist(pMeta, pNode);
L
Liu Jicong 已提交
535 536 537 538 539 540 541 542
        return 0;
      }
      return -1;
    } else {
      pNode = pNode->next;
    }
  }
  return -2;
L
Liu Jicong 已提交
543 544 545
}

int32_t tqHandleDel(TqMetaStore* pMeta, int64_t key) {
L
Liu Jicong 已提交
546 547 548
  int64_t bucketKey = key & TQ_BUCKET_SIZE;
  TqMetaList* pNode = pMeta->bucket[bucketKey];
  while(pNode) {
L
Liu Jicong 已提交
549 550
      if(pNode->handle.valueInTxn
          && pNode->handle.valueInTxn != TQ_DELETE_TOKEN) {
L
Liu Jicong 已提交
551
      pMeta->deleter(pNode->handle.valueInTxn);
L
Liu Jicong 已提交
552 553
      pNode->handle.valueInTxn = TQ_DELETE_TOKEN;
      tqLinkUnpersist(pMeta, pNode);
L
Liu Jicong 已提交
554
      return 0;
L
Liu Jicong 已提交
555 556 557 558
    } else {
      pNode = pNode->next;
    }
  }
L
Liu Jicong 已提交
559 560
  //no such key
  return -1;
L
Liu Jicong 已提交
561 562 563
}

int32_t tqHandleClear(TqMetaStore* pMeta, int64_t key) {
L
Liu Jicong 已提交
564 565 566 567 568 569 570
  int64_t bucketKey = key & TQ_BUCKET_SIZE;
  TqMetaList* pNode = pMeta->bucket[bucketKey];
  bool exist = false;
  while(pNode) {
    if(pNode->handle.key == key) {
      if(pNode->handle.valueInUse != NULL) {
        exist = true;
L
Liu Jicong 已提交
571 572 573 574
        if(pNode->handle.valueInUse != TQ_DELETE_TOKEN) {
          pMeta->deleter(pNode->handle.valueInUse);
        }
        pNode->handle.valueInUse = TQ_DELETE_TOKEN;
L
Liu Jicong 已提交
575 576 577
      }
      if(pNode->handle.valueInTxn != NULL) {
        exist = true;
L
Liu Jicong 已提交
578 579 580 581
        if(pNode->handle.valueInTxn != TQ_DELETE_TOKEN) {
          pMeta->deleter(pNode->handle.valueInTxn);
        }
        pNode->handle.valueInTxn = TQ_DELETE_TOKEN;
L
Liu Jicong 已提交
582 583
      }
      if(exist) {
L
Liu Jicong 已提交
584
        tqLinkUnpersist(pMeta, pNode);
L
Liu Jicong 已提交
585 586 587 588 589 590 591 592
        return 0;
      }
      return -1;
    } else {
      pNode = pNode->next;
    }
  }
  return -2;
L
Liu Jicong 已提交
593
}