GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/ingestion/item.cc
Date: 2026-10-04 02:40:33
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 3058731 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 3058731 bool has_legacy_bulk_chunk)
26 3058731 : source_(source)
27 3058731 , compression_algorithm_(compression_algorithm)
28 3058731 , hash_algorithm_(hash_algorithm)
29 3058731 , hash_suffix_(hash_suffix)
30 3058731 , has_legacy_bulk_chunk_(has_legacy_bulk_chunk)
31 3058731 , size_(kSizeUnknown)
32 3058731 , may_have_chunks_(may_have_chunks)
33
1/2
✓ Branch 1 taken 3058731 times.
✗ Branch 2 not taken.
3058731 , chunk_detector_(min_chunk_size, avg_chunk_size, max_chunk_size)
34
1/2
✓ Branch 1 taken 3058731 times.
✗ Branch 2 not taken.
3058731 , bulk_hash_(hash_algorithm)
35
1/2
✓ Branch 2 taken 3058731 times.
✗ Branch 3 not taken.
6117462 , chunks_(1) {
36 3058731 const int retval = pthread_mutex_init(&lock_, NULL);
37
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3058731 times.
3058731 assert(retval == 0);
38 3058731 atomic_init64(&nchunks_in_fly_);
39 3058731 atomic_init32(&is_fully_chunked_);
40 3058731 }
41
42 3056725 FileItem::~FileItem() { pthread_mutex_destroy(&lock_); }
43
44 3007043 void FileItem::RegisterChunk(const FileChunk &file_chunk) {
45 3007043 const MutexLockGuard lock_guard(lock_);
46
47
2/2
✓ Branch 1 taken 5420 times.
✓ Branch 2 taken 3001623 times.
3007043 switch (file_chunk.content_hash().suffix) {
48 5420 case shash::kSuffixPartial:
49
1/2
✓ Branch 1 taken 5420 times.
✗ Branch 2 not taken.
5420 chunks_.PushBack(file_chunk);
50 5420 break;
51
52 3001623 default:
53
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3001623 times.
3001623 assert(file_chunk.offset() == 0);
54
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3001623 times.
3001623 assert(file_chunk.size() == size_);
55 3001623 bulk_hash_ = file_chunk.content_hash();
56 3001623 break;
57 }
58 3007043 atomic_dec64(&nchunks_in_fly_);
59 3007043 }
60
61
62 //------------------------------------------------------------------------------
63
64
65 3084519 ChunkItem::ChunkItem(FileItem *file_item, uint64_t offset)
66 3084291 : file_item_(file_item)
67 3084291 , offset_(offset)
68 3084291 , size_(0)
69 3084291 , is_bulk_chunk_(false)
70 3084291 , upload_handle_(NULL)
71
1/2
✓ Branch 4 taken 3083631 times.
✗ Branch 5 not taken.
3084519 , compressor_(nullptr) {
72 3083631 hash_ctx_.algorithm = file_item->hash_algorithm();
73
1/2
✓ Branch 1 taken 3083691 times.
✗ Branch 2 not taken.
3083811 hash_ctx_.size = shash::GetContextSize(hash_ctx_.algorithm);
74 3083691 hash_ctx_.buffer = hash_ctx_buffer_;
75
1/2
✓ Branch 1 taken 3083739 times.
✗ Branch 2 not taken.
3083691 shash::Init(hash_ctx_);
76 3083739 hash_value_.algorithm = hash_ctx_.algorithm;
77 3083739 hash_value_.suffix = shash::kSuffixPartial;
78 3083739 file_item_->IncNchunksInFly();
79 3086163 }
80
81
82 2998071 void ChunkItem::MakeBulkChunk() {
83 2998071 is_bulk_chunk_ = true;
84 2998071 hash_value_.suffix = file_item_->hash_suffix();
85 2997675 }
86
87
88 6988146 zlib::Compressor *ChunkItem::GetCompressor() {
89
2/2
✓ Branch 1 taken 3003835 times.
✓ Branch 2 taken 3984659 times.
6988146 if (compressor_.get() == nullptr) {
90 6010778 compressor_ = std::unique_ptr<zlib::Compressor>(
91
1/2
✓ Branch 2 taken 3005503 times.
✗ Branch 3 not taken.
9014529 zlib::Compressor::Construct(file_item_->compression_algorithm()));
92 }
93 6990054 return compressor_.get();
94 }
95
96
97 3005419 void ChunkItem::ReleaseCompressor() { compressor_.reset(); }
98
99
100 //------------------------------------------------------------------------------
101
102 atomic_int64 BlockItem::managed_bytes_ = 0;
103
104
105 56832 BlockItem::BlockItem(ItemAllocator *allocator)
106 56832 : allocator_(allocator)
107 56832 , type_(kBlockHollow)
108 56832 , tag_(-1)
109 56832 , file_item_(NULL)
110 56832 , chunk_item_(NULL)
111 56832 , data_(NULL)
112 56832 , capacity_(0)
113 56832 , size_(0) { }
114
115
116 22251586 BlockItem::BlockItem(int64_t tag, ItemAllocator *allocator)
117 22232346 : allocator_(allocator)
118 22232346 , type_(kBlockHollow)
119 22232346 , tag_(tag)
120 22232346 , file_item_(NULL)
121 22232346 , chunk_item_(NULL)
122 22232346 , data_(NULL)
123 22232346 , capacity_(0)
124 22251586 , size_(0) {
125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22232346 times.
22232346 assert(tag_ >= 0);
126 22232346 }
127
128
129 22316924 BlockItem::~BlockItem() {
130
2/2
✓ Branch 0 taken 9390610 times.
✓ Branch 1 taken 12926314 times.
22316924 if (data_)
131 9390610 allocator_->Free(data_);
132 22319828 atomic_xadd64(&managed_bytes_, -static_cast<int64_t>(capacity_));
133 22341015 }
134
135
136 3311151 void BlockItem::Discharge() {
137 3311151 data_ = NULL;
138 3311151 size_ = capacity_ = 0;
139 3311151 }
140
141
142 9085457 void BlockItem::MakeStop() {
143
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9085457 times.
9085457 assert(type_ == kBlockHollow);
144 9085457 type_ = kBlockStop;
145 9085457 }
146
147
148 4638703 void BlockItem::MakeData(uint32_t capacity) {
149
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4638703 times.
4638703 assert(type_ == kBlockHollow);
150
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4638703 times.
4638703 assert(allocator_ != NULL);
151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4638703 times.
4638703 assert(capacity > 0);
152
153 4638703 type_ = kBlockData;
154 4638703 capacity_ = capacity;
155 4638703 data_ = reinterpret_cast<unsigned char *>(allocator_->Malloc(capacity_));
156 4640995 atomic_xadd64(&managed_bytes_, static_cast<int64_t>(capacity_));
157 4641079 }
158
159
160 /**
161 * Move data from one block to another.
162 */
163 3309891 void BlockItem::MakeDataMove(BlockItem *other) {
164
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3309891 times.
3309891 assert(type_ == kBlockHollow);
165
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3309891 times.
3309891 assert(other->type_ == kBlockData);
166
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3309891 times.
3309891 assert(other->size_ > 0);
167
168 3309891 type_ = kBlockData;
169 3309891 capacity_ = size_ = other->size_;
170 3309891 data_ = other->data_;
171 3309891 allocator_ = other->allocator_;
172
173 3309891 other->Discharge();
174 3310755 }
175
176
177 /**
178 * Copy a piece of one block's data into a new block.
179 */
180 5224247 void BlockItem::MakeDataCopy(const unsigned char *data, uint32_t size) {
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5224247 times.
5224247 assert(type_ == kBlockHollow);
182
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5224247 times.
5224247 assert(allocator_ != NULL);
183
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5224247 times.
5224247 assert(size > 0);
184
185 5224247 type_ = kBlockData;
186 5224247 capacity_ = size_ = size;
187 5224247 data_ = reinterpret_cast<unsigned char *>(allocator_->Malloc(capacity_));
188 5239843 memcpy(data_, data, size);
189 5239843 atomic_xadd64(&managed_bytes_, static_cast<int64_t>(capacity_));
190 5236719 }
191
192
193 488816 void BlockItem::Reset() {
194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 488816 times.
488816 assert(type_ == kBlockData);
195
196 488816 atomic_xadd64(&managed_bytes_, -static_cast<int64_t>(capacity_));
197 488936 allocator_->Free(data_);
198 488936 data_ = NULL;
199 488936 size_ = capacity_ = 0;
200 488936 type_ = kBlockHollow;
201 488936 }
202
203
204 15455784 void BlockItem::SetChunkItem(ChunkItem *value) {
205
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15455784 times.
15455784 assert(value != NULL);
206
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15455784 times.
15455784 assert(chunk_item_ == NULL);
207 15455784 chunk_item_ = value;
208 15455784 }
209
210
211 22221022 void BlockItem::SetFileItem(FileItem *value) {
212
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22221022 times.
22221022 assert(value != NULL);
213
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22221022 times.
22221022 assert(file_item_ == NULL);
214 22221022 file_item_ = value;
215 22221022 }
216
217
218 8192 uint32_t BlockItem::Write(void *buf, uint32_t count) {
219
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8192 times.
8192 assert(type_ == kBlockData);
220
221 8192 const uint32_t remaining = capacity_ - size_;
222 8192 const uint32_t nbytes = std::min(remaining, count);
223 8192 memcpy(data_ + size_, buf, nbytes);
224 8192 size_ += nbytes;
225 8192 return nbytes;
226 }
227