crovl_builder_increment.cpp 21.5 KB
Newer Older
D
dangyifei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

D
dangyifei 已提交
15
#include "cube-builder/crovl_builder_increment.h"
D
dangyifei 已提交
16 17 18 19 20 21 22
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fstream>
#include <iostream>
#include "boost/lexical_cast.hpp"
D
dangyifei 已提交
23 24 25
#include "cube-builder/curl_simple.h"
#include "cube-builder/define.h"
#include "cube-builder/util.h"
D
dangyifei 已提交
26 27 28 29 30 31 32 33 34 35 36 37 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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
#include "json/json.h"

static const uint64_t MB = 1048576;
// CSpinLock g_sLock;

CROVLBuilderIncremental::CROVLBuilderIncremental() {
  _index = NULL;
  _data = NULL;
  // _index_file = NULL;
  _inner_sort_size = 32 * MB;
  _memory_quota = 4 * 1024 * MB;
}

bool CROVLBuilderIncremental::Clear() {
  if (_index != NULL) {
    delete[] _index;
    _index = NULL;
  }

  if (_data != NULL) {
    delete[] _data;
    _data = NULL;
  }

  _data_file_list.clear();

  return true;
}

CROVLBuilderIncremental::~CROVLBuilderIncremental() { Clear(); }

bool CROVLBuilderIncremental::Init(int index_type,
                                   uint32_t data_file_len,
                                   const char *mode,
                                   const char *data_dir,
                                   const char *data_real_dir,
                                   const char *dict_name,
                                   const std::string &shard_id,
                                   const std::string &split_id,
                                   const std::string &last_version,
                                   const std::string &cur_version,
                                   const std::string &depend_version,
                                   const std::string &master_address,
                                   const std::string &id,
                                   const std::string &key,
                                   const std::string &extra,
                                   bool bFix,
                                   uint32_t nFixLen) {
  if (index_type != IT_HASH) {
    LOG(ERROR) << "Error: incremental_build not support IT_SORT";
    return false;
  }

  if (index_type != IT_HASH && index_type != IT_SORT) {
    LOG(ERROR) << "Error: invalid index type";
    return false;
  }

  if (index_type == IT_HASH && bFix) {
    LOG(ERROR) << "Error: can not set fix when index type is IT_HASH";
    return false;
  }

  if (nFixLen > MAX_FIXDATA_LEN) {
    LOG(ERROR) << "Error: fix length too large";
    return false;
  }

  if (data_file_len < MAX_DATA_LEN + 4 || data_file_len > MAX_DATA_FILE_LEN) {
    LOG(ERROR) << "Error: invalid data file length" << data_file_len;
    return false;
  }

  if (strlen(data_dir) > MAX_DATA_DIR_LEN) {
    LOG(ERROR) << "Error: data directory too long :" << data_dir;
    return false;
  }

  Clear();

  strncpy(_data_dir, data_dir, sizeof(_data_dir));
  strncpy(_data_real_dir, data_real_dir, sizeof(_data_real_dir));

  _index_type = index_type;
  _data_file_len = data_file_len;
  _data_file = 0;
  _data_buf_len = 0;
  _index_buf_len = 0;
  _index_file_num = 0;  // incremental_build for index
  _count = 0;
  _cur_count = 0;
  _fix = bFix;
  _fix_len = nFixLen;
  _index_file_len = 0;

  _dict_name = dict_name;
  _shard_id = shard_id;
  _split_id = split_id;
  _last_version = last_version;
  _cur_version = cur_version;
  _depend_version = depend_version;
  _master_address = master_address;
  _id = id;
  _key = key;
  _extra = extra;

  if (_fix) {
    _rec_size = nFixLen + sizeof(uint64_t);
  } else {
    _rec_size = 2 * sizeof(uint64_t);
  }

  _index = new uint64_t[INDEX_BUFFER];

  if (!_fix) {
    _data = new char[_data_file_len];
  } else if (_fix_len != 8) {
    _data = new char[_rec_size * INDEX_BUFFER];
  }

  if (!checkDirectory(_data_dir)) {
    LOG(ERROR) << "create _data_dir path failed: " << _data_dir;
    return false;
  }

  // read old info from data.n and index.n
  _mode = mode;
  LOG(INFO) << "mode: " << mode;

  if (strcmp(mode, "base") != 0) {
    if (_master_address == "") {
      if (!read_last_meta_from_local()) {
        LOG(ERROR) << "read last meta from db error! ";
        return false;
      }
    } else {
      if (!read_last_meta_from_transfer()) {
        LOG(ERROR) << "read last meta from transfer error! ";
        return false;
      }
    }
  }
  LOG(INFO) << "incremental_build  data file:" << _data_file
            << ", count:" << _count << ", index file:" << _index_file_num;
  return true;
}

bool CROVLBuilderIncremental::flush_data() {
  LOG(INFO) << "flush data" << _data_file;
  if (_data_buf_len == 0) {
    LOG(ERROR) << "_data_buf_len == 0";
    return true;
  }

  char file[MAX_DATA_DIR_LEN * 2];
D
dangyifei 已提交
181
  snprintf(file, sizeof(file), "%s/data.%lu", _data_dir, _data_file);
D
dangyifei 已提交
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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 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 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 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 358 359 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
  FILE *fp;
  if ((fp = fopen(file, "wb")) == NULL) {
    LOG(ERROR) << "open file failed! " << file;
    return false;
  }

  if (fwrite(_data, 1, _data_buf_len, fp) != _data_buf_len) {
    fclose(fp);
    LOG(ERROR) << "write file:" << file << " error!";
    return false;
  }
  fclose(fp);

  _data_file_list.push_back(_data_buf_len);
  _data_buf_len = 0;
  ++_data_file;
  return true;
}

bool CROVLBuilderIncremental::FlushIndex() {
  LOG(INFO) << "flush index:" << _index_file_num;

  if (_index_buf_len == 0) {
    LOG(ERROR) << "_index_buf_len == 0";
    return true;
  }

  char file[MAX_DATA_DIR_LEN * 2];
  snprintf(file, sizeof(file), "%s/index.%d", _data_dir, _index_file_num);
  FILE *fp;
  if ((fp = fopen(file, "wb")) == NULL) {
    LOG(ERROR) << "open file failed! " << file;
    return false;
  }

  if (_fix && _fix_len != 8) {
    if (fwrite(_data, _rec_size, _index_buf_len, fp) != _index_buf_len) {
      LOG(ERROR) << "_index_buf_len error:" << _index_buf_len;
      return false;
    }
    _index_buf_len = 0;
    return true;
  }

  if (fwrite(_index, sizeof(uint64_t), _index_buf_len, fp) != _index_buf_len) {
    LOG(ERROR) << "write file:" << file << " error!";
    return false;
  }
  fclose(fp);

  _index_file_len += _index_buf_len * sizeof(uint64_t);
  _index_buf_len = 0;
  return true;
}

int CROVLBuilderIncremental::add(uint64_t nKey,
                                 uint32_t nLen,
                                 const char *pData) {
  uint64_t naddr;
  uint32_t nTotalLen;

  if (nLen > MAX_DATA_LEN || _fix) {
    return 0;
  }

  nTotalLen = nLen + 4;

  do {
    if (_data_buf_len + nTotalLen > _data_file_len) {
      if (!flush_data()) {
        break;
      }
    }

    *reinterpret_cast<uint32_t *>(_data + _data_buf_len) = nTotalLen;
    memcpy(_data + _data_buf_len + 4, pData, nLen);
    naddr = (_data_file << 32) + _data_buf_len;
    _data_buf_len += nTotalLen;

    if (_index_buf_len + 2 > INDEX_BUFFER) {
      if (!FlushIndex()) {
        break;
      }
    }

    _index[_index_buf_len] = nKey;
    _index[_index_buf_len + 1] = naddr;
    _index_buf_len += 2;
    ++_count;
    ++_cur_count;
    return 1;
  } while (false);

  return -1;
}

int CROVLBuilderIncremental::add(uint64_t nKey, uint64_t nValue) {
  if (!_fix || _fix_len != 8) {
    return 0;
  }

  if (_index_buf_len + 2 > INDEX_BUFFER) {
    if (!FlushIndex()) {
      return -1;
    }
  }

  _index[_index_buf_len] = nKey;
  _index[_index_buf_len + 1] = nValue;
  _index_buf_len += 2;
  ++_count;
  ++_cur_count;
  return 1;
}

int CROVLBuilderIncremental::add(uint64_t nKey, const char *pData) {
  if (!_fix) {
    return 0;
  }

  if (_fix && _fix_len == 8) {
    return add(nKey, *reinterpret_cast<uint64_t *>(const_cast<char *>(pData)));
  }

  if (_index_buf_len + 1 > INDEX_BUFFER) {
    if (!FlushIndex()) {
      return -1;
    }
  }

  *reinterpret_cast<uint64_t *>(_data + _index_buf_len * _rec_size) = nKey;
  memcpy(
      _data + (_index_buf_len * _rec_size + sizeof(uint64_t)), pData, _fix_len);
  _index_buf_len++;
  ++_count;
  ++_cur_count;
  return 1;
}

bool CROVLBuilderIncremental::done() {
  LOG(INFO) << "done";
  if (!_fix) {
    if (!flush_data()) {
      return false;
    }
  }

  if (!FlushIndex()) {
    return false;
  }

  if (!_fix) {
    if (_data_file == 0) {
      return false;
    }
  }

  char buffer[64];
  char file[MAX_DATA_DIR_LEN * 2];

  // write data.n
  snprintf(file, sizeof(file), "%s/data.n", _data_dir);
  FILE *fp;
  if ((fp = fopen(file, "wb")) == NULL) {
    LOG(ERROR) << "open file failed! " << file;
    return false;
  }

  *reinterpret_cast<uint32_t *>(buffer) = _data_file;
  if (fwrite(buffer, sizeof(uint32_t), 1, fp) != 1) {
    fclose(fp);
    return false;
  }

  for (uint32_t i = 0; i < _data_file_list.size(); ++i) {
    *reinterpret_cast<uint32_t *>(buffer) = _data_file_list[i];

    if (fwrite(buffer, sizeof(uint32_t), 1, fp) != 1) {
      fclose(fp);
      return false;
    }
  }
  fclose(fp);

  // write index.n
  if (_index_type == IT_HASH) {
    if (_count > (uint64_t)((uint32_t)-1)) {
      return false;
    }

    snprintf(file, sizeof(file), "%s/index.n", _data_dir);

    if ((fp = fopen(file, "wb")) == nullptr) {
      LOG(ERROR) << "open file failed! " << file;
      return false;
    }

    *reinterpret_cast<uint32_t *>(buffer) = IT_HASH;
    *reinterpret_cast<uint32_t *>(buffer + sizeof(uint32_t)) = (uint32_t)_count;

    if (fwrite(buffer, sizeof(uint32_t), 2, fp) != 2) {
      fclose(fp);
      return false;
    }

    // fix empty data bug
    // if the version no data, filter this version, index_file_num no add
D
dangyifei 已提交
389
    // only can patch is null, not base
D
dangyifei 已提交
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
    if (_cur_count > 0) {
      *reinterpret_cast<uint32_t *>(buffer) = ++_index_file_num;
    } else {
      *reinterpret_cast<uint32_t *>(buffer) = _index_file_num;
    }

    if (fwrite(buffer, sizeof(uint32_t), 1, fp) != 1) {
      fclose(fp);
      return false;
    }

    if (_cur_count > 0) {
      _index_file_list.push_back(_index_file_len);
    }
    for (uint32_t i = 0; i < _index_file_list.size(); ++i) {
      *reinterpret_cast<uint64_t *>(buffer) = _index_file_list[i];

      if (fwrite(buffer, sizeof(uint64_t), 1, fp) != 1) {
        fclose(fp);
        return false;
      }
    }
    fclose(fp);

    if (_master_address == "") {
      if (!write_cur_meta_to_local()) {
        LOG(ERROR) << "write cur meta to local error";
        return false;
      }
    } else {
      if (!write_cur_meta_to_transfer()) {
        LOG(ERROR) << "write cur meta to db error master addr:"
                   << _master_address.c_str();
        return false;
      }
    }

  } else {
    snprintf(file, sizeof(file), "%s/index.d", _data_dir);
    LOG(ERROR) << "HASH is error!";
  }

  if (!Clear()) {
    LOG(ERROR) << "clear error!";
    return false;
  }

  return true;
}

void CROVLBuilderIncremental::archive() { CmdTarfiles(_data_dir); }

void CROVLBuilderIncremental::md5sum() { CmdMd5sum(_data_dir); }

bool CROVLBuilderIncremental::read_last_meta_from_transfer() {
  std::string url = "http://" + _master_address + "/dict/meta_info?name=" +
                    _dict_name + "&shard=" + _shard_id + "&split=" + _split_id +
                    "&version=" + _last_version + "&depend=" + _depend_version;

  LOG(INFO) << "name:" << _dict_name.c_str() << " shard:" << _shard_id.c_str()
            << " split:" << _split_id.c_str()
            << " last version:" << _last_version.c_str()
            << " depend version:" << _depend_version.c_str();

  CurlSimple cs;
  std::string result = cs.curl_get(url.c_str());

  if (result == "") {
    LOG(ERROR) << "curl get error!";
    return false;
  }

  LOG(INFO) << "curl result:" << result.c_str();
  Json::Reader reader(Json::Features::strictMode());
  Json::Value val;

  if (!reader.parse(result, val)) {
    LOG(ERROR) << "parse result json error!";
    return false;
  }

  if (!val.isObject()) {
    LOG(ERROR) << "no valild json error!";
    return false;
  }

  if (val["success"].isNull() || !val["success"].isString()) {
    LOG(ERROR) << "parse field success error!";
    return false;
  }

  std::string success = val["success"].asString();

  if (success != "0") {
    LOG(ERROR) << "parse field success error!" << success.c_str();
    return false;
  }

  if (val["data"].isNull() || !val["data"].isObject()) {
    LOG(ERROR) << "parse field data error!";
    return false;
  }

  Json::Value data = val["data"];

  if (data["meta"].isNull() || !data["meta"].isString()) {
    LOG(ERROR) << "parse field meta error!";
    return false;
  }

  Json::Reader meta_reader(Json::Features::strictMode());
  Json::Value meta;

  if (!meta_reader.parse(data["meta"].asString(), meta)) {
    LOG(ERROR) << "parse meta json error!";
    return false;
  }

  if (!meta.isObject()) {
    LOG(ERROR) << "parse meta json error!";
    return false;
  }

  if (meta["data_len_list"].isNull() || !meta["data_len_list"].isArray()) {
    LOG(ERROR) << "parse data_len_list json error!";
    return false;
  }

  Json::Value data_len_list = meta["data_len_list"];
  _data_file = data_len_list.size();

  if (_data_file == 0) {
    LOG(ERROR) << "data len list size is 0!";
    return false;
  }

  for (int i = 0; i < static_cast<int>(_data_file); ++i) {
    LOG(INFO) << "data_len:" << data_len_list[i].asString().c_str();

    try {
      _data_file_list.push_back(
          boost::lexical_cast<uint32_t>(data_len_list[i].asString()));
    } catch (boost::bad_lexical_cast &e) {
      LOG(ERROR) << "bad lexical cast:" << e.what();
      return false;
    }
  }

  if (meta["index_len_list"].isNull() || !meta["index_len_list"].isArray()) {
    LOG(ERROR) << "parse index_len_list json error!";
    return false;
  }

  Json::Value index_len_list = meta["index_len_list"];
  _index_file_num = index_len_list.size();

  if (_index_file_num == 0) {
    LOG(ERROR) << "index len list size is 0!";
    return false;
  }

  for (uint32_t i = 0; i < _index_file_num; ++i) {
    LOG(INFO) << "index_len:" << index_len_list[i].asString().c_str();

    try {
      _index_file_list.push_back(
          boost::lexical_cast<uint64_t>(index_len_list[i].asString()));
    } catch (boost::bad_lexical_cast &e) {
      LOG(ERROR) << "bad lexical cast:" << e.what();
      return false;
    }
  }

  if (meta["index_total_count"].isNull() ||
      !meta["index_total_count"].isString()) {
    LOG(ERROR) << "parse index_total_count json error!";
    return false;
  }

  LOG(INFO) << "index_total_count:"
            << meta["index_total_count"].asString().c_str();
  try {
    _count =
        boost::lexical_cast<uint64_t>(meta["index_total_count"].asString());
  } catch (boost::bad_lexical_cast &e) {
    LOG(ERROR) << "bad lexical cast:" << e.what();
    return false;
  }

  return true;
}

bool CROVLBuilderIncremental::write_cur_meta_to_transfer() {
  CurlSimple cs;
  std::string url = "http://" + _master_address + "/dict/meta_info/register";

  std::map<std::string, std::string> args;
  args["name"] = _dict_name;
  args["shard"] = _shard_id;
  args["split"] = _split_id;
  args["version"] = _cur_version;
  args["depend"] = _depend_version;

  LOG(INFO) << "name:" << _dict_name.c_str() << " shard:" << _shard_id.c_str()
            << " split:" << _split_id.c_str()
            << " cur version:" << _cur_version.c_str()
            << " depend version:" << _depend_version.c_str();

  Json::Value root;

  try {
    root["index_total_count"] = boost::lexical_cast<std::string>(_count);
  } catch (boost::bad_lexical_cast &e) {
    LOG(ERROR) << "bad lexical cast:" << e.what();
    return false;
  }

  Json::Value data;

  for (size_t i = 0; i < _data_file_list.size(); ++i) {
    try {
      data.append(boost::lexical_cast<std::string>(_data_file_list[i]));
    } catch (boost::bad_lexical_cast &e) {
      LOG(ERROR) << "bad lexical cast:" << e.what();
      return false;
    }
  }

  root["data_len_list"] = data;

  Json::Value index;

  for (size_t i = 0; i < _index_file_list.size(); ++i) {
    try {
      index.append(boost::lexical_cast<std::string>(_index_file_list[i]));
    } catch (boost::bad_lexical_cast &e) {
      LOG(ERROR) << "bad lexical cast:" << e.what();
      return false;
    }
  }

  root["index_len_list"] = index;

  Json::FastWriter writer;
  // Json::StreamWriterBuilder writer;
  std::string meta = writer.write(root);

  // std::string meta = Json::writeString(writer, root);
  if (meta[meta.size() - 1] == '\n') {
    meta.erase(meta.size() - 1, 1);
  }

  LOG(INFO) << "meta:" << meta.c_str() << " size:" << meta.size();
  args["meta"] = meta;
  std::string result = cs.curl_post(url.c_str(), args);

  if (result == "") {
    LOG(ERROR) << "curl get error!";
    return false;
  }

  LOG(INFO) << "curl result:" << result.c_str();

  Json::Reader reader(Json::Features::strictMode());
  Json::Value val;

  if (!reader.parse(result, val)) {
    LOG(ERROR) << "parse result json error!";
    return false;
  }

  if (!val.isObject()) {
    LOG(ERROR) << "no valild json error!";
    return false;
  }

  if (val["success"].isNull() || !val["success"].isString()) {
    LOG(ERROR) << "parse field success error!";
    return false;
  }

  std::string success = val["success"].asString();

  if (success != "0") {
    LOG(ERROR) << "parse field success error!" << success.c_str();
    return false;
  }

  LOG(INFO) << "curl post ok";
  return true;
}

bool CROVLBuilderIncremental::read_last_meta_from_local() {
  char file[MAX_DATA_DIR_LEN * 2];

  snprintf(file,
           sizeof(file),
           "%s/../../meta_info/%s_%s_%s_%s.json",
           _data_dir,
           _last_version.c_str(),
           _depend_version.c_str(),
           _shard_id.c_str(),
           _split_id.c_str());

  // read from local meta file
  std::string input_meta;
  Json::Reader meta_reader(Json::Features::strictMode());
  Json::Value meta;

  std::ifstream ifs;
  ifs.open(file);
  if (!ifs) {
    LOG(ERROR) << "open file failed! " << file;
    return false;
  }

  if (!meta_reader.parse(ifs, meta)) {
    LOG(ERROR) << "parse meta json error!";
    return false;
  }
  ifs.close();

  if (!meta.isObject()) {
    LOG(ERROR) << "parse meta json error!";
    return false;
  }

  if (meta["data_len_list"].isNull() || !meta["data_len_list"].isArray()) {
    LOG(ERROR) << "parse data_len_list json error !";
    return false;
  }

  Json::Value data_len_list = meta["data_len_list"];
  _data_file = data_len_list.size();

  if (_data_file == 0) {
    LOG(ERROR) << "data len list size is 0 !";
    return false;
  }

  for (int i = 0; i < static_cast<int>(_data_file); ++i) {
    LOG(INFO) << "data_len:" << data_len_list[i].asString().c_str();

    try {
      _data_file_list.push_back(
          boost::lexical_cast<uint32_t>(data_len_list[i].asString()));
    } catch (boost::bad_lexical_cast &e) {
      LOG(ERROR) << "bad lexical cast:" << e.what();
      return false;
    }
  }

  if (meta["index_len_list"].isNull() || !meta["index_len_list"].isArray()) {
    LOG(ERROR) << "parse index_len_list json error!";
    return false;
  }

  Json::Value index_len_list = meta["index_len_list"];
  _index_file_num = index_len_list.size();

  if (_index_file_num == 0) {
    LOG(ERROR) << "index len list size is 0!";
    return false;
  }

  for (uint32_t i = 0; i < _index_file_num; ++i) {
    LOG(INFO) << "index_len:" << index_len_list[i].asString().c_str();

    try {
      _index_file_list.push_back(
          boost::lexical_cast<uint64_t>(index_len_list[i].asString()));
    } catch (boost::bad_lexical_cast &e) {
      LOG(ERROR) << "bad lexical cast:" << e.what();
      return false;
    }
  }

  if (meta["index_total_count"].isNull() ||
      !meta["index_total_count"].isString()) {
    LOG(ERROR) << "parse index_total_count json error!";
    return false;
  }

  LOG(INFO) << "index_total_count:"
            << meta["index_total_count"].asString().c_str();
  try {
    _count =
        boost::lexical_cast<uint64_t>(meta["index_total_count"].asString());
  } catch (boost::bad_lexical_cast &e) {
    LOG(ERROR) << "bad lexical cast:" << e.what();
    return false;
  }

  return true;
}

bool CROVLBuilderIncremental::write_cur_meta_to_local() {
  char file[MAX_DATA_DIR_LEN * 2];
  std::string meta_path = _data_dir;
  meta_path = meta_path + "/../../meta_info";
  if (!checkDirectory(meta_path)) {
    LOG(ERROR) << "create meta_path path failed:" << meta_path.c_str();
    return false;
  }
  snprintf(file,
           sizeof(file),
           "%s/%s_%s_%s_%s.json",
           meta_path.c_str(),
           _cur_version.c_str(),
           _depend_version.c_str(),
           _shard_id.c_str(),
           _split_id.c_str());

  Json::Value meta;

  try {
    meta["index_total_count"] = boost::lexical_cast<std::string>(_count);
  } catch (boost::bad_lexical_cast &e) {
    LOG(ERROR) << "bad lexical cast:" << e.what();
    return false;
  }

  Json::Value data;

  for (size_t i = 0; i < _data_file_list.size(); ++i) {
    try {
      data.append(boost::lexical_cast<std::string>(_data_file_list[i]));
    } catch (boost::bad_lexical_cast &e) {
      LOG(ERROR) << "bad lexical cast:" << e.what();
      return false;
    }
  }

  meta["data_len_list"] = data;

  Json::Value index;

  for (size_t i = 0; i < _index_file_list.size(); ++i) {
    try {
      index.append(boost::lexical_cast<std::string>(_index_file_list[i]));
    } catch (boost::bad_lexical_cast &e) {
      LOG(ERROR) << "bad lexical cast:" << e.what();
      return false;
    }
  }

  meta["index_len_list"] = index;

  std::ofstream ofs;
  ofs.open(file);
  if (!ofs) {
    LOG(ERROR) << "open file failed!" << file;
    return false;
  }
  ofs << meta.toStyledString();
  ofs.close();
  return true;
}