blob: 9c995e8e8bef4b19456b9a53cc8ecbd7100dedab [file] [log] [blame]
// Copyright 2018 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef PERIDOT_BIN_LEDGER_P2P_SYNC_IMPL_COMMIT_BATCH_H_
#define PERIDOT_BIN_LEDGER_P2P_SYNC_IMPL_COMMIT_BATCH_H_
#include <list>
#include <string>
#include <vector>
#include <lib/fit/function.h>
#include <lib/fxl/memory/weak_ptr.h>
#include <lib/fxl/strings/string_view.h>
#include "peridot/bin/ledger/storage/public/page_storage.h"
namespace p2p_sync {
// CommitBatch holds all commits that should be added together in PageStorage.
class CommitBatch {
public:
// Delegate is used by |CommitBatch| to request missing commits.
class Delegate {
public:
// Request missing commits from this batch. Commits will be added later
// through CommitBatch::AddBatch.
virtual void RequestCommits(fxl::StringView device,
std::vector<storage::CommitId> ids) = 0;
};
CommitBatch(std::string device, Delegate* delegate,
storage::PageStorage* storage);
CommitBatch(const CommitBatch&) = delete;
const CommitBatch& operator=(const CommitBatch&) = delete;
// Registers a callback to be called when the batch processing is completed,
// either through success or an unrecoverable error. Part of the
// callback::AutoCleanable* client API.
void set_on_empty(fit::closure on_empty);
// Adds the provided commits to this batch.
// This method will attempt to add the whole batch to |PageStorage|, and may
// request additional commits through the |Delegate| if needed.
void AddToBatch(
std::vector<storage::PageStorage::CommitIdAndBytes> new_commits);
private:
std::string const device_;
Delegate* const delegate_;
storage::PageStorage* const storage_;
std::list<storage::PageStorage::CommitIdAndBytes> commits_;
fit::closure on_empty_;
// This must be the last member of the class.
fxl::WeakPtrFactory<CommitBatch> weak_factory_;
};
} // namespace p2p_sync
#endif // PERIDOT_BIN_LEDGER_P2P_SYNC_IMPL_COMMIT_BATCH_H_