/
store.cpp
138 lines (115 loc) · 3.88 KB
/
store.cpp
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
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
/**
* Copyright (c) 2011-2018 libbitcoin developers (see AUTHORS)
*
* This file is part of libbitcoin.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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 <bitcoin/database/store.hpp>
#include <cstddef>
#include <memory>
#include <boost/filesystem.hpp>
#include <bitcoin/bitcoin.hpp>
namespace libbitcoin {
namespace database {
using namespace bc::chain;
using namespace bc::database;
using namespace boost::filesystem;
using namespace boost::system;
// Database file names.
const std::string store::FLUSH_LOCK = "flush_lock";
const std::string store::EXCLUSIVE_LOCK = "exclusive_lock";
const std::string store::BLOCK_TABLE = "block_table";
const std::string store::CANDIDATE_INDEX = "candidate_index";
const std::string store::CONFIRMED_INDEX = "confirmed_index";
const std::string store::TRANSACTION_INDEX = "transaction_index";
const std::string store::TRANSACTION_TABLE = "transaction_table";
const std::string store::ADDRESS_TABLE = "address_table";
const std::string store::ADDRESS_ROWS = "address_rows";
// Create a single file with one byte of arbitrary data.
static bool create_file(const path& file_path)
{
// Disallow create with existing file.
if (bc::ifstream(file_path.string()).good())
return false;
bc::ofstream file(file_path.string());
if (!file.good())
return false;
// Write one byte so file is nonzero size (for memory map validation).
file.put('x');
return true;
}
// Construct.
// ------------------------------------------------------------------------
store::store(const path& prefix, bool with_indexes, bool flush_each_write)
: prefix_(prefix),
with_indexes_(with_indexes),
flush_each_write_(flush_each_write),
flush_lock_(prefix / FLUSH_LOCK),
exclusive_lock_(prefix / EXCLUSIVE_LOCK),
// Content store.
block_table(prefix / BLOCK_TABLE),
candidate_index(prefix / CANDIDATE_INDEX),
confirmed_index(prefix / CONFIRMED_INDEX),
transaction_index(prefix / TRANSACTION_INDEX),
transaction_table(prefix / TRANSACTION_TABLE),
// Optional indexes.
address_table(prefix / ADDRESS_TABLE),
address_rows(prefix / ADDRESS_ROWS)
{
}
// Open and close.
// ------------------------------------------------------------------------
// Create files.
bool store::create()
{
error_code ec;
create_directories(prefix_, ec);
const auto created = !ec &&
create_file(block_table) &&
create_file(candidate_index) &&
create_file(confirmed_index) &&
create_file(transaction_index) &&
create_file(transaction_table);
if (!with_indexes_)
return created;
return
created &&
create_file(address_table) &&
create_file(address_rows);
}
bool store::open()
{
return exclusive_lock_.lock() && flush_lock_.try_lock() &&
(flush_each_write() || flush_lock_.lock_shared());
}
bool store::close()
{
return (flush_each_write() || flush_lock_.unlock_shared()) &&
exclusive_lock_.unlock();
}
bool store::begin_write() const
{
return !flush_each_write() || flush_lock_.lock_shared();
}
bool store::end_write() const
{
return !flush_each_write() || (flush() && flush_lock_.unlock_shared());
}
bool store::flush_each_write() const
{
return flush_each_write_;
}
} // namespace database
} // namespace libbitcoin