Skip to content

Commit

Permalink
Rename Attributes to EntryMetadata (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
koolkdev authored Nov 19, 2024
1 parent 5fa95f3 commit 6a9e85a
Show file tree
Hide file tree
Showing 19 changed files with 135 additions and 135 deletions.
2 changes: 1 addition & 1 deletion include/wfslib/directory.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Directory : public Entry, public std::enable_shared_from_this<Directory> {
using iterator = DirectoryIterator;

// TODO: Replace name with tree iterator?
Directory(std::string name, AttributesRef attributes, std::shared_ptr<QuotaArea> quota, std::shared_ptr<Block> block);
Directory(std::string name, MetadataRef metadata, std::shared_ptr<QuotaArea> quota, std::shared_ptr<Block> block);

std::expected<std::shared_ptr<Entry>, WfsError> GetEntry(const std::string& name) const;
std::expected<std::shared_ptr<Directory>, WfsError> GetDirectory(const std::string& name) const;
Expand Down
26 changes: 13 additions & 13 deletions include/wfslib/entry.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,29 @@

class QuotaArea;

using AttributesRef = Block::DataRef<Attributes>;

class Entry {
public:
Entry(std::string name, AttributesRef block);
using MetadataRef = Block::DataRef<EntryMetadata>;

Entry(std::string name, MetadataRef block);
virtual ~Entry();

const std::string& name() const { return name_; }
bool is_directory() const { return !attributes()->is_link() && attributes()->is_directory(); }
bool is_file() const { return !attributes()->is_link() && !attributes()->is_directory(); }
bool is_link() const { return attributes()->is_link(); }
bool is_quota() const { return attributes()->is_directory() && attributes()->is_quota(); }
bool is_directory() const { return !metadata()->is_link() && metadata()->is_directory(); }
bool is_file() const { return !metadata()->is_link() && !metadata()->is_directory(); }
bool is_link() const { return metadata()->is_link(); }
bool is_quota() const { return metadata()->is_directory() && metadata()->is_quota(); }

static std::expected<std::shared_ptr<Entry>, WfsError> Load(std::shared_ptr<QuotaArea> quota,
std::string name,
AttributesRef attributes_ref);
MetadataRef metadata_ref);

protected:
// TODO: Attributes copy as it can change?
Attributes* mutable_attributes() { return attributes_.get_mutable(); }
const Attributes* attributes() const { return attributes_.get(); }
const std::shared_ptr<Block>& attributes_block() const { return attributes_.block; }
// TODO: Metadata copy as it can change?
EntryMetadata* mutable_metadata() { return metadata_.get_mutable(); }
const EntryMetadata* metadata() const { return metadata_.get(); }
const std::shared_ptr<Block>& metadata_block() const { return metadata_.block; }

std::string name_;
AttributesRef attributes_;
MetadataRef metadata_;
};
4 changes: 2 additions & 2 deletions include/wfslib/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class File : public Entry, public std::enable_shared_from_this<File> {
class DataCategory3Reader;
class DataCategory4Reader;

File(std::string name, AttributesRef attributes, std::shared_ptr<QuotaArea> quota)
: Entry(std::move(name), std::move(attributes)), quota_(std::move(quota)) {}
File(std::string name, MetadataRef metadata, std::shared_ptr<QuotaArea> quota)
: Entry(std::move(name), std::move(metadata)), quota_(std::move(quota)) {}

uint32_t Size() const;
uint32_t SizeOnDisk() const;
Expand Down
4 changes: 2 additions & 2 deletions include/wfslib/link.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class QuotaArea;

class Link : public Entry, public std::enable_shared_from_this<Link> {
public:
Link(std::string name, AttributesRef attributes, std::shared_ptr<QuotaArea> quota)
: Entry(std::move(name), std::move(attributes)), quota_(std::move(quota)) {}
Link(std::string name, MetadataRef metadata, std::shared_ptr<QuotaArea> quota)
: Entry(std::move(name), std::move(metadata)), quota_(std::move(quota)) {}

private:
// TODO: We may have cyclic reference here if we do cache in area.
Expand Down
4 changes: 2 additions & 2 deletions src/directory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
#include "structs.h"

Directory::Directory(std::string name,
AttributesRef attributes,
MetadataRef metadata,
std::shared_ptr<QuotaArea> quota,
std::shared_ptr<Block> block)
: Entry(std::move(name), std::move(attributes)),
: Entry(std::move(name), std::move(metadata)),
quota_(std::move(quota)),
block_(std::move(block)),
map_{quota_, block_} {}
Expand Down
4 changes: 2 additions & 2 deletions src/directory_iterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ DirectoryIterator::DirectoryIterator(DirectoryMapIterator base) : base_(base) {}

DirectoryIterator::reference DirectoryIterator::operator*() const {
auto val = *base_;
auto name = val.attributes.get()->GetCaseSensitiveName(val.name);
return {name, Entry::Load(base_.quota(), name, val.attributes)};
auto name = val.metadata.get()->GetCaseSensitiveName(val.name);
return {name, Entry::Load(base_.quota(), name, val.metadata)};
}

DirectoryIterator& DirectoryIterator::operator++() {
Expand Down
8 changes: 4 additions & 4 deletions src/directory_leaf_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ void DirectoryLeafTree::Init(bool is_root) {
void DirectoryLeafTree::copy_value(DirectoryTree& new_tree,
parent_node& new_node,
dir_leaf_tree_value_type value) const {
Block::RawDataRef<Attributes> attributes{block().get(), value};
auto size = 1 << attributes.get()->entry_log2_size.value();
Block::RawDataRef<EntryMetadata> metadata{block().get(), value};
auto size = 1 << metadata.get()->metadata_log2_size.value();
auto new_offset = new_tree.Alloc(static_cast<uint16_t>(size));
assert(new_offset.has_value());
Block::RawDataRef<Attributes> new_attributes{new_tree.block().get(), *new_offset};
std::memcpy(new_attributes.get_mutable(), attributes.get(), size);
Block::RawDataRef<EntryMetadata> new_metadata{new_tree.block().get(), *new_offset};
std::memcpy(new_metadata.get_mutable(), metadata.get(), size);
new_node.set_leaf(*new_offset);
}
12 changes: 6 additions & 6 deletions src/directory_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ size_t DirectoryMap::CalcSizeOfDirectoryBlock(std::shared_ptr<Block> block) cons
}
};

bool DirectoryMap::insert(std::string_view name, const Attributes* attributes) {
bool DirectoryMap::insert(std::string_view name, const EntryMetadata* metadata) {
auto it = find(name);
if (!it.is_end()) {
// Already in tree
Expand All @@ -83,12 +83,12 @@ bool DirectoryMap::insert(std::string_view name, const Attributes* attributes) {
auto parents = it.parents();
auto leaf_tree = it.leaf().node;
while (true) {
auto size = static_cast<uint16_t>(1 << attributes->entry_log2_size.value());
auto size = static_cast<uint16_t>(1 << metadata->metadata_log2_size.value());
auto new_offset = leaf_tree.Alloc(size);
if (new_offset.has_value()) {
Block::RawDataRef<Attributes> new_attributes{leaf_tree.block().get(), *new_offset};
Block::RawDataRef<EntryMetadata> new_metadata{leaf_tree.block().get(), *new_offset};
if (leaf_tree.insert({name | std::ranges::to<std::string>(), *new_offset})) {
std::memcpy(new_attributes.get_mutable(), attributes, size);
std::memcpy(new_metadata.get_mutable(), metadata, size);
return true;
}
leaf_tree.Free(*new_offset, size);
Expand All @@ -104,9 +104,9 @@ bool DirectoryMap::erase(std::string_view name) {
return false;
}
auto parents = it.parents();
// Free the entry attributes first
// Free the entry metadata first
it.leaf().node.Free((*it.leaf().iterator).value(),
static_cast<uint16_t>(1 << (*it).attributes->entry_log2_size.value()));
static_cast<uint16_t>(1 << (*it).metadata->metadata_log2_size.value()));
it.leaf().node.erase(it.leaf().iterator);
bool last_empty = it.leaf().node.empty();
if (!last_empty)
Expand Down
2 changes: 1 addition & 1 deletion src/directory_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class DirectoryMap {

iterator find(std::string_view key) const;

bool insert(std::string_view name, const Attributes* attributes);
bool insert(std::string_view name, const EntryMetadata* metadata);
bool erase(std::string_view name);

void Init();
Expand Down
4 changes: 2 additions & 2 deletions src/directory_map_iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
#include "directory_parent_tree.h"

class QuotaArea;
struct Attributes;
struct EntryMetadata;

struct DiretoryMapEntry {
std::string name;
Block::DataRef<Attributes> attributes;
Block::DataRef<EntryMetadata> metadata;
};

class DirectoryMapIterator {
Expand Down
29 changes: 14 additions & 15 deletions src/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,34 @@
#include "link.h"
#include "quota_area.h"

Entry::Entry(std::string name, AttributesRef attributes) : name_(std::move(name)), attributes_(std::move(attributes)) {}
Entry::Entry(std::string name, MetadataRef metadata) : name_(std::move(name)), metadata_(std::move(metadata)) {}

Entry::~Entry() = default;

// static
std::expected<std::shared_ptr<Entry>, WfsError> Entry::Load(std::shared_ptr<QuotaArea> quota,
std::string name,
AttributesRef attributes_ref) {
auto* attributes = attributes_ref.get();
if (attributes->is_link()) {
// TODO, I think that the link info is in the attributes metadata
return std::make_shared<Link>(std::move(name), std::move(attributes_ref), std::move(quota));
} else if (attributes->is_directory()) {
if (attributes->flags.value() & attributes->Flags::QUOTA) {
MetadataRef metadata_ref) {
auto* metadata = metadata_ref.get();
if (metadata->is_link()) {
// TODO, I think that the link info is in the metadata metadata
return std::make_shared<Link>(std::move(name), std::move(metadata_ref), std::move(quota));
} else if (metadata->is_directory()) {
if (metadata->flags.value() & metadata->Flags::QUOTA) {
// The directory is quota, aka new area
auto block_size = BlockSize::Physical;
if (!(attributes->flags.value() & attributes->Flags::AREA_SIZE_BASIC) &&
(attributes->flags.value() & attributes->Flags::AREA_SIZE_REGULAR))
if (!(metadata->flags.value() & metadata->Flags::AREA_SIZE_BASIC) &&
(metadata->flags.value() & metadata->Flags::AREA_SIZE_REGULAR))
block_size = BlockSize::Logical;
auto new_quota = quota->LoadQuotaArea(attributes->directory_block_number.value(), block_size);
auto new_quota = quota->LoadQuotaArea(metadata->directory_block_number.value(), block_size);
if (!new_quota.has_value())
return std::unexpected(new_quota.error());
return (*new_quota)->LoadRootDirectory(std::move(name), std::move(attributes_ref));
return (*new_quota)->LoadRootDirectory(std::move(name), std::move(metadata_ref));
} else {
return quota->LoadDirectory(attributes->directory_block_number.value(), std::move(name),
std::move(attributes_ref));
return quota->LoadDirectory(metadata->directory_block_number.value(), std::move(name), std::move(metadata_ref));
}
} else {
// IsFile()
return std::make_shared<File>(std::move(name), std::move(attributes_ref), std::move(quota));
return std::make_shared<File>(std::move(name), std::move(metadata_ref), std::move(quota));
}
}
46 changes: 23 additions & 23 deletions src/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ struct FileDataChunkInfo {
};

uint32_t File::Size() const {
return attributes()->file_size.value();
return metadata()->file_size.value();
}

uint32_t File::SizeOnDisk() const {
return attributes()->size_on_disk.value();
return metadata()->size_on_disk.value();
}

class File::DataCategoryReader {
Expand Down Expand Up @@ -52,16 +52,16 @@ class File::DataCategoryReader {
std::shared_ptr<File> file_;

size_t GetAttributesMetadataOffset() const {
return file_->attributes_block()->to_offset(file_->attributes()) + file_->attributes()->size();
return file_->metadata_block()->to_offset(file_->metadata()) + file_->metadata()->size();
}
size_t GetAttributesMetadataEndOffset() const {
return file_->attributes_block()->to_offset(file_->attributes()) +
align_to_power_of_2(file_->attributes()->size() + GetAttributesMetadataSize());
return file_->metadata_block()->to_offset(file_->metadata()) +
align_to_power_of_2(file_->metadata()->size() + GetAttributesMetadataSize());
}
const std::byte* GetAttributesMetadataEnd() const {
// We can't do get_object<std::byte> because it might point to data.end(), so in debug it will cause an
// error
return file_->attributes_block()->data().data() + GetAttributesMetadataEndOffset();
return file_->metadata_block()->data().data() + GetAttributesMetadataEndOffset();
}
};

Expand All @@ -70,15 +70,15 @@ class File::DataCategory0Reader : public File::DataCategoryReader {
public:
DataCategory0Reader(const std::shared_ptr<File>& file) : DataCategoryReader(file) {}

virtual size_t GetAttributesMetadataSize() const { return file_->attributes()->size_on_disk.value(); }
virtual size_t GetAttributesMetadataSize() const { return file_->metadata()->size_on_disk.value(); }

virtual FileDataChunkInfo GetFileDataChunkInfo(size_t offset, size_t size) {
return FileDataChunkInfo{file_->attributes_block(), GetAttributesMetadataOffset() + offset, size};
return FileDataChunkInfo{file_->metadata_block(), GetAttributesMetadataOffset() + offset, size};
}

virtual void Resize(size_t new_size) {
// Just update the attribute, the data in the metadata block
file_->mutable_attributes()->file_size = static_cast<uint32_t>(new_size);
file_->mutable_metadata()->file_size = static_cast<uint32_t>(new_size);
}
};

Expand All @@ -88,27 +88,27 @@ class File::RegularDataCategoryReader : public File::DataCategoryReader {

virtual size_t GetAttributesMetadataSize() const {
// round up dividation
size_t data_blocks_count = ((file_->attributes()->size_on_disk.value() - 1) >> GetDataBlockSize()) + 1;
size_t data_blocks_count = ((file_->metadata()->size_on_disk.value() - 1) >> GetDataBlockSize()) + 1;
return sizeof(DataBlockMetadata) * data_blocks_count;
}

virtual FileDataChunkInfo GetFileDataChunkInfo(size_t offset, size_t size) {
auto blocks_list = reinterpret_cast<const DataBlockMetadata*>(GetAttributesMetadataEnd());
int64_t block_index = offset >> GetDataBlockSize();
size_t offset_in_block = offset & ((1 << GetDataBlockSize()) - 1);
auto hash_block = file_->attributes_block();
auto hash_block = file_->metadata_block();
uint32_t block_offset = static_cast<uint32_t>((offset >> GetDataBlockSize()) << GetDataBlockSize());

LoadDataBlock(blocks_list[-block_index - 1].block_number.value(),
static_cast<uint32_t>(
std::min(1U << GetDataBlockSize(), file_->attributes()->file_size.value() - block_offset)),
std::min(1U << GetDataBlockSize(), file_->metadata()->file_size.value() - block_offset)),
{hash_block, hash_block->to_offset(&blocks_list[-block_index - 1].hash)});
size = std::min(size, current_data_block->size() - offset_in_block);
return FileDataChunkInfo{current_data_block, offset_in_block, size};
}

virtual void Resize(size_t new_size) {
size_t old_size = file_->attributes()->file_size.value();
size_t old_size = file_->metadata()->file_size.value();
while (old_size != new_size) {
std::shared_ptr<Block> current_block;
size_t new_block_size = 0;
Expand Down Expand Up @@ -139,7 +139,7 @@ class File::RegularDataCategoryReader : public File::DataCategoryReader {
old_size += new_block_size;
}
}
file_->mutable_attributes()->file_size = static_cast<uint32_t>(old_size);
file_->mutable_metadata()->file_size = static_cast<uint32_t>(old_size);
if (current_block) {
current_block->Resize(static_cast<uint32_t>(new_block_size));
}
Expand All @@ -159,7 +159,7 @@ class File::RegularDataCategoryReader : public File::DataCategoryReader {
return;
auto block = file_->quota()->LoadDataBlock(block_number, static_cast<BlockSize>(file_->quota()->block_size_log2()),
GetBlocksLog2CountInDataBlock(), data_size, std::move(data_hash),
!(file_->attributes()->flags.value() & Attributes::UNENCRYPTED_FILE));
!(file_->metadata()->flags.value() & EntryMetadata::UNENCRYPTED_FILE));
if (!block.has_value())
throw WfsException(WfsError::kFileDataCorrupted);
current_data_block = std::move(*block);
Expand Down Expand Up @@ -194,13 +194,13 @@ class File::DataCategory3Reader : public File::DataCategory2Reader {
DataCategory3Reader(const std::shared_ptr<File>& file) : DataCategory2Reader(file) {}

virtual size_t GetAttributesMetadataSize() const {
size_t data_blocks_clusters_count = ((file_->attributes()->size_on_disk.value() - 1) >> ClusterDataLog2Size()) + 1;
size_t data_blocks_clusters_count = ((file_->metadata()->size_on_disk.value() - 1) >> ClusterDataLog2Size()) + 1;
return sizeof(DataBlocksClusterMetadata) * data_blocks_clusters_count;
}

virtual FileDataChunkInfo GetFileDataChunkInfo(size_t offset, size_t size) {
return GetFileDataChunkInfoFromClustersList(
offset, offset, size, file_->attributes_block(),
offset, offset, size, file_->metadata_block(),
reinterpret_cast<const DataBlocksClusterMetadata*>(GetAttributesMetadataEnd()), true);
}

Expand All @@ -225,7 +225,7 @@ class File::DataCategory3Reader : public File::DataCategory2Reader {
LoadDataBlock(cluster->block_number.value() +
static_cast<uint32_t>(block_index << log2_size(GetBlocksLog2CountInDataBlock())),
static_cast<uint32_t>(
std::min(1U << GetDataBlockSize(), file_->attributes()->file_size.value() - block_offset)),
std::min(1U << GetDataBlockSize(), file_->metadata()->file_size.value() - block_offset)),
{metadata_block, metadata_block->to_offset(&cluster->hash[block_index])});
size = std::min(size, current_data_block->size() - offset_in_block);
return FileDataChunkInfo{current_data_block, offset_in_block, size};
Expand All @@ -242,7 +242,7 @@ class File::DataCategory4Reader : public File::DataCategory3Reader {
DataCategory4Reader(const std::shared_ptr<File>& file) : DataCategory3Reader(file) {}

virtual size_t GetAttributesMetadataSize() const {
size_t data_blocks_clusters_count = ((file_->attributes()->size_on_disk.value() - 1) >> ClusterDataLog2Size()) + 1;
size_t data_blocks_clusters_count = ((file_->metadata()->size_on_disk.value() - 1) >> ClusterDataLog2Size()) + 1;
size_t blocks_count = ((data_blocks_clusters_count - 1) / ClustersInBlock()) + 1;
return sizeof(uint32_be_t) * blocks_count;
}
Expand Down Expand Up @@ -279,7 +279,7 @@ class File::DataCategory4Reader : public File::DataCategory3Reader {
};

std::shared_ptr<File::DataCategoryReader> File::CreateReader(std::shared_ptr<File> file) {
switch (file->attributes()->size_category.value()) {
switch (file->metadata()->size_category.value()) {
case 0:
return std::make_shared<DataCategory0Reader>(file);
case 1:
Expand All @@ -297,8 +297,8 @@ std::shared_ptr<File::DataCategoryReader> File::CreateReader(std::shared_ptr<Fil

void File::Resize(size_t new_size) {
// TODO: implment it, write now change up to size_on_disk without ever chaning size_on_disk
new_size = std::min(new_size, static_cast<size_t>(attributes_.get()->size_on_disk.value()));
size_t old_size = attributes_.get()->file_size.value();
new_size = std::min(new_size, static_cast<size_t>(metadata_.get()->size_on_disk.value()));
size_t old_size = metadata_.get()->file_size.value();
if (new_size != old_size) {
CreateReader(shared_from_this())->Resize(new_size);
}
Expand All @@ -307,7 +307,7 @@ void File::Resize(size_t new_size) {
File::file_device::file_device(const std::shared_ptr<File>& file) : file_(file), reader_(CreateReader(file)), pos_(0) {}

size_t File::file_device::size() const {
return file_->attributes()->file_size.value();
return file_->metadata()->file_size.value();
}

std::streamsize File::file_device::read(char_type* s, std::streamsize n) {
Expand Down
Loading

0 comments on commit 6a9e85a

Please sign in to comment.