github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/fs/ir_manager/api/ir_api.cpp (about)

     1  // Copyright (C) 2018 go-nebulas authors
     2  //
     3  // This file is part of the go-nebulas library.
     4  //
     5  // the go-nebulas library is free software: you can redistribute it and/or
     6  // modify
     7  // it under the terms of the GNU General Public License as published by
     8  // the Free Software Foundation, either version 3 of the License, or
     9  // (at your option) any later version.
    10  //
    11  // the go-nebulas library is distributed in the hope that it will be useful,
    12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  // GNU General Public License for more details.
    15  //
    16  // You should have received a copy of the GNU General Public License
    17  // along with the go-nebulas library.  If not, see
    18  // <http://www.gnu.org/licenses/>.
    19  //
    20  
    21  #include "fs/ir_manager/api/ir_api.h"
    22  #include "common/configuration.h"
    23  #include "common/version.h"
    24  #include <boost/foreach.hpp>
    25  #include <boost/property_tree/json_parser.hpp>
    26  
    27  namespace neb {
    28  namespace fs {
    29  
    30  std::unique_ptr<std::vector<std::string>>
    31  ir_api::get_ir_list(rocksdb_storage *rs) {
    32    auto ret = std::make_unique<std::vector<std::string>>();
    33  
    34    std::string ir_list = neb::configuration::instance().ir_list_name();
    35    neb::bytes ir_list_bytes;
    36    try {
    37      ir_list_bytes = rs->get(ir_list);
    38    } catch (const std::exception &e) {
    39      LOG(INFO) << "ir list empty, get ir list failed " << e.what();
    40      return ret;
    41    }
    42  
    43    boost::property_tree::ptree root;
    44    std::stringstream ss(neb::byte_to_string(ir_list_bytes));
    45    boost::property_tree::json_parser::read_json(ss, root);
    46  
    47    BOOST_FOREACH (boost::property_tree::ptree::value_type &name,
    48                   root.get_child(ir_list)) {
    49      boost::property_tree::ptree pt = name.second;
    50      ret->push_back(pt.get<std::string>(std::string()));
    51    }
    52    return ret;
    53  }
    54  
    55  std::unique_ptr<std::vector<version_t>>
    56  ir_api::get_ir_versions(const std::string &name, rocksdb_storage *rs) {
    57    auto ret = std::make_unique<std::vector<version_t>>();
    58  
    59    neb::bytes ir_versions_bytes;
    60    try {
    61      ir_versions_bytes = rs->get(name);
    62    } catch (const std::exception &e) {
    63      LOG(INFO) << "ir with name " << name << " versions empty, get " << name
    64                << " versions failed " << e.what();
    65      return ret;
    66    }
    67  
    68    boost::property_tree::ptree root;
    69    std::stringstream ss(neb::byte_to_string(ir_versions_bytes));
    70    boost::property_tree::json_parser::read_json(ss, root);
    71  
    72    BOOST_FOREACH (boost::property_tree::ptree::value_type &version,
    73                   root.get_child(name)) {
    74      boost::property_tree::ptree pt = version.second;
    75      ret->push_back(pt.get<version_t>(std::string()));
    76    }
    77  
    78    sort(ret->begin(), ret->end(), [](const version_t &v1, const version_t &v2) {
    79      neb::version obj_v1(v1);
    80      neb::version obj_v2(v2);
    81      return obj_v1 > obj_v2;
    82    });
    83    return ret;
    84  }
    85  
    86  ir_api::ir_ret_type ir_api::get_ir(const std::string &name, version_t version,
    87                                     rocksdb_storage *rs) {
    88    ir_ret_type ret_pair;
    89    ret_pair.second = std::make_unique<nbre::NBREIR>();
    90    auto &nbre_ir = ret_pair.second;
    91    bytes nbre_bytes;
    92    try {
    93      std::stringstream ss;
    94      ss << name << version;
    95      nbre_bytes = rs->get(ss.str());
    96    } catch (const std::exception &e) {
    97      LOG(INFO) << "no such ir named " << name << " with version " << version
    98                << ' ' << e.what();
    99      ret_pair.first = false;
   100      return ret_pair;
   101    }
   102  
   103    bool ret = nbre_ir->ParseFromArray(nbre_bytes.value(), nbre_bytes.size());
   104    if (!ret) {
   105      throw std::runtime_error("parse nbre failed");
   106    }
   107    ret_pair.first = true;
   108    return ret_pair;
   109  }
   110  
   111  void ir_api::get_ir_depends(
   112      const std::string &name, version_t version, rocksdb_storage *rs,
   113      std::vector<std::pair<std::string, version_t>> &irs) {
   114  
   115    std::unordered_set<std::string> s;
   116    std::queue<std::pair<std::string, version_t>> q;
   117    q.push(std::make_pair(name, version));
   118    std::stringstream ss;
   119  
   120    while (!q.empty()) {
   121      auto &ele = q.front();
   122      q.pop();
   123      ss.clear();
   124      ss << ele.first << ele.second;
   125      if (s.find(ss.str()) == s.end()) {
   126        auto ret = get_ir(ele.first, ele.second, rs);
   127        if (ret.first) {
   128          for (auto &dep : ret.second->depends()) {
   129            q.push(std::make_pair(dep.name(), dep.version()));
   130          }
   131          irs.push_back(std::make_pair(ele.first, ele.second));
   132        }
   133        s.insert(ss.str());
   134      }
   135    }
   136  }
   137  
   138  bool ir_api::ir_exist(const std::string &name, version_t version,
   139                        rocksdb_storage *rs) {
   140    auto ret = get_ir(name, version, rs);
   141    return ret.first;
   142  }
   143  } // namespace fs
   144  } // namespace neb