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