tkvstore.c 15.4 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
/*
 * 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/>.
 */
#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include "hash.h"
#include "os.h"
#include "taoserror.h"
#include "tchecksum.h"
#include "tcoding.h"
#include "tkvstore.h"
#include "tulog.h"

#define TD_KVSTORE_HEADER_SIZE 512
#define TD_KVSTORE_MAJOR_VERSION 1
#define TD_KVSTORE_MAINOR_VERSION 0
#define TD_KVSTORE_SNAP_SUFFIX ".snap"
#define TD_KVSTORE_NEW_SUFFIX ".new"

H
Hongze Cheng 已提交
37
typedef struct {
H
more  
Hongze Cheng 已提交
38 39
  uint64_t uid;
  int64_t  offset;
H
Hongze Cheng 已提交
40
  int64_t  size;
H
more  
Hongze Cheng 已提交
41 42
} SKVRecord;

H
Hongze Cheng 已提交
43 44
static int       tdInitKVStoreHeader(int fd, char *fname);
static void *    tdEncodeStoreInfo(void *buf, SStoreInfo *pInfo);
H
more  
Hongze Cheng 已提交
45
static void *    tdDecodeStoreInfo(void *buf, SStoreInfo *pInfo);
H
Hongze Cheng 已提交
46 47 48 49
static SKVStore *tdNewKVStore(char *fname, iterFunc iFunc, afterFunc aFunc, void *appH);
static char *    tdGetKVStoreSnapshotFname(char *fdata);
static char *    tdGetKVStoreNewFname(char *fdata);
static void      tdFreeKVStore(SKVStore *pStore);
H
Hongze Cheng 已提交
50
static int       tdUpdateKVStoreHeader(int fd, char *fname, SStoreInfo *pInfo);
H
more  
Hongze Cheng 已提交
51
static int       tdLoadKVStoreHeader(int fd, char *fname, SStoreInfo *pInfo);
H
Hongze Cheng 已提交
52
static void *    tdEncodeKVRecord(void *buf, SKVRecord *pRecord);
H
more  
Hongze Cheng 已提交
53 54
static void *    tdDecodeKVRecord(void *buf, SKVRecord *pRecord);
static int       tdRestoreKVStore(SKVStore *pStore);
H
Hongze Cheng 已提交
55 56 57 58 59

int tdCreateKVStore(char *fname) {
  int fd = open(fname, O_RDWR | O_CREAT, 0755);
  if (fd < 0) {
    uError("failed to open file %s since %s", fname, strerror(errno));
H
Hongze Cheng 已提交
60 61
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
H
Hongze Cheng 已提交
62 63
  }

H
Hongze Cheng 已提交
64 65 66 67
  if (tdInitKVStoreHeader(fd, fname) < 0) {
    close(fd);
    return -1;
  }
H
Hongze Cheng 已提交
68 69 70

  if (fsync(fd) < 0) {
    uError("failed to fsync file %s since %s", fname, strerror(errno));
H
Hongze Cheng 已提交
71 72 73
    terrno = TAOS_SYSTEM_ERROR(errno);
    close(fd);
    return -1;
H
Hongze Cheng 已提交
74 75 76 77
  }

  if (close(fd) < 0) {
    uError("failed to close file %s since %s", fname, strerror(errno));
H
Hongze Cheng 已提交
78 79 80 81 82 83 84 85 86 87 88 89
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

  return 0;
}

int tdDestroyKVStore(char *fname) {
  if (remove(fname) < 0) {
    uError("failed to remove file %s since %s", fname, strerror(errno));
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
H
Hongze Cheng 已提交
90 91
  }

H
Hongze Cheng 已提交
92
  return 0;
H
Hongze Cheng 已提交
93 94 95
}

SKVStore *tdOpenKVStore(char *fname, iterFunc iFunc, afterFunc aFunc, void *appH) {
H
more  
Hongze Cheng 已提交
96
  SStoreInfo info = {0};
H
more  
Hongze Cheng 已提交
97

H
Hongze Cheng 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
  SKVStore *pStore = tdNewKVStore(fname, iFunc, aFunc, appH);
  if (pStore == NULL) return NULL;

  pStore->fd = open(pStore->fname, O_RDWR);
  if (pStore->fd < 0) {
    uError("failed to open file %s since %s", pStore->fname, strerror(errno));
    terrno = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  if (access(pStore->fsnap, F_OK) == 0) {
    uTrace("file %s exists, try to recover the KV store", pStore->fsnap);
    pStore->sfd = open(pStore->fsnap, O_RDONLY);
    if (pStore->sfd < 0) {
      uError("failed to open file %s since %s", pStore->fsnap, strerror(errno));
      terrno = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }

H
more  
Hongze Cheng 已提交
117 118 119
    if (tdLoadKVStoreHeader(pStore->sfd, pStore->fsnap, &info) < 0) goto _err;

    if (ftruncate(pStore->fd, info.size) < 0) {
B
Bomin Zhang 已提交
120
      uError("failed to truncate %s to %" PRId64 " size since %s", pStore->fname, info.size, strerror(errno));
H
more  
Hongze Cheng 已提交
121 122 123
      terrno = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }
H
Hongze Cheng 已提交
124

H
Hongze Cheng 已提交
125 126
    if (tdUpdateKVStoreHeader(pStore->fd, pStore->fname, &info) < 0) goto _err;

H
Hongze Cheng 已提交
127 128 129 130 131
    close(pStore->sfd);
    pStore->sfd = -1;
    remove(pStore->fsnap);
  }

H
Hongze Cheng 已提交
132 133 134 135 136 137 138 139 140 141
  if (tdLoadKVStoreHeader(pStore->fd, pStore->fname, &info) < 0) goto _err;

  struct stat tfstat;
  if (fstat(pStore->fd, &tfstat) < 0) {
    uError("failed to fstat file %s since %s", pStore->fname, strerror(errno));
    terrno = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  ASSERT(info.size == tfstat.st_size);
H
more  
Hongze Cheng 已提交
142 143 144 145 146 147 148

  if (lseek(pStore->fd, TD_KVSTORE_HEADER_SIZE, SEEK_SET) < 0) {
    uError("failed to lseek file %s since %s", pStore->fname, strerror(errno));
    terrno = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

H
Hongze Cheng 已提交
149 150 151
  pStore->info.size += TD_KVSTORE_HEADER_SIZE;

  if (tdRestoreKVStore(pStore) < 0) goto _err;
H
more  
Hongze Cheng 已提交
152 153 154

  close(pStore->fd);
  pStore->fd = -1;
H
Hongze Cheng 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

  return pStore;

_err:
  if (pStore->fd > 0) {
    close(pStore->fd);
    pStore->fd = -1;
  }
  if (pStore->sfd > 0) {
    close(pStore->sfd);
    pStore->sfd = -1;
  }
  tdFreeKVStore(pStore);
  return NULL;
}

H
Hongze Cheng 已提交
171 172
void tdCloseKVStore(SKVStore *pStore) { tdFreeKVStore(pStore); }

H
Hongze Cheng 已提交
173
int tdKVStoreStartCommit(SKVStore *pStore) {
H
Hongze Cheng 已提交
174 175
  ASSERT(pStore->fd < 0);

H
Hongze Cheng 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
  pStore->fd = open(pStore->fname, O_RDWR);
  if (pStore->fd < 0) {
    uError("failed to open file %s since %s", pStore->fname, strerror(errno));
    terrno = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  pStore->sfd = open(pStore->fsnap, O_WRONLY | O_CREAT, 0755);
  if (pStore->sfd < 0) {
    uError("failed to open file %s since %s", pStore->fsnap, strerror(errno));
    terrno = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  if (tsendfile(pStore->sfd, pStore->fd, NULL, TD_KVSTORE_HEADER_SIZE) < TD_KVSTORE_HEADER_SIZE) {
    uError("failed to send file %d bytes since %s", TD_KVSTORE_HEADER_SIZE, strerror(errno));
    terrno = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  if (fsync(pStore->sfd) < 0) {
    uError("failed to fsync file %s since %s", pStore->fsnap, strerror(errno));
    terrno = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  if (close(pStore->sfd) < 0) {
    uError("failed to close file %s since %s", pStore->fsnap, strerror(errno));
    terrno = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }
  pStore->sfd = -1;

H
Hongze Cheng 已提交
209 210 211 212 213 214 215 216
  if (lseek(pStore->fd, 0, SEEK_END) < 0) {
    uError("failed to lseek file %s since %s", pStore->fname, strerror(errno));
    terrno = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  ASSERT(pStore->info.size == lseek(pStore->fd, 0, SEEK_CUR));

H
Hongze Cheng 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
  return 0;

_err:
  if (pStore->sfd > 0) {
    close(pStore->sfd);
    pStore->sfd = -1;
    remove(pStore->fsnap);
  }
  if (pStore->fd > 0) {
    close(pStore->fd);
    pStore->fd = -1;
  }
  return -1;
}

H
Hongze Cheng 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
int tdUpdateKVStoreRecord(SKVStore *pStore, uint64_t uid, void *cont, int contLen) {
  SKVRecord *pRecord = taosHashGet(pStore->map, (void *)&uid, sizeof(uid));
  if (pRecord != NULL) {
    pStore->info.tombSize += (pRecord->size + sizeof(SKVRecord));
  }

  // TODO
  return 0;
}

int tdDropKVStoreRecord(SKVStore *pStore, uint64_t uid) {
  SKVRecord rInfo = {0};
  char      buf[128] = "\0";

  SKVRecord *pRecord = taosHashGet(pStore->map, &uid, sizeof(uid));
  if (pRecord == NULL) {
B
Bomin Zhang 已提交
248
    uError("failed to drop KV store record with key %" PRIu64 " since not find", uid);
H
Hongze Cheng 已提交
249 250 251 252 253 254 255 256 257 258
    return -1;
  }

  rInfo.offset = -pRecord->offset;
  rInfo.uid = pRecord->uid;
  rInfo.size = pRecord->size;

  void *pBuf = tdEncodeKVRecord(buf, &rInfo);

  if (twrite(pStore->fd, buf, POINTER_DISTANCE(pBuf, buf)) < POINTER_DISTANCE(pBuf, buf)) {
B
Bomin Zhang 已提交
259
    uError("failed to write %" PRId64 " bytes to file %s since %s", POINTER_DISTANCE(pBuf, buf), pStore->fname, strerror(errno));
H
Hongze Cheng 已提交
260 261 262 263 264 265 266 267 268 269 270 271
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

  pStore->info.size += POINTER_DISTANCE(pBuf, buf);
  pStore->info.nDels++;
  pStore->info.nRecords--;
  pStore->info.tombSize += (rInfo.size + sizeof(SKVRecord) * 2);

  return 0;
}

H
Hongze Cheng 已提交
272 273 274
int tdKVStoreEndCommit(SKVStore *pStore) {
  ASSERT(pStore->fd > 0);

H
Hongze Cheng 已提交
275
  if (tdUpdateKVStoreHeader(pStore->fd, pStore->fname, &(pStore->info)) < 0) return -1;
H
Hongze Cheng 已提交
276

H
Hongze Cheng 已提交
277 278 279 280 281 282 283 284 285 286 287
  if (fsync(pStore->fd) < 0) {
    uError("failed to fsync file %s since %s", pStore->fname, strerror(errno));
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

  if (close(pStore->fd) < 0) {
    uError("failed to close file %s since %s", pStore->fname, strerror(errno));
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }
H
Hongze Cheng 已提交
288
  pStore->fd = -1;
H
Hongze Cheng 已提交
289 290 291 292 293

  remove(pStore->fsnap);
  return 0;
}

H
more  
Hongze Cheng 已提交
294 295 296
static int tdLoadKVStoreHeader(int fd, char *fname, SStoreInfo *pInfo) {
  char buf[TD_KVSTORE_HEADER_SIZE] = "\0";

H
Hongze Cheng 已提交
297 298 299 300 301 302
  if (lseek(fd, 0, SEEK_SET) < 0) {
    uError("failed to lseek file %s since %s", fname, strerror(errno));
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

H
more  
Hongze Cheng 已提交
303
  if (tread(fd, buf, TD_KVSTORE_HEADER_SIZE) < TD_KVSTORE_HEADER_SIZE) {
H
Hongze Cheng 已提交
304
    uError("failed to read %d bytes from file %s since %s", TD_KVSTORE_HEADER_SIZE, fname, strerror(errno));
H
more  
Hongze Cheng 已提交
305 306 307 308 309 310
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

  if (!taosCheckChecksumWhole((uint8_t *)buf, TD_KVSTORE_HEADER_SIZE)) {
    uError("file %s is broken", fname);
H
Hongze Cheng 已提交
311
    terrno = TSDB_CODE_COM_FILE_CORRUPTED;
H
more  
Hongze Cheng 已提交
312 313 314 315 316 317 318 319
    return -1;
  }

  tdDecodeStoreInfo(buf, pInfo);

  return 0;
}

H
Hongze Cheng 已提交
320 321 322 323 324
static int tdUpdateKVStoreHeader(int fd, char *fname, SStoreInfo *pInfo) {
  char buf[TD_KVSTORE_HEADER_SIZE] = "\0";

  if (lseek(fd, 0, SEEK_SET) < 0) {
    uError("failed to lseek file %s since %s", fname, strerror(errno));
H
Hongze Cheng 已提交
325 326
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
H
Hongze Cheng 已提交
327 328 329 330 331
  }

  tdEncodeStoreInfo(buf, pInfo);
  taosCalcChecksumAppend(0, (uint8_t *)buf, TD_KVSTORE_HEADER_SIZE);
  if (twrite(fd, buf, TD_KVSTORE_HEADER_SIZE) < TD_KVSTORE_HEADER_SIZE) {
H
Hongze Cheng 已提交
332 333 334
    uError("failed to write %d bytes to file %s since %s", TD_KVSTORE_HEADER_SIZE, fname, strerror(errno));
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
H
Hongze Cheng 已提交
335 336
  }

H
Hongze Cheng 已提交
337
  return 0;
H
Hongze Cheng 已提交
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
}

static int tdInitKVStoreHeader(int fd, char *fname) {
  SStoreInfo info = {TD_KVSTORE_HEADER_SIZE, 0, 0, 0};

  return tdUpdateKVStoreHeader(fd, fname, &info);
}

static void *tdEncodeStoreInfo(void *buf, SStoreInfo *pInfo) {
  buf = taosEncodeVariantI64(buf, pInfo->size);
  buf = taosEncodeVariantI64(buf, pInfo->tombSize);
  buf = taosEncodeVariantI64(buf, pInfo->nRecords);
  buf = taosEncodeVariantI64(buf, pInfo->nDels);

  return buf;
}

H
more  
Hongze Cheng 已提交
355 356 357 358 359
static void *tdDecodeStoreInfo(void *buf, SStoreInfo *pInfo) {
  buf = taosDecodeVariantI64(buf, &(pInfo->size));
  buf = taosDecodeVariantI64(buf, &(pInfo->tombSize));
  buf = taosDecodeVariantI64(buf, &(pInfo->nRecords));
  buf = taosDecodeVariantI64(buf, &(pInfo->nDels));
H
Hongze Cheng 已提交
360

H
more  
Hongze Cheng 已提交
361 362
  return buf;
}
H
Hongze Cheng 已提交
363 364 365 366 367 368

static SKVStore *tdNewKVStore(char *fname, iterFunc iFunc, afterFunc aFunc, void *appH) {
  SKVStore *pStore = (SKVStore *)malloc(sizeof(SKVStore));
  if (pStore == NULL) goto _err;

  pStore->fname = strdup(fname);
H
Hongze Cheng 已提交
369 370 371 372
  if (pStore->map == NULL) {
    terrno = TSDB_CODE_COM_OUT_OF_MEMORY;
    goto _err;
  }
H
Hongze Cheng 已提交
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387

  pStore->fsnap = tdGetKVStoreSnapshotFname(fname);
  if (pStore->fsnap == NULL) goto _err;

  pStore->fnew = tdGetKVStoreNewFname(fname);
  if (pStore->fnew == NULL) goto _err;

  pStore->fd = -1;
  pStore->sfd = -1;
  pStore->nfd = -1;
  pStore->iFunc = iFunc;
  pStore->aFunc = aFunc;
  pStore->appH = appH;
  pStore->map = taosHashInit(4096, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false);
  if (pStore->map == NULL) {
H
Hongze Cheng 已提交
388
    terrno = TSDB_CODE_COM_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
    goto _err;
  }

  return pStore;

_err:
  tdFreeKVStore(pStore);
  return NULL;
}

static void tdFreeKVStore(SKVStore *pStore) {
  if (pStore) {
    tfree(pStore->fname);
    tfree(pStore->fsnap);
    tfree(pStore->fnew);
    taosHashCleanup(pStore->map);
    free(pStore);
  }
}

static char *tdGetKVStoreSnapshotFname(char *fdata) {
  size_t size = strlen(fdata) + strlen(TD_KVSTORE_SNAP_SUFFIX) + 1;
  char * fname = malloc(size);
  if (fname == NULL) {
H
Hongze Cheng 已提交
413
    terrno = TSDB_CODE_COM_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
414 415 416 417 418 419 420 421 422 423
    return NULL;
  }
  sprintf(fname, "%s%s", fdata, TD_KVSTORE_SNAP_SUFFIX);
  return fname;
}

static char *tdGetKVStoreNewFname(char *fdata) {
  size_t size = strlen(fdata) + strlen(TD_KVSTORE_NEW_SUFFIX) + 1;
  char * fname = malloc(size);
  if (fname == NULL) {
H
Hongze Cheng 已提交
424
    terrno = TSDB_CODE_COM_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
425 426 427 428
    return NULL;
  }
  sprintf(fname, "%s%s", fdata, TD_KVSTORE_NEW_SUFFIX);
  return fname;
H
more  
Hongze Cheng 已提交
429 430
}

H
Hongze Cheng 已提交
431 432 433
static void *tdEncodeKVRecord(void *buf, SKVRecord *pRecord) {
  buf = taosEncodeFixedU64(buf, pRecord->uid);
  buf = taosEncodeFixedI64(buf, pRecord->offset);
H
Hongze Cheng 已提交
434
  buf = taosEncodeFixedI64(buf, pRecord->size);
H
more  
Hongze Cheng 已提交
435

H
Hongze Cheng 已提交
436 437
  return buf;
}
H
more  
Hongze Cheng 已提交
438 439 440 441

static void *tdDecodeKVRecord(void *buf, SKVRecord *pRecord) {
  buf = taosDecodeFixedU64(buf, &(pRecord->uid));
  buf = taosDecodeFixedI64(buf, &(pRecord->offset));
H
Hongze Cheng 已提交
442
  buf = taosDecodeFixedI64(buf, &(pRecord->size));
H
more  
Hongze Cheng 已提交
443 444 445 446 447 448

  return buf;
}

static int tdRestoreKVStore(SKVStore *pStore) {
  char      tbuf[128] = "\0";
H
Hongze Cheng 已提交
449 450
  void *    buf = NULL;
  int       maxBufSize = 0;
H
more  
Hongze Cheng 已提交
451 452
  SKVRecord rInfo = {0};

H
Hongze Cheng 已提交
453 454
  ASSERT(TD_KVSTORE_HEADER_SIZE == lseek(pStore->fd, 0, SEEK_CUR));

H
more  
Hongze Cheng 已提交
455 456 457 458
  while (true) {
    ssize_t tsize = tread(pStore->fd, tbuf, sizeof(SKVRecord));
    if (tsize == 0) break;
    if (tsize < sizeof(SKVRecord)) {
B
Bomin Zhang 已提交
459
      uError("failed to read %zu bytes from file %s since %s", sizeof(SKVRecord), pStore->fname, strerror(errno));
H
more  
Hongze Cheng 已提交
460 461 462 463 464 465
      terrno = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }

    char *pBuf = tdDecodeKVRecord(tbuf, &rInfo);
    ASSERT(POINTER_DISTANCE(pBuf, tbuf) == sizeof(SKVRecord));
H
Hongze Cheng 已提交
466
    ASSERT(rInfo.offset > 0 ? pStore->info.size == rInfo.offset : true);
H
more  
Hongze Cheng 已提交
467 468 469

    if (rInfo.offset < 0) {
      taosHashRemove(pStore->map, (void *)(&rInfo.uid), sizeof(rInfo.uid));
H
Hongze Cheng 已提交
470 471 472 473
      pStore->info.size += sizeof(SKVRecord);
      pStore->info.nRecords--;
      pStore->info.nDels++;
      pStore->info.tombSize += (rInfo.size + sizeof(SKVRecord) + sizeof(SKVRecord));
H
more  
Hongze Cheng 已提交
474
    } else {
H
Hongze Cheng 已提交
475 476
      // TODO: add statistics
      ASSERT(rInfo.offset > 0 && rInfo.size > 0);
H
more  
Hongze Cheng 已提交
477 478 479 480 481 482
      if (taosHashPut(pStore->map, (void *)(&rInfo.uid), sizeof(rInfo.uid), &rInfo, sizeof(rInfo)) < 0) {
        uError("failed to put record in KV store %s", pStore->fname);
        terrno = TSDB_CODE_COM_OUT_OF_MEMORY;
        goto _err;
      }

H
Hongze Cheng 已提交
483 484
      maxBufSize = MAX(maxBufSize, rInfo.size);

H
more  
Hongze Cheng 已提交
485 486 487 488 489 490 491 492
      if (lseek(pStore->fd, rInfo.size, SEEK_CUR) < 0) {
        uError("failed to lseek file %s since %s", pStore->fname, strerror(errno));
        terrno = TAOS_SYSTEM_ERROR(errno);
        goto _err;
      }
    }
  }

H
Hongze Cheng 已提交
493 494 495 496 497 498 499
  buf = malloc(maxBufSize);
  if (buf == NULL) {
    uError("failed to allocate %d bytes in KV store %s", maxBufSize, pStore->fname);
    terrno = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

H
more  
Hongze Cheng 已提交
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
  SHashMutableIterator *pIter = taosHashCreateIter(pStore->map);
  if (pIter == NULL) {
    uError("failed to create hash iter while opening KV store %s", pStore->fname);
    terrno = TSDB_CODE_COM_OUT_OF_MEMORY;
    goto _err;
  }

  while (taosHashIterNext(pIter)) {
    SKVRecord *pRecord = taosHashIterGet(pIter);

    if (lseek(pStore->fd, pRecord->offset, SEEK_SET) < 0) {
      uError("failed to lseek file %s since %s", pStore->fname, strerror(errno));
      terrno = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }

    if (tread(pStore->fd, buf, pRecord->size) < pRecord->size) {
B
Bomin Zhang 已提交
517
      uError("failed to read %" PRId64 " bytes from file %s since %s", pRecord->size, pStore->fname, strerror(errno));
H
more  
Hongze Cheng 已提交
518 519 520 521 522
      terrno = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }

    if (!taosCheckChecksumWhole((uint8_t *)buf, pRecord->size)) {
B
Bomin Zhang 已提交
523
      uError("file %s has checksum error, offset %" PRId64 " size %" PRId64, pStore->fname, pRecord->offset, pRecord->size);
H
Hongze Cheng 已提交
524
      terrno = TSDB_CODE_COM_FILE_CORRUPTED;
H
more  
Hongze Cheng 已提交
525 526 527
      goto _err;
    }

H
Hongze Cheng 已提交
528
    if (pStore->iFunc) (*pStore->iFunc)(pStore->appH, buf, pRecord->size);
H
more  
Hongze Cheng 已提交
529 530 531 532 533 534 535
  }

  taosHashDestroyIter(pIter);

  if (pStore->aFunc) (*pStore->aFunc)(pStore->appH);

  tfree(buf);
H
Hongze Cheng 已提交
536
  return 0;
H
more  
Hongze Cheng 已提交
537 538 539

_err:
  tfree(buf);
H
Hui Li 已提交
540
  taosHashDestroyIter(pIter);
H
Hongze Cheng 已提交
541
  return -1;
H
Hongze Cheng 已提交
542
}