Directory: | cvmfs/ |
---|---|
File: | cvmfs/catalog_rw.h |
Date: | 2025-04-20 02:34:28 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 30 | 30 | 100.0% |
Branches: | 9 | 16 | 56.2% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /** | ||
2 | * This file is part of the CernVM File System | ||
3 | * | ||
4 | * The WritableCatalog class is derived from Catalog. It is used by the | ||
5 | * WritableCatalogManager on the server side. | ||
6 | * | ||
7 | * The main functionality is in: | ||
8 | * - AddEntry | ||
9 | * - UpdateEntry | ||
10 | * - RemoveEntry | ||
11 | * | ||
12 | * Catalogs not thread safe. | ||
13 | */ | ||
14 | |||
15 | #ifndef CVMFS_CATALOG_RW_H_ | ||
16 | #define CVMFS_CATALOG_RW_H_ | ||
17 | |||
18 | #include <stdint.h> | ||
19 | |||
20 | #include <string> | ||
21 | #include <vector> | ||
22 | |||
23 | #include "catalog.h" | ||
24 | #include "util/posix.h" | ||
25 | |||
26 | class XattrList; | ||
27 | |||
28 | namespace swissknife { | ||
29 | class CommandMigrate; | ||
30 | } | ||
31 | |||
32 | namespace catalog { | ||
33 | |||
34 | class WritableCatalogManager; | ||
35 | |||
36 | class WritableCatalog : public Catalog { | ||
37 | friend class WritableCatalogManager; | ||
38 | friend class swissknife::CommandMigrate; // needed for catalog migrations | ||
39 | friend class VirtualCatalog; // needed for /.cvmfs creation | ||
40 | |||
41 | public: | ||
42 | WritableCatalog(const std::string &path, | ||
43 | const shash::Any &catalog_hash, | ||
44 | Catalog *parent, | ||
45 | const bool is_not_root = false); | ||
46 | virtual ~WritableCatalog(); | ||
47 | |||
48 | static WritableCatalog *AttachFreely(const std::string &root_path, | ||
49 | const std::string &file, | ||
50 | const shash::Any &catalog_hash, | ||
51 | Catalog *parent = NULL, | ||
52 | const bool is_not_root = false); | ||
53 | |||
54 | void Transaction(); | ||
55 | void Commit(); | ||
56 | |||
57 | 55 | inline bool IsDirty() const { return dirty_; } | |
58 | 457 | inline bool IsWritable() const { return true; } | |
59 | uint32_t GetMaxLinkId() const; | ||
60 | |||
61 | void AddEntry(const DirectoryEntry &entry, | ||
62 | const XattrList &xattr, | ||
63 | const std::string &entry_path, | ||
64 | const std::string &parent_path); | ||
65 | void TouchEntry(const DirectoryEntryBase &entry, | ||
66 | const XattrList &xattrs, | ||
67 | const shash::Md5 &path_hash); | ||
68 | 4 | inline void TouchEntry( | |
69 | const DirectoryEntryBase &entry, | ||
70 | const XattrList &xattrs, | ||
71 | const std::string &path) | ||
72 | { | ||
73 |
2/4✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
|
4 | TouchEntry(entry, xattrs, shash::Md5(shash::AsciiPtr(path))); |
74 | 4 | } | |
75 | void RemoveEntry(const std::string &entry_path); | ||
76 | void IncLinkcount(const std::string &path_within_group, const int delta); | ||
77 | void AddFileChunk(const std::string &entry_path, const FileChunk &chunk); | ||
78 | void RemoveFileChunks(const std::string &entry_path); | ||
79 | |||
80 | // Creation and removal of catalogs | ||
81 | void Partition(WritableCatalog *new_nested_catalog); | ||
82 | void MergeIntoParent(); | ||
83 | void RemoveFromParent(); | ||
84 | |||
85 | // Nested catalog references | ||
86 | void InsertNestedCatalog(const std::string &mountpoint, | ||
87 | Catalog *attached_reference, | ||
88 | const shash::Any content_hash, | ||
89 | const uint64_t size); | ||
90 | void InsertBindMountpoint(const std::string &mountpoint, | ||
91 | const shash::Any content_hash, | ||
92 | const uint64_t size); | ||
93 | void UpdateNestedCatalog(const std::string &path, | ||
94 | const shash::Any &hash, | ||
95 | const uint64_t size, | ||
96 | const DeltaCounters &child_counters); | ||
97 | void RemoveNestedCatalog(const std::string &mountpoint, | ||
98 | Catalog **attached_reference); | ||
99 | void RemoveBindMountpoint(const std::string &mountpoint); | ||
100 | |||
101 | void UpdateLastModified(); | ||
102 | void IncrementRevision(); | ||
103 | void SetRevision(const uint64_t new_revision); | ||
104 | void SetBranch(const std::string &branch_name); | ||
105 | void SetPreviousRevision(const shash::Any &hash); | ||
106 | void SetTTL(const uint64_t new_ttl); | ||
107 | bool SetVOMSAuthz(const std::string &voms_authz); | ||
108 | |||
109 | protected: | ||
110 | static const double kMaximalFreePageRatio; // = 0.2 | ||
111 | static const double kMaximalRowIdWasteRatio; // = 0.25; | ||
112 | |||
113 | 98 | CatalogDatabase::OpenMode DatabaseOpenMode() const { | |
114 | 98 | return CatalogDatabase::kOpenReadWrite; | |
115 | } | ||
116 | |||
117 | void UpdateEntry(const DirectoryEntry &entry, const shash::Md5 &path_hash); | ||
118 | 214 | inline void UpdateEntry( | |
119 | const DirectoryEntry &entry, | ||
120 | const std::string &path) | ||
121 | { | ||
122 |
2/4✓ Branch 2 taken 214 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 214 times.
✗ Branch 6 not taken.
|
214 | UpdateEntry(entry, shash::Md5(shash::AsciiPtr(path))); |
123 | 214 | } | |
124 | |||
125 | 132 | inline void AddEntry( | |
126 | const DirectoryEntry &entry, | ||
127 | const XattrList &xattrs, | ||
128 | const std::string &path) | ||
129 | { | ||
130 |
1/2✓ Branch 2 taken 132 times.
✗ Branch 3 not taken.
|
132 | AddEntry(entry, xattrs, path, GetParentPath(path)); |
131 | 132 | } | |
132 | |||
133 | void InitPreparedStatements(); | ||
134 | void FinalizePreparedStatements(); | ||
135 | |||
136 | 73 | inline WritableCatalog* GetWritableParent() const { | |
137 | 73 | Catalog *parent = this->parent(); | |
138 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 73 times.
|
73 | assert(parent->IsWritable()); |
139 | 73 | return static_cast<WritableCatalog *>(parent); | |
140 | } | ||
141 | |||
142 | int dirty_children() const { return atomic_read32(&dirty_children_); } | ||
143 | 55 | void set_dirty_children(const int count) { | |
144 | 55 | atomic_write32(&dirty_children_, count); | |
145 | 55 | } | |
146 | 32 | int DecrementDirtyChildren() { | |
147 | 32 | return atomic_xadd32(&dirty_children_, -1) - 1; | |
148 | } | ||
149 | |||
150 | private: | ||
151 | SqlDirentInsert *sql_insert_; | ||
152 | SqlDirentUnlink *sql_unlink_; | ||
153 | SqlDirentTouch *sql_touch_; | ||
154 | SqlDirentUpdate *sql_update_; | ||
155 | SqlChunkInsert *sql_chunk_insert_; | ||
156 | SqlChunksRemove *sql_chunks_remove_; | ||
157 | SqlChunksCount *sql_chunks_count_; | ||
158 | SqlMaxHardlinkGroup *sql_max_link_id_; | ||
159 | SqlIncLinkcount *sql_inc_linkcount_; | ||
160 | |||
161 | bool dirty_; /**< Indicates if the catalog has been changed */ | ||
162 | |||
163 | DeltaCounters delta_counters_; | ||
164 | |||
165 | // parallel commit state | ||
166 | mutable atomic_int32 dirty_children_; | ||
167 | |||
168 | 955 | inline void SetDirty() { | |
169 |
2/2✓ Branch 0 taken 89 times.
✓ Branch 1 taken 866 times.
|
955 | if (!dirty_) |
170 | 89 | Transaction(); | |
171 | 955 | dirty_ = true; | |
172 | 955 | } | |
173 | |||
174 | // Helpers for nested catalog creation and removal | ||
175 | void MakeTransitionPoint(const std::string &mountpoint); | ||
176 | void MakeNestedRoot(); | ||
177 | 35 | inline void MoveToNested( | |
178 | const std::string &dir_structure_root, | ||
179 | WritableCatalog *new_nested_catalog, | ||
180 | std::vector<std::string> *grand_child_mountpoints) | ||
181 | { | ||
182 |
1/2✓ Branch 2 taken 35 times.
✗ Branch 3 not taken.
|
35 | MoveToNestedRecursively(dir_structure_root, |
183 | new_nested_catalog, | ||
184 | grand_child_mountpoints); | ||
185 | 35 | } | |
186 | void MoveToNestedRecursively( | ||
187 | const std::string dir_structure_root, | ||
188 | WritableCatalog *new_nested_catalog, | ||
189 | std::vector<std::string> *grand_child_mountpoints); | ||
190 | void MoveCatalogsToNested(const std::vector<std::string> &nested_catalogs, | ||
191 | WritableCatalog *new_nested_catalog); | ||
192 | void MoveFileChunksToNested(const std::string &full_path, | ||
193 | const shash::Algorithms algorithm, | ||
194 | WritableCatalog *new_nested_catalog); | ||
195 | |||
196 | void CopyToParent(); | ||
197 | void CopyCatalogsToParent(); | ||
198 | |||
199 | void UpdateCounters(); | ||
200 | void VacuumDatabaseIfNecessary(); | ||
201 | }; // class WritableCatalog | ||
202 | |||
203 | typedef std::vector<WritableCatalog *> WritableCatalogList; | ||
204 | |||
205 | } // namespace catalog | ||
206 | |||
207 | #endif // CVMFS_CATALOG_RW_H_ | ||
208 |