Add a few more content addressing methods

Good to round out the library interface.
This commit is contained in:
John Ericson 2023-04-19 14:13:30 -04:00
parent 20decfd302
commit aba8a8a83a
2 changed files with 67 additions and 24 deletions

View file

@ -154,38 +154,32 @@ std::string renderContentAddress(std::optional<ContentAddress> ca)
return ca ? ca->render() : ""; return ca ? ca->render() : "";
} }
ContentAddressWithReferences ContentAddressWithReferences::fromParts( ContentAddress ContentAddress::fromParts(
ContentAddressMethod method, Hash hash, StoreReferences refs) ContentAddressMethod method, Hash hash)
{ {
return std::visit(overloaded { return std::visit(overloaded {
[&](TextIngestionMethod _) -> ContentAddressWithReferences { [&](TextIngestionMethod _) -> ContentAddress {
if (refs.self) return TextHash {
throw UsageError("Cannot have a self reference with text hashing scheme"); .hash = std::move(hash),
return TextInfo {
.hash = { .hash = std::move(hash) },
.references = std::move(refs.others),
}; };
}, },
[&](FileIngestionMethod m2) -> ContentAddressWithReferences { [&](FileIngestionMethod m2) -> ContentAddress {
return FixedOutputInfo { return FixedOutputHash {
.hash = { .method = std::move(m2),
.method = m2, .hash = std::move(hash),
.hash = std::move(hash),
},
.references = std::move(refs),
}; };
}, },
}, method.raw); }, method.raw);
} }
ContentAddressMethod ContentAddressWithReferences::getMethod() const ContentAddressMethod ContentAddress::getMethod() const
{ {
return std::visit(overloaded { return std::visit(overloaded {
[](const TextInfo & th) -> ContentAddressMethod { [](const TextHash & th) -> ContentAddressMethod {
return TextIngestionMethod {}; return TextIngestionMethod {};
}, },
[](const FixedOutputInfo & fsh) -> ContentAddressMethod { [](const FixedOutputHash & fsh) -> ContentAddressMethod {
return fsh.hash.method; return fsh.method;
}, },
}, raw); }, raw);
} }
@ -229,6 +223,42 @@ ContentAddressWithReferences ContentAddressWithReferences::withoutRefs(const Con
}, ca.raw); }, ca.raw);
} }
ContentAddressWithReferences ContentAddressWithReferences::fromParts(
ContentAddressMethod method, Hash hash, StoreReferences refs)
{
return std::visit(overloaded {
[&](TextIngestionMethod _) -> ContentAddressWithReferences {
if (refs.self)
throw UsageError("Cannot have a self reference with text hashing scheme");
return TextInfo {
.hash = { .hash = std::move(hash) },
.references = std::move(refs.others),
};
},
[&](FileIngestionMethod m2) -> ContentAddressWithReferences {
return FixedOutputInfo {
.hash = {
.method = m2,
.hash = std::move(hash),
},
.references = std::move(refs),
};
},
}, method.raw);
}
ContentAddressMethod ContentAddressWithReferences::getMethod() const
{
return std::visit(overloaded {
[](const TextInfo & th) -> ContentAddressMethod {
return TextIngestionMethod {};
},
[](const FixedOutputInfo & fsh) -> ContentAddressMethod {
return fsh.hash.method;
},
}, raw);
}
Hash ContentAddressWithReferences::getHash() const Hash ContentAddressWithReferences::getHash() const
{ {
return std::visit(overloaded { return std::visit(overloaded {

View file

@ -154,8 +154,9 @@ struct ContentAddress
{ } { }
/** /**
* Compute the content-addressability assertion (ValidPathInfo::ca) for * Compute the content-addressability assertion
* paths created by Store::makeFixedOutputPath() / Store::addToStore(). * (`ValidPathInfo::ca`) for paths created by
* `Store::makeFixedOutputPath()` / `Store::addToStore()`.
*/ */
std::string render() const; std::string render() const;
@ -163,6 +164,18 @@ struct ContentAddress
static std::optional<ContentAddress> parseOpt(std::string_view rawCaOpt); static std::optional<ContentAddress> parseOpt(std::string_view rawCaOpt);
/**
* Create a `ContentAddress` from 2 parts:
*
* @param method Way ingesting the file system data.
*
* @param hash Hash of ingested file system data.
*/
static ContentAddress fromParts(
ContentAddressMethod method, Hash hash);
ContentAddressMethod getMethod() const;
const Hash & getHash() const; const Hash & getHash() const;
}; };
@ -251,13 +264,13 @@ struct ContentAddressWithReferences
{ } { }
/** /**
* Create a ContentAddressWithReferences from a mere ContentAddress, by * Create a `ContentAddressWithReferences` from a mere
* assuming no references in all cases. * `ContentAddress`, by assuming no references in all cases.
*/ */
static ContentAddressWithReferences withoutRefs(const ContentAddress &); static ContentAddressWithReferences withoutRefs(const ContentAddress &);
/** /**
* Create a ContentAddressWithReferences from 3 parts: * Create a `ContentAddressWithReferences` from 3 parts:
* *
* @param method Way ingesting the file system data. * @param method Way ingesting the file system data.
* *