GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/ingestion/item.cc
Date: 2026-06-28 02:36:10
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 320826 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 320826 bool has_legacy_bulk_chunk)
26 320826 : source_(source)
27 320826 , compression_algorithm_(compression_algorithm)
28 320826 , hash_algorithm_(hash_algorithm)
29 320826 , hash_suffix_(hash_suffix)
30 320826 , has_legacy_bulk_chunk_(has_legacy_bulk_chunk)
31 320826 , size_(kSizeUnknown)
32 320826 , may_have_chunks_(may_have_chunks)
33
1/2
✓ Branch 1 taken 320826 times.
✗ Branch 2 not taken.
320826 , chunk_detector_(min_chunk_size, avg_chunk_size, max_chunk_size)
34
1/2
✓ Branch 1 taken 320826 times.
✗ Branch 2 not taken.
320826 , bulk_hash_(hash_algorithm)
35
1/2
✓ Branch 2 taken 320826 times.
✗ Branch 3 not taken.
641652 , chunks_(1) {
36 320826 const int retval = pthread_mutex_init(&lock_, NULL);
37
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 320826 times.
320826 assert(retval == 0);
38 320826 atomic_init64(&nchunks_in_fly_);
39 320826 atomic_init32(&is_fully_chunked_);
40 320826 }
41
42 320069 FileItem::~FileItem() { pthread_mutex_destroy(&lock_); }
43
44 253642 void FileItem::RegisterChunk(const FileChunk &file_chunk) {
45 253642 const MutexLockGuard lock_guard(lock_);
46
47
2/2
✓ Branch 1 taken 1433 times.
✓ Branch 2 taken 252209 times.
253642 switch (file_chunk.content_hash().suffix) {
48 1433 case shash::kSuffixPartial:
49
1/2
✓ Branch 1 taken 1433 times.
✗ Branch 2 not taken.
1433 chunks_.PushBack(file_chunk);
50 1433 break;
51
52 252209 default:
53
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 252209 times.
252209 assert(file_chunk.offset() == 0);
54
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 252209 times.
252209 assert(file_chunk.size() == size_);
55 252209 bulk_hash_ = file_chunk.content_hash();
56 252209 break;
57 }
58 253642 atomic_dec64(&nchunks_in_fly_);
59 253642 }
60
61
62 //------------------------------------------------------------------------------
63
64
65 413624 ChunkItem::ChunkItem(FileItem *file_item, uint64_t offset)
66 413525 : file_item_(file_item)
67 413525 , offset_(offset)
68 413525 , size_(0)
69 413525 , is_bulk_chunk_(false)
70 413525 , upload_handle_(NULL)
71
1/2
✓ Branch 4 taken 413588 times.
✗ Branch 5 not taken.
413624 , compressor_(NULL) {
72 413588 hash_ctx_.algorithm = file_item->hash_algorithm();
73
1/2
✓ Branch 1 taken 413461 times.
✗ Branch 2 not taken.
413504 hash_ctx_.size = shash::GetContextSize(hash_ctx_.algorithm);
74 413461 hash_ctx_.buffer = hash_ctx_buffer_;
75
1/2
✓ Branch 1 taken 413446 times.
✗ Branch 2 not taken.
413461 shash::Init(hash_ctx_);
76 413446 hash_value_.algorithm = hash_ctx_.algorithm;
77 413446 hash_value_.suffix = shash::kSuffixPartial;
78 413446 file_item_->IncNchunksInFly();
79 413865 }
80
81
82 251850 void ChunkItem::MakeBulkChunk() {
83 251850 is_bulk_chunk_ = true;
84 251850 hash_value_.suffix = file_item_->hash_suffix();
85 251801 }
86
87
88 597589 zlib::Compressor *ChunkItem::GetCompressor() {
89
2/2
✓ Branch 1 taken 251704 times.
✓ Branch 2 taken 346040 times.
597589 if (!compressor_.IsValid()) {
90 compressor_ = zlib::Compressor::Construct(
91
2/4
✓ Branch 2 taken 252309 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 252185 times.
✗ Branch 6 not taken.
251704 file_item_->compression_algorithm());
92 }
93 598225 return compressor_.weak_ref();
94 }
95
96
97 252081 void ChunkItem::ReleaseCompressor() { compressor_.Destroy(); }
98
99
100 //------------------------------------------------------------------------------
101
102 atomic_int64 BlockItem::managed_bytes_ = 0;
103
104
105 139072 BlockItem::BlockItem(ItemAllocator *allocator)
106 139072 : allocator_(allocator)
107 139072 , type_(kBlockHollow)
108 139072 , tag_(-1)
109 139072 , file_item_(NULL)
110 139072 , chunk_item_(NULL)
111 139072 , data_(NULL)
112 139072 , capacity_(0)
113 139072 , size_(0) { }
114
115
116 3794598 BlockItem::BlockItem(int64_t tag, ItemAllocator *allocator)
117 3784342 : allocator_(allocator)
118 3784342 , type_(kBlockHollow)
119 3784342 , tag_(tag)
120 3784342 , file_item_(NULL)
121 3784342 , chunk_item_(NULL)
122 3784342 , data_(NULL)
123 3784342 , capacity_(0)
124 3794598 , size_(0) {
125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3784342 times.
3784342 assert(tag_ >= 0);
126 3784342 }
127
128
129 3925824 BlockItem::~BlockItem() {
130
2/2
✓ Branch 0 taken 2346193 times.
✓ Branch 1 taken 1579631 times.
3925824 if (data_)
131 2346193 allocator_->Free(data_);
132 3926935 atomic_xadd64(&managed_bytes_, -static_cast<int64_t>(capacity_));
133 3941433 }
134
135
136 277615 void BlockItem::Discharge() {
137 277615 data_ = NULL;
138 277615 size_ = capacity_ = 0;
139 277615 }
140
141
142 914174 void BlockItem::MakeStop() {
143
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 914174 times.
914174 assert(type_ == kBlockHollow);
144 914174 type_ = kBlockStop;
145 914174 }
146
147
148 467301 void BlockItem::MakeData(uint32_t capacity) {
149
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 467301 times.
467301 assert(type_ == kBlockHollow);
150
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 467301 times.
467301 assert(allocator_ != NULL);
151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 467301 times.
467301 assert(capacity > 0);
152
153 467301 type_ = kBlockData;
154 467301 capacity_ = capacity;
155 467301 data_ = reinterpret_cast<unsigned char *>(allocator_->Malloc(capacity_));
156 468302 atomic_xadd64(&managed_bytes_, static_cast<int64_t>(capacity_));
157 468438 }
158
159
160 /**
161 * Move data from one block to another.
162 */
163 277598 void BlockItem::MakeDataMove(BlockItem *other) {
164
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 277598 times.
277598 assert(type_ == kBlockHollow);
165
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 277598 times.
277598 assert(other->type_ == kBlockData);
166
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 277598 times.
277598 assert(other->size_ > 0);
167
168 277598 type_ = kBlockData;
169 277598 capacity_ = size_ = other->size_;
170 277598 data_ = other->data_;
171 277598 allocator_ = other->allocator_;
172
173 277598 other->Discharge();
174 277596 }
175
176
177 /**
178 * Copy a piece of one block's data into a new block.
179 */
180 2225140 void BlockItem::MakeDataCopy(const unsigned char *data, uint32_t size) {
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2225140 times.
2225140 assert(type_ == kBlockHollow);
182
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2225140 times.
2225140 assert(allocator_ != NULL);
183
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2225140 times.
2225140 assert(size > 0);
184
185 2225140 type_ = kBlockData;
186 2225140 capacity_ = size_ = size;
187 2225140 data_ = reinterpret_cast<unsigned char *>(allocator_->Malloc(capacity_));
188 2227139 memcpy(data_, data, size);
189 2227139 atomic_xadd64(&managed_bytes_, static_cast<int64_t>(capacity_));
190 2225570 }
191
192
193 347894 void BlockItem::Reset() {
194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 347894 times.
347894 assert(type_ == kBlockData);
195
196 347894 atomic_xadd64(&managed_bytes_, -static_cast<int64_t>(capacity_));
197 347901 allocator_->Free(data_);
198 347897 data_ = NULL;
199 347897 size_ = capacity_ = 0;
200 347897 type_ = kBlockHollow;
201 347897 }
202
203
204 2919680 void BlockItem::SetChunkItem(ChunkItem *value) {
205
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2919680 times.
2919680 assert(value != NULL);
206
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2919680 times.
2919680 assert(chunk_item_ == NULL);
207 2919680 chunk_item_ = value;
208 2919680 }
209
210
211 3780299 void BlockItem::SetFileItem(FileItem *value) {
212
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3780299 times.
3780299 assert(value != NULL);
213
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3780299 times.
3780299 assert(file_item_ == NULL);
214 3780299 file_item_ = value;
215 3780299 }
216
217
218 16384 uint32_t BlockItem::Write(void *buf, uint32_t count) {
219
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16384 times.
16384 assert(type_ == kBlockData);
220
221 16384 const uint32_t remaining = capacity_ - size_;
222 16384 const uint32_t nbytes = std::min(remaining, count);
223 16384 memcpy(data_ + size_, buf, nbytes);
224 16384 size_ += nbytes;
225 16384 return nbytes;
226 }
227