GCC Code Coverage Report
Directory: cvmfs/ Exec Total Coverage
File: cvmfs/history_sqlite.h Lines: 3 7 42.9 %
Date: 2019-02-03 02:48:13 Branches: 1 8 12.5 %

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 "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
117
class SqliteHistory : public History {
26
 protected:
27
  static const std::string kPreviousRevisionKey;
28
29
 public:
30

234
  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
  bool Tips(std::vector<Tag> *channel_tips) const;
99
100
  virtual bool GetBranchHead(const std::string &branch_name, Tag *tag) const;
101
  virtual bool ExistsBranch(const std::string &branch_name) const;
102
  virtual bool InsertBranch(const Branch &branch);
103
  virtual bool PruneBranches();
104
  virtual bool ListBranches(std::vector<Branch> *branches) const;
105
106
  bool ListRecycleBin(std::vector<shash::Any> *hashes) const;
107
  bool EmptyRecycleBin();
108
109
  /**
110
   * Rolls back the history to the provided target tag and deletes all tags
111
   * of the containing channel in between.
112
   *
113
   * Note: this assumes that the provided target tag was already updated with
114
   *       the republished root catalog information.
115
   *
116
   * @param updated_target_tag  the tag to be rolled back to (updated: see Note)
117
   * @return                    true on success
118
   */
119
  bool Rollback(const Tag &updated_target_tag);
120
121
  /**
122
   * Lists the tags that would be deleted by a rollback to the tag specified.
123
   *
124
   * Note: This doesn't change the database but is mainly used for sanity checks
125
   *       and user output.
126
   *
127
   * @param target_tag_name  the tag name for the planned rollback
128
   * @param tags             pointer to the result tag list to be filled
129
   * @return                 true on success
130
   */
131
  bool ListTagsAffectedByRollback(const std::string  &target_tag_name,
132
                                  std::vector<Tag>   *tags) const;
133
134
  /**
135
   * Provides a list of all referenced catalog hashes in this History.
136
   * The hashes will be ordered by their associated revision number in
137
   * acending order.
138
   *
139
   * @param hashes  pointer to the result vector to be filled
140
   */
141
  bool GetHashes(std::vector<shash::Any> *hashes) const;
142
143
  // database file management controls
144
  void TakeDatabaseFileOwnership();
145
  void DropDatabaseFileOwnership();
146
  bool OwnsDatabaseFile() const {
147
    return database_.IsValid() && database_->OwnsFile();
148
  }
149
  std::string filename() const { return database_->filename(); }
150
151
 protected:
152
  static SqliteHistory* Open(const std::string &file_name,
153
                             const bool read_write);
154
  bool OpenDatabase(const std::string &file_name, const bool read_write);
155
  bool CreateDatabase(const std::string &file_name, const std::string &fqrn);
156
  void PrepareQueries();
157
158
 private:
159
  template <class SqlListingT>
160
11
  bool RunListing(std::vector<Tag> *list, SqlListingT *sql) const;
161
162
 private:
163
  UniquePtr<HistoryDatabase>        database_;
164
165
  UniquePtr<SqlInsertTag>           insert_tag_;
166
  UniquePtr<SqlRemoveTag>           remove_tag_;
167
  UniquePtr<SqlFindTag>             find_tag_;
168
  UniquePtr<SqlFindTagByDate>       find_tag_by_date_;
169
  UniquePtr<SqlCountTags>           count_tags_;
170
  UniquePtr<SqlListTags>            list_tags_;
171
  UniquePtr<SqlGetChannelTips>      channel_tips_;
172
  UniquePtr<SqlGetHashes>           get_hashes_;
173
  UniquePtr<SqlRollbackTag>         rollback_tag_;
174
  UniquePtr<SqlListRollbackTags>    list_rollback_tags_;
175
  UniquePtr<SqlListBranches>        list_branches_;
176
  UniquePtr<SqlInsertBranch>        insert_branch_;
177
  UniquePtr<SqlFindBranchHead>      find_branch_head_;
178
  UniquePtr<SqlRecycleBinList>      recycle_list_;
179
  UniquePtr<SqlRecycleBinFlush>     recycle_empty_;
180
};
181
182
}  // namespace history
183
184
#endif  // CVMFS_HISTORY_SQLITE_H_