GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/history_sqlite.h
Date: 2024-04-28 02:33:07
Exec Total Coverage
Lines: 1 5 20.0%
Branches: 0 4 0.0%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 */
4
5 #ifndef CVMFS_HISTORY_SQLITE_H_
6 #define CVMFS_HISTORY_SQLITE_H_
7
8 #include <stdint.h>
9 #include <time.h>
10
11 #include <string>
12 #include <vector>
13
14 #include "crypto/hash.h"
15 #include "history.h"
16 #include "history_sql.h"
17
18 namespace history {
19
20 /**
21 * This class wraps the history of a repository, i.e. it contains a database
22 * of named snapshots or tags. Internally it uses the HistoryDatabase class
23 * to store those tags in an SQLite file.
24 */
25 class SqliteHistory : public History {
26 protected:
27 static const std::string kPreviousRevisionKey;
28
29 public:
30 452 virtual ~SqliteHistory() { }
31
32 /**
33 * Opens an available history database file in read-only mode and returns
34 * a pointer to a History object wrapping this database.
35 * Note: The caller is assumed to retain ownership of the pointer and the
36 * history database is closed on deletion of the History object.
37 *
38 * @param file_name the path to the history SQLite file to be opened
39 * @return pointer to History object or NULL on error
40 */
41 static SqliteHistory* Open(const std::string &file_name);
42
43 /**
44 * Same as SqliteHistory::Open(), but opens the history database file in
45 * read/write mode. This allows to use the modifying methods of the History
46 * object.
47 *
48 * @param file_name the path to the history SQLite file to be opened
49 * @return pointer to History object or NULL on error
50 */
51 static SqliteHistory* OpenWritable(const std::string &file_name);
52
53 /**
54 * Creates an empty History database. Since a History object is always
55 * associated to a specific repository, one needs to specify the fully
56 * qualified repository name (FQRN) on creation of the History database.
57 * Note: pointer ownership is assumed to be retained by the caller.
58 *
59 * @param file_name the path of the new history file.
60 * @param fqrn the FQRN of the repository containing this History
61 * @return pointer to empty History object or NULL on error
62 */
63 static SqliteHistory* Create(const std::string &file_name,
64 const std::string &fqrn);
65
66 bool IsWritable() const;
67 unsigned GetNumberOfTags() const;
68
69 /**
70 * Opens a new database transaction in the underlying SQLite database
71 * This can greatly improve performance when used before inserting or
72 * removing multiple tags.
73 */
74 bool BeginTransaction() const;
75
76 /**
77 * Closes a transaction (see BeginTransaction())
78 */
79 bool CommitTransaction() const;
80
81 virtual bool Vacuum() { return database_->Vacuum(); }
82
83 /**
84 * Sets the internal pointer to the previous revision of this History file.
85 * Note: This must be handled by the user code.
86 *
87 * @param history_hash the content hash of the previous revision
88 */
89 bool SetPreviousRevision(const shash::Any &history_hash);
90 shash::Any previous_revision() const;
91
92 bool Insert(const Tag &tag);
93 bool Remove(const std::string &name);
94 bool Exists(const std::string &name) const;
95 bool GetByName(const std::string &name, Tag *tag) const;
96 bool GetByDate(const time_t timestamp, Tag *tag) const;
97 bool List(std::vector<Tag> *tags) const;
98
99 virtual bool GetBranchHead(const std::string &branch_name, Tag *tag) const;
100 virtual bool ExistsBranch(const std::string &branch_name) const;
101 virtual bool InsertBranch(const Branch &branch);
102 virtual bool PruneBranches();
103 virtual bool ListBranches(std::vector<Branch> *branches) const;
104
105 bool ListRecycleBin(std::vector<shash::Any> *hashes) const;
106 bool EmptyRecycleBin();
107
108 /**
109 * Rolls back the history to the provided target tag and deletes all tags
110 * of the containing channel in between.
111 *
112 * Note: this assumes that the provided target tag was already updated with
113 * the republished root catalog information.
114 *
115 * @param updated_target_tag the tag to be rolled back to (updated: see Note)
116 * @return true on success
117 */
118 bool Rollback(const Tag &updated_target_tag);
119
120 /**
121 * Lists the tags that would be deleted by a rollback to the tag specified.
122 *
123 * Note: This doesn't change the database but is mainly used for sanity checks
124 * and user output.
125 *
126 * @param target_tag_name the tag name for the planned rollback
127 * @param tags pointer to the result tag list to be filled
128 * @return true on success
129 */
130 bool ListTagsAffectedByRollback(const std::string &target_tag_name,
131 std::vector<Tag> *tags) const;
132
133 /**
134 * Provides a list of all referenced catalog hashes in this History.
135 * The hashes will be ordered by their associated revision number in
136 * ascending order.
137 *
138 * @param hashes pointer to the result vector to be filled
139 */
140 bool GetHashes(std::vector<shash::Any> *hashes) const;
141
142 // database file management controls
143 void TakeDatabaseFileOwnership();
144 void DropDatabaseFileOwnership();
145 bool OwnsDatabaseFile() const {
146 return database_.IsValid() && database_->OwnsFile();
147 }
148 std::string filename() const { return database_->filename(); }
149
150 protected:
151 static SqliteHistory* Open(const std::string &file_name,
152 const bool read_write);
153 bool OpenDatabase(const std::string &file_name, const bool read_write);
154 bool CreateDatabase(const std::string &file_name, const std::string &fqrn);
155 void PrepareQueries();
156
157 private:
158 template <class SqlListingT>
159 bool RunListing(std::vector<Tag> *list, SqlListingT *sql) const;
160
161 private:
162 UniquePtr<HistoryDatabase> database_;
163
164 UniquePtr<SqlInsertTag> insert_tag_;
165 UniquePtr<SqlRemoveTag> remove_tag_;
166 UniquePtr<SqlFindTag> find_tag_;
167 UniquePtr<SqlFindTagByDate> find_tag_by_date_;
168 UniquePtr<SqlCountTags> count_tags_;
169 UniquePtr<SqlListTags> list_tags_;
170 UniquePtr<SqlGetHashes> get_hashes_;
171 UniquePtr<SqlRollbackTag> rollback_tag_;
172 UniquePtr<SqlListRollbackTags> list_rollback_tags_;
173 UniquePtr<SqlListBranches> list_branches_;
174 UniquePtr<SqlInsertBranch> insert_branch_;
175 UniquePtr<SqlFindBranchHead> find_branch_head_;
176 UniquePtr<SqlRecycleBinList> recycle_list_;
177 UniquePtr<SqlRecycleBinFlush> recycle_empty_;
178 };
179
180 } // namespace history
181
182 #endif // CVMFS_HISTORY_SQLITE_H_
183