mirror of
https://github.com/NixOS/nix.git
synced 2025-11-08 19:46:02 +01:00
binary-cache-store: UpsertFile accept Source & instead of std::istream
This commit is contained in:
parent
e3c41407f9
commit
e947c895ec
6 changed files with 16 additions and 38 deletions
|
|
@ -79,7 +79,8 @@ std::optional<std::string> BinaryCacheStore::getNixCacheInfo()
|
||||||
void BinaryCacheStore::upsertFile(
|
void BinaryCacheStore::upsertFile(
|
||||||
const std::string & path, std::string && data, const std::string & mimeType, uint64_t sizeHint)
|
const std::string & path, std::string && data, const std::string & mimeType, uint64_t sizeHint)
|
||||||
{
|
{
|
||||||
upsertFile(path, std::make_shared<std::stringstream>(std::move(data)), mimeType, sizeHint);
|
StringSource source{data};
|
||||||
|
upsertFile(path, source, mimeType, sizeHint);
|
||||||
}
|
}
|
||||||
|
|
||||||
void BinaryCacheStore::getFile(const std::string & path, Callback<std::optional<std::string>> callback) noexcept
|
void BinaryCacheStore::getFile(const std::string & path, Callback<std::optional<std::string>> callback) noexcept
|
||||||
|
|
@ -271,12 +272,10 @@ ref<const ValidPathInfo> BinaryCacheStore::addToStoreCommon(
|
||||||
|
|
||||||
/* Atomically write the NAR file. */
|
/* Atomically write the NAR file. */
|
||||||
if (repair || !fileExists(narInfo->url)) {
|
if (repair || !fileExists(narInfo->url)) {
|
||||||
|
AutoCloseFD fd = toDescriptor(open(fnTemp.c_str(), O_RDONLY));
|
||||||
|
FdSource source{fd.get()};
|
||||||
stats.narWrite++;
|
stats.narWrite++;
|
||||||
upsertFile(
|
upsertFile(narInfo->url, source, "application/x-nix-nar", narInfo->fileSize);
|
||||||
narInfo->url,
|
|
||||||
std::make_shared<std::fstream>(fnTemp, std::ios_base::in | std::ios_base::binary),
|
|
||||||
"application/x-nix-nar",
|
|
||||||
narInfo->fileSize);
|
|
||||||
} else
|
} else
|
||||||
stats.narWriteAverted++;
|
stats.narWriteAverted++;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -135,15 +135,11 @@ bool HttpBinaryCacheStore::fileExists(const std::string & path)
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpBinaryCacheStore::upsertFile(
|
void HttpBinaryCacheStore::upsertFile(
|
||||||
const std::string & path,
|
const std::string & path, Source & source, const std::string & mimeType, uint64_t sizeHint)
|
||||||
std::shared_ptr<std::basic_iostream<char>> istream,
|
|
||||||
const std::string & mimeType,
|
|
||||||
uint64_t sizeHint)
|
|
||||||
{
|
{
|
||||||
auto req = makeRequest(path);
|
auto req = makeRequest(path);
|
||||||
req.method = HttpMethod::PUT;
|
req.method = HttpMethod::PUT;
|
||||||
auto data = StreamToSourceAdapter(istream).drain();
|
auto data = source.drain();
|
||||||
|
|
||||||
auto compressionMethod = getCompressionMethod(path);
|
auto compressionMethod = getCompressionMethod(path);
|
||||||
|
|
||||||
if (compressionMethod) {
|
if (compressionMethod) {
|
||||||
|
|
|
||||||
|
|
@ -100,11 +100,8 @@ public:
|
||||||
|
|
||||||
virtual bool fileExists(const std::string & path) = 0;
|
virtual bool fileExists(const std::string & path) = 0;
|
||||||
|
|
||||||
virtual void upsertFile(
|
virtual void
|
||||||
const std::string & path,
|
upsertFile(const std::string & path, Source & source, const std::string & mimeType, uint64_t sizeHint) = 0;
|
||||||
std::shared_ptr<std::basic_iostream<char>> istream,
|
|
||||||
const std::string & mimeType,
|
|
||||||
uint64_t sizeHint) = 0;
|
|
||||||
|
|
||||||
void upsertFile(
|
void upsertFile(
|
||||||
const std::string & path,
|
const std::string & path,
|
||||||
|
|
|
||||||
|
|
@ -80,11 +80,8 @@ protected:
|
||||||
|
|
||||||
bool fileExists(const std::string & path) override;
|
bool fileExists(const std::string & path) override;
|
||||||
|
|
||||||
void upsertFile(
|
void
|
||||||
const std::string & path,
|
upsertFile(const std::string & path, Source & source, const std::string & mimeType, uint64_t sizeHint) override;
|
||||||
std::shared_ptr<std::basic_iostream<char>> istream,
|
|
||||||
const std::string & mimeType,
|
|
||||||
uint64_t sizeHint) override;
|
|
||||||
|
|
||||||
FileTransferRequest makeRequest(std::string_view path);
|
FileTransferRequest makeRequest(std::string_view path);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -53,17 +53,12 @@ protected:
|
||||||
|
|
||||||
bool fileExists(const std::string & path) override;
|
bool fileExists(const std::string & path) override;
|
||||||
|
|
||||||
void upsertFile(
|
void upsertFile(const std::string & path, Source & source, const std::string & mimeType, uint64_t sizeHint) override
|
||||||
const std::string & path,
|
|
||||||
std::shared_ptr<std::basic_iostream<char>> istream,
|
|
||||||
const std::string & mimeType,
|
|
||||||
uint64_t sizeHint) override
|
|
||||||
{
|
{
|
||||||
auto path2 = config->binaryCacheDir + "/" + path;
|
auto path2 = config->binaryCacheDir + "/" + path;
|
||||||
static std::atomic<int> counter{0};
|
static std::atomic<int> counter{0};
|
||||||
Path tmp = fmt("%s.tmp.%d.%d", path2, getpid(), ++counter);
|
Path tmp = fmt("%s.tmp.%d.%d", path2, getpid(), ++counter);
|
||||||
AutoDelete del(tmp, false);
|
AutoDelete del(tmp, false);
|
||||||
StreamToSourceAdapter source(istream);
|
|
||||||
writeFile(tmp, source);
|
writeFile(tmp, source);
|
||||||
std::filesystem::rename(tmp, path2);
|
std::filesystem::rename(tmp, path2);
|
||||||
del.cancel();
|
del.cancel();
|
||||||
|
|
|
||||||
|
|
@ -20,11 +20,8 @@ public:
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void upsertFile(
|
void
|
||||||
const std::string & path,
|
upsertFile(const std::string & path, Source & source, const std::string & mimeType, uint64_t sizeHint) override;
|
||||||
std::shared_ptr<std::basic_iostream<char>> istream,
|
|
||||||
const std::string & mimeType,
|
|
||||||
uint64_t sizeHint) override;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ref<S3BinaryCacheStoreConfig> s3Config;
|
ref<S3BinaryCacheStoreConfig> s3Config;
|
||||||
|
|
@ -70,12 +67,9 @@ private:
|
||||||
};
|
};
|
||||||
|
|
||||||
void S3BinaryCacheStore::upsertFile(
|
void S3BinaryCacheStore::upsertFile(
|
||||||
const std::string & path,
|
const std::string & path, Source & source, const std::string & mimeType, uint64_t sizeHint)
|
||||||
std::shared_ptr<std::basic_iostream<char>> istream,
|
|
||||||
const std::string & mimeType,
|
|
||||||
uint64_t sizeHint)
|
|
||||||
{
|
{
|
||||||
HttpBinaryCacheStore::upsertFile(path, istream, mimeType, sizeHint);
|
HttpBinaryCacheStore::upsertFile(path, source, mimeType, sizeHint);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string S3BinaryCacheStore::createMultipartUpload(
|
std::string S3BinaryCacheStore::createMultipartUpload(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue