| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/ingestion/item.cc |
| Date: | 2025-12-21 02:39:23 |
| 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 | 9861388 | 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 | 9861388 | bool has_legacy_bulk_chunk) | |
| 26 | 9861388 | : source_(source) | |
| 27 | 9861388 | , compression_algorithm_(compression_algorithm) | |
| 28 | 9861388 | , hash_algorithm_(hash_algorithm) | |
| 29 | 9861388 | , hash_suffix_(hash_suffix) | |
| 30 | 9861388 | , has_legacy_bulk_chunk_(has_legacy_bulk_chunk) | |
| 31 | 9861388 | , size_(kSizeUnknown) | |
| 32 | 9861388 | , may_have_chunks_(may_have_chunks) | |
| 33 |
1/2✓ Branch 1 taken 9861388 times.
✗ Branch 2 not taken.
|
9861388 | , chunk_detector_(min_chunk_size, avg_chunk_size, max_chunk_size) |
| 34 |
1/2✓ Branch 1 taken 9861388 times.
✗ Branch 2 not taken.
|
9861388 | , bulk_hash_(hash_algorithm) |
| 35 |
1/2✓ Branch 2 taken 9861388 times.
✗ Branch 3 not taken.
|
19722776 | , chunks_(1) { |
| 36 | 9861388 | const int retval = pthread_mutex_init(&lock_, NULL); | |
| 37 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9861388 times.
|
9861388 | assert(retval == 0); |
| 38 | 9861388 | atomic_init64(&nchunks_in_fly_); | |
| 39 | 9861388 | atomic_init32(&is_fully_chunked_); | |
| 40 | 9861388 | } | |
| 41 | |||
| 42 | 9858293 | FileItem::~FileItem() { pthread_mutex_destroy(&lock_); } | |
| 43 | |||
| 44 | 9770194 | void FileItem::RegisterChunk(const FileChunk &file_chunk) { | |
| 45 | 9770194 | const MutexLockGuard lock_guard(lock_); | |
| 46 | |||
| 47 |
2/2✓ Branch 1 taken 17295 times.
✓ Branch 2 taken 9752899 times.
|
9770194 | switch (file_chunk.content_hash().suffix) { |
| 48 | 17295 | case shash::kSuffixPartial: | |
| 49 |
1/2✓ Branch 1 taken 17295 times.
✗ Branch 2 not taken.
|
17295 | chunks_.PushBack(file_chunk); |
| 50 | 17295 | break; | |
| 51 | |||
| 52 | 9752899 | default: | |
| 53 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 9752899 times.
|
9752899 | assert(file_chunk.offset() == 0); |
| 54 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 9752899 times.
|
9752899 | assert(file_chunk.size() == size_); |
| 55 | 9752899 | bulk_hash_ = file_chunk.content_hash(); | |
| 56 | 9752899 | break; | |
| 57 | } | ||
| 58 | 9770194 | atomic_dec64(&nchunks_in_fly_); | |
| 59 | 9770194 | } | |
| 60 | |||
| 61 | |||
| 62 | //------------------------------------------------------------------------------ | ||
| 63 | |||
| 64 | |||
| 65 | 9967318 | ChunkItem::ChunkItem(FileItem *file_item, uint64_t offset) | |
| 66 | 9963925 | : file_item_(file_item) | |
| 67 | 9963925 | , offset_(offset) | |
| 68 | 9963925 | , size_(0) | |
| 69 | 9963925 | , is_bulk_chunk_(false) | |
| 70 | 9963925 | , upload_handle_(NULL) | |
| 71 |
1/2✓ Branch 4 taken 9966577 times.
✗ Branch 5 not taken.
|
9967318 | , compressor_(NULL) { |
| 72 | 9966577 | hash_ctx_.algorithm = file_item->hash_algorithm(); | |
| 73 |
1/2✓ Branch 1 taken 9963964 times.
✗ Branch 2 not taken.
|
9964666 | hash_ctx_.size = shash::GetContextSize(hash_ctx_.algorithm); |
| 74 | 9963964 | hash_ctx_.buffer = hash_ctx_buffer_; | |
| 75 |
1/2✓ Branch 1 taken 9961390 times.
✗ Branch 2 not taken.
|
9963964 | shash::Init(hash_ctx_); |
| 76 | 9961390 | hash_value_.algorithm = hash_ctx_.algorithm; | |
| 77 | 9961390 | hash_value_.suffix = shash::kSuffixPartial; | |
| 78 | 9961390 | file_item_->IncNchunksInFly(); | |
| 79 | 9975898 | } | |
| 80 | |||
| 81 | |||
| 82 | 9736528 | void ChunkItem::MakeBulkChunk() { | |
| 83 | 9736528 | is_bulk_chunk_ = true; | |
| 84 | 9736528 | hash_value_.suffix = file_item_->hash_suffix(); | |
| 85 | 9735358 | } | |
| 86 | |||
| 87 | |||
| 88 | 22559045 | zlib::Compressor *ChunkItem::GetCompressor() { | |
| 89 |
2/2✓ Branch 1 taken 9736561 times.
✓ Branch 2 taken 12825760 times.
|
22559045 | if (!compressor_.IsValid()) { |
| 90 | compressor_ = zlib::Compressor::Construct( | ||
| 91 |
2/4✓ Branch 2 taken 9754618 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 9753487 times.
✗ Branch 6 not taken.
|
9736561 | file_item_->compression_algorithm()); |
| 92 | } | ||
| 93 | 22579247 | return compressor_.weak_ref(); | |
| 94 | } | ||
| 95 | |||
| 96 | |||
| 97 | 9748729 | void ChunkItem::ReleaseCompressor() { compressor_.Destroy(); } | |
| 98 | |||
| 99 | |||
| 100 | //------------------------------------------------------------------------------ | ||
| 101 | |||
| 102 | atomic_int64 BlockItem::managed_bytes_ = 0; | ||
| 103 | |||
| 104 | |||
| 105 | 159328 | BlockItem::BlockItem(ItemAllocator *allocator) | |
| 106 | 159328 | : allocator_(allocator) | |
| 107 | 159328 | , type_(kBlockHollow) | |
| 108 | 159328 | , tag_(-1) | |
| 109 | 159328 | , file_item_(NULL) | |
| 110 | 159328 | , chunk_item_(NULL) | |
| 111 | 159328 | , data_(NULL) | |
| 112 | 159328 | , capacity_(0) | |
| 113 | 159328 | , size_(0) { } | |
| 114 | |||
| 115 | |||
| 116 | 71201337 | BlockItem::BlockItem(int64_t tag, ItemAllocator *allocator) | |
| 117 | 70896072 | : allocator_(allocator) | |
| 118 | 70896072 | , type_(kBlockHollow) | |
| 119 | 70896072 | , tag_(tag) | |
| 120 | 70896072 | , file_item_(NULL) | |
| 121 | 70896072 | , chunk_item_(NULL) | |
| 122 | 70896072 | , data_(NULL) | |
| 123 | 70896072 | , capacity_(0) | |
| 124 | 71201337 | , size_(0) { | |
| 125 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 70896072 times.
|
70896072 | assert(tag_ >= 0); |
| 126 | 70896072 | } | |
| 127 | |||
| 128 | |||
| 129 | 71028314 | BlockItem::~BlockItem() { | |
| 130 |
2/2✓ Branch 0 taken 30025766 times.
✓ Branch 1 taken 41002548 times.
|
71028314 | if (data_) |
| 131 | 30025766 | allocator_->Free(data_); | |
| 132 | 71067158 | atomic_xadd64(&managed_bytes_, -static_cast<int64_t>(capacity_)); | |
| 133 | 71561042 | } | |
| 134 | |||
| 135 | |||
| 136 | 10750867 | void BlockItem::Discharge() { | |
| 137 | 10750867 | data_ = NULL; | |
| 138 | 10750867 | size_ = capacity_ = 0; | |
| 139 | 10750867 | } | |
| 140 | |||
| 141 | |||
| 142 | 29336883 | void BlockItem::MakeStop() { | |
| 143 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 29336883 times.
|
29336883 | assert(type_ == kBlockHollow); |
| 144 | 29336883 | type_ = kBlockStop; | |
| 145 | 29336883 | } | |
| 146 | |||
| 147 | |||
| 148 | 15078811 | void BlockItem::MakeData(uint32_t capacity) { | |
| 149 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15078811 times.
|
15078811 | assert(type_ == kBlockHollow); |
| 150 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15078811 times.
|
15078811 | assert(allocator_ != NULL); |
| 151 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15078811 times.
|
15078811 | assert(capacity > 0); |
| 152 | |||
| 153 | 15078811 | type_ = kBlockData; | |
| 154 | 15078811 | capacity_ = capacity; | |
| 155 | 15078811 | data_ = reinterpret_cast<unsigned char *>(allocator_->Malloc(capacity_)); | |
| 156 | 15118162 | atomic_xadd64(&managed_bytes_, static_cast<int64_t>(capacity_)); | |
| 157 | 15122725 | } | |
| 158 | |||
| 159 | |||
| 160 | /** | ||
| 161 | * Move data from one block to another. | ||
| 162 | */ | ||
| 163 | 10751608 | void BlockItem::MakeDataMove(BlockItem *other) { | |
| 164 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10751608 times.
|
10751608 | assert(type_ == kBlockHollow); |
| 165 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10751608 times.
|
10751608 | assert(other->type_ == kBlockData); |
| 166 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10751608 times.
|
10751608 | assert(other->size_ > 0); |
| 167 | |||
| 168 | 10751608 | type_ = kBlockData; | |
| 169 | 10751608 | capacity_ = size_ = other->size_; | |
| 170 | 10751608 | data_ = other->data_; | |
| 171 | 10751608 | allocator_ = other->allocator_; | |
| 172 | |||
| 173 | 10751608 | other->Discharge(); | |
| 174 | 10749697 | } | |
| 175 | |||
| 176 | |||
| 177 | /** | ||
| 178 | * Copy a piece of one block's data into a new block. | ||
| 179 | */ | ||
| 180 | 16417312 | void BlockItem::MakeDataCopy(const unsigned char *data, uint32_t size) { | |
| 181 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16417312 times.
|
16417312 | assert(type_ == kBlockHollow); |
| 182 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16417312 times.
|
16417312 | assert(allocator_ != NULL); |
| 183 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16417312 times.
|
16417312 | assert(size > 0); |
| 184 | |||
| 185 | 16417312 | type_ = kBlockData; | |
| 186 | 16417312 | capacity_ = size_ = size; | |
| 187 | 16417312 | data_ = reinterpret_cast<unsigned char *>(allocator_->Malloc(capacity_)); | |
| 188 | 16441798 | memcpy(data_, data, size); | |
| 189 | 16441798 | atomic_xadd64(&managed_bytes_, static_cast<int64_t>(capacity_)); | |
| 190 | 16436362 | } | |
| 191 | |||
| 192 | |||
| 193 | 1488648 | void BlockItem::Reset() { | |
| 194 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1488648 times.
|
1488648 | assert(type_ == kBlockData); |
| 195 | |||
| 196 | 1488648 | atomic_xadd64(&managed_bytes_, -static_cast<int64_t>(capacity_)); | |
| 197 | 1488882 | allocator_->Free(data_); | |
| 198 | 1488726 | data_ = NULL; | |
| 199 | 1488726 | size_ = capacity_ = 0; | |
| 200 | 1488726 | type_ = kBlockHollow; | |
| 201 | 1488726 | } | |
| 202 | |||
| 203 | |||
| 204 | 49266247 | void BlockItem::SetChunkItem(ChunkItem *value) { | |
| 205 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 49266247 times.
|
49266247 | assert(value != NULL); |
| 206 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 49266247 times.
|
49266247 | assert(chunk_item_ == NULL); |
| 207 | 49266247 | chunk_item_ = value; | |
| 208 | 49266247 | } | |
| 209 | |||
| 210 | |||
| 211 | 70717116 | void BlockItem::SetFileItem(FileItem *value) { | |
| 212 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 70717116 times.
|
70717116 | assert(value != NULL); |
| 213 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 70717116 times.
|
70717116 | assert(file_item_ == NULL); |
| 214 | 70717116 | file_item_ = value; | |
| 215 | 70717116 | } | |
| 216 | |||
| 217 | |||
| 218 | 21504 | uint32_t BlockItem::Write(void *buf, uint32_t count) { | |
| 219 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21504 times.
|
21504 | assert(type_ == kBlockData); |
| 220 | |||
| 221 | 21504 | const uint32_t remaining = capacity_ - size_; | |
| 222 | 21504 | const uint32_t nbytes = std::min(remaining, count); | |
| 223 | 21504 | memcpy(data_ + size_, buf, nbytes); | |
| 224 | 21504 | size_ += nbytes; | |
| 225 | 21504 | return nbytes; | |
| 226 | } | ||
| 227 |