2424#include " space_manager.h"
2525#include " file/file.h"
2626#include " logger/logger.h"
27+ #include " space_shard_temp_layout.h"
2728
2829namespace UC {
2930
30- Status SpaceManager::Setup (const std::vector<std::string>& storageBackends, const size_t blockSize)
31+ std::unique_ptr<SpaceLayout> MakeSpaceLayout (const bool tempDumpDirEnable)
32+ {
33+ try {
34+ if (tempDumpDirEnable) { return std::make_unique<SpaceShardTempLayout>(); }
35+ return std::make_unique<SpaceShardLayout>();
36+ } catch (const std::exception& e) {
37+ UC_ERROR (" Failed({}) to make space layout object." , e.what ());
38+ }
39+ return nullptr ;
40+ }
41+
42+ Status SpaceManager::Setup (const std::vector<std::string>& storageBackends, const size_t blockSize,
43+ const bool tempDumpDirEnable)
3144{
3245 if (blockSize == 0 ) {
3346 UC_ERROR (" Invalid block size({})." , blockSize);
3447 return Status::InvalidParam ();
3548 }
36- auto status = this ->layout_ .Setup (storageBackends);
49+ this ->layout_ = MakeSpaceLayout (tempDumpDirEnable);
50+ if (!this ->layout_ ) { return Status::OutOfMemory (); }
51+ auto status = this ->layout_ ->Setup (storageBackends);
3752 if (status.Failure ()) { return status; }
3853 this ->blockSize_ = blockSize;
3954 return Status::OK ();
4055}
4156
4257Status SpaceManager::NewBlock (const std::string& blockId) const
4358{
44- auto parent = File::Make (this ->layout_ .DataFileParent (blockId));
45- auto file = File::Make (this ->layout_ .DataFilePath (blockId, true ));
59+ constexpr auto activated = true ;
60+ auto parent = File::Make (this ->layout_ ->DataFileParent (blockId, activated));
61+ auto file = File::Make (this ->layout_ ->DataFilePath (blockId, activated));
4662 if (!parent || !file) {
4763 UC_ERROR (" Failed to new block({})." , blockId);
4864 return Status::OutOfMemory ();
@@ -53,7 +69,7 @@ Status SpaceManager::NewBlock(const std::string& blockId) const
5369 UC_ERROR (" Failed({}) to new block({})." , status, blockId);
5470 return status;
5571 }
56- if ((File::Access (this ->layout_ . DataFilePath (blockId, false ), IFile::AccessMode::EXIST))
72+ if ((File::Access (this ->layout_ -> DataFilePath (blockId, false ), IFile::AccessMode::EXIST))
5773 .Success ()) {
5874 status = Status::DuplicateKey ();
5975 UC_ERROR (" Failed({}) to new block({})." , status, blockId);
@@ -77,29 +93,31 @@ Status SpaceManager::NewBlock(const std::string& blockId) const
7793
7894Status SpaceManager::CommitBlock (const std::string& blockId, bool success) const
7995{
80- auto file = File::Make (this ->layout_ .DataFilePath (blockId, true ));
81- if (!file) {
82- UC_ERROR (" Failed to {} block({})." , success ? " commit" : " cancel" , blockId);
83- return Status::OutOfMemory ();
84- }
85- if (success) {
86- auto status = file->Rename (this ->layout_ .DataFilePath (blockId, false ));
87- if (status.Failure ()) { UC_ERROR (" Failed({}) to commit block({})." , status, blockId); }
88- return status;
89- }
90- auto parent = File::Make (this ->layout_ .DataFileParent (blockId));
91- if (!parent) {
92- UC_ERROR (" Failed to cancel block({})." , blockId);
93- return Status::OutOfMemory ();
96+ const auto activatedParent = this ->layout_ ->DataFileParent (blockId, true );
97+ const auto activatedFile = this ->layout_ ->DataFilePath (blockId, true );
98+ auto status = Status::OK ();
99+ do {
100+ if (!success) { break ; }
101+ const auto archivedParent = this ->layout_ ->DataFileParent (blockId, false );
102+ const auto archivedFile = this ->layout_ ->DataFilePath (blockId, false );
103+ if (archivedParent != activatedParent) {
104+ status = File::MkDir (archivedParent);
105+ if (status == Status::DuplicateKey ()) { status = Status::OK (); }
106+ if (status.Failure ()) { break ; }
107+ }
108+ status = File::Rename (activatedFile, archivedFile);
109+ } while (0 );
110+ File::Remove (activatedFile);
111+ File::RmDir (activatedParent);
112+ if (status.Failure ()) {
113+ UC_ERROR (" Failed({}) to {} block({})." , status, success ? " commit" : " cancel" , blockId);
94114 }
95- file->Remove ();
96- parent->RmDir ();
97- return Status::OK ();
115+ return status;
98116}
99117
100118bool SpaceManager::LookupBlock (const std::string& blockId) const
101119{
102- auto path = this ->layout_ . DataFilePath (blockId, false );
120+ auto path = this ->layout_ -> DataFilePath (blockId, false );
103121 auto file = File::Make (path);
104122 if (!file) {
105123 UC_ERROR (" Failed to make file smart pointer, path: {}." , path);
@@ -116,6 +134,6 @@ bool SpaceManager::LookupBlock(const std::string& blockId) const
116134 return true ;
117135}
118136
119- const SpaceLayout* SpaceManager::GetSpaceLayout () const { return & this ->layout_ ; }
137+ const SpaceLayout* SpaceManager::GetSpaceLayout () const { return this ->layout_ . get () ; }
120138
121139} // namespace UC
0 commit comments