xref: /freebsd/contrib/kyua/store/metadata.cpp (revision b0d29bc4)
1*b0d29bc4SBrooks Davis // Copyright 2011 The Kyua Authors.
2*b0d29bc4SBrooks Davis // All rights reserved.
3*b0d29bc4SBrooks Davis //
4*b0d29bc4SBrooks Davis // Redistribution and use in source and binary forms, with or without
5*b0d29bc4SBrooks Davis // modification, are permitted provided that the following conditions are
6*b0d29bc4SBrooks Davis // met:
7*b0d29bc4SBrooks Davis //
8*b0d29bc4SBrooks Davis // * Redistributions of source code must retain the above copyright
9*b0d29bc4SBrooks Davis //   notice, this list of conditions and the following disclaimer.
10*b0d29bc4SBrooks Davis // * Redistributions in binary form must reproduce the above copyright
11*b0d29bc4SBrooks Davis //   notice, this list of conditions and the following disclaimer in the
12*b0d29bc4SBrooks Davis //   documentation and/or other materials provided with the distribution.
13*b0d29bc4SBrooks Davis // * Neither the name of Google Inc. nor the names of its contributors
14*b0d29bc4SBrooks Davis //   may be used to endorse or promote products derived from this software
15*b0d29bc4SBrooks Davis //   without specific prior written permission.
16*b0d29bc4SBrooks Davis //
17*b0d29bc4SBrooks Davis // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*b0d29bc4SBrooks Davis // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*b0d29bc4SBrooks Davis // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*b0d29bc4SBrooks Davis // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*b0d29bc4SBrooks Davis // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*b0d29bc4SBrooks Davis // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*b0d29bc4SBrooks Davis // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*b0d29bc4SBrooks Davis // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*b0d29bc4SBrooks Davis // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*b0d29bc4SBrooks Davis // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*b0d29bc4SBrooks Davis // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*b0d29bc4SBrooks Davis 
29*b0d29bc4SBrooks Davis #include "store/metadata.hpp"
30*b0d29bc4SBrooks Davis 
31*b0d29bc4SBrooks Davis #include "store/exceptions.hpp"
32*b0d29bc4SBrooks Davis #include "utils/format/macros.hpp"
33*b0d29bc4SBrooks Davis #include "utils/sanity.hpp"
34*b0d29bc4SBrooks Davis #include "utils/sqlite/database.hpp"
35*b0d29bc4SBrooks Davis #include "utils/sqlite/exceptions.hpp"
36*b0d29bc4SBrooks Davis #include "utils/sqlite/statement.ipp"
37*b0d29bc4SBrooks Davis 
38*b0d29bc4SBrooks Davis namespace sqlite = utils::sqlite;
39*b0d29bc4SBrooks Davis 
40*b0d29bc4SBrooks Davis 
41*b0d29bc4SBrooks Davis namespace {
42*b0d29bc4SBrooks Davis 
43*b0d29bc4SBrooks Davis 
44*b0d29bc4SBrooks Davis /// Fetches an integer column from a statement of the 'metadata' table.
45*b0d29bc4SBrooks Davis ///
46*b0d29bc4SBrooks Davis /// \param stmt The statement from which to get the column value.
47*b0d29bc4SBrooks Davis /// \param column The name of the column to retrieve.
48*b0d29bc4SBrooks Davis ///
49*b0d29bc4SBrooks Davis /// \return The value of the column.
50*b0d29bc4SBrooks Davis ///
51*b0d29bc4SBrooks Davis /// \throw store::integrity_error If there is a problem fetching the value
52*b0d29bc4SBrooks Davis ///     caused by an invalid schema or data.
53*b0d29bc4SBrooks Davis static int64_t
int64_column(sqlite::statement & stmt,const char * column)54*b0d29bc4SBrooks Davis int64_column(sqlite::statement& stmt, const char* column)
55*b0d29bc4SBrooks Davis {
56*b0d29bc4SBrooks Davis     int index;
57*b0d29bc4SBrooks Davis     try {
58*b0d29bc4SBrooks Davis         index = stmt.column_id(column);
59*b0d29bc4SBrooks Davis     } catch (const sqlite::invalid_column_error& e) {
60*b0d29bc4SBrooks Davis         UNREACHABLE_MSG("Invalid column specification; the SELECT statement "
61*b0d29bc4SBrooks Davis                         "should have caught this");
62*b0d29bc4SBrooks Davis     }
63*b0d29bc4SBrooks Davis     if (stmt.column_type(index) != sqlite::type_integer)
64*b0d29bc4SBrooks Davis         throw store::integrity_error(F("The '%s' column in 'metadata' table "
65*b0d29bc4SBrooks Davis                                        "has an invalid type") % column);
66*b0d29bc4SBrooks Davis     return stmt.column_int64(index);
67*b0d29bc4SBrooks Davis }
68*b0d29bc4SBrooks Davis 
69*b0d29bc4SBrooks Davis 
70*b0d29bc4SBrooks Davis }  // anonymous namespace
71*b0d29bc4SBrooks Davis 
72*b0d29bc4SBrooks Davis 
73*b0d29bc4SBrooks Davis /// Constructs a new metadata object.
74*b0d29bc4SBrooks Davis ///
75*b0d29bc4SBrooks Davis /// \param schema_version_ The schema version.
76*b0d29bc4SBrooks Davis /// \param timestamp_ The time at which this version was created.
metadata(const int schema_version_,const int64_t timestamp_)77*b0d29bc4SBrooks Davis store::metadata::metadata(const int schema_version_, const int64_t timestamp_) :
78*b0d29bc4SBrooks Davis     _schema_version(schema_version_),
79*b0d29bc4SBrooks Davis     _timestamp(timestamp_)
80*b0d29bc4SBrooks Davis {
81*b0d29bc4SBrooks Davis }
82*b0d29bc4SBrooks Davis 
83*b0d29bc4SBrooks Davis 
84*b0d29bc4SBrooks Davis /// Returns the timestamp of this entry.
85*b0d29bc4SBrooks Davis ///
86*b0d29bc4SBrooks Davis /// \return The timestamp in this metadata entry.
87*b0d29bc4SBrooks Davis int64_t
timestamp(void) const88*b0d29bc4SBrooks Davis store::metadata::timestamp(void) const
89*b0d29bc4SBrooks Davis {
90*b0d29bc4SBrooks Davis     return _timestamp;
91*b0d29bc4SBrooks Davis }
92*b0d29bc4SBrooks Davis 
93*b0d29bc4SBrooks Davis 
94*b0d29bc4SBrooks Davis /// Returns the schema version.
95*b0d29bc4SBrooks Davis ///
96*b0d29bc4SBrooks Davis /// \return The schema version in this metadata entry.
97*b0d29bc4SBrooks Davis int
schema_version(void) const98*b0d29bc4SBrooks Davis store::metadata::schema_version(void) const
99*b0d29bc4SBrooks Davis {
100*b0d29bc4SBrooks Davis     return _schema_version;
101*b0d29bc4SBrooks Davis }
102*b0d29bc4SBrooks Davis 
103*b0d29bc4SBrooks Davis 
104*b0d29bc4SBrooks Davis /// Reads the latest metadata entry from the database.
105*b0d29bc4SBrooks Davis ///
106*b0d29bc4SBrooks Davis /// \param db The database from which to read the metadata from.
107*b0d29bc4SBrooks Davis ///
108*b0d29bc4SBrooks Davis /// \return The current metadata of the database.  It is not OK for the metadata
109*b0d29bc4SBrooks Davis /// table to be empty, so this is guaranteed to return a value unless there is
110*b0d29bc4SBrooks Davis /// an error.
111*b0d29bc4SBrooks Davis ///
112*b0d29bc4SBrooks Davis /// \throw store::integrity_error If the metadata in the database is empty,
113*b0d29bc4SBrooks Davis ///     has an invalid schema or its contents are bogus.
114*b0d29bc4SBrooks Davis store::metadata
fetch_latest(sqlite::database & db)115*b0d29bc4SBrooks Davis store::metadata::fetch_latest(sqlite::database& db)
116*b0d29bc4SBrooks Davis {
117*b0d29bc4SBrooks Davis     try {
118*b0d29bc4SBrooks Davis         sqlite::statement stmt = db.create_statement(
119*b0d29bc4SBrooks Davis             "SELECT schema_version, timestamp FROM metadata "
120*b0d29bc4SBrooks Davis             "ORDER BY schema_version DESC LIMIT 1");
121*b0d29bc4SBrooks Davis         if (!stmt.step())
122*b0d29bc4SBrooks Davis             throw store::integrity_error("The 'metadata' table is empty");
123*b0d29bc4SBrooks Davis 
124*b0d29bc4SBrooks Davis         const int schema_version_ =
125*b0d29bc4SBrooks Davis             static_cast< int >(int64_column(stmt, "schema_version"));
126*b0d29bc4SBrooks Davis         const int64_t timestamp_ = int64_column(stmt, "timestamp");
127*b0d29bc4SBrooks Davis 
128*b0d29bc4SBrooks Davis         if (stmt.step())
129*b0d29bc4SBrooks Davis             UNREACHABLE_MSG("Got more than one result from a query that "
130*b0d29bc4SBrooks Davis                             "does not permit this; any pragmas defined?");
131*b0d29bc4SBrooks Davis 
132*b0d29bc4SBrooks Davis         return metadata(schema_version_, timestamp_);
133*b0d29bc4SBrooks Davis     } catch (const sqlite::error& e) {
134*b0d29bc4SBrooks Davis         throw store::integrity_error(F("Invalid metadata schema: %s") %
135*b0d29bc4SBrooks Davis                                      e.what());
136*b0d29bc4SBrooks Davis     }
137*b0d29bc4SBrooks Davis }
138