GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/ingestion/item.cc
Date: 2026-07-19 02:35:15
Exec Total Coverage
Lines: 142 142 100.0%
Branches: 35 64 54.7%

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 1312784 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 1312784 bool has_legacy_bulk_chunk)
26 1312784 : source_(source)
27 1312784 , compression_algorithm_(compression_algorithm)
28 1312784 , hash_algorithm_(hash_algorithm)
29 1312784 , hash_suffix_(hash_suffix)
30 1312784 , has_legacy_bulk_chunk_(has_legacy_bulk_chunk)
31 1312784 , size_(kSizeUnknown)
32 1312784 , may_have_chunks_(may_have_chunks)
33
1/2
✓ Branch 1 taken 1312784 times.
✗ Branch 2 not taken.
1312784 , chunk_detector_(min_chunk_size, avg_chunk_size, max_chunk_size)
34
1/2
✓ Branch 1 taken 1312784 times.
✗ Branch 2 not taken.
1312784 , bulk_hash_(hash_algorithm)
35
1/2
✓ Branch 2 taken 1312784 times.
✗ Branch 3 not taken.
2625568 , chunks_(1) {
36 1312784 const int retval = pthread_mutex_init(&lock_, NULL);
37
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1312784 times.
1312784 assert(retval == 0);
38 1312784 atomic_init64(&nchunks_in_fly_);
39 1312784 atomic_init32(&is_fully_chunked_);
40 1312784 }
41
42 1311547 FileItem::~FileItem() { pthread_mutex_destroy(&lock_); }
43
44 1254847 void FileItem::RegisterChunk(const FileChunk &file_chunk) {
45 1254847 const MutexLockGuard lock_guard(lock_);
46
47
2/2
✓ Branch 1 taken 2845 times.
✓ Branch 2 taken 1252002 times.
1254847 switch (file_chunk.content_hash().suffix) {
48 2845 case shash::kSuffixPartial:
49
1/2
✓ Branch 1 taken 2845 times.
✗ Branch 2 not taken.
2845 chunks_.PushBack(file_chunk);
50 2845 break;
51
52 1252002 default:
53
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1252002 times.
1252002 assert(file_chunk.offset() == 0);
54
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1252002 times.
1252002 assert(file_chunk.size() == size_);
55 1252002 bulk_hash_ = file_chunk.content_hash();
56 1252002 break;
57 }
58 1254847 atomic_dec64(&nchunks_in_fly_);
59 1254847 }
60
61
62 //------------------------------------------------------------------------------
63
64
65 1377907 ChunkItem::ChunkItem(FileItem *file_item, uint64_t offset)
66 1377162 : file_item_(file_item)
67 1377162 , offset_(offset)
68 1377162 , size_(0)
69 1377162 , is_bulk_chunk_(false)
70 1377162 , upload_handle_(NULL)
71
1/2
✓ Branch 4 taken 1377917 times.
✗ Branch 5 not taken.
1377907 , compressor_(NULL) {
72 1377917 hash_ctx_.algorithm = file_item->hash_algorithm();
73
1/2
✓ Branch 1 taken 1376907 times.
✗ Branch 2 not taken.
1377357 hash_ctx_.size = shash::GetContextSize(hash_ctx_.algorithm);
74 1376907 hash_ctx_.buffer = hash_ctx_buffer_;
75
1/2
✓ Branch 1 taken 1376967 times.
✗ Branch 2 not taken.
1376907 shash::Init(hash_ctx_);
76 1376967 hash_value_.algorithm = hash_ctx_.algorithm;
77 1376967 hash_value_.suffix = shash::kSuffixPartial;
78 1376967 file_item_->IncNchunksInFly();
79 1379182 }
80
81
82 1249257 void ChunkItem::MakeBulkChunk() {
83 1249257 is_bulk_chunk_ = true;
84 1249257 hash_value_.suffix = file_item_->hash_suffix();
85 1248977 }
86
87
88 2894158 zlib::Compressor *ChunkItem::GetCompressor() {
89
2/2
✓ Branch 1 taken 1250267 times.
✓ Branch 2 taken 1655821 times.
2894158 if (!compressor_.IsValid()) {
90 compressor_ = zlib::Compressor::Construct(
91
2/4
✓ Branch 2 taken 1251677 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1251107 times.
✗ Branch 6 not taken.
1250267 file_item_->compression_algorithm());
92 }
93 2906928 return compressor_.weak_ref();
94 }
95
96
97 1250777 void ChunkItem::ReleaseCompressor() { compressor_.Destroy(); }
98
99
100 //------------------------------------------------------------------------------
101
102 atomic_int64 BlockItem::managed_bytes_ = 0;
103
104
105 133976 BlockItem::BlockItem(ItemAllocator *allocator)
106 133976 : allocator_(allocator)
107 133976 , type_(kBlockHollow)
108 133976 , tag_(-1)
109 133976 , file_item_(NULL)
110 133976 , chunk_item_(NULL)
111 133976 , data_(NULL)
112 133976 , capacity_(0)
113 133976 , size_(0) { }
114
115
116 10365146 BlockItem::BlockItem(int64_t tag, ItemAllocator *allocator)
117 10307331 : allocator_(allocator)
118 10307331 , type_(kBlockHollow)
119 10307331 , tag_(tag)
120 10307331 , file_item_(NULL)
121 10307331 , chunk_item_(NULL)
122 10307331 , data_(NULL)
123 10307331 , capacity_(0)
124 10365146 , size_(0) {
125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10307331 times.
10307331 assert(tag_ >= 0);
126 10307331 }
127
128
129 10454091 BlockItem::~BlockItem() {
130
2/2
✓ Branch 0 taken 4872880 times.
✓ Branch 1 taken 5581211 times.
10454091 if (data_)
131 4872880 allocator_->Free(data_);
132 10458286 atomic_xadd64(&managed_bytes_, -static_cast<int64_t>(capacity_));
133 10529067 }
134
135
136 1379206 void BlockItem::Discharge() {
137 1379206 data_ = NULL;
138 1379206 size_ = capacity_ = 0;
139 1379206 }
140
141
142 3857816 void BlockItem::MakeStop() {
143
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3857816 times.
3857816 assert(type_ == kBlockHollow);
144 3857816 type_ = kBlockStop;
145 3857816 }
146
147
148 2006610 void BlockItem::MakeData(uint32_t capacity) {
149
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2006610 times.
2006610 assert(type_ == kBlockHollow);
150
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2006610 times.
2006610 assert(allocator_ != NULL);
151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2006610 times.
2006610 assert(capacity > 0);
152
153 2006610 type_ = kBlockData;
154 2006610 capacity_ = capacity;
155 2006610 data_ = reinterpret_cast<unsigned char *>(allocator_->Malloc(capacity_));
156 2012730 atomic_xadd64(&managed_bytes_, static_cast<int64_t>(capacity_));
157 2013530 }
158
159
160 /**
161 * Move data from one block to another.
162 */
163 1379171 void BlockItem::MakeDataMove(BlockItem *other) {
164
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1379171 times.
1379171 assert(type_ == kBlockHollow);
165
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1379171 times.
1379171 assert(other->type_ == kBlockData);
166
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1379171 times.
1379171 assert(other->size_ > 0);
167
168 1379171 type_ = kBlockData;
169 1379171 capacity_ = size_ = other->size_;
170 1379171 data_ = other->data_;
171 1379171 allocator_ = other->allocator_;
172
173 1379171 other->Discharge();
174 1378936 }
175
176
177 /**
178 * Copy a piece of one block's data into a new block.
179 */
180 3249870 void BlockItem::MakeDataCopy(const unsigned char *data, uint32_t size) {
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3249870 times.
3249870 assert(type_ == kBlockHollow);
182
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3249870 times.
3249870 assert(allocator_ != NULL);
183
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3249870 times.
3249870 assert(size > 0);
184
185 3249870 type_ = kBlockData;
186 3249870 capacity_ = size_ = size;
187 3249870 data_ = reinterpret_cast<unsigned char *>(allocator_->Malloc(capacity_));
188 3253065 memcpy(data_, data, size);
189 3253065 atomic_xadd64(&managed_bytes_, static_cast<int64_t>(capacity_));
190 3251870 }
191
192
193 387310 void BlockItem::Reset() {
194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 387310 times.
387310 assert(type_ == kBlockData);
195
196 387310 atomic_xadd64(&managed_bytes_, -static_cast<int64_t>(capacity_));
197 387340 allocator_->Free(data_);
198 387310 data_ = NULL;
199 387310 size_ = capacity_ = 0;
200 387310 type_ = kBlockHollow;
201 387310 }
202
203
204 7348643 void BlockItem::SetChunkItem(ChunkItem *value) {
205
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7348643 times.
7348643 assert(value != NULL);
206
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7348643 times.
7348643 assert(chunk_item_ == NULL);
207 7348643 chunk_item_ = value;
208 7348643 }
209
210
211 10277796 void BlockItem::SetFileItem(FileItem *value) {
212
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10277796 times.
10277796 assert(value != NULL);
213
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10277796 times.
10277796 assert(file_item_ == NULL);
214 10277796 file_item_ = value;
215 10277796 }
216
217
218 12800 uint32_t BlockItem::Write(void *buf, uint32_t count) {
219
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12800 times.
12800 assert(type_ == kBlockData);
220
221 12800 const uint32_t remaining = capacity_ - size_;
222 12800 const uint32_t nbytes = std::min(remaining, count);
223 12800 memcpy(data_ + size_, buf, nbytes);
224 12800 size_ += nbytes;
225 12800 return nbytes;
226 }
227