Directory: | cvmfs/ |
---|---|
File: | cvmfs/ingestion/item.cc |
Date: | 2025-04-20 02:34:28 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 147 | 147 | 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 | 253411 | FileItem::FileItem( | |
18 | IngestionSource* source, | ||
19 | uint64_t min_chunk_size, | ||
20 | uint64_t avg_chunk_size, | ||
21 | uint64_t max_chunk_size, | ||
22 | zlib::Algorithms compression_algorithm, | ||
23 | shash::Algorithms hash_algorithm, | ||
24 | shash::Suffix hash_suffix, | ||
25 | bool may_have_chunks, | ||
26 | 253411 | bool has_legacy_bulk_chunk) | |
27 | 253411 | : source_(source) | |
28 | 253411 | , compression_algorithm_(compression_algorithm) | |
29 | 253411 | , hash_algorithm_(hash_algorithm) | |
30 | 253411 | , hash_suffix_(hash_suffix) | |
31 | 253411 | , has_legacy_bulk_chunk_(has_legacy_bulk_chunk) | |
32 | 253411 | , size_(kSizeUnknown) | |
33 | 253411 | , may_have_chunks_(may_have_chunks) | |
34 |
1/2✓ Branch 1 taken 253411 times.
✗ Branch 2 not taken.
|
253411 | , chunk_detector_(min_chunk_size, avg_chunk_size, max_chunk_size) |
35 |
1/2✓ Branch 1 taken 253411 times.
✗ Branch 2 not taken.
|
253411 | , bulk_hash_(hash_algorithm) |
36 |
1/2✓ Branch 2 taken 253411 times.
✗ Branch 3 not taken.
|
506822 | , chunks_(1) |
37 | { | ||
38 | 253411 | int retval = pthread_mutex_init(&lock_, NULL); | |
39 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 253411 times.
|
253411 | assert(retval == 0); |
40 | 253411 | atomic_init64(&nchunks_in_fly_); | |
41 | 253411 | atomic_init32(&is_fully_chunked_); | |
42 | 253411 | } | |
43 | |||
44 | 253322 | FileItem::~FileItem() { | |
45 | 253322 | pthread_mutex_destroy(&lock_); | |
46 | 253306 | } | |
47 | |||
48 | 250530 | void FileItem::RegisterChunk(const FileChunk &file_chunk) { | |
49 | 250530 | MutexLockGuard lock_guard(lock_); | |
50 | |||
51 |
2/2✓ Branch 1 taken 441 times.
✓ Branch 2 taken 250089 times.
|
250530 | switch (file_chunk.content_hash().suffix) { |
52 | 441 | case shash::kSuffixPartial: | |
53 |
1/2✓ Branch 1 taken 441 times.
✗ Branch 2 not taken.
|
441 | chunks_.PushBack(file_chunk); |
54 | 441 | break; | |
55 | |||
56 | 250089 | default: | |
57 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 250089 times.
|
250089 | assert(file_chunk.offset() == 0); |
58 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 250089 times.
|
250089 | assert(file_chunk.size() == size_); |
59 | 250089 | bulk_hash_ = file_chunk.content_hash(); | |
60 | 250089 | break; | |
61 | } | ||
62 | 250530 | atomic_dec64(&nchunks_in_fly_); | |
63 | 250530 | } | |
64 | |||
65 | |||
66 | //------------------------------------------------------------------------------ | ||
67 | |||
68 | |||
69 | 255118 | ChunkItem::ChunkItem(FileItem *file_item, uint64_t offset) | |
70 | 255016 | : file_item_(file_item) | |
71 | 255016 | , offset_(offset) | |
72 | 255016 | , size_(0) | |
73 | 255016 | , is_bulk_chunk_(false) | |
74 | 255016 | , upload_handle_(NULL) | |
75 |
1/2✓ Branch 4 taken 255067 times.
✗ Branch 5 not taken.
|
255118 | , compressor_(NULL) |
76 | { | ||
77 | 255067 | hash_ctx_.algorithm = file_item->hash_algorithm(); | |
78 |
1/2✓ Branch 1 taken 254891 times.
✗ Branch 2 not taken.
|
254943 | hash_ctx_.size = shash::GetContextSize(hash_ctx_.algorithm); |
79 | 254891 | hash_ctx_.buffer = hash_ctx_buffer_; | |
80 |
1/2✓ Branch 1 taken 254817 times.
✗ Branch 2 not taken.
|
254891 | shash::Init(hash_ctx_); |
81 | 254817 | hash_value_.algorithm = hash_ctx_.algorithm; | |
82 | 254817 | hash_value_.suffix = shash::kSuffixPartial; | |
83 | 254817 | file_item_->IncNchunksInFly(); | |
84 | 255393 | } | |
85 | |||
86 | |||
87 | 249482 | void ChunkItem::MakeBulkChunk() { | |
88 | 249482 | is_bulk_chunk_ = true; | |
89 | 249482 | hash_value_.suffix = file_item_->hash_suffix(); | |
90 | 249431 | } | |
91 | |||
92 | |||
93 | 578498 | zlib::Compressor *ChunkItem::GetCompressor() { | |
94 |
2/2✓ Branch 1 taken 249615 times.
✓ Branch 2 taken 328951 times.
|
578498 | if (!compressor_.IsValid()) { |
95 | compressor_ = | ||
96 |
2/4✓ Branch 2 taken 250022 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 249932 times.
✗ Branch 6 not taken.
|
249615 | zlib::Compressor::Construct(file_item_->compression_algorithm()); |
97 | } | ||
98 | 578883 | return compressor_.weak_ref(); | |
99 | } | ||
100 | |||
101 | |||
102 | 249857 | void ChunkItem::ReleaseCompressor() { | |
103 | 249857 | compressor_.Destroy(); | |
104 | 249979 | } | |
105 | |||
106 | |||
107 | //------------------------------------------------------------------------------ | ||
108 | |||
109 | atomic_int64 BlockItem::managed_bytes_ = 0; | ||
110 | |||
111 | |||
112 | 4568 | BlockItem::BlockItem(ItemAllocator *allocator) | |
113 | 4568 | : allocator_(allocator) | |
114 | 4568 | , type_(kBlockHollow) | |
115 | 4568 | , tag_(-1) | |
116 | 4568 | , file_item_(NULL) | |
117 | 4568 | , chunk_item_(NULL) | |
118 | 4568 | , data_(NULL) | |
119 | 4568 | , capacity_(0) | |
120 | 4568 | , size_(0) | |
121 | 4568 | { } | |
122 | |||
123 | |||
124 | 1820295 | BlockItem::BlockItem(int64_t tag, ItemAllocator *allocator) | |
125 | 1811330 | : allocator_(allocator) | |
126 | 1811330 | , type_(kBlockHollow) | |
127 | 1811330 | , tag_(tag) | |
128 | 1811330 | , file_item_(NULL) | |
129 | 1811330 | , chunk_item_(NULL) | |
130 | 1811330 | , data_(NULL) | |
131 | 1811330 | , capacity_(0) | |
132 | 1820295 | , size_(0) | |
133 | { | ||
134 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1811330 times.
|
1811330 | assert(tag_ >= 0); |
135 | 1811330 | } | |
136 | |||
137 | |||
138 | 1818370 | BlockItem::~BlockItem() { | |
139 |
2/2✓ Branch 0 taken 766155 times.
✓ Branch 1 taken 1052215 times.
|
1818370 | if (data_) |
140 | 766155 | allocator_->Free(data_); | |
141 | 1819447 | atomic_xadd64(&managed_bytes_, -static_cast<int64_t>(capacity_)); | |
142 | 1831672 | } | |
143 | |||
144 | |||
145 | 275581 | void BlockItem::Discharge() { | |
146 | 275581 | data_ = NULL; | |
147 | 275581 | size_ = capacity_ = 0; | |
148 | 275581 | } | |
149 | |||
150 | |||
151 | 751011 | void BlockItem::MakeStop() { | |
152 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 751011 times.
|
751011 | assert(type_ == kBlockHollow); |
153 | 751011 | type_ = kBlockStop; | |
154 | 751011 | } | |
155 | |||
156 | |||
157 | 386463 | void BlockItem::MakeData(uint32_t capacity) { | |
158 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 386463 times.
|
386463 | assert(type_ == kBlockHollow); |
159 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 386463 times.
|
386463 | assert(allocator_ != NULL); |
160 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 386463 times.
|
386463 | assert(capacity > 0); |
161 | |||
162 | 386463 | type_ = kBlockData; | |
163 | 386463 | capacity_ = capacity; | |
164 | 386463 | data_ = reinterpret_cast<unsigned char *>(allocator_->Malloc(capacity_)); | |
165 | 387683 | atomic_xadd64(&managed_bytes_, static_cast<int64_t>(capacity_)); | |
166 | 387769 | } | |
167 | |||
168 | |||
169 | /** | ||
170 | * Move data from one block to another. | ||
171 | */ | ||
172 | 275500 | void BlockItem::MakeDataMove(BlockItem *other) { | |
173 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 275500 times.
|
275500 | assert(type_ == kBlockHollow); |
174 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 275500 times.
|
275500 | assert(other->type_ == kBlockData); |
175 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 275500 times.
|
275500 | assert(other->size_ > 0); |
176 | |||
177 | 275500 | type_ = kBlockData; | |
178 | 275500 | capacity_ = size_ = other->size_; | |
179 | 275500 | data_ = other->data_; | |
180 | 275500 | allocator_ = other->allocator_; | |
181 | |||
182 | 275500 | other->Discharge(); | |
183 | 275523 | } | |
184 | |||
185 | |||
186 | /** | ||
187 | * Copy a piece of one block's data into a new block. | ||
188 | */ | ||
189 | 416424 | void BlockItem::MakeDataCopy( | |
190 | const unsigned char *data, | ||
191 | uint32_t size) | ||
192 | { | ||
193 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 416424 times.
|
416424 | assert(type_ == kBlockHollow); |
194 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 416424 times.
|
416424 | assert(allocator_ != NULL); |
195 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 416424 times.
|
416424 | assert(size > 0); |
196 | |||
197 | 416424 | type_ = kBlockData; | |
198 | 416424 | capacity_ = size_ = size; | |
199 | 416424 | data_ = reinterpret_cast<unsigned char *>(allocator_->Malloc(capacity_)); | |
200 | 417126 | memcpy(data_, data, size); | |
201 | 417126 | atomic_xadd64(&managed_bytes_, static_cast<int64_t>(capacity_)); | |
202 | 417060 | } | |
203 | |||
204 | |||
205 | 37401 | void BlockItem::Reset() { | |
206 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 37401 times.
|
37401 | assert(type_ == kBlockData); |
207 | |||
208 | 37401 | atomic_xadd64(&managed_bytes_, -static_cast<int64_t>(capacity_)); | |
209 | 37406 | allocator_->Free(data_); | |
210 | 37400 | data_ = NULL; | |
211 | 37400 | size_ = capacity_ = 0; | |
212 | 37400 | type_ = kBlockHollow; | |
213 | 37400 | } | |
214 | |||
215 | |||
216 | 1257859 | void BlockItem::SetChunkItem(ChunkItem *value) { | |
217 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1257859 times.
|
1257859 | assert(value != NULL); |
218 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1257859 times.
|
1257859 | assert(chunk_item_ == NULL); |
219 | 1257859 | chunk_item_ = value; | |
220 | 1257859 | } | |
221 | |||
222 | |||
223 | 1806759 | void BlockItem::SetFileItem(FileItem *value) { | |
224 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1806759 times.
|
1806759 | assert(value != NULL); |
225 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1806759 times.
|
1806759 | assert(file_item_ == NULL); |
226 | 1806759 | file_item_ = value; | |
227 | 1806759 | } | |
228 | |||
229 | |||
230 | 512 | uint32_t BlockItem::Write(void *buf, uint32_t count) { | |
231 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 512 times.
|
512 | assert(type_ == kBlockData); |
232 | |||
233 | 512 | uint32_t remaining = capacity_ - size_; | |
234 | 512 | uint32_t nbytes = std::min(remaining, count); | |
235 | 512 | memcpy(data_ + size_, buf, nbytes); | |
236 | 512 | size_ += nbytes; | |
237 | 512 | return nbytes; | |
238 | } | ||
239 |