Directory: | cvmfs/ |
---|---|
File: | cvmfs/ingestion/item.cc |
Date: | 2025-02-02 02:34:22 |
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 | 253259 | 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 | 253259 | bool has_legacy_bulk_chunk) | |
27 | 253259 | : source_(source) | |
28 | 253259 | , compression_algorithm_(compression_algorithm) | |
29 | 253259 | , hash_algorithm_(hash_algorithm) | |
30 | 253259 | , hash_suffix_(hash_suffix) | |
31 | 253259 | , has_legacy_bulk_chunk_(has_legacy_bulk_chunk) | |
32 | 253259 | , size_(kSizeUnknown) | |
33 | 253259 | , may_have_chunks_(may_have_chunks) | |
34 |
1/2✓ Branch 1 taken 253259 times.
✗ Branch 2 not taken.
|
253259 | , chunk_detector_(min_chunk_size, avg_chunk_size, max_chunk_size) |
35 |
1/2✓ Branch 1 taken 253259 times.
✗ Branch 2 not taken.
|
253259 | , bulk_hash_(hash_algorithm) |
36 |
1/2✓ Branch 2 taken 253259 times.
✗ Branch 3 not taken.
|
506518 | , chunks_(1) |
37 | { | ||
38 | 253259 | int retval = pthread_mutex_init(&lock_, NULL); | |
39 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 253259 times.
|
253259 | assert(retval == 0); |
40 | 253259 | atomic_init64(&nchunks_in_fly_); | |
41 | 253259 | atomic_init32(&is_fully_chunked_); | |
42 | 253259 | } | |
43 | |||
44 | 253116 | FileItem::~FileItem() { | |
45 | 253116 | pthread_mutex_destroy(&lock_); | |
46 | 253087 | } | |
47 | |||
48 | 250522 | void FileItem::RegisterChunk(const FileChunk &file_chunk) { | |
49 | 250522 | MutexLockGuard lock_guard(lock_); | |
50 | |||
51 |
2/2✓ Branch 1 taken 441 times.
✓ Branch 2 taken 250081 times.
|
250522 | 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 | 250081 | default: | |
57 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 250081 times.
|
250081 | assert(file_chunk.offset() == 0); |
58 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 250081 times.
|
250081 | assert(file_chunk.size() == size_); |
59 | 250081 | bulk_hash_ = file_chunk.content_hash(); | |
60 | 250081 | break; | |
61 | } | ||
62 | 250522 | atomic_dec64(&nchunks_in_fly_); | |
63 | 250522 | } | |
64 | |||
65 | |||
66 | //------------------------------------------------------------------------------ | ||
67 | |||
68 | |||
69 | 255094 | ChunkItem::ChunkItem(FileItem *file_item, uint64_t offset) | |
70 | 255091 | : file_item_(file_item) | |
71 | 255091 | , offset_(offset) | |
72 | 255091 | , size_(0) | |
73 | 255091 | , is_bulk_chunk_(false) | |
74 | 255091 | , upload_handle_(NULL) | |
75 |
1/2✓ Branch 4 taken 255161 times.
✗ Branch 5 not taken.
|
255094 | , compressor_(NULL) |
76 | { | ||
77 | 255161 | hash_ctx_.algorithm = file_item->hash_algorithm(); | |
78 |
1/2✓ Branch 1 taken 255008 times.
✗ Branch 2 not taken.
|
255057 | hash_ctx_.size = shash::GetContextSize(hash_ctx_.algorithm); |
79 | 255008 | hash_ctx_.buffer = hash_ctx_buffer_; | |
80 |
1/2✓ Branch 1 taken 254888 times.
✗ Branch 2 not taken.
|
255008 | shash::Init(hash_ctx_); |
81 | 254888 | hash_value_.algorithm = hash_ctx_.algorithm; | |
82 | 254888 | hash_value_.suffix = shash::kSuffixPartial; | |
83 | 254888 | file_item_->IncNchunksInFly(); | |
84 | 255385 | } | |
85 | |||
86 | |||
87 | 249560 | void ChunkItem::MakeBulkChunk() { | |
88 | 249560 | is_bulk_chunk_ = true; | |
89 | 249560 | hash_value_.suffix = file_item_->hash_suffix(); | |
90 | 249504 | } | |
91 | |||
92 | |||
93 | 577450 | zlib::Compressor *ChunkItem::GetCompressor() { | |
94 |
2/2✓ Branch 1 taken 249550 times.
✓ Branch 2 taken 328035 times.
|
577450 | if (!compressor_.IsValid()) { |
95 | compressor_ = | ||
96 |
2/4✓ Branch 2 taken 250073 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 250001 times.
✗ Branch 6 not taken.
|
249550 | zlib::Compressor::Construct(file_item_->compression_algorithm()); |
97 | } | ||
98 | 578036 | return compressor_.weak_ref(); | |
99 | } | ||
100 | |||
101 | |||
102 | 249911 | void ChunkItem::ReleaseCompressor() { | |
103 | 249911 | compressor_.Destroy(); | |
104 | 250061 | } | |
105 | |||
106 | |||
107 | //------------------------------------------------------------------------------ | ||
108 | |||
109 | atomic_int64 BlockItem::managed_bytes_ = 0; | ||
110 | |||
111 | |||
112 | 4440 | BlockItem::BlockItem(ItemAllocator *allocator) | |
113 | 4440 | : allocator_(allocator) | |
114 | 4440 | , type_(kBlockHollow) | |
115 | 4440 | , tag_(-1) | |
116 | 4440 | , file_item_(NULL) | |
117 | 4440 | , chunk_item_(NULL) | |
118 | 4440 | , data_(NULL) | |
119 | 4440 | , capacity_(0) | |
120 | 4440 | , size_(0) | |
121 | 4440 | { } | |
122 | |||
123 | |||
124 | 1817452 | BlockItem::BlockItem(int64_t tag, ItemAllocator *allocator) | |
125 | 1805744 | : allocator_(allocator) | |
126 | 1805744 | , type_(kBlockHollow) | |
127 | 1805744 | , tag_(tag) | |
128 | 1805744 | , file_item_(NULL) | |
129 | 1805744 | , chunk_item_(NULL) | |
130 | 1805744 | , data_(NULL) | |
131 | 1805744 | , capacity_(0) | |
132 | 1817452 | , size_(0) | |
133 | { | ||
134 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1805744 times.
|
1805744 | assert(tag_ >= 0); |
135 | 1805744 | } | |
136 | |||
137 | |||
138 | 1812458 | BlockItem::~BlockItem() { | |
139 |
2/2✓ Branch 0 taken 766120 times.
✓ Branch 1 taken 1046338 times.
|
1812458 | if (data_) |
140 | 766120 | allocator_->Free(data_); | |
141 | 1813387 | atomic_xadd64(&managed_bytes_, -static_cast<int64_t>(capacity_)); | |
142 | 1828756 | } | |
143 | |||
144 | |||
145 | 275265 | void BlockItem::Discharge() { | |
146 | 275265 | data_ = NULL; | |
147 | 275265 | size_ = capacity_ = 0; | |
148 | 275265 | } | |
149 | |||
150 | |||
151 | 750853 | void BlockItem::MakeStop() { | |
152 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 750853 times.
|
750853 | assert(type_ == kBlockHollow); |
153 | 750853 | type_ = kBlockStop; | |
154 | 750853 | } | |
155 | |||
156 | |||
157 | 386393 | void BlockItem::MakeData(uint32_t capacity) { | |
158 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 386393 times.
|
386393 | assert(type_ == kBlockHollow); |
159 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 386393 times.
|
386393 | assert(allocator_ != NULL); |
160 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 386393 times.
|
386393 | assert(capacity > 0); |
161 | |||
162 | 386393 | type_ = kBlockData; | |
163 | 386393 | capacity_ = capacity; | |
164 | 386393 | data_ = reinterpret_cast<unsigned char *>(allocator_->Malloc(capacity_)); | |
165 | 387633 | atomic_xadd64(&managed_bytes_, static_cast<int64_t>(capacity_)); | |
166 | 387754 | } | |
167 | |||
168 | |||
169 | /** | ||
170 | * Move data from one block to another. | ||
171 | */ | ||
172 | 275182 | void BlockItem::MakeDataMove(BlockItem *other) { | |
173 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 275182 times.
|
275182 | assert(type_ == kBlockHollow); |
174 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 275182 times.
|
275182 | assert(other->type_ == kBlockData); |
175 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 275182 times.
|
275182 | assert(other->size_ > 0); |
176 | |||
177 | 275182 | type_ = kBlockData; | |
178 | 275182 | capacity_ = size_ = other->size_; | |
179 | 275182 | data_ = other->data_; | |
180 | 275182 | allocator_ = other->allocator_; | |
181 | |||
182 | 275182 | other->Discharge(); | |
183 | 275220 | } | |
184 | |||
185 | |||
186 | /** | ||
187 | * Copy a piece of one block's data into a new block. | ||
188 | */ | ||
189 | 416348 | void BlockItem::MakeDataCopy( | |
190 | const unsigned char *data, | ||
191 | uint32_t size) | ||
192 | { | ||
193 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 416348 times.
|
416348 | assert(type_ == kBlockHollow); |
194 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 416348 times.
|
416348 | assert(allocator_ != NULL); |
195 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 416348 times.
|
416348 | assert(size > 0); |
196 | |||
197 | 416348 | type_ = kBlockData; | |
198 | 416348 | capacity_ = size_ = size; | |
199 | 416348 | data_ = reinterpret_cast<unsigned char *>(allocator_->Malloc(capacity_)); | |
200 | 417049 | memcpy(data_, data, size); | |
201 | 417049 | atomic_xadd64(&managed_bytes_, static_cast<int64_t>(capacity_)); | |
202 | 416922 | } | |
203 | |||
204 | |||
205 | 37395 | void BlockItem::Reset() { | |
206 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 37395 times.
|
37395 | assert(type_ == kBlockData); |
207 | |||
208 | 37395 | atomic_xadd64(&managed_bytes_, -static_cast<int64_t>(capacity_)); | |
209 | 37406 | allocator_->Free(data_); | |
210 | 37406 | data_ = NULL; | |
211 | 37406 | size_ = capacity_ = 0; | |
212 | 37406 | type_ = kBlockHollow; | |
213 | 37406 | } | |
214 | |||
215 | |||
216 | 1255406 | void BlockItem::SetChunkItem(ChunkItem *value) { | |
217 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1255406 times.
|
1255406 | assert(value != NULL); |
218 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1255406 times.
|
1255406 | assert(chunk_item_ == NULL); |
219 | 1255406 | chunk_item_ = value; | |
220 | 1255406 | } | |
221 | |||
222 | |||
223 | 1801214 | void BlockItem::SetFileItem(FileItem *value) { | |
224 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1801214 times.
|
1801214 | assert(value != NULL); |
225 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1801214 times.
|
1801214 | assert(file_item_ == NULL); |
226 | 1801214 | file_item_ = value; | |
227 | 1801214 | } | |
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 |