Store the realisations as JSON in the binary cache

Fix #4332
This commit is contained in:
regnat 2020-12-08 21:07:52 +01:00 committed by Théophane Hufschmitt
parent 3ac9d74eb1
commit 8914e01e37
3 changed files with 25 additions and 46 deletions

View file

@ -449,7 +449,8 @@ std::optional<const Realisation> BinaryCacheStore::queryRealisation(const DrvOut
auto rawOutputInfo = getFile(outputInfoFilePath); auto rawOutputInfo = getFile(outputInfoFilePath);
if (rawOutputInfo) { if (rawOutputInfo) {
return { Realisation::parse(*rawOutputInfo, outputInfoFilePath) }; return {Realisation::fromJSON(
nlohmann::json::parse(*rawOutputInfo), outputInfoFilePath)};
} else { } else {
return std::nullopt; return std::nullopt;
} }
@ -457,7 +458,7 @@ std::optional<const Realisation> BinaryCacheStore::queryRealisation(const DrvOut
void BinaryCacheStore::registerDrvOutput(const Realisation& info) { void BinaryCacheStore::registerDrvOutput(const Realisation& info) {
auto filePath = realisationsPrefix + "/" + info.id.to_string() + ".doi"; auto filePath = realisationsPrefix + "/" + info.id.to_string() + ".doi";
upsertFile(filePath, info.to_string(), "text/x-nix-derivertopath"); upsertFile(filePath, info.toJSON(), "application/json");
} }
ref<FSAccessor> BinaryCacheStore::getFSAccessor() ref<FSAccessor> BinaryCacheStore::getFSAccessor()

View file

@ -1,5 +1,6 @@
#include "realisation.hh" #include "realisation.hh"
#include "store-api.hh" #include "store-api.hh"
#include <nlohmann/json.hpp>
namespace nix { namespace nix {
@ -20,52 +21,28 @@ std::string DrvOutput::to_string() const {
return std::string(drvPath.to_string()) + "!" + outputName; return std::string(drvPath.to_string()) + "!" + outputName;
} }
std::string Realisation::to_string() const { nlohmann::json Realisation::toJSON() const {
std::string res; return nlohmann::json{
{"id", id.to_string()},
res += "Id: " + id.to_string() + '\n'; {"outPath", outPath.to_string()},
res += "OutPath: " + std::string(outPath.to_string()) + '\n'; };
return res;
} }
Realisation Realisation::parse(const std::string & s, const std::string & whence) Realisation Realisation::fromJSON(
{ const nlohmann::json& json,
// XXX: Copy-pasted from NarInfo::NarInfo. Should be factored out const std::string& whence) {
auto corrupt = [&]() { auto getField = [&](std::string fieldName) -> std::string {
return Error("Drv output info file '%1%' is corrupt", whence); auto fieldIterator = json.find(fieldName);
if (fieldIterator == json.end())
throw Error(
"Drv output info file '%1%' is corrupt, missing field %2%",
whence, fieldName);
return *fieldIterator;
}; };
std::optional<DrvOutput> id; return Realisation{
std::optional<StorePath> outPath; .id = DrvOutput::parse(getField("id")),
.outPath = StorePath(getField("outPath")),
size_t pos = 0;
while (pos < s.size()) {
size_t colon = s.find(':', pos);
if (colon == std::string::npos) throw corrupt();
std::string name(s, pos, colon - pos);
size_t eol = s.find('\n', colon + 2);
if (eol == std::string::npos) throw corrupt();
std::string value(s, colon + 2, eol - colon - 2);
if (name == "Id")
id = DrvOutput::parse(value);
if (name == "OutPath")
outPath = StorePath(value);
pos = eol + 1;
}
if (!outPath) corrupt();
if (!id) corrupt();
return Realisation {
.id = *id,
.outPath = *outPath,
}; };
} }

View file

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "path.hh" #include "path.hh"
#include <nlohmann/json_fwd.hpp>
namespace nix { namespace nix {
@ -25,8 +26,8 @@ struct Realisation {
DrvOutput id; DrvOutput id;
StorePath outPath; StorePath outPath;
std::string to_string() const; nlohmann::json toJSON() const;
static Realisation parse(const std::string & s, const std::string & whence); static Realisation fromJSON(const nlohmann::json& json, const std::string& whence);
}; };
typedef std::map<DrvOutput, Realisation> DrvOutputs; typedef std::map<DrvOutput, Realisation> DrvOutputs;