tqMetaStore.c 18.3 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
static inline void tqLinkUnpersist(TqMetaStore *pMeta, TqMetaList* pNode) {
L
Liu Jicong 已提交
30 31 32 33 34 35
  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 已提交
36 37
}

L
Liu Jicong 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
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);
}

//TODO: the struct is tightly coupled with index entry
typedef struct TqIdxPageHead {
  int16_t writeOffset;
  int8_t  unused[14];
} TqIdxPageHead;

typedef struct TqIdxPageBuf {
  TqIdxPageHead head;
  char buffer[TQ_IDX_PAGE_BODY_SIZE];
} TqIdxPageBuf;

static inline int tqReadLastPage(int fd, TqIdxPageBuf* pBuf) {
  int offset = tqSeekLastPage(fd);
  int nBytes;
  if((nBytes = read(fd, pBuf, TQ_PAGE_SIZE)) == -1) {
    return -1;
  }
  if(nBytes == 0) {
    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
TqMetaStore* tqStoreOpen(const char* path,
L
Liu Jicong 已提交
72 73
    int serializer(const void* pObj, TqSerializedHead** ppHead),
    const void* deserializer(const TqSerializedHead* pHead, void** ppObj),
L
Liu Jicong 已提交
74 75 76
    void deleter(void* pObj),
    int32_t tqConfigFlag
    ) {
L
Liu Jicong 已提交
77 78 79 80 81
  TqMetaStore* pMeta = malloc(sizeof(TqMetaStore)); 
  if(pMeta == NULL) {
    //close
    return NULL;
  }
L
Liu Jicong 已提交
82
  memset(pMeta, 0, sizeof(TqMetaStore));
L
Liu Jicong 已提交
83 84 85

  //concat data file name and index file name
  size_t pathLen = strlen(path);
L
Liu Jicong 已提交
86 87 88 89 90 91
  pMeta->dirPath = malloc(pathLen+1);
  if(pMeta->dirPath != NULL) {
    //TODO: memory insufficient
  }
  strcpy(pMeta->dirPath, path);
  
L
Liu Jicong 已提交
92 93
  char name[pathLen+10];

L
Liu Jicong 已提交
94
  strcpy(name, path);
L
Liu Jicong 已提交
95 96 97
  if(!taosDirExist(name) && !taosMkDir(name)) {
    ASSERT(false);
  }
L
Liu Jicong 已提交
98
  strcat(name, "/" TQ_IDX_NAME);
L
Liu Jicong 已提交
99
  int idxFd = open(name, O_RDWR | O_CREAT, 0755);
L
Liu Jicong 已提交
100
  if(idxFd < 0) {
L
Liu Jicong 已提交
101
    ASSERT(false);
L
Liu Jicong 已提交
102 103 104 105
    //close file
    //free memory
    return NULL;
  }
L
Liu Jicong 已提交
106

L
Liu Jicong 已提交
107 108 109
  pMeta->idxFd = idxFd;
  pMeta->unpersistHead = malloc(sizeof(TqMetaList));
  if(pMeta->unpersistHead == NULL) {
L
Liu Jicong 已提交
110
    ASSERT(false);
L
Liu Jicong 已提交
111 112 113 114
    //close file
    //free memory
    return NULL;
  }
L
Liu Jicong 已提交
115 116 117 118
  memset(pMeta->unpersistHead, 0, sizeof(TqMetaList));
  pMeta->unpersistHead->unpersistNext
    = pMeta->unpersistHead->unpersistPrev
    = pMeta->unpersistHead;
L
Liu Jicong 已提交
119 120 121

  strcpy(name, path);
  strcat(name, "/" TQ_META_NAME);
L
Liu Jicong 已提交
122 123 124 125 126
  int fileFd = open(name, O_RDWR | O_CREAT, 0755);
  if(fileFd < 0){
    ASSERT(false);
    return NULL;
  }
L
Liu Jicong 已提交
127 128 129

  pMeta->fileFd = fileFd;
  
L
Liu Jicong 已提交
130 131 132
  pMeta->serializer = serializer;
  pMeta->deserializer = deserializer;
  pMeta->deleter = deleter;
L
Liu Jicong 已提交
133
  pMeta->tqConfigFlag = tqConfigFlag;
L
Liu Jicong 已提交
134 135

  //read idx file and load into memory
L
Liu Jicong 已提交
136
  TqIdxPageBuf idxBuf;
L
Liu Jicong 已提交
137 138
  TqSerializedHead* serializedObj = malloc(TQ_PAGE_SIZE);
  if(serializedObj == NULL) {
L
Liu Jicong 已提交
139 140
    //TODO:memory insufficient
  }
L
Liu Jicong 已提交
141 142
  int idxRead;
  int allocated = TQ_PAGE_SIZE;
L
Liu Jicong 已提交
143 144
  bool readEnd = false;
  while((idxRead = read(idxFd, &idxBuf, TQ_PAGE_SIZE))) {
L
Liu Jicong 已提交
145 146 147 148
    if(idxRead == -1) {
      //TODO: handle error
      ASSERT(false);
    }
L
Liu Jicong 已提交
149
    ASSERT(idxBuf.head.writeOffset == idxRead);
L
Liu Jicong 已提交
150
    //loop read every entry
L
Liu Jicong 已提交
151
    for(int i = 0; i < idxBuf.head.writeOffset - TQ_IDX_PAGE_HEAD_SIZE; i += TQ_IDX_SIZE) {
L
Liu Jicong 已提交
152
      TqMetaList *pNode = malloc(sizeof(TqMetaList));
L
Liu Jicong 已提交
153 154 155
      if(pNode == NULL) {
        //TODO: free memory and return error
      }
L
Liu Jicong 已提交
156
      memset(pNode, 0, sizeof(TqMetaList));
L
Liu Jicong 已提交
157 158 159
      memcpy(&pNode->handle, &idxBuf.buffer[i], TQ_IDX_SIZE);

      lseek(fileFd, pNode->handle.offset, SEEK_SET);
L
Liu Jicong 已提交
160 161
      if(allocated < pNode->handle.serializedSize) {
        void *ptr = realloc(serializedObj, pNode->handle.serializedSize);
L
Liu Jicong 已提交
162 163 164
        if(ptr == NULL) {
          //TODO: memory insufficient
        }
L
Liu Jicong 已提交
165 166
        serializedObj = ptr;
        allocated = pNode->handle.serializedSize;
L
Liu Jicong 已提交
167
      }
L
Liu Jicong 已提交
168 169
      serializedObj->ssize = pNode->handle.serializedSize;
      if(read(fileFd, serializedObj, pNode->handle.serializedSize) != pNode->handle.serializedSize) {
L
Liu Jicong 已提交
170 171
        //TODO: read error
      }
L
Liu Jicong 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
      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;
        }
L
Liu Jicong 已提交
190 191 192
        TqSerializedHead* ptr = POINTER_SHIFT(serializedObj, serializedObj->ssize);
        if(ptr->ssize != sizeof(TqSerializedHead)) {
          pMeta->deserializer(ptr, &pNode->handle.valueInTxn);
L
Liu Jicong 已提交
193 194 195 196 197 198
        } else {
          pNode->handle.valueInTxn = TQ_DELETE_TOKEN;
        }
      } else {
        ASSERT(0);
      }
L
Liu Jicong 已提交
199 200

      //put into list
L
Liu Jicong 已提交
201
      int bucketKey = pNode->handle.key & TQ_BUCKET_MASK;
L
Liu Jicong 已提交
202 203 204 205 206 207 208 209
      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 &&
L
Liu Jicong 已提交
210
            pBucketNode->next->handle.key != pNode->handle.key) {
L
Liu Jicong 已提交
211 212 213 214
          pBucketNode = pBucketNode->next; 
        }
        if(pBucketNode->next) {
          ASSERT(pBucketNode->next->handle.key == pNode->handle.key);
L
Liu Jicong 已提交
215 216 217 218
          TqMetaList *pNodeFound = pBucketNode->next;
          pNode->next = pNodeFound->next;
          pBucketNode->next = pNode;
          pBucketNode = pNodeFound;
L
Liu Jicong 已提交
219
        } else {
L
Liu Jicong 已提交
220 221
          pNode->next = pMeta->bucket[bucketKey];
          pMeta->bucket[bucketKey] = pNode;
L
Liu Jicong 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234 235
          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 已提交
236 237
    }
  }
L
Liu Jicong 已提交
238
  free(serializedObj);
L
Liu Jicong 已提交
239 240 241 242 243
  return pMeta;
}

int32_t tqStoreClose(TqMetaStore* pMeta) {
  //commit data and idx
L
Liu Jicong 已提交
244 245 246 247
  tqStorePersist(pMeta);
  ASSERT(pMeta->unpersistHead && pMeta->unpersistHead->next==NULL);
  close(pMeta->fileFd);
  close(pMeta->idxFd);
L
Liu Jicong 已提交
248
  //free memory
L
Liu Jicong 已提交
249
  for(int i = 0; i < TQ_BUCKET_SIZE; i++) {
L
Liu Jicong 已提交
250 251 252 253
    TqMetaList* pNode = pMeta->bucket[i];
    while(pNode) {
      ASSERT(pNode->unpersistNext == NULL);
      ASSERT(pNode->unpersistPrev == NULL);
L
Liu Jicong 已提交
254 255
      if(pNode->handle.valueInTxn
          && pNode->handle.valueInTxn != TQ_DELETE_TOKEN) {
L
Liu Jicong 已提交
256
        pMeta->deleter(pNode->handle.valueInTxn);
L
Liu Jicong 已提交
257
      }
L
Liu Jicong 已提交
258 259
      if(pNode->handle.valueInUse
          && pNode->handle.valueInUse != TQ_DELETE_TOKEN) {
L
Liu Jicong 已提交
260
        pMeta->deleter(pNode->handle.valueInUse);
L
Liu Jicong 已提交
261
      }
L
Liu Jicong 已提交
262 263 264
      TqMetaList* next = pNode->next;
      free(pNode);
      pNode = next;
L
Liu Jicong 已提交
265 266
    }
  }
L
Liu Jicong 已提交
267
  free(pMeta->dirPath);
L
Liu Jicong 已提交
268
  free(pMeta->unpersistHead);
L
Liu Jicong 已提交
269
  free(pMeta);
L
Liu Jicong 已提交
270 271 272 273
  return 0;
}

int32_t tqStoreDelete(TqMetaStore* pMeta) {
L
Liu Jicong 已提交
274 275
  close(pMeta->fileFd);
  close(pMeta->idxFd);
L
Liu Jicong 已提交
276
  //free memory
L
Liu Jicong 已提交
277 278 279 280
  for(int i = 0; i < TQ_BUCKET_SIZE; i++) {
    TqMetaList* pNode = pMeta->bucket[i];
    pMeta->bucket[i] = NULL;
    while(pNode) {
L
Liu Jicong 已提交
281 282
      if(pNode->handle.valueInTxn
          && pNode->handle.valueInTxn != TQ_DELETE_TOKEN) {
L
Liu Jicong 已提交
283 284
        pMeta->deleter(pNode->handle.valueInTxn);
      }
L
Liu Jicong 已提交
285 286
      if(pNode->handle.valueInUse
          && pNode->handle.valueInUse != TQ_DELETE_TOKEN) {
L
Liu Jicong 已提交
287 288 289 290 291 292 293 294 295 296 297
        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 已提交
298 299 300
  return 0;
}

L
Liu Jicong 已提交
301
//TODO: wrap in tfile
L
Liu Jicong 已提交
302
int32_t tqStorePersist(TqMetaStore* pMeta) {
L
Liu Jicong 已提交
303 304
  TqIdxPageBuf idxBuf;
  int64_t* bufPtr = (int64_t*)idxBuf.buffer;
L
Liu Jicong 已提交
305 306
  TqMetaList *pHead = pMeta->unpersistHead;
  TqMetaList *pNode = pHead->unpersistNext;
L
Liu Jicong 已提交
307 308 309 310 311 312 313 314 315 316
  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 已提交
317 318 319 320 321 322 323 324 325 326 327

  tqReadLastPage(pMeta->idxFd, &idxBuf);

  if(idxBuf.head.writeOffset == TQ_PAGE_SIZE) {
    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 已提交
328
  while(pHead != pNode) {
L
Liu Jicong 已提交
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
    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 已提交
358 359
    pNode->handle.offset = offset;
    offset += nBytes;
L
Liu Jicong 已提交
360

L
Liu Jicong 已提交
361 362 363 364 365
    //write idx file
    //TODO: endian check and convert
    *(bufPtr++) = pNode->handle.key;
    *(bufPtr++) = pNode->handle.offset;
    *(bufPtr++) = (int64_t)nBytes;
L
Liu Jicong 已提交
366
    idxBuf.head.writeOffset += TQ_IDX_SIZE;
L
Liu Jicong 已提交
367

L
Liu Jicong 已提交
368 369
    if(idxBuf.head.writeOffset >= TQ_PAGE_SIZE) {
      nBytes = write(pMeta->idxFd, &idxBuf, TQ_PAGE_SIZE);
L
Liu Jicong 已提交
370
      //TODO: handle error with tfile
L
Liu Jicong 已提交
371 372
      ASSERT(nBytes == TQ_PAGE_SIZE);
      memset(&idxBuf, 0, TQ_PAGE_SIZE);
L
Liu Jicong 已提交
373
      idxBuf.head.writeOffset = TQ_IDX_PAGE_HEAD_SIZE;
L
Liu Jicong 已提交
374
      bufPtr = (int64_t*)&idxBuf.buffer;
L
Liu Jicong 已提交
375 376 377 378 379 380 381 382 383 384 385
    }
    //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 已提交
386
      int bucketKey = pNode->handle.key & TQ_BUCKET_MASK;
L
Liu Jicong 已提交
387 388
      TqMetaList* pBucketHead = pMeta->bucket[bucketKey];
      if(pBucketHead == pNode) {
L
Liu Jicong 已提交
389
        pMeta->bucket[bucketKey] = pNode->next;
L
Liu Jicong 已提交
390 391 392 393 394 395
      } else {
        TqMetaList* pBucketNode = pBucketHead;
        while(pBucketNode->next != NULL
            && pBucketNode->next != pNode) {
          pBucketNode = pBucketNode->next; 
        }
L
Liu Jicong 已提交
396 397 398
        //impossible for pBucket->next == NULL
        ASSERT(pBucketNode->next == pNode);
        pBucketNode->next = pNode->next;
L
Liu Jicong 已提交
399
      }
L
Liu Jicong 已提交
400
      free(pNode);
L
Liu Jicong 已提交
401
    }
L
Liu Jicong 已提交
402
  }
L
Liu Jicong 已提交
403

L
Liu Jicong 已提交
404
  //write left bytes
L
Liu Jicong 已提交
405
  free(pSHead);
L
Liu Jicong 已提交
406 407 408
  //TODO: write new version in tfile
  if((char*)bufPtr != idxBuf.buffer) {
    int nBytes = write(pMeta->idxFd, &idxBuf, idxBuf.head.writeOffset);
L
Liu Jicong 已提交
409
    //TODO: handle error in tfile
L
Liu Jicong 已提交
410
    ASSERT(nBytes == idxBuf.head.writeOffset);
L
Liu Jicong 已提交
411 412 413 414
  }
  //TODO: using fsync in tfile
  fsync(pMeta->idxFd);
  fsync(pMeta->fileFd);
L
Liu Jicong 已提交
415 416 417
  return 0;
}

L
Liu Jicong 已提交
418
static int32_t tqHandlePutCommitted(TqMetaStore* pMeta, int64_t key, void* value) {
L
Liu Jicong 已提交
419
  int64_t bucketKey = key & TQ_BUCKET_MASK;
L
Liu Jicong 已提交
420 421 422 423
  TqMetaList* pNode = pMeta->bucket[bucketKey];
  while(pNode) {
    if(pNode->handle.key == key) {
      //TODO: think about thread safety
L
Liu Jicong 已提交
424 425
      if(pNode->handle.valueInUse
          && pNode->handle.valueInUse != TQ_DELETE_TOKEN) {
L
Liu Jicong 已提交
426 427
        pMeta->deleter(pNode->handle.valueInUse);
      }
L
Liu Jicong 已提交
428 429
      //change pointer ownership
      pNode->handle.valueInUse = value;
L
Liu Jicong 已提交
430
      return 0;
L
Liu Jicong 已提交
431 432 433 434
    } else {
      pNode = pNode->next;
    }
  }
L
Liu Jicong 已提交
435 436 437 438 439 440 441 442 443 444 445 446 447
  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 已提交
448 449 450
  return 0;
}

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

L
Liu Jicong 已提交
469
void* tqHandleTouchGet(TqMetaStore* pMeta, int64_t key) {
L
Liu Jicong 已提交
470
  int64_t bucketKey = key & TQ_BUCKET_MASK;
L
Liu Jicong 已提交
471 472 473
  TqMetaList* pNode = pMeta->bucket[bucketKey];
  while(pNode) {
    if(pNode->handle.key == key) {
L
Liu Jicong 已提交
474 475 476 477 478 479
      if(pNode->handle.valueInUse != NULL
          && pNode->handle.valueInUse != TQ_DELETE_TOKEN) {
        tqLinkUnpersist(pMeta, pNode);
        return pNode->handle.valueInUse;
      } else {
        return NULL;
L
Liu Jicong 已提交
480
      }
L
Liu Jicong 已提交
481 482 483 484
    } else {
      pNode = pNode->next;
    }
  }
L
Liu Jicong 已提交
485
  return NULL;
L
Liu Jicong 已提交
486 487
}

L
Liu Jicong 已提交
488
static inline int32_t tqHandlePutImpl(TqMetaStore* pMeta, int64_t key, void* value) {
L
Liu Jicong 已提交
489
  int64_t bucketKey = key & TQ_BUCKET_MASK;
L
Liu Jicong 已提交
490 491 492 493
  TqMetaList* pNode = pMeta->bucket[bucketKey];
  while(pNode) {
    if(pNode->handle.key == key) {
      //TODO: think about thread safety
L
Liu Jicong 已提交
494 495 496 497 498 499 500
      if(pNode->handle.valueInTxn) {
        if(TqDupIntxnReject(pMeta->tqConfigFlag)) {
          return -2;
        }
        if(pNode->handle.valueInTxn != TQ_DELETE_TOKEN) {
          pMeta->deleter(pNode->handle.valueInTxn);
        }
L
Liu Jicong 已提交
501
      }
L
Liu Jicong 已提交
502
      pNode->handle.valueInTxn = value;
L
Liu Jicong 已提交
503
      tqLinkUnpersist(pMeta, pNode);
L
Liu Jicong 已提交
504 505 506 507 508 509 510 511 512 513 514 515
      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;
L
Liu Jicong 已提交
516
  pNewNode->handle.valueInTxn = value;
L
Liu Jicong 已提交
517
  pNewNode->next = pMeta->bucket[bucketKey];
L
Liu Jicong 已提交
518
  pMeta->bucket[bucketKey] = pNewNode;
L
Liu Jicong 已提交
519
  tqLinkUnpersist(pMeta, pNewNode);
L
Liu Jicong 已提交
520 521 522
  return 0;
}

L
Liu Jicong 已提交
523 524 525 526 527 528 529 530 531 532 533 534 535 536
int32_t tqHandleMovePut(TqMetaStore* pMeta, int64_t key, void* value) {
  return tqHandlePutImpl(pMeta, key, value);
}

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);
  return tqHandlePutImpl(pMeta, key, vmem);
}

L
Liu Jicong 已提交
537
static void* tqHandleGetUncommitted(TqMetaStore* pMeta, int64_t key) {
L
Liu Jicong 已提交
538
  int64_t bucketKey = key & TQ_BUCKET_MASK;
L
Liu Jicong 已提交
539 540 541
  TqMetaList* pNode = pMeta->bucket[bucketKey];
  while(pNode) {
    if(pNode->handle.key == key) {
L
Liu Jicong 已提交
542 543
      if(pNode->handle.valueInTxn != NULL
          && pNode->handle.valueInTxn != TQ_DELETE_TOKEN) {
L
Liu Jicong 已提交
544
        return pNode->handle.valueInTxn;
L
Liu Jicong 已提交
545 546 547 548 549 550 551
      } else {
        return NULL;
      }
    } else {
      pNode = pNode->next;
    }
  }
L
Liu Jicong 已提交
552 553 554 555
  return NULL;
}

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

int32_t tqHandleAbort(TqMetaStore* pMeta, int64_t key) {
L
Liu Jicong 已提交
579
  int64_t bucketKey = key & TQ_BUCKET_MASK;
L
Liu Jicong 已提交
580 581 582
  TqMetaList* pNode = pMeta->bucket[bucketKey];
  while(pNode) {
    if(pNode->handle.key == key) {
L
Liu Jicong 已提交
583 584 585 586
      if(pNode->handle.valueInTxn) {
        if(pNode->handle.valueInTxn != TQ_DELETE_TOKEN) {
          pMeta->deleter(pNode->handle.valueInTxn);
        }
L
Liu Jicong 已提交
587
        pNode->handle.valueInTxn = NULL;
L
Liu Jicong 已提交
588
        tqLinkUnpersist(pMeta, pNode);
L
Liu Jicong 已提交
589 590 591 592 593 594 595 596
        return 0;
      }
      return -1;
    } else {
      pNode = pNode->next;
    }
  }
  return -2;
L
Liu Jicong 已提交
597 598 599
}

int32_t tqHandleDel(TqMetaStore* pMeta, int64_t key) {
L
Liu Jicong 已提交
600
  int64_t bucketKey = key & TQ_BUCKET_MASK;
L
Liu Jicong 已提交
601 602
  TqMetaList* pNode = pMeta->bucket[bucketKey];
  while(pNode) {
L
Liu Jicong 已提交
603 604 605 606
    if(pNode->handle.valueInTxn != TQ_DELETE_TOKEN) {
      if(pNode->handle.valueInTxn) {
        pMeta->deleter(pNode->handle.valueInTxn);
      }
L
Liu Jicong 已提交
607 608
      pNode->handle.valueInTxn = TQ_DELETE_TOKEN;
      tqLinkUnpersist(pMeta, pNode);
L
Liu Jicong 已提交
609
      return 0;
L
Liu Jicong 已提交
610 611 612 613
    } else {
      pNode = pNode->next;
    }
  }
L
Liu Jicong 已提交
614 615
  //no such key
  return -1;
L
Liu Jicong 已提交
616 617
}

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