forked from stellar/stellar-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored checkValid with ReadOnlyState
- Loading branch information
Showing
5 changed files
with
248 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Copyright 2024 Stellar Development Foundation and contributors. Licensed | ||
// under the Apache License, Version 2.0. See the COPYING file at the root | ||
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
#include "ledger/ReadOnlyState.h" | ||
#include "bucket/BucketListSnapshot.h" | ||
#include "ledger/LedgerTxn.h" | ||
#include "util/GlobalChecks.h" | ||
|
||
namespace stellar | ||
{ | ||
|
||
LtxReadOnlyResult::LtxReadOnlyResult(LedgerTxnEntry&& entry) | ||
: mEntry(std::move(entry)) | ||
{ | ||
} | ||
|
||
ReadOnlyResultPtr | ||
LtxReadOnlyResult::create(LedgerTxnEntry&& entry) | ||
{ | ||
return ReadOnlyResultPtr(new LtxReadOnlyResult(std::move(entry))); | ||
} | ||
|
||
LedgerEntry const& | ||
LtxReadOnlyResult::entry() const | ||
{ | ||
return mEntry.current(); | ||
} | ||
|
||
bool | ||
LtxReadOnlyResult::isDead() const | ||
{ | ||
return !static_cast<bool>(mEntry); | ||
} | ||
|
||
BucketListReadOnlyResult::BucketListReadOnlyResult( | ||
std::shared_ptr<LedgerEntry> entry) | ||
: mEntry(entry) | ||
{ | ||
} | ||
|
||
ReadOnlyResultPtr | ||
BucketListReadOnlyResult::create(std::shared_ptr<LedgerEntry> entry) | ||
{ | ||
return ReadOnlyResultPtr(new BucketListReadOnlyResult(entry)); | ||
} | ||
|
||
LedgerEntry const& | ||
BucketListReadOnlyResult::entry() const | ||
{ | ||
releaseAssertOrThrow(mEntry); | ||
return *mEntry; | ||
} | ||
|
||
bool | ||
BucketListReadOnlyResult::isDead() const | ||
{ | ||
return !static_cast<bool>(mEntry); | ||
} | ||
|
||
BucketListReadOnlyState::BucketListReadOnlyState( | ||
SearchableBucketListSnapshot& snapshot) | ||
: mSnapshot(snapshot) | ||
{ | ||
} | ||
|
||
ReadOnlyResultPtr | ||
BucketListReadOnlyState::loadEntry(LedgerKey const& key) | ||
{ | ||
auto result = mSnapshot.getLedgerEntry(key); | ||
return BucketListReadOnlyResult::create(result); | ||
} | ||
|
||
LtxReadOnlyState::LtxReadOnlyState(AbstractLedgerTxn& ltx) : mLtx(ltx) | ||
{ | ||
releaseAssert(threadIsMain()); | ||
} | ||
|
||
ReadOnlyResultPtr | ||
LtxReadOnlyState::loadEntry(LedgerKey const& key) | ||
{ | ||
releaseAssert(threadIsMain()); | ||
auto result = mLtx.load(key); | ||
return LtxReadOnlyResult::create(std::move(result)); | ||
} | ||
|
||
AbstractLedgerTxn& | ||
LtxReadOnlyState::getLedgerTxn() | ||
{ | ||
return mLtx; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#pragma once | ||
|
||
// Copyright 2024 Stellar Development Foundation and contributors. Licensed | ||
// under the Apache License, Version 2.0. See the COPYING file at the root | ||
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
#include "ledger/LedgerTxnEntry.h" | ||
#include "util/NonCopyable.h" | ||
#include "util/types.h" | ||
|
||
#include <memory> | ||
|
||
namespace stellar | ||
{ | ||
|
||
class AbstractLedgerTxn; | ||
class SearchableBucketListSnapshot; | ||
class ReadOnlyResult; | ||
class ReadOnlyState; | ||
|
||
typedef std::shared_ptr<ReadOnlyResult> ReadOnlyResultPtr; | ||
|
||
// ReadOnlyResult is a generic wrapper for the result of loading a LedgerEntry | ||
// loaded either from a LedgerTxn or from a BucketList snapshot. | ||
class ReadOnlyResult | ||
{ | ||
public: | ||
virtual LedgerEntry const& entry() const = 0; | ||
virtual bool isDead() const = 0; | ||
}; | ||
|
||
class LtxReadOnlyResult : public ReadOnlyResult | ||
{ | ||
private: | ||
LedgerTxnEntry mEntry; | ||
LtxReadOnlyResult(LedgerTxnEntry&& entry); | ||
|
||
public: | ||
static ReadOnlyResultPtr create(LedgerTxnEntry&& entry); | ||
|
||
LedgerEntry const& entry() const override; | ||
bool isDead() const override; | ||
}; | ||
|
||
class BucketListReadOnlyResult : public ReadOnlyResult | ||
{ | ||
private: | ||
std::shared_ptr<LedgerEntry> mEntry; | ||
BucketListReadOnlyResult(std::shared_ptr<LedgerEntry> entry); | ||
|
||
public: | ||
static ReadOnlyResultPtr create(std::shared_ptr<LedgerEntry> entry); | ||
|
||
LedgerEntry const& entry() const override; | ||
bool isDead() const override; | ||
}; | ||
|
||
// ReadOnlyState is an interface for loading LedgerEntries from either a | ||
// LedgerTxn object or a SearchableBucketListSnapshot | ||
class ReadOnlyState | ||
{ | ||
public: | ||
virtual ReadOnlyResultPtr loadEntry(LedgerKey const& key) = 0; | ||
}; | ||
|
||
class BucketListReadOnlyState : public ReadOnlyState | ||
{ | ||
private: | ||
SearchableBucketListSnapshot& mSnapshot; | ||
|
||
public: | ||
BucketListReadOnlyState(SearchableBucketListSnapshot& snapshot); | ||
|
||
ReadOnlyResultPtr loadEntry(LedgerKey const& key) override; | ||
}; | ||
|
||
class LtxReadOnlyState : public ReadOnlyState | ||
{ | ||
private: | ||
AbstractLedgerTxn& mLtx; | ||
|
||
public: | ||
LtxReadOnlyState(AbstractLedgerTxn& ledgerTxn); | ||
|
||
ReadOnlyResultPtr loadEntry(LedgerKey const& key) override; | ||
AbstractLedgerTxn& getLedgerTxn(); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.