Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f0d9beb1 authored by Daniel Rosenberg's avatar Daniel Rosenberg
Browse files

libsnapshot: Cleanup iterators

The libsnapshot iterators can all use the same interface instead of
duplicating the interface. We don't have any need for the internal class
variable iterators, so remove them.

Test: Builds, does not impact cow_api_test/cow_snapuserd_test
Change-Id: I5f008401e067a55a57812b7bf101a472ad97df18
parent 83127b73
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -432,11 +432,11 @@ class CowOpIter final : public ICowOpIter {

CowOpIter::CowOpIter(std::shared_ptr<std::vector<CowOperation>>& ops) {
    ops_ = ops;
    op_iter_ = ops_.get()->begin();
    op_iter_ = ops_->begin();
}

bool CowOpIter::Done() {
    return op_iter_ == ops_.get()->end();
    return op_iter_ == ops_->end();
}

void CowOpIter::Next() {
@@ -449,7 +449,7 @@ const CowOperation& CowOpIter::Get() {
    return (*op_iter_);
}

class CowOpReverseIter final : public ICowOpReverseIter {
class CowOpReverseIter final : public ICowOpIter {
  public:
    explicit CowOpReverseIter(std::shared_ptr<std::vector<CowOperation>> ops);

@@ -485,7 +485,7 @@ std::unique_ptr<ICowOpIter> CowReader::GetOpIter() {
    return std::make_unique<CowOpIter>(ops_);
}

std::unique_ptr<ICowOpReverseIter> CowReader::GetRevOpIter() {
std::unique_ptr<ICowOpIter> CowReader::GetRevOpIter() {
    return std::make_unique<CowOpReverseIter>(ops_);
}

+2 −18
Original line number Diff line number Diff line
@@ -27,7 +27,6 @@ namespace android {
namespace snapshot {

class ICowOpIter;
class ICowOpReverseIter;

// A ByteSink object handles requests for a buffer of a specific size. It
// always owns the underlying buffer. It's designed to minimize potential
@@ -76,7 +75,7 @@ class ICowReader {
    virtual std::unique_ptr<ICowOpIter> GetOpIter() = 0;

    // Return an reverse iterator for retrieving CowOperation entries.
    virtual std::unique_ptr<ICowOpReverseIter> GetRevOpIter() = 0;
    virtual std::unique_ptr<ICowOpIter> GetRevOpIter() = 0;

    // Get decoded bytes from the data section, handling any decompression.
    // All retrieved data is passed to the sink.
@@ -98,21 +97,6 @@ class ICowOpIter {
    virtual void Next() = 0;
};

// Reverse Iterate over a sequence of COW operations.
class ICowOpReverseIter {
  public:
    virtual ~ICowOpReverseIter() {}

    // True if there are more items to read, false otherwise.
    virtual bool Done() = 0;

    // Read the current operation.
    virtual const CowOperation& Get() = 0;

    // Advance to the next item.
    virtual void Next() = 0;
};

class CowReader : public ICowReader {
  public:
    CowReader();
@@ -135,7 +119,7 @@ class CowReader : public ICowReader {
    // whose lifetime depends on the CowOpIter object; the return
    // value of these will never be null.
    std::unique_ptr<ICowOpIter> GetOpIter() override;
    std::unique_ptr<ICowOpReverseIter> GetRevOpIter() override;
    std::unique_ptr<ICowOpIter> GetRevOpIter() override;

    bool ReadData(const CowOperation& op, IByteSink* sink) override;

+12 −12
Original line number Diff line number Diff line
@@ -361,7 +361,7 @@ bool Snapuserd::ReadMetadata() {
    }

    // Initialize the iterator for reading metadata
    cowop_riter_ = reader_->GetRevOpIter();
    std::unique_ptr<ICowOpIter> cowop_riter = reader_->GetRevOpIter();

    exceptions_per_area_ = (CHUNK_SIZE << SECTOR_SHIFT) / sizeof(struct disk_exception);

@@ -379,13 +379,13 @@ bool Snapuserd::ReadMetadata() {
    // this memset will ensure that metadata read is completed.
    memset(de_ptr.get(), 0, (exceptions_per_area_ * sizeof(struct disk_exception)));

    while (!cowop_riter_->Done()) {
        const CowOperation* cow_op = &cowop_riter_->Get();
    while (!cowop_riter->Done()) {
        const CowOperation* cow_op = &cowop_riter->Get();
        struct disk_exception* de =
                reinterpret_cast<struct disk_exception*>((char*)de_ptr.get() + offset);

        if (IsMetadataOp(*cow_op)) {
            cowop_riter_->Next();
            cowop_riter->Next();
            continue;
        }

@@ -415,7 +415,7 @@ bool Snapuserd::ReadMetadata() {
        chunk_vec_.push_back(std::make_pair(ChunkToSector(data_chunk_id), cow_op));
        num_ops += 1;
        offset += sizeof(struct disk_exception);
        cowop_riter_->Next();
        cowop_riter->Next();

        SNAP_LOG(DEBUG) << num_ops << ":"
                        << " Old-chunk: " << de->old_chunk << " New-chunk: " << de->new_chunk;
@@ -432,7 +432,7 @@ bool Snapuserd::ReadMetadata() {
                                                 sizeof(struct disk_exception));
            memset(de_ptr.get(), 0, (exceptions_per_area_ * sizeof(struct disk_exception)));

            if (cowop_riter_->Done()) {
            if (cowop_riter->Done()) {
                vec_.push_back(std::move(de_ptr));
            }
        }
@@ -450,11 +450,11 @@ bool Snapuserd::ReadMetadata() {
    SNAP_LOG(DEBUG) << " Processing copy-ops at Area: " << vec_.size()
                    << " Number of replace/zero ops completed in this area: " << num_ops
                    << " Pending copy ops for this area: " << pending_copy_ops;
    while (!cowop_riter_->Done()) {
    while (!cowop_riter->Done()) {
        do {
            const CowOperation* cow_op = &cowop_riter_->Get();
            const CowOperation* cow_op = &cowop_riter->Get();
            if (IsMetadataOp(*cow_op)) {
                cowop_riter_->Next();
                cowop_riter->Next();
                continue;
            }

@@ -572,8 +572,8 @@ bool Snapuserd::ReadMetadata() {
            map[cow_op->new_block] = cow_op;
            dest_blocks.insert(cow_op->source);
            prev_id = cow_op->new_block;
            cowop_riter_->Next();
        } while (!cowop_riter_->Done() && pending_copy_ops);
            cowop_riter->Next();
        } while (!cowop_riter->Done() && pending_copy_ops);

        data_chunk_id = GetNextAllocatableChunkId(data_chunk_id);
        SNAP_LOG(DEBUG) << "Batch Merge copy-ops of size: " << map.size()
@@ -611,7 +611,7 @@ bool Snapuserd::ReadMetadata() {
                                                     sizeof(struct disk_exception));
                memset(de_ptr.get(), 0, (exceptions_per_area_ * sizeof(struct disk_exception)));

                if (cowop_riter_->Done()) {
                if (cowop_riter->Done()) {
                    vec_.push_back(std::move(de_ptr));
                    SNAP_LOG(DEBUG) << "ReadMetadata() completed; Number of Areas: " << vec_.size();
                }
+0 −2
Original line number Diff line number Diff line
@@ -306,8 +306,6 @@ class Snapuserd : public std::enable_shared_from_this<Snapuserd> {
    uint32_t exceptions_per_area_;
    uint64_t num_sectors_;

    std::unique_ptr<ICowOpIter> cowop_iter_;
    std::unique_ptr<ICowOpReverseIter> cowop_riter_;
    std::unique_ptr<CowReader> reader_;

    // Vector of disk exception which is a