mirror of
https://github.com/trapexit/mergerfs.git
synced 2026-11-22 16:06:27 -05:00
rework policy code
This commit is contained in:
parent
345d0bbdc4
commit
aab90b0503
35 changed files with 983 additions and 991 deletions
259
src/policy.cpp
259
src/policy.cpp
|
|
@ -22,236 +22,57 @@
|
|||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "policy.hpp"
|
||||
#include "fs.hpp"
|
||||
#include "buildvector.hpp"
|
||||
|
||||
#define POLICY(X) (Policy(Policy::Enum::X,#X,fs::find::X))
|
||||
|
||||
namespace mergerfs
|
||||
{
|
||||
Policy::Create::Create(Type value)
|
||||
const std::vector<Policy> Policy::_policies_ =
|
||||
buildvector<Policy,true>
|
||||
(POLICY(invalid))
|
||||
(POLICY(all))
|
||||
(POLICY(epmfs))
|
||||
(POLICY(ff))
|
||||
(POLICY(ffwp))
|
||||
(POLICY(mfs))
|
||||
(POLICY(newest))
|
||||
(POLICY(rand));
|
||||
|
||||
const Policy * const Policy::policies = &_policies_[1];
|
||||
|
||||
const Policy &Policy::invalid = Policy::policies[Policy::Enum::invalid];
|
||||
const Policy &Policy::all = Policy::policies[Policy::Enum::all];
|
||||
const Policy &Policy::epmfs = Policy::policies[Policy::Enum::epmfs];
|
||||
const Policy &Policy::ff = Policy::policies[Policy::Enum::ff];
|
||||
const Policy &Policy::ffwp = Policy::policies[Policy::Enum::ffwp];
|
||||
const Policy &Policy::mfs = Policy::policies[Policy::Enum::mfs];
|
||||
const Policy &Policy::newest = Policy::policies[Policy::Enum::newest];
|
||||
const Policy &Policy::rand = Policy::policies[Policy::Enum::rand];
|
||||
|
||||
const Policy&
|
||||
Policy::find(const std::string str)
|
||||
{
|
||||
*this = value;
|
||||
}
|
||||
|
||||
Policy::Create&
|
||||
Policy::Create::operator=(const Policy::Create::Type value)
|
||||
{
|
||||
_str = toString(value);
|
||||
_value = fromString(_str);
|
||||
_func = findFunc(_value);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
Policy::Create&
|
||||
Policy::Create::operator=(const std::string str)
|
||||
{
|
||||
_value = fromString(str);
|
||||
_str = toString(_value);
|
||||
_func = findFunc(_value);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
Policy::Create::Type
|
||||
Policy::Create::fromString(const std::string str)
|
||||
{
|
||||
if(str == "ep")
|
||||
return Policy::Create::ExistingPath;
|
||||
else if(str == "mfs")
|
||||
return Policy::Create::MostFreeSpace;
|
||||
else if(str == "epmfs")
|
||||
return Policy::Create::ExistingPathMostFreeSpace;
|
||||
else if(str == "rand")
|
||||
return Policy::Create::Random;
|
||||
|
||||
return Policy::Create::Invalid;
|
||||
}
|
||||
|
||||
std::string
|
||||
Policy::Create::toString(const Type value)
|
||||
{
|
||||
switch(value)
|
||||
for(int i = Enum::BEGIN; i != Enum::END; ++i)
|
||||
{
|
||||
case Policy::Create::ExistingPath:
|
||||
return "ep";
|
||||
case Policy::Create::ExistingPathMostFreeSpace:
|
||||
return "epmfs";
|
||||
case Policy::Create::MostFreeSpace:
|
||||
return "mfs";
|
||||
case Policy::Create::Random:
|
||||
return "rand";
|
||||
case Policy::Create::Invalid:
|
||||
default:
|
||||
return "invalid";
|
||||
if(policies[i] == str)
|
||||
return policies[i];
|
||||
}
|
||||
|
||||
return invalid;
|
||||
}
|
||||
|
||||
Policy::Create::Func
|
||||
Policy::Create::findFunc(Policy::Create::Type value)
|
||||
const Policy&
|
||||
Policy::find(const Policy::Enum::Type i)
|
||||
{
|
||||
switch(value)
|
||||
{
|
||||
case Policy::Create::ExistingPath:
|
||||
return fs::find_first;
|
||||
case Policy::Create::ExistingPathMostFreeSpace:
|
||||
return fs::find_mfs_existing;
|
||||
case Policy::Create::MostFreeSpace:
|
||||
return fs::find_mfs;
|
||||
case Policy::Create::Random:
|
||||
return fs::find_random;
|
||||
case Policy::Create::Invalid:
|
||||
default:
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
if(i >= Policy::Enum::BEGIN &&
|
||||
i < Policy::Enum::END)
|
||||
return policies[i];
|
||||
|
||||
Policy::Search::Search(Type value)
|
||||
{
|
||||
*this = value;
|
||||
}
|
||||
|
||||
Policy::Search&
|
||||
Policy::Search::operator=(const Policy::Search::Type value)
|
||||
{
|
||||
_str = toString(value);
|
||||
_value = fromString(_str);
|
||||
_func = findFunc(_value);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
Policy::Search&
|
||||
Policy::Search::operator=(const std::string str)
|
||||
{
|
||||
_value = fromString(str);
|
||||
_str = toString(_value);
|
||||
_func = findFunc(_value);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
Policy::Search::Type
|
||||
Policy::Search::fromString(const std::string str)
|
||||
{
|
||||
if(str == "ff")
|
||||
return Policy::Search::FirstFound;
|
||||
else if(str == "ffwp")
|
||||
return Policy::Search::FirstFoundWithPermissions;
|
||||
else if(str == "newest")
|
||||
return Policy::Search::Newest;
|
||||
|
||||
return Policy::Search::Invalid;
|
||||
}
|
||||
|
||||
std::string
|
||||
Policy::Search::toString(Policy::Search::Type value)
|
||||
{
|
||||
switch(value)
|
||||
{
|
||||
case Policy::Search::FirstFound:
|
||||
return "ff";
|
||||
case Policy::Search::FirstFoundWithPermissions:
|
||||
return "ffwp";
|
||||
case Policy::Search::Newest:
|
||||
return "newest";
|
||||
case Policy::Search::Invalid:
|
||||
default:
|
||||
return "invalid";
|
||||
}
|
||||
}
|
||||
|
||||
Policy::Search::Func
|
||||
Policy::Search::findFunc(Policy::Search::Type value)
|
||||
{
|
||||
switch(value)
|
||||
{
|
||||
case Policy::Search::FirstFound:
|
||||
return fs::find_first;
|
||||
case Policy::Search::FirstFoundWithPermissions:
|
||||
return fs::find_first_with_permission;
|
||||
case Policy::Search::Newest:
|
||||
return fs::find_newest;
|
||||
case Policy::Search::Invalid:
|
||||
default:
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
Policy::Action::Action(const Policy::Action::Type value)
|
||||
{
|
||||
*this = value;
|
||||
}
|
||||
|
||||
Policy::Action&
|
||||
Policy::Action::operator=(const Policy::Action::Type value)
|
||||
{
|
||||
_str = toString(value);
|
||||
_value = fromString(_str);
|
||||
_func = findFunc(_value);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
Policy::Action&
|
||||
Policy::Action::operator=(const std::string str)
|
||||
{
|
||||
_value = fromString(str);
|
||||
_str = toString(_value);
|
||||
_func = findFunc(_value);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
Policy::Action::Type
|
||||
Policy::Action::fromString(const std::string str)
|
||||
{
|
||||
if(str == "ff")
|
||||
return Policy::Action::FirstFound;
|
||||
else if(str == "ffwp")
|
||||
return Policy::Action::FirstFoundWithPermissions;
|
||||
else if(str == "newest")
|
||||
return Policy::Action::Newest;
|
||||
else if(str == "all")
|
||||
return Policy::Action::All;
|
||||
|
||||
return Policy::Action::Invalid;
|
||||
}
|
||||
|
||||
std::string
|
||||
Policy::Action::toString(Policy::Action::Type value)
|
||||
{
|
||||
switch(value)
|
||||
{
|
||||
case Policy::Action::FirstFound:
|
||||
return "ff";
|
||||
case Policy::Action::FirstFoundWithPermissions:
|
||||
return "ffwp";
|
||||
case Policy::Action::Newest:
|
||||
return "newest";
|
||||
case Policy::Action::All:
|
||||
return "all";
|
||||
case Policy::Action::Invalid:
|
||||
default:
|
||||
return "invalid";
|
||||
}
|
||||
}
|
||||
|
||||
Policy::Action::Func
|
||||
Policy::Action::findFunc(const Policy::Action::Type value)
|
||||
{
|
||||
switch(value)
|
||||
{
|
||||
case Policy::Action::FirstFound:
|
||||
return fs::find_first;
|
||||
case Policy::Action::FirstFoundWithPermissions:
|
||||
return fs::find_first_with_permission;
|
||||
case Policy::Action::Newest:
|
||||
return fs::find_newest;
|
||||
case Policy::Action::All:
|
||||
return fs::find_all;
|
||||
case Policy::Action::Invalid:
|
||||
default:
|
||||
return NULL;
|
||||
}
|
||||
return invalid;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue