<commit_before>/**
 * Copyright (c) 2011-2015 libbitcoin developers (see AUTHORS)
 * Copyright (c) 2016-2017 metaverse core developers (see MVS-AUTHORS)
 *
 * This file is part of metaverse.
 *
 * metaverse is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License with
 * additional permissions to the one published by the Free Software
 * Foundation, either version 3 of the License, or (at your option)
 * any later version. For more information see LICENSE.
 *
 * 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.  See the
 * GNU Affero General Public License for more details.
 *
 * 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 <metaverse/database/data_base.hpp>

#include <cstdint>
#include <cstddef>
#include <memory>
#include <stdexcept>
#include <boost/filesystem.hpp>
#include <metaverse/bitcoin.hpp>
#include <metaverse/database/memory/memory_map.hpp>
#include <metaverse/database/settings.hpp>
#include <metaverse/bitcoin/utility/path.hpp>

#include <algorithm> 
#include <metaverse/bitcoin/config/base16.hpp>  // used by db_metadata and push_attachment
#include <metaverse/database/version.hpp>
namespace libbitcoin {
namespace database {

using namespace boost::filesystem;
using namespace bc::chain;
using namespace bc::wallet;
using namespace libbitcoin::config;

// BIP30 exception blocks.
// github.com/bitcoin/bips/blob/master/bip-0030.mediawiki#specification
static const config::checkpoint exception1 =
{ "00000000000a4d0a398161ffc163c503763b1f4360639393e0e4c8e300e0caec", 91842 };
static const config::checkpoint exception2 =
{ "00000000000743f190a18c5577a3c2d2a1f610ae9601ac046a38084ccb7cd721", 91880 };

bool data_base::touch_file(const path& file_path)
{
    bc::ofstream file(file_path.string());
    if (file.bad())
        return false;

    // Write one byte so file is nonzero size.
    file.write("X", 1);
    return true;
}

bool data_base::initialize(const path& prefix, const chain::block& genesis)
{
    // Create paths.
    const store paths(prefix);

    if (!paths.touch_all())
        return false;

    data_base instance(paths, 0, 0);

    if (!instance.create())
        return false;
	auto metadata_path = prefix / db_metadata::file_name;
	auto metadata = db_metadata(db_metadata::current_version);
	data_base::write_metadata(metadata_path, metadata);
    instance.push(genesis);
    return instance.stop();
}
bool data_base::is_lower_database(const path& prefix)
{
	auto metadata_path = prefix / db_metadata::file_name;
	auto metadata = db_metadata();
	data_base::read_metadata(metadata_path, metadata);
	return metadata.version_ < db_metadata::current_version;
}
bool data_base::upgrade_database(const settings& settings, const chain::block& genesis)
{
	log::info("database") << "upgrading blockchain database......";
    // Create paths.
    auto prefix = default_data_path() / settings.directory;
    const blockchain_store paths(prefix);

    if (!paths.touch_all())
        return false;
	
	data_base instance(settings);

    if (!instance.blockchain_create())
        return false;
	if (!instance.account_db_start())
		return false;
	// metadata
	auto metadata_path = default_data_path() / settings.directory / db_metadata::file_name;
	//auto metadata = db_metadata();
	//data_base::read_metadata(metadata_path, metadata); // maybe do recover according db version
	auto metadata = db_metadata(db_metadata::current_version);<commit_msg>#36 only update prompt message when database upgrading<commit_after>/**
 * Copyright (c) 2011-2015 libbitcoin developers (see AUTHORS)
 * Copyright (c) 2016-2017 metaverse core developers (see MVS-AUTHORS)
 *
 * This file is part of metaverse.
 *
 * metaverse is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License with
 * additional permissions to the one published by the Free Software
 * Foundation, either version 3 of the License, or (at your option)
 * any later version. For more information see LICENSE.
 *
 * 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.  See the
 * GNU Affero General Public License for more details.
 *
 * 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 <metaverse/database/data_base.hpp>

#include <cstdint>
#include <cstddef>
#include <memory>
#include <stdexcept>
#include <boost/filesystem.hpp>
#include <metaverse/bitcoin.hpp>
#include <metaverse/database/memory/memory_map.hpp>
#include <metaverse/database/settings.hpp>
#include <metaverse/bitcoin/utility/path.hpp>

#include <algorithm> 
#include <metaverse/bitcoin/config/base16.hpp>  // used by db_metadata and push_attachment
#include <metaverse/database/version.hpp>
namespace libbitcoin {
namespace database {

using namespace boost::filesystem;
using namespace bc::chain;
using namespace bc::wallet;
using namespace libbitcoin::config;

// BIP30 exception blocks.
// github.com/bitcoin/bips/blob/master/bip-0030.mediawiki#specification
static const config::checkpoint exception1 =
{ "00000000000a4d0a398161ffc163c503763b1f4360639393e0e4c8e300e0caec", 91842 };
static const config::checkpoint exception2 =
{ "00000000000743f190a18c5577a3c2d2a1f610ae9601ac046a38084ccb7cd721", 91880 };

bool data_base::touch_file(const path& file_path)
{
    bc::ofstream file(file_path.string());
    if (file.bad())
        return false;

    // Write one byte so file is nonzero size.
    file.write("X", 1);
    return true;
}

bool data_base::initialize(const path& prefix, const chain::block& genesis)
{
    // Create paths.
    const store paths(prefix);

    if (!paths.touch_all())
        return false;

    data_base instance(paths, 0, 0);

    if (!instance.create())
        return false;
	auto metadata_path = prefix / db_metadata::file_name;
	auto metadata = db_metadata(db_metadata::current_version);
	data_base::write_metadata(metadata_path, metadata);
    instance.push(genesis);
    return instance.stop();
}
bool data_base::is_lower_database(const path& prefix)
{
	auto metadata_path = prefix / db_metadata::file_name;
	auto metadata = db_metadata();
	data_base::read_metadata(metadata_path, metadata);
	return metadata.version_ < db_metadata::current_version;
}
bool data_base::upgrade_database(const settings& settings, const chain::block& genesis)
{
	log::info("database") << "The local database is being upgraded, it may take a while to re-synchronize the block data, please wait...";
    // Create paths.
    auto prefix = default_data_path() / settings.directory;
    const blockchain_store paths(prefix);

    if (!paths.touch_all())
        return false;
	
	data_base instance(settings);

    if (!instance.blockchain_create())
        return false;
	if (!instance.account_db_start())
		return false;
	// metadata
	auto metadata_path = default_data_path() / settings.directory / db_metadata::file_name;
	//auto metadata = db_metadata();
	//data_base::read_metadata(metadata_path, metadata); // maybe do recover according db version
	auto metadata = db_metadata(db_metadata::current_version);
{'fraction_non_alphanumeric': 0.10410557184750734, 'fraction_numerical': 0.03545721141029059, 'mean_word_length': 4.758250191864927, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3078806', 'n_tokens_mistral': 2408, 'n_tokens_neox': 2211, 'n_words': 669}