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