CernVM-FS  2.13.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));")
31  .Execute();
32 }
33 
34 
37  return true; // only one schema version at the moment
38 }
39 
40 
43  return true; // only one schema revision at the moment, i.e. no migration...
44 }
45 
46 
47 bool ReflogDatabase::InsertInitialValues(const std::string &repo_name) {
48  assert(read_write());
49  return this->SetProperty(kFqrnKey, repo_name);
50 }
51 
52 
53 //------------------------------------------------------------------------------
54 
55 #define DB_FIELDS_V1R0 "hash, type, timestamp"
56 #define DB_PLACEHOLDERS ":hash, :type, :timestamp"
57 
58 #define MAKE_STATEMENT(STMT_TMPL, REV) \
59  static const std::string REV = ReplaceAll( \
60  ReplaceAll(STMT_TMPL, "@DB_FIELDS@", DB_FIELDS_##REV), \
61  "@DB_PLACEHOLDERS@", DB_PLACEHOLDERS)
62 
63 #define MAKE_STATEMENTS(STMT_TMPL) MAKE_STATEMENT(STMT_TMPL, V1R0)
64 
65 #define DEFERRED_INIT(DB, REV) DeferredInit((DB)->sqlite_db(), (REV).c_str())
66 
67 #define DEFERRED_INITS(DB) DEFERRED_INIT((DB), V1R0)
68 
69 
71  switch (type) {
72  case kRefCatalog:
73  return shash::kSuffixCatalog;
74  case kRefCertificate:
76  case kRefHistory:
77  return shash::kSuffixHistory;
78  case kRefMetainfo:
80  default:
81  assert(false && "unknown reference type");
82  }
83 }
84 
85 
86 //------------------------------------------------------------------------------
87 
88 
90  MAKE_STATEMENTS("INSERT OR REPLACE INTO refs (@DB_FIELDS@) "
91  "VALUES (@DB_PLACEHOLDERS@);");
92  DEFERRED_INITS(database);
93 }
94 
95 bool SqlInsertReference::BindReference(const shash::Any &reference_hash,
96  const ReferenceType type) {
97  return BindTextTransient(1, reference_hash.ToString())
98  && BindInt64(2, static_cast<uint64_t>(type))
99  && BindInt64(3, static_cast<uint64_t>(time(NULL)));
100 }
101 
102 
103 //------------------------------------------------------------------------------
104 
105 
107  DeferredInit(database->sqlite_db(), "SELECT count(*) as count FROM refs;");
108 }
109 
111  return static_cast<uint64_t>(RetrieveInt64(0));
112 }
113 
114 
115 //------------------------------------------------------------------------------
116 
117 
119  DeferredInit(database->sqlite_db(), "SELECT hash, type FROM refs "
120  "WHERE type = :type AND "
121  "timestamp < :timestamp "
122  "ORDER BY timestamp DESC;");
123 }
124 
126  return BindInt64(1, static_cast<uint64_t>(type));
127 }
128 
129 bool SqlListReferences::BindOlderThan(const uint64_t timestamp) {
130  int64_t sqlite_timestamp = static_cast<uint64_t>(timestamp);
131  if (sqlite_timestamp < 0) {
132  sqlite_timestamp = std::numeric_limits<int64_t>::max();
133  }
134  return BindInt64(2, sqlite_timestamp);
135 }
136 
138  const ReferenceType type = static_cast<ReferenceType>(RetrieveInt64(1));
139  const shash::Suffix suffix = ToSuffix(type);
141 }
142 
143 
144 //------------------------------------------------------------------------------
145 
146 
148  DeferredInit(database->sqlite_db(), "DELETE FROM refs WHERE hash = :hash "
149  "AND type = :type;");
150 }
151 
153  const ReferenceType type) {
154  return BindTextTransient(1, reference_hash.ToString())
155  && BindInt64(2, static_cast<uint64_t>(type));
156 }
157 
158 
159 //------------------------------------------------------------------------------
160 
161 
163  DeferredInit(database->sqlite_db(), "SELECT count(*) as answer FROM refs "
164  "WHERE type = :type "
165  " AND hash = :hash");
166 }
167 
169  const ReferenceType type) {
170  return BindInt64(1, static_cast<uint64_t>(type))
171  && BindTextTransient(2, reference_hash.ToString());
172 }
173 
175  const int64_t count = RetrieveInt64(0);
176  assert(count == 0 || count == 1);
177  return count > 0;
178 }
179 
180 
181 //------------------------------------------------------------------------------
182 
183 
185  DeferredInit(database->sqlite_db(), "SELECT timestamp FROM refs "
186  "WHERE type = :type "
187  " AND hash = :hash");
188 }
189 
190 bool SqlGetTimestamp::BindReference(const shash::Any &reference_hash,
191  const ReferenceType type) {
192  return BindInt64(1, static_cast<uint64_t>(type))
193  && BindTextTransient(2, reference_hash.ToString());
194 }
195 
bool CheckSchemaCompatibility()
Definition: reflog_sql.cc:35
bool BindOlderThan(const uint64_t timestamp)
Definition: reflog_sql.cc:129
bool BindTextTransient(const int index, const std::string &value)
Definition: sql.h:386
bool Execute()
Definition: sql.cc:41
uint64_t RetrieveTimestamp()
Definition: reflog_sql.cc:196
static const std::string kFqrnKey
Definition: reflog_sql.h:20
shash::Any RetrieveHash() const
Definition: reflog_sql.cc:137
SqlListReferences(const ReflogDatabase *database)
Definition: reflog_sql.cc:118
SqlGetTimestamp(const ReflogDatabase *database)
Definition: reflog_sql.cc:184
const char kSuffixCertificate
Definition: hash.h:59
std::string ToString(const bool with_suffix=false) const
Definition: hash.h:241
bool BindType(const ReferenceType type)
Definition: reflog_sql.cc:125
bool BindReference(const shash::Any &reference_hash, const ReferenceType type)
Definition: reflog_sql.cc:95
assert((mem||(size==0))&&"Out Of Memory")
float schema_version() const
Definition: sql.h:147
#define MAKE_STATEMENTS(STMT_TMPL)
Definition: reflog_sql.cc:63
bool BindReference(const shash::Any &reference_hash, const ReferenceType type)
Definition: reflog_sql.cc:168
uint64_t RetrieveCount()
Definition: reflog_sql.cc:110
unsigned schema_revision() const
Definition: sql.h:148
bool LiveSchemaUpgradeIfNecessary()
Definition: reflog_sql.cc:41
static const float kLatestSupportedSchema
Definition: reflog_sql.h:16
void DeferredInit(const sqlite3 *database, const char *statement)
Definition: sql.cc:159
static const float kLatestSchema
Definition: reflog_sql.h:15
SqlContainsReference(const ReflogDatabase *database)
Definition: reflog_sql.cc:162
bool BindReference(const shash::Any &reference_hash, const ReferenceType type)
Definition: reflog_sql.cc:152
const char kSuffixCatalog
Definition: hash.h:54
SqlCountReferences(const ReflogDatabase *database)
Definition: reflog_sql.cc:106
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:145
bool BindInt64(const int index, const sqlite3_int64 value)
Definition: sql.h:376
const char kSuffixHistory
Definition: hash.h:55
bool SetProperty(const std::string &key, const T value)
SqlRemoveReference(const ReflogDatabase *database)
Definition: reflog_sql.cc:147
char Suffix
Definition: hash.h:111
bool InsertInitialValues(const std::string &repo_name)
Definition: reflog_sql.cc:47
static const unsigned kLatestSchemaRevision
Definition: reflog_sql.h:18
bool IsEqualSchema(const float value, const float compare) const
Definition: sql.h:129
#define DEFERRED_INITS(DB)
Definition: reflog_sql.cc:67
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:82
bool BindReference(const shash::Any &reference_hash, const ReferenceType type)
Definition: reflog_sql.cc:190
SqlInsertReference(const ReflogDatabase *database)
Definition: reflog_sql.cc:89
static shash::Suffix ToSuffix(const ReferenceType type)
Definition: reflog_sql.cc:70