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