GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/ingestion/item.cc
Date: 2026-11-08 02:41:18
Exec Total Coverage
Lines: 143 143 100.0%
Branches: 34 62 54.8%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 */
4
5 #include "item.h"
6
7 #include <algorithm>
8 #include <cassert>
9 #include <cstdlib>
10 #include <cstring>
11
12 #include "ingestion/ingestion_source.h"
13 #include "item_mem.h"
14 #include "util/concurrency.h"
15 #include "util/smalloc.h"
16
17 1540302 FileItem::FileItem(IngestionSource *source,
18 uint64_t min_chunk_size,
19 uint64_t avg_chunk_size,
20 uint64_t max_chunk_size,
21 zlib::Algorithms compression_algorithm,
22 shash::Algorithms hash_algorithm,
23 shash::Suffix hash_suffix,
24 bool may_have_chunks,
25 1540302 bool has_legacy_bulk_chunk)
26 1540302 : source_(source)
27 1540302 , compression_algorithm_(compression_algorithm)
28 1540302 , hash_algorithm_(hash_algorithm)
29 1540302 , hash_suffix_(hash_suffix)
30 1540302 , has_legacy_bulk_chunk_(has_legacy_bulk_chunk)
31 1540302 , size_(kSizeUnknown)
32 1540302 , may_have_chunks_(may_have_chunks)
33
1/2
✓ Branch 1 taken 1540302 times.
✗ Branch 2 not taken.
1540302 , chunk_detector_(min_chunk_size, avg_chunk_size, max_chunk_size)
34
1/2
✓ Branch 1 taken 1540302 times.
✗ Branch 2 not taken.
1540302 , bulk_hash_(hash_algorithm)
35
1/2
✓ Branch 1 taken 1540302 times.
✗ Branch 2 not taken.
1540302 , chunks_(1) {
36 1540302 const int retval = pthread_mutex_init(&lock_, NULL);
37
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1540302 times.
1540302 assert(retval == 0);
38 1540302 nchunks_in_fly_.store(0);
39 1540302 is_fully_chunked_.store(0);
40 1540302 }
41
42 1538448 FileItem::~FileItem() { pthread_mutex_destroy(&lock_); }
43
44 1503881 void FileItem::RegisterChunk(const FileChunk &file_chunk) {
45 1503881 const MutexLockGuard lock_guard(lock_);
46
47
2/2
✓ Branch 1 taken 2870 times.
✓ Branch 2 taken 1501011 times.
1503881 switch (file_chunk.content_hash().suffix) {
48 2870 case shash::kSuffixPartial:
49
1/2
✓ Branch 1 taken 2870 times.
✗ Branch 2 not taken.
2870 chunks_.PushBack(file_chunk);
50 2870 break;
51
52 1501011 default:
53
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1501011 times.
1501011 assert(file_chunk.offset() == 0);
54
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1501011 times.
1501011 assert(file_chunk.size() == size_);
55 1501011 bulk_hash_ = file_chunk.content_hash();
56 1501011 break;
57 }
58 1503881 nchunks_in_fly_.fetch_sub(1);
59 1503881 }
60
61
62 //------------------------------------------------------------------------------
63
64
65 1567788 ChunkItem::ChunkItem(FileItem *file_item, uint64_t offset)
66 1567788 : file_item_(file_item)
67 1567788 , offset_(offset)
68 1567788 , size_(0)
69 1567788 , is_bulk_chunk_(false)
70 1567788 , upload_handle_(NULL)
71
1/2
✓ Branch 3 taken 1567206 times.
✗ Branch 4 not taken.
1567788 , compressor_(nullptr) {
72 1567206 hash_ctx_.algorithm = file_item->hash_algorithm();
73
1/2
✓ Branch 1 taken 1567344 times.
✗ Branch 2 not taken.
1567326 hash_ctx_.size = shash::GetContextSize(hash_ctx_.algorithm);
74 1567344 hash_ctx_.buffer = hash_ctx_buffer_;
75
1/2
✓ Branch 1 taken 1567260 times.
✗ Branch 2 not taken.
1567344 shash::Init(hash_ctx_);
76 1567260 hash_value_.algorithm = hash_ctx_.algorithm;
77 1567260 hash_value_.suffix = shash::kSuffixPartial;
78 1567260 file_item_->IncNchunksInFly();
79 1568808 }
80
81
82 1499784 void ChunkItem::MakeBulkChunk() {
83 1499784 is_bulk_chunk_ = true;
84 1499784 hash_value_.suffix = file_item_->hash_suffix();
85 1499478 }
86
87
88 3497140 zlib::Compressor *ChunkItem::GetCompressor() {
89
2/2
✓ Branch 1 taken 1502080 times.
✓ Branch 2 taken 1995186 times.
3497140 if (compressor_.get() == nullptr) {
90 3005126 compressor_ = std::unique_ptr<zlib::Compressor>(
91
1/2
✓ Branch 2 taken 1502920 times.
✗ Branch 3 not taken.
4506606 zlib::Compressor::Construct(file_item_->compression_algorithm()));
92 }
93 3497542 return compressor_.get();
94 }
95
96
97 1502698 void ChunkItem::ReleaseCompressor() { compressor_.reset(); }
98
99
100 //------------------------------------------------------------------------------
101
102 std::atomic<int64_t> BlockItem::managed_bytes_(0);
103
104
105 44392 BlockItem::BlockItem(ItemAllocator *allocator)
106 44392 : allocator_(allocator)
107 44392 , type_(kBlockHollow)
108 44392 , tag_(-1)
109 44392 , file_item_(NULL)
110 44392 , chunk_item_(NULL)
111 44392 , data_(NULL)
112 44392 , capacity_(0)
113 44392 , size_(0) { }
114
115
116 11446660 BlockItem::BlockItem(int64_t tag, ItemAllocator *allocator)
117 11446660 : allocator_(allocator)
118 11446660 , type_(kBlockHollow)
119 11446660 , tag_(tag)
120 11446660 , file_item_(NULL)
121 11446660 , chunk_item_(NULL)
122 11446660 , data_(NULL)
123 11446660 , capacity_(0)
124 11446660 , size_(0) {
125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11446660 times.
11446660 assert(tag_ >= 0);
126 11446660 }
127
128
129 11496022 BlockItem::~BlockItem() {
130
2/2
✓ Branch 0 taken 4944772 times.
✓ Branch 1 taken 6551250 times.
11496022 if (data_)
131 4944772 allocator_->Free(data_);
132 11505622 managed_bytes_.fetch_add(-static_cast<int64_t>(capacity_));
133 11505622 }
134
135
136 1656094 void BlockItem::Discharge() {
137 1656094 data_ = NULL;
138 1656094 size_ = capacity_ = 0;
139 1656094 }
140
141
142 4569773 void BlockItem::MakeStop() {
143
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4569773 times.
4569773 assert(type_ == kBlockHollow);
144 4569773 type_ = kBlockStop;
145 4569773 }
146
147
148 2326210 void BlockItem::MakeData(uint32_t capacity) {
149
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2326210 times.
2326210 assert(type_ == kBlockHollow);
150
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2326210 times.
2326210 assert(allocator_ != NULL);
151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2326210 times.
2326210 assert(capacity > 0);
152
153 2326210 type_ = kBlockData;
154 2326210 capacity_ = capacity;
155 2326210 data_ = reinterpret_cast<unsigned char *>(allocator_->Malloc(capacity_));
156 2327482 managed_bytes_.fetch_add(static_cast<int64_t>(capacity_));
157 2327482 }
158
159
160 /**
161 * Move data from one block to another.
162 */
163 1656286 void BlockItem::MakeDataMove(BlockItem *other) {
164
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1656286 times.
1656286 assert(type_ == kBlockHollow);
165
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1656286 times.
1656286 assert(other->type_ == kBlockData);
166
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1656286 times.
1656286 assert(other->size_ > 0);
167
168 1656286 type_ = kBlockData;
169 1656286 capacity_ = size_ = other->size_;
170 1656286 data_ = other->data_;
171 1656286 allocator_ = other->allocator_;
172
173 1656286 other->Discharge();
174 1656064 }
175
176
177 /**
178 * Copy a piece of one block's data into a new block.
179 */
180 2908355 void BlockItem::MakeDataCopy(const unsigned char *data, uint32_t size) {
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2908355 times.
2908355 assert(type_ == kBlockHollow);
182
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2908355 times.
2908355 assert(allocator_ != NULL);
183
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2908355 times.
2908355 assert(size > 0);
184
185 2908355 type_ = kBlockData;
186 2908355 capacity_ = size_ = size;
187 2908355 data_ = reinterpret_cast<unsigned char *>(allocator_->Malloc(capacity_));
188 2911928 memcpy(data_, data, size);
189 2911928 managed_bytes_.fetch_add(static_cast<int64_t>(capacity_));
190 2911928 }
191
192
193 294482 void BlockItem::Reset() {
194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 294482 times.
294482 assert(type_ == kBlockData);
195
196 294482 managed_bytes_.fetch_add(-static_cast<int64_t>(capacity_));
197 294482 allocator_->Free(data_);
198 294554 data_ = NULL;
199 294554 size_ = capacity_ = 0;
200 294554 type_ = kBlockHollow;
201 294554 }
202
203
204 7997371 void BlockItem::SetChunkItem(ChunkItem *value) {
205
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7997371 times.
7997371 assert(value != NULL);
206
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7997371 times.
7997371 assert(chunk_item_ == NULL);
207 7997371 chunk_item_ = value;
208 7997371 }
209
210
211 11439540 void BlockItem::SetFileItem(FileItem *value) {
212
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11439540 times.
11439540 assert(value != NULL);
213
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11439540 times.
11439540 assert(file_item_ == NULL);
214 11439540 file_item_ = value;
215 11439540 }
216
217
218 6656 uint32_t BlockItem::Write(void *buf, uint32_t count) {
219
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6656 times.
6656 assert(type_ == kBlockData);
220
221 6656 const uint32_t remaining = capacity_ - size_;
222 6656 const uint32_t nbytes = std::min(remaining, count);
223 6656 memcpy(data_ + size_, buf, nbytes);
224 6656 size_ += nbytes;
225 6656 return nbytes;
226 }
227
228