CernVM-FS  2.12.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
reflog_sql.cc
Go to the documentation of this file.
1 
5 #include "reflog_sql.h"
6 
7 #include <cassert>
8 #include <limits>
9 
10 #include "util/string.h"
11 
12 const float ReflogDatabase::kLatestSchema = 1.0;
14 const unsigned ReflogDatabase::kLatestSchemaRevision = 0;
15 
24 const std::string ReflogDatabase::kFqrnKey = "fqrn";
25 
27  return sqlite::Sql(sqlite_db(),
28  "CREATE TABLE refs (hash TEXT, type INTEGER, "
29  "timestamp INTEGER, "
30  "CONSTRAINT pk_refs PRIMARY KEY (hash));").Execute();
31 }
32 
33 
36  return true; // only one schema version at the moment
37 }
38 
39 
42  return true; // only one schema revision at the moment, i.e. no migration...
43 }
44 
45 
46 bool ReflogDatabase::InsertInitialValues(const std::string &repo_name) {
47  assert(read_write());
48  return this->SetProperty(kFqrnKey, repo_name);
49 }
50 
51 
52 //------------------------------------------------------------------------------
53 
54 #define DB_FIELDS_V1R0 "hash, type, timestamp"
55 #define DB_PLACEHOLDERS ":hash, :type, :timestamp"
56 
57 #define MAKE_STATEMENT(STMT_TMPL, REV) \
58 static const std::string REV = \
59  ReplaceAll( \
60  ReplaceAll(STMT_TMPL, \
61  "@DB_FIELDS@", DB_FIELDS_ ## REV), \
62  "@DB_PLACEHOLDERS@", DB_PLACEHOLDERS)
63 
64 #define MAKE_STATEMENTS(STMT_TMPL) \
65  MAKE_STATEMENT(STMT_TMPL, V1R0)
66 
67 #define DEFERRED_INIT(DB, REV) \
68  DeferredInit((DB)->sqlite_db(), (REV).c_str())
69 
70 #define DEFERRED_INITS(DB) \
71  DEFERRED_INIT((DB), V1R0)
72 
73 
75  switch (type) {
76  case kRefCatalog:
77  return shash::kSuffixCatalog;
78  case kRefCertificate:
80  case kRefHistory:
81  return shash::kSuffixHistory;
82  case kRefMetainfo:
84  default:
85  assert(false && "unknown reference type");
86  }
87 }
88 
89 
90 //------------------------------------------------------------------------------
91 
92 
94  MAKE_STATEMENTS("INSERT OR REPLACE INTO refs (@DB_FIELDS@) "
95  "VALUES (@DB_PLACEHOLDERS@);");
96  DEFERRED_INITS(database);
97 }
98 
99 bool SqlInsertReference::BindReference(const shash::Any &reference_hash,
100  const ReferenceType type) {
101  return
102  BindTextTransient(1, reference_hash.ToString()) &&
103  BindInt64(2, static_cast<uint64_t>(type)) &&
104  BindInt64(3, static_cast<uint64_t>(time(NULL)));
105 }
106 
107 
108 //------------------------------------------------------------------------------
109 
110 
112  DeferredInit(database->sqlite_db(), "SELECT count(*) as count FROM refs;");
113 }
114 
116  return static_cast<uint64_t>(RetrieveInt64(0));
117 }
118 
119 
120 //------------------------------------------------------------------------------
121 
122 
124  DeferredInit(database->sqlite_db(), "SELECT hash, type FROM refs "
125  "WHERE type = :type AND "
126  "timestamp < :timestamp "
127  "ORDER BY timestamp DESC;");
128 }
129 
131  return BindInt64(1, static_cast<uint64_t>(type));
132 }
133 
134 bool SqlListReferences::BindOlderThan(const uint64_t timestamp) {
135  int64_t sqlite_timestamp = static_cast<uint64_t>(timestamp);
136  if (sqlite_timestamp < 0) {
137  sqlite_timestamp = std::numeric_limits<int64_t>::max();
138  }
139  return BindInt64(2, sqlite_timestamp);
140 }
141 
143  const ReferenceType type = static_cast<ReferenceType>(RetrieveInt64(1));
144  const shash::Suffix suffix = ToSuffix(type);
146 }
147 
148 
149 //------------------------------------------------------------------------------
150 
151 
153  DeferredInit(database->sqlite_db(), "DELETE FROM refs WHERE hash = :hash "
154  "AND type = :type;");
155 }
156 
158  const ReferenceType type) {
159  return
160  BindTextTransient(1, reference_hash.ToString()) &&
161  BindInt64(2, static_cast<uint64_t>(type));
162 }
163 
164 
165 //------------------------------------------------------------------------------
166 
167 
169  DeferredInit(database->sqlite_db(), "SELECT count(*) as answer FROM refs "
170  "WHERE type = :type "
171  " AND hash = :hash");
172 }
173 
175  const ReferenceType type) {
176  return
177  BindInt64(1, static_cast<uint64_t>(type)) &&
178  BindTextTransient(2, reference_hash.ToString());
179 }
180 
182  const int64_t count = RetrieveInt64(0);
183  assert(count == 0 || count == 1);
184  return count > 0;
185 }
186 
187 
188 //------------------------------------------------------------------------------
189 
190 
192  DeferredInit(database->sqlite_db(), "SELECT timestamp FROM refs "
193  "WHERE type = :type "
194  " AND hash = :hash");
195 }
196 
197 bool SqlGetTimestamp::BindReference(const shash::Any &reference_hash,
198  const ReferenceType type) {
199  return
200  BindInt64(1, static_cast<uint64_t>(type)) &&
201  BindTextTransient(2, reference_hash.ToString());
202 }
203 
205  return RetrieveInt64(0);
206 }
bool CheckSchemaCompatibility()
Definition: reflog_sql.cc:34
bool BindOlderThan(const uint64_t timestamp)
Definition: reflog_sql.cc:134
bool BindTextTransient(const int index, const std::string &value)
Definition: sql.h:391
bool Execute()
Definition: sql.cc:42
uint64_t RetrieveTimestamp()
Definition: reflog_sql.cc:204
static const std::string kFqrnKey
Definition: reflog_sql.h:20
shash::Any RetrieveHash() const
Definition: reflog_sql.cc:142
SqlListReferences(const ReflogDatabase *database)
Definition: reflog_sql.cc:123
SqlGetTimestamp(const ReflogDatabase *database)
Definition: reflog_sql.cc:191
const char kSuffixCertificate
Definition: hash.h:59
std::string ToString(const bool with_suffix=false) const
Definition: hash.h:249
bool BindType(const ReferenceType type)
Definition: reflog_sql.cc:130
bool BindReference(const shash::Any &reference_hash, const ReferenceType type)
Definition: reflog_sql.cc:99
assert((mem||(size==0))&&"Out Of Memory")
float schema_version() const
Definition: sql.h:149
#define MAKE_STATEMENTS(STMT_TMPL)
Definition: reflog_sql.cc:64
bool BindReference(const shash::Any &reference_hash, const ReferenceType type)
Definition: reflog_sql.cc:174
uint64_t RetrieveCount()
Definition: reflog_sql.cc:115
unsigned schema_revision() const
Definition: sql.h:150
bool LiveSchemaUpgradeIfNecessary()
Definition: reflog_sql.cc:40
static const float kLatestSupportedSchema
Definition: reflog_sql.h:16
void DeferredInit(const sqlite3 *database, const char *statement)
Definition: sql.cc:158
static const float kLatestSchema
Definition: reflog_sql.h:15
SqlContainsReference(const ReflogDatabase *database)
Definition: reflog_sql.cc:168
bool BindReference(const shash::Any &reference_hash, const ReferenceType type)
Definition: reflog_sql.cc:157
const char kSuffixCatalog
Definition: hash.h:54
SqlCountReferences(const ReflogDatabase *database)
Definition: reflog_sql.cc:111
sqlite3_int64 RetrieveInt64(const int idx_column) const
Definition: sql.h:445
const char kSuffixMetainfo
Definition: hash.h:60
sqlite3 * sqlite_db() const
Definition: sql.h:147
bool BindInt64(const int index, const sqlite3_int64 value)
Definition: sql.h:381
const char kSuffixHistory
Definition: hash.h:55
bool SetProperty(const std::string &key, const T value)
SqlRemoveReference(const ReflogDatabase *database)
Definition: reflog_sql.cc:152
char Suffix
Definition: hash.h:114
bool InsertInitialValues(const std::string &repo_name)
Definition: reflog_sql.cc:46
static const unsigned kLatestSchemaRevision
Definition: reflog_sql.h:18
bool IsEqualSchema(const float value, const float compare) const
Definition: sql.h:131
#define DEFERRED_INITS(DB)
Definition: reflog_sql.cc:70
std::string RetrieveString(const int idx_column) const
Definition: sql.h:451
bool CreateEmptyDatabase()
Definition: reflog_sql.cc:26
Any MkFromHexPtr(const HexPtr hex, const char suffix)
Definition: hash.cc:83
bool BindReference(const shash::Any &reference_hash, const ReferenceType type)
Definition: reflog_sql.cc:197
SqlInsertReference(const ReflogDatabase *database)
Definition: reflog_sql.cc:93
static shash::Suffix ToSuffix(const ReferenceType type)
Definition: reflog_sql.cc:74