tqMetaStore.c 11.0 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 20
#include <unistd.h>

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

L
Liu Jicong 已提交
24 25 26 27

static int32_t       tqHandlePutCommitted(TqMetaStore*, int64_t key, void* value);
static TqMetaHandle* tqHandleGetUncommitted(TqMetaStore*, int64_t key);

L
Liu Jicong 已提交
28 29 30 31 32
typedef struct TqMetaPageBuf {
  int16_t offset;
  char buffer[TQ_PAGE_SIZE];
} TqMetaPageBuf;

L
Liu Jicong 已提交
33 34 35 36
TqMetaStore* tqStoreOpen(const char* path,
    int serializer(TqGroupHandle*, void**),
    const void* deserializer(const void*, TqGroupHandle*),
    void deleter(void*)) {
L
Liu Jicong 已提交
37 38 39 40 41
  TqMetaStore* pMeta = malloc(sizeof(TqMetaStore)); 
  if(pMeta == NULL) {
    //close
    return NULL;
  }
L
Liu Jicong 已提交
42 43 44 45 46

  //concat data file name and index file name
  size_t pathLen = strlen(path);
  char name[pathLen+10];

L
Liu Jicong 已提交
47 48 49
  strcpy(name, path);
  strcat(name, "/" TQ_IDX_NAME);
  int idxFd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0755);
L
Liu Jicong 已提交
50 51 52 53 54
  if(idxFd < 0) {
    //close file
    //free memory
    return NULL;
  }
L
Liu Jicong 已提交
55

L
Liu Jicong 已提交
56 57 58 59 60 61 62
  pMeta->idxFd = idxFd;
  pMeta->unpersistHead = malloc(sizeof(TqMetaList));
  if(pMeta->unpersistHead == NULL) {
    //close file
    //free memory
    return NULL;
  }
L
Liu Jicong 已提交
63 64 65 66 67 68 69 70 71

  strcpy(name, path);
  strcat(name, "/" TQ_META_NAME);
  int fileFd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0755);
  if(fileFd < 0) return NULL;

  memset(pMeta, 0, sizeof(TqMetaStore));
  pMeta->fileFd = fileFd;
  
L
Liu Jicong 已提交
72 73 74
  pMeta->serializer = serializer;
  pMeta->deserializer = deserializer;
  pMeta->deleter = deleter;
L
Liu Jicong 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

  //read idx file and load into memory
  char readBuf[TQ_PAGE_SIZE];
  int readSize;
  while((readSize = read(idxFd, readBuf, TQ_PAGE_SIZE)) != -1) {
    //loop read every entry
    for(int i = 0; i < readSize; i += TQ_IDX_ENTRY_SIZE) {
      TqMetaList *pNode = malloc(sizeof(TqMetaHandle));
      memset(pNode, 0, sizeof(TqMetaList));
      if(pNode == NULL) {
        //TODO: free memory and return error
      }
      memcpy(&pNode->handle, &readBuf[i], TQ_IDX_ENTRY_SIZE);
      int bucketKey = pNode->handle.key & TQ_BUCKET_SIZE;
      pNode->next = pMeta->bucket[bucketKey];
      pMeta->bucket[bucketKey] = pNode;
    }
  }

L
Liu Jicong 已提交
94 95 96 97 98
  return pMeta;
}

int32_t tqStoreClose(TqMetaStore* pMeta) {
  //commit data and idx
L
Liu Jicong 已提交
99 100 101 102
  tqStorePersist(pMeta);
  ASSERT(pMeta->unpersistHead && pMeta->unpersistHead->next==NULL);
  close(pMeta->fileFd);
  close(pMeta->idxFd);
L
Liu Jicong 已提交
103
  //free memory
L
Liu Jicong 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
  for(int i = 0; i < TQ_BUCKET_SIZE; i++) {
    TqMetaList* node = pMeta->bucket[i];
    pMeta->bucket[i] = NULL;
    while(node) {
      ASSERT(node->unpersistNext == NULL);
      ASSERT(node->unpersistPrev == NULL);
      if(node->handle.valueInTxn) {
        pMeta->deleter(node->handle.valueInTxn);
      }
      if(node->handle.valueInUse) {
        pMeta->deleter(node->handle.valueInUse);
      }
      TqMetaList* next = node->next;
      free(node);
      node = next;
    }
  }
  free(pMeta);
L
Liu Jicong 已提交
122 123 124 125 126 127 128 129 130 131
  return 0;
}

int32_t tqStoreDelete(TqMetaStore* pMeta) {
  //close file
  //delete file
  //free memory
  return 0;
}

L
Liu Jicong 已提交
132
//TODO: wrap in tfile
L
Liu Jicong 已提交
133
int32_t tqStorePersist(TqMetaStore* pMeta) {
L
Liu Jicong 已提交
134 135
  char writeBuf[TQ_PAGE_SIZE];
  int64_t* bufPtr = (int64_t*)writeBuf;
L
Liu Jicong 已提交
136 137 138
  TqMetaList *pHead = pMeta->unpersistHead;
  TqMetaList *pNode = pHead->unpersistNext;
  while(pHead != pNode) {
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(pNode->handle.valueInUse == NULL) {
      //put delete token in data file
      uint32_t delete = TQ_ACTION_DELETE;
      int nBytes = write(pMeta->fileFd, &delete, sizeof(uint32_t));
      ASSERT(nBytes == sizeof(uint32_t));

      //remove from list
      int bucketKey = pNode->handle.key & TQ_BUCKET_SIZE;
      TqMetaList* pBucketHead = pMeta->bucket[bucketKey];
      if(pBucketHead == pNode) {
        pMeta->bucket[bucketKey] = pBucketHead->next;
      } else {
        TqMetaList* pBucketNode = pBucketHead;
        while(pBucketNode->next != NULL
            && pBucketNode->next != pNode) {
          pBucketNode = pBucketNode->next; 
        }
        if(pBucketNode->next != NULL) {
          ASSERT(pBucketNode->next == pNode);
          pBucketNode->next = pNode->next;
          if(pNode->handle.valueInUse) {
            pMeta->deleter(pNode->handle.valueInUse);
          }
          free(pNode);
        }
      }
    }
L
Liu Jicong 已提交
166
    //serialize
L
Liu Jicong 已提交
167 168 169 170
    void* pBytes = NULL;
    int sz = pMeta->serializer(pNode->handle.valueInUse, &pBytes);
    ASSERT(pBytes != NULL);
    //get current offset
L
Liu Jicong 已提交
171
    //append data
L
Liu Jicong 已提交
172
    int64_t offset = lseek(pMeta->fileFd, 0, SEEK_CUR);
L
Liu Jicong 已提交
173 174 175 176
    int nBytes = write(pMeta->fileFd, pBytes, sz);
    //TODO: handle error in tfile
    ASSERT(nBytes == sz);

L
Liu Jicong 已提交
177 178 179
    pNode->handle.offset = offset;
    pNode->handle.serializedSize = sz;

L
Liu Jicong 已提交
180 181
    //write idx
    //TODO: endian check and convert
L
Liu Jicong 已提交
182 183 184 185 186 187 188 189 190 191
    *(bufPtr++) = pNode->handle.key;
    *(bufPtr++) = pNode->handle.offset;
    *(bufPtr++) = (int64_t)sz;
    if((char*)(bufPtr + 3) > writeBuf + TQ_PAGE_SIZE) {
      nBytes = write(pMeta->idxFd, writeBuf, sizeof(writeBuf));
      //TODO: handle error in tfile
      ASSERT(nBytes == sizeof(writeBuf));
      memset(writeBuf, 0, TQ_PAGE_SIZE);
      bufPtr = (int64_t*)writeBuf;
    }
L
Liu Jicong 已提交
192

L
Liu Jicong 已提交
193
    //remove from unpersist list
L
Liu Jicong 已提交
194 195 196 197 198
    pHead->unpersistNext = pNode->unpersistNext;
    pHead->unpersistNext->unpersistPrev = pHead;

    pNode->unpersistPrev = pNode->unpersistNext = NULL;
    pNode = pHead->unpersistNext;
L
Liu Jicong 已提交
199
  }
L
Liu Jicong 已提交
200 201 202 203 204 205 206 207 208 209
  //write left bytes
  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 已提交
210 211 212
  return 0;
}

L
Liu Jicong 已提交
213
static int32_t tqHandlePutCommitted(TqMetaStore* pMeta, int64_t key, void* value) {
L
Liu Jicong 已提交
214 215 216 217 218 219 220 221
  int64_t bucketKey = key & TQ_BUCKET_SIZE;
  TqMetaList* pNode = pMeta->bucket[bucketKey];
  while(pNode) {
    if(pNode->handle.key == key) {
      //TODO: think about thread safety
      pMeta->deleter(pNode->handle.valueInUse);
      //change pointer ownership
      pNode->handle.valueInUse = value;
L
Liu Jicong 已提交
222
      return 0;
L
Liu Jicong 已提交
223 224 225 226
    } else {
      pNode = pNode->next;
    }
  }
L
Liu Jicong 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239
  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 已提交
240 241 242
  return 0;
}

L
Liu Jicong 已提交
243
TqMetaHandle* tqHandleGet(TqMetaStore* pMeta, int64_t key) {
L
Liu Jicong 已提交
244 245 246 247 248 249 250 251 252 253 254 255 256
  int64_t bucketKey = key & TQ_BUCKET_SIZE;
  TqMetaList* pNode = pMeta->bucket[bucketKey];
  while(pNode) {
    if(pNode->handle.key == key) {
      if(pNode->handle.valueInUse != NULL) {
        return &pNode->handle;
      } else {
        return NULL;
      }
    } else {
      pNode = pNode->next;
    }
  }
L
Liu Jicong 已提交
257 258 259
  return NULL;
}

L
Liu Jicong 已提交
260
int32_t tqHandlePut(TqMetaStore* pMeta, int64_t key, void* value) {
L
Liu Jicong 已提交
261 262 263 264 265 266 267 268
  int64_t bucketKey = key & TQ_BUCKET_SIZE;
  TqMetaList* pNode = pMeta->bucket[bucketKey];
  while(pNode) {
    if(pNode->handle.key == key) {
      //TODO: think about thread safety
      pMeta->deleter(pNode->handle.valueInTxn);
      //change pointer ownership
      pNode->handle.valueInTxn = value;
L
Liu Jicong 已提交
269
      return 0;
L
Liu Jicong 已提交
270 271 272 273
    } else {
      pNode = pNode->next;
    }
  }
L
Liu Jicong 已提交
274 275 276 277 278 279 280 281
  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 已提交
282 283 284
  return 0;
}

L
Liu Jicong 已提交
285
static TqMetaHandle* tqHandleGetUncommitted(TqMetaStore* pMeta, int64_t key) {
L
Liu Jicong 已提交
286 287 288 289 290 291 292 293 294 295 296 297 298
  int64_t bucketKey = key & TQ_BUCKET_SIZE;
  TqMetaList* pNode = pMeta->bucket[bucketKey];
  while(pNode) {
    if(pNode->handle.key == key) {
      if(pNode->handle.valueInTxn != NULL) {
        return &pNode->handle;
      } else {
        return NULL;
      }
    } else {
      pNode = pNode->next;
    }
  }
L
Liu Jicong 已提交
299 300 301 302
  return NULL;
}

int32_t tqHandleCommit(TqMetaStore* pMeta, int64_t key) {
L
Liu Jicong 已提交
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
  int64_t bucketKey = key & TQ_BUCKET_SIZE;
  TqMetaList* pNode = pMeta->bucket[bucketKey];
  while(pNode) {
    if(pNode->handle.key == key) {
      if(pNode->handle.valueInUse != NULL) {
        pMeta->deleter(pNode->handle.valueInUse);
      }
      pNode->handle.valueInUse = pNode->handle.valueInTxn;
      if(pNode->unpersistNext == NULL) {
        pNode->unpersistNext = pMeta->unpersistHead->unpersistNext;
        pNode->unpersistPrev = pMeta->unpersistHead;
        pMeta->unpersistHead->unpersistNext->unpersistPrev = pNode;
        pMeta->unpersistHead->unpersistNext = pNode;
      }
      return 0;
    } else {
      pNode = pNode->next;
    }
  }
  return -1;
L
Liu Jicong 已提交
323 324 325
}

int32_t tqHandleAbort(TqMetaStore* pMeta, int64_t key) {
L
Liu Jicong 已提交
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
  int64_t bucketKey = key & TQ_BUCKET_SIZE;
  TqMetaList* pNode = pMeta->bucket[bucketKey];
  while(pNode) {
    if(pNode->handle.key == key) {
      if(pNode->handle.valueInTxn != NULL) {
        pMeta->deleter(pNode->handle.valueInTxn);
        pNode->handle.valueInTxn = NULL;
        return 0;
      }
      return -1;
    } else {
      pNode = pNode->next;
    }
  }
  return -2;
L
Liu Jicong 已提交
341 342 343
}

int32_t tqHandleDel(TqMetaStore* pMeta, int64_t key) {
L
Liu Jicong 已提交
344 345 346 347
  int64_t bucketKey = key & TQ_BUCKET_SIZE;
  TqMetaList* pNode = pMeta->bucket[bucketKey];
  while(pNode) {
    if(pNode->handle.key == key) {
L
Liu Jicong 已提交
348 349 350
      pMeta->deleter(pNode->handle.valueInTxn);
      pNode->handle.valueInTxn = NULL;
      return 0;
L
Liu Jicong 已提交
351 352 353 354
    } else {
      pNode = pNode->next;
    }
  }
L
Liu Jicong 已提交
355 356
  //no such key
  return -1;
L
Liu Jicong 已提交
357 358 359
}

int32_t tqHandleClear(TqMetaStore* pMeta, int64_t key) {
L
Liu Jicong 已提交
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
  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;
        pMeta->deleter(pNode->handle.valueInUse);
        pNode->handle.valueInUse = NULL;
      }
      if(pNode->handle.valueInTxn != NULL) {
        exist = true;
        pMeta->deleter(pNode->handle.valueInTxn);
        pNode->handle.valueInTxn = NULL;
      }
      if(exist) {
        if(pNode->unpersistNext == NULL) {
          pNode->unpersistNext = pMeta->unpersistHead->unpersistNext;
          pNode->unpersistPrev = pMeta->unpersistHead;
          pMeta->unpersistHead->unpersistNext->unpersistPrev = pNode;
          pMeta->unpersistHead->unpersistNext = pNode;
        }
        return 0;
      }
      return -1;
    } else {
      pNode = pNode->next;
    }
  }
  return -2;
L
Liu Jicong 已提交
390
}