1 // Copyright 2011 The Kyua Authors.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 #include "store/write_backend.hpp"
30
31 #include <stdexcept>
32
33 #include "store/exceptions.hpp"
34 #include "store/metadata.hpp"
35 #include "store/read_backend.hpp"
36 #include "store/write_transaction.hpp"
37 #include "utils/env.hpp"
38 #include "utils/format/macros.hpp"
39 #include "utils/fs/path.hpp"
40 #include "utils/logging/macros.hpp"
41 #include "utils/noncopyable.hpp"
42 #include "utils/sanity.hpp"
43 #include "utils/stream.hpp"
44 #include "utils/sqlite/database.hpp"
45 #include "utils/sqlite/exceptions.hpp"
46 #include "utils/sqlite/statement.ipp"
47
48 namespace fs = utils::fs;
49 namespace sqlite = utils::sqlite;
50
51
52 /// The current schema version.
53 ///
54 /// Any new database gets this schema version. Existing databases with an older
55 /// schema version must be first migrated to the current schema with
56 /// migrate_schema() before they can be used.
57 ///
58 /// This must be kept in sync with the value in the corresponding schema_vX.sql
59 /// file, where X matches this version number.
60 ///
61 /// This variable is not const to allow tests to modify it. No other code
62 /// should change its value.
63 int store::detail::current_schema_version = 3;
64
65
66 namespace {
67
68
69 /// Checks if a database is empty (i.e. if it is new).
70 ///
71 /// \param db The database to check.
72 ///
73 /// \return True if the database is empty.
74 static bool
empty_database(sqlite::database & db)75 empty_database(sqlite::database& db)
76 {
77 sqlite::statement stmt = db.create_statement("SELECT * FROM sqlite_master");
78 return !stmt.step();
79 }
80
81
82 } // anonymous namespace
83
84
85 /// Calculates the path to the schema file for the database.
86 ///
87 /// \return The path to the installed schema_vX.sql file that matches the
88 /// current_schema_version.
89 fs::path
schema_file(void)90 store::detail::schema_file(void)
91 {
92 return fs::path(utils::getenv_with_default("KYUA_STOREDIR", KYUA_STOREDIR))
93 / (F("schema_v%s.sql") % current_schema_version);
94 }
95
96
97 /// Initializes an empty database.
98 ///
99 /// \param db The database to initialize.
100 ///
101 /// \return The metadata record written into the new database.
102 ///
103 /// \throw store::error If there is a problem initializing the database.
104 store::metadata
initialize(sqlite::database & db)105 store::detail::initialize(sqlite::database& db)
106 {
107 PRE(empty_database(db));
108
109 const fs::path schema = schema_file();
110
111 LI(F("Populating new database with schema from %s") % schema);
112 try {
113 db.exec(utils::read_file(schema));
114
115 const metadata metadata = metadata::fetch_latest(db);
116 LI(F("New metadata entry %s") % metadata.timestamp());
117 if (metadata.schema_version() != detail::current_schema_version) {
118 UNREACHABLE_MSG(F("current_schema_version is out of sync with "
119 "%s") % schema);
120 }
121 return metadata;
122 } catch (const store::integrity_error& e) {
123 // Could be raised by metadata::fetch_latest.
124 UNREACHABLE_MSG("Inconsistent code while creating a database");
125 } catch (const sqlite::error& e) {
126 throw error(F("Failed to initialize database: %s") % e.what());
127 } catch (const std::runtime_error& e) {
128 throw error(F("Cannot read database schema '%s'") % schema);
129 }
130 }
131
132
133 /// Internal implementation for the backend.
134 struct store::write_backend::impl : utils::noncopyable {
135 /// The SQLite database this backend talks to.
136 sqlite::database database;
137
138 /// Constructor.
139 ///
140 /// \param database_ The SQLite database instance.
implstore::write_backend::impl141 impl(sqlite::database& database_) : database(database_)
142 {
143 }
144 };
145
146
147 /// Constructs a new backend.
148 ///
149 /// \param pimpl_ The internal data.
write_backend(impl * pimpl_)150 store::write_backend::write_backend(impl* pimpl_) :
151 _pimpl(pimpl_)
152 {
153 }
154
155
156 /// Destructor.
~write_backend(void)157 store::write_backend::~write_backend(void)
158 {
159 }
160
161
162 /// Opens a database in read-write mode and creates it if necessary.
163 ///
164 /// \param file The database file to be opened.
165 ///
166 /// \return The backend representation.
167 ///
168 /// \throw store::error If there is any problem opening or creating
169 /// the database.
170 store::write_backend
open_rw(const fs::path & file)171 store::write_backend::open_rw(const fs::path& file)
172 {
173 sqlite::database db = detail::open_and_setup(
174 file, sqlite::open_readwrite | sqlite::open_create);
175 if (!empty_database(db))
176 throw error(F("%s already exists and is not empty; cannot open "
177 "for write") % file);
178 detail::initialize(db);
179 return write_backend(new impl(db));
180 }
181
182
183 /// Closes the SQLite database.
184 void
close(void)185 store::write_backend::close(void)
186 {
187 _pimpl->database.close();
188 }
189
190
191 /// Gets the connection to the SQLite database.
192 ///
193 /// \return A database connection.
194 sqlite::database&
database(void)195 store::write_backend::database(void)
196 {
197 return _pimpl->database;
198 }
199
200
201 /// Opens a write-only transaction.
202 ///
203 /// \return A new transaction.
204 store::write_transaction
start_write(void)205 store::write_backend::start_write(void)
206 {
207 return write_transaction(*this);
208 }
209